-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: salaheldinsoliman <[email protected]>
- Loading branch information
1 parent
2008cf9
commit 972be78
Showing
6 changed files
with
118 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
docs/content/developer/iota-101/move-overview/structs-and-abilities/key.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# The Key Ability | ||
|
||
We already covered two out of four abilities - | ||
[Drop](./drop.mdx) and [Copy](./copy.mdx). They affect the behaviour of the value in a | ||
scope and are not directly related to storage. It is time to cover the `key` ability, which allows | ||
the struct to be stored. | ||
|
||
Historically, the `key` ability was created to mark the type as a _key in the storage_. A type with | ||
the `key` ability could be stored at top-level in the storage, and could be _directly owned_ by an | ||
account or address. With the introduction of the [Object Model](../../objects/object-model.mdx), the `key` ability | ||
naturally became the defining ability for the object. | ||
|
||
|
||
## Object Definition | ||
|
||
A struct with the `key` ability is considered an object and can be used in the storage functions. | ||
The Sui Verifier will require the first field of the struct to be named `id` and have the type | ||
`UID`. | ||
|
||
```move | ||
public struct Object has key { | ||
id: UID, // required | ||
name: String, | ||
} | ||
/// Creates a new Object with a Unique ID | ||
public fun new(name: String, ctx: &mut TxContext): Object { | ||
Object { | ||
id: object::new(ctx), // creates a new UID | ||
name, | ||
} | ||
} | ||
``` | ||
|
||
A struct with the `key` ability is still a struct, and can have any number of fields and associated | ||
functions. There is no special handling or syntax for packing, accessing or unpacking the struct. | ||
|
||
However, because the first field of an object struct must be of type `UID` - a non-copyable and | ||
non-droppable type (we will get to it very soon!), the struct transitively cannot have `drop` and | ||
`copy` abilities. Thus, the object is non-discardable by design. | ||
|
||
|
||
## Types with the `key` Ability | ||
|
||
Due to the `UID` requirement for types with `key`, none of the native types in Move can have the | ||
`key` ability, nor can any of the [Standard Library](../../../../references/framework/move-stdlib/_category_.json) types. | ||
The `key` ability is only present in the [IOTA Framework](../../../../references/framework/iota-framework/_category_.json) and | ||
custom types. | ||
|
||
## Next Steps | ||
|
||
Key ability defines the object in Move, and objects are intended to be _stored_. In the next section | ||
we present the `iota::transfer` module, which provides native storage functions for objects. |
50 changes: 50 additions & 0 deletions
50
docs/content/developer/iota-101/move-overview/structs-and-abilities/store.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Ability: Store | ||
|
||
## Definition | ||
|
||
The `store` is a special ability that allows a type to be _stored_ in objects. This ability is | ||
required for the type to be used as a field in a struct that has the `key` ability. Another way to | ||
put it is that the `store` ability allows the value to be _wrapped_ in an object. | ||
|
||
:::info | ||
The `store` ability also relaxes restrictions on transfer operations. We talk about it more in the | ||
[Custome Transfer Rules](../../objects/transfers/custom-rules.mdx) section. | ||
::: | ||
|
||
## Example | ||
|
||
In previous sections we already used types with the `key` ability: all objects must have a `UID` | ||
field, which we used in examples; we also used the `Storable` type as a part of the `Config` struct. | ||
The `Config` type also has the `store` ability. | ||
|
||
```move | ||
/// This type has the `store` ability. | ||
public struct Storable has store {} | ||
/// Config contains a `Storable` field which must have the `store` ability. | ||
public struct Config has key, store { | ||
id: UID, | ||
stores: Storable, | ||
} | ||
/// MegaConfig contains a `Config` field which has the `store` ability. | ||
public struct MegaConfig has key { | ||
id: UID, | ||
config: Config, // there it is! | ||
} | ||
``` | ||
|
||
## Types with the `store` Ability | ||
|
||
All native types (except for references) in Move have the `store` ability. This includes: | ||
|
||
- bool | ||
- unsigned integers | ||
- [vector](../../../../references/framework/move-stdlib/vector.mdx) | ||
- [address](../../../../references/framework/move-stdlib/address.mdx) | ||
|
||
All of the types defined in the standard library have the `store` ability as well. This includes: | ||
|
||
- [Option](../../../../references/framework/move-stdlib/option.mdx) | ||
- [String](../../../../references/framework/move-stdlib/string.mdx) | ||
- [TypeName](../../../../references/framework/move-stdlib/type_name.mdx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters