Reputation: 20141
I understand that Substrate will return the 0 or null value when Storage is queried for a key that doesn't exist. Does it do something special with Option types?
I ask because in the default node-template, the storage item Something is declared as
Something: Option<u32>;
But later in the do_something
setter, the method signature declares something: u32
, but passes it directly without wrapping it in Some()
:
Something::put(something);
instead of
Something::put(Some(something));
If Substrate is interpreting a call to store T
as storing Some<T>
, does that also mean if I store None
in a map against a key then it will delete the key? Is it storing Some(T)
or T
?
Or is it just that Rust is building a specialised method call for Something::put(some: T)
case as well as Something::put(some: Option<T>)
?
Upvotes: 1
Views: 475
Reputation: 516
Indeed decl_storage
expand differently if the value is an Option or not.
The trait implemented is srml_support::storage::StorageValue
Option<MyType>
the trait implemented issrml_support::storage::StorageValue<MyType> {
type Query = Option<MyType>;
}
in the implementation get
will return None if no value is found in the trie at the key used by this storage.
MyType
the trait implemented issrml_support::storage::StorageValue<MyType> {
type Query = MyType;
}
in the implementation get
will return Default if no value is found in the trie at the key used by this storage.
Note that this default can be overriden using the syntax:
NotOptionSomething: u32 = MyDifferentDefault;
If Substrate is interpreting a call to store T as storing Some, does that also mean if I store None in a map against a key then it will delete the key? Is it storing Some(T) or T?
At this point this is no longer substrate interpretation but just a rust trait implementation. The put
function is implemented to store the encoded value in the trie. Not wrapped in an option.
Upvotes: 2