Skip to content

Commit

Permalink
[move docs] Add enums to Move reference book (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzakian authored Jun 12, 2024
1 parent d28a6a5 commit d4bdbc2
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
repo: MystenLabs/sui
platform: ubuntu
version: mainnet
version: main
cache: enable

# Run the tests in every directory using the latest mainnet binary
Expand Down
26 changes: 26 additions & 0 deletions packages/reference/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 2
manifest_digest = "868EC315260CC0A203EABE09C39C72CFDDB66DE119BDF7E24CE5EAABE30EFCC1"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "main", subdir = "crates/sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "main", subdir = "crates/sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.27.0"
edition = "2024.alpha"
flavor = "sui"
2 changes: 1 addition & 1 deletion packages/reference/Move.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reference"
edition = "2024.beta"
edition = "2024.alpha"

[dependencies.Sui]
git = "https://github.com/MystenLabs/sui.git"
Expand Down
8 changes: 6 additions & 2 deletions packages/reference/sources/abilities.move
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
#[allow(unused_field)]
module ref::abilities {

// ANCHOR: annotating_structs
// ANCHOR: annotating_datatypes
public struct Ignorable has drop { f: u64 }
public struct Pair has copy, drop, store { x: u64, y: u64 }
public struct MyVec(vector<u64>) has copy, drop, store;
// ANCHOR_END: annotating_structs

public enum IgnorableEnum has drop { Variant }
public enum PairEnum has copy, drop, store { Variant }
public enum MyVecEnum { Variant } has copy, drop, store;
// ANCHOR_END: annotating_datatypes

// ANCHOR: conditional_abilities
// public struct Cup<T> has copy, drop, store, key { item: T }
Expand Down
32 changes: 26 additions & 6 deletions reference/src/abilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ All primitive, builtin types have `copy`, `drop`, and `store`.
Note that none of the primitive types have `key`, meaning none of them can be used directly with
storage operations.

## Annotating Structs
## Annotating Structs and Enums

To declare that a `struct` has an ability, it is declared with `has <ability>` after the struct name
and either before or after the fields. For example:
To declare that a `struct` or `enum` has an ability, it is declared with `has <ability>` after the
datatype name and either before or after the fields/variants. For example:

```move
{{#include ../../packages/reference/sources/abilities.move:annotating_structs}}
{{#include ../../packages/reference/sources/abilities.move:annotating_datatypes}}
```

In this case: `Ignorable` has the `drop` ability. `Pair` and `MyVec` both have `copy`, `drop`, and
`store`.
In this case: `Ignorable*` has the `drop` ability. `Pair*` and `MyVec*` both have `copy`, `drop`,
and `store`.

All of these abilities have strong guarantees over these gated operations. The operation can be
performed on the value only if it has that ability; even if the value is deeply nested inside of
Expand All @@ -124,6 +124,16 @@ reachability rules for the abilities given above. If a struct is declared with t
- `key`, all fields must have `store`.
- `key` is the only ability currently that doesn’t require itself.

An enum can have any of these abilities with the exception of `key`, which enums cannot have because
they cannot be top-level values (objects) in storage. The same rules apply to fields of enum
variants as they do for struct fields though. In particular, if an enum is declared with the
ability...

- `copy`, all fields of all variants must have `copy`.
- `drop`, all fields of all variants must have `drop`.
- `store`, all fields of all variants must have `store`.
- `key`, is not allowed on enums as previously mentioned.

For example:

```move
Expand All @@ -133,6 +143,11 @@ public struct NoAbilities {}
public struct WantsCopy has copy {
f: NoAbilities, // ERROR 'NoAbilities' does not have 'copy'
}
public enum WantsCopyEnum has copy {
Variant1
Variant2(NoAbilities), // ERROR 'NoAbilities' does not have 'copy'
}
```

and similarly:
Expand All @@ -144,6 +159,11 @@ public struct NoAbilities {}
public struct MyData has key {
f: NoAbilities, // Error 'NoAbilities' does not have 'store'
}
public struct MyDataEnum has store {
Variant1,
Variant2(NoAbilities), // Error 'NoAbilities' does not have 'store'
}
```

## Conditional Abilities and Generic Types
Expand Down
Loading

0 comments on commit d4bdbc2

Please sign in to comment.