Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions book/move-basics/struct-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ structs.
> `public use fun hero_health as Hero.health`, which provides controlled access to the private
> field.

<!-- ## Aliasing an external module's method
## Aliasing an external module's method

It is also possible to associate a function defined in another module with a struct from the current
module. Following the same approach, we can create an alias for the method defined in another
Expand All @@ -66,7 +66,7 @@ associate it with the `Hero` struct. It will allow serializing the `Hero` struct
bytes.

```move file=packages/samples/sources/move-basics/struct-methods-3.move anchor=hero_to_bytes
``` -->
```

## Further Reading

Expand Down
20 changes: 12 additions & 8 deletions packages/samples/sources/move-basics/struct-methods-3.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

// ANCHOR: hero_to_bytes
// TODO: better example (external module...)
module book::hero_to_bytes;

// Alias for the `bcs::to_bytes` method. Imported aliases should be defined
// in the top of the module.
// public use fun bcs::to_bytes as Hero.to_bytes;
use sui::bcs;

// Alias for the `bcs::to_bytes` method. This allows us to use the method
// on the Hero struct as if it were a native method.
public use fun bcs::to_bytes as Hero.to_bytes;

/// A struct representing a hero.
public struct Hero has drop {
Expand All @@ -19,10 +20,13 @@ public struct Hero has drop {
public fun new(): Hero { Hero { health: 100, mana: 100 } }

#[test]
// Test the methods of the `Hero` struct.
// Test the `to_bytes` method on the Hero struct.
fun test_hero_serialize() {
// let mut hero = new();
// let serialized = hero.to_bytes();
// assert!(serialized.length() == 3, 1);
let hero = new();
// Call the `to_bytes` method as if it's a method of Hero
let serialized = hero.to_bytes();

// Verify the serialization worked
assert!(serialized.length() > 0, 1);
}
// ANCHOR_END: hero_to_bytes
Loading