Store & Get
Storage can be manipulated directly through the use of store() & get() functions. They utilize generics to store and retrieve values.
Declaration
To use store() & get() we must import them however we are not required to declare a storage block.
use std::storage::storage_api::{read, write};
Store
To store a generic value T we must provide a key of type b256.
In this example we store some number of type u64.
#[storage(write)]
fn store(key: b256, value: u64) {
// write(key, SLOT, T) where T = generic type
write(key, 0, value);
}
Get
To retrieve a generic value T at the position of key we must specify the type that we are retrieving.
In this example we retrieve some u64 at the position of key.
#[storage(read)]
fn get(key: b256) {
// read::<T>(key, SLOT) where T = generic type
let value = read::<u64>(key, 0);
}
The function get returns an Option; if the storage slots requested have not been set before, get will return None.