Reading & Writing

When dealing with storage we have two options, we can either read from or write to storage. In both cases we must use a storage annotation to indicate the purity of the function.

When referencing a variable in storage we must explicitly indicate that the variable comes from storage and not a local scope.

This is done via the syntax storage.variable_name e.g. storage.counter.

storage {
    counter: u64 = 0,
}

Reading from Storage

When dealing with a built-in type we can retrieve the variable without the use of any special methods.

#[storage(read)]
fn read() {
    let counter = storage.counter.read();
}

Writing to Storage

When dealing with a built-in type we can update the variable without the use of any special methods.

#[storage(write)]
fn write() {
    storage.counter.write(storage.counter.read() + 1);
}

Read & Write

We can read and write to storage by using both keywords in the attribute.

#[storage(read, write)]
fn read_write() {
    let counter = storage.counter.read();
    storage.counter.write(counter + 1);
}