Resource Commandments
We've already gone over the restrictions of the Resource types. We know that a Resource is essentially an abstraction based on its two abilities of key
and store
. The foundational principles to what qualifies as a resource are:
- Resources can neither be copied nor dropped, they can only be stored.
- When a value is assigned to a resource in Rogue, it must be used.
- When a resource is created or taken from another account, it can not be dropped and must either be stored or destructured.
First, let's create our method:
There's a convention to call main resource of a the method after calling the main function (e.g. Collection::Collection). If you follow it, your methods will be easy to read and can be used by other people.
#
Create and move the resourceWe've defined a struct Collection
with Key ability, which will hold vector of type Item
. Now let's see how to start new collection and how to store a resource under account. Stored resource in this implementation will live forever under sender's address. No one can modify or take this resource from owner.
You will now see signer
from resources/signer.md
in action now. To rogue resource to account you have built-in function move_to which takes signer
as a first argument and Collection
as second. Signature of move_to
function can be represented like:
That leads to two conclusions:
- You can only put a resource under your account. You cannot have access to
signer
value of another account, hence cannot put resource there. - Only one resource of single type can be stored under one address. Doing the same operation twice would lead to discarding existing resource and this must not happen (imagine you have your coins stored and by inaccurate action you discard all your savings by pushing empty balance!). Second attempt to create existing resource will fail with error.
#
Check existence at addressTo check if resource exists at given address Rogue has exists
function, which signature looks similar to this.
By using generics this function is made type-independent and you can use any resource type to check if it exists at address. Actually, anyone can check if resource exists at given address. But checking existence is not accessing stored value!
Let's write a function to check if user already has collection:
Now you know how to create a resource, how to rogue it to sender and how to check if resource already exists. It's time to learn to read this resource and to modify it!