Abilities
Abilities
Abilities are a typing feature in Move that control what actions are permissible for values of a given type. This system grants fine-grained control over the "linear" typing behavior of values, as well as if and how values are used in global storage. This is implemented by gating access to certain bytecode instructions so that for a value to be used with the bytecode instruction, it must have the ability required (if one is required at allβnot every instruction is gated by an ability).
The Four Abilities
The four abilities are:
copy
Allows values of types with this ability to be copied.
drop
Allows values of types with this ability to be popped/dropped.
store
Allows values of types with this ability to exist inside a struct in global storage.
key
Allows the type to serve as a key for global storage operations.
copy
copy
The copy
ability allows values of types with that ability to be copied. It gates the ability to copy values out of local variables with the copy
operator and to copy values via references with dereference *e
.
If a value has copy
, all values contained inside of that value have copy
.
drop
drop
The drop
ability allows values of types with that ability to be dropped. By dropped, we mean that value is not transferred and is effectively destroyed as the Move program executes. As such, this ability gates the ability to ignore values in a multitude of locations, including:
not using the value in a local variable or parameter
not using the value in a sequence via
;
overwriting values in variables in assignments
overwriting values via references when writing
*e1 = e2
.
If a value has drop
, all values contained inside of that value have drop
.
store
store
The store
ability allows values of types with this ability to exist inside a struct (resource) in global storage, but not necessarily as a top-level resource in global storage. This is the only ability that does not directly gate an operation. Instead, it gates the existence in global storage when used in tandem with key
.
If a value has store
, all values contained inside of that value have store
key
key
The key
ability allows the type to serve as a key for global storage operations. It gates all global storage operations, so in order for a type to be used with move_to
, borrow_global
, move_from
, etc., the type must have the key
ability. Note that the operations still must be used in the module where the key
type is defined (in a sense, the operations are private to the defining module).
If a value has key
, all values contained inside of that value have store
. This is the only ability with this sort of asymmetry.
Builtin Types
Most primitive, builtin types have copy
, drop
, and store
except for signer
, which just has drop
bool
,u8
,u16
,u32
,u64
,u128
,u256
, andaddress
all havecopy
,drop
, andstore
.signer
hasdrop
Cannot be copied and cannot be put into global storage
vector<T>
may havecopy
,drop
, andstore
depending on the abilities ofT
.See Conditional Abilities and Generic Types for more details.
Immutable references
&
and mutable references&mut
both havecopy
anddrop
.This refers to copying and dropping the reference itself, not what they refer to.
References cannot appear in global storage, hence they do not have
store
.
None of the primitive types have key
, meaning none of them can be used directly with the global storage operations.
Annotating Structs
To declare that a struct
has an ability, it is declared with has <ability>
after the struct name but before the fields. For example:
In this case: Ignorable
has the drop
ability. Pair
has 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 some other collection!
As such: when declaring a structβs abilities, certain requirements are placed on the fields. All fields must satisfy these constraints. These rules are necessary so that structs satisfy the reachability rules for the abilities given above. If a struct is declared with the ability...
copy
, all fields must havecopy
.drop
, all fields must havedrop
.store
, all fields must havestore
.key
, all fields must havestore
.key
is the only ability currently that doesn't require itself.
For example:
and similarly:
Conditional Abilities and Generic Types
When abilities are annotated on a generic type, not all instances of that type are guaranteed to have that ability. Consider this struct declaration:
It might be very helpful if Cup
could hold any type, regardless of its abilities. The type system can see the type parameter, so it should be able to remove abilities from Cup
if it sees a type parameter that would violate the guarantees for that ability.
This behavior might sound a bit confusing at first, but it might be more understandable if we think about collection types. We could consider the builtin type vector
to have the following type declaration:
We want vector
s to work with any type. We don't want separate vector
types for different abilities. So what are the rules we would want? Precisely the same that we would want with the field rules above. So, it would be safe to copy a vector
value only if the inner elements can be copied. It would be safe to ignore a vector
value only if the inner elements can be ignored/dropped. And, it would be safe to put a vector
in global storage only if the inner elements can be in global storage.
To have this extra expressiveness, a type might not have all the abilities it was declared with depending on the instantiation of that type; instead, the abilities a type will have depends on both its declaration and its type arguments. For any type, type parameters are pessimistically assumed to be used inside the struct, so the abilities are only granted if the type parameters meet the requirements described above for fields. Taking Cup
from above as an example:
Cup
has the abilitycopy
only ifT
hascopy
.It has
drop
only ifT
hasdrop
.It has
store
only ifT
hasstore
.It has
key
only ifT
hasstore
.
Here are examples for this conditional system for each ability:
Example: conditional copy
copy
Example: conditional drop
drop
Example: conditional store
store
Example: conditional key
key
Last updated