In my coding, I find that I can use a value or a value's reference as parameter in function StorageValue::put(). The same case is in function StorageMap::insert(). In my view, I can not input a value of different kind that the function needs. I found the parameter's kind in these functions are EncodeLike, I am a green hand of rust, so I am not very clear about the usage of this style. Who can explain this for me?
1 Answer
As you already found out this works because these functions take some generic type that implements EncodeLike
the value type of your storage item. This is just some scheme to support passing either references or values to the function. You could even pass different types as long as they use the same encoding. As the FRAME storage SCALE encodes all values and decodes them when you call get
, the type that you pass to put
isn't important, as long as it encodes into the same format ;)
This EncodeLike
trait is some marker trait that only acts as some sort of information source at compilation time. It doesn't provide any further utility.
MyStorageItem::put(val);
let val = MyStorageItem::get();
Between this put
and get
the val
is encoded and stored in the state. The get
then retrieves the val
from the state as an blob of bytes and decodes it.