Struct std::storage::storage_key::StorageKey
pub struct StorageKey<T> {
/// The assigned location in storage.
slot: b256,
/// The assigned offset based on the data structure `T`.
offset: u64,
/// A unique identifier.
field_id: b256,
}
Expand description
Describes a location in storage.
Additional Information
The location in storage is specified by the b256
key of a particular storage slot and an
offset, in words, from the start of the storage slot at key
. The parameter T
is the type of
the data to be read from or written to at offset
.
field_id
is a unique identifier for the storage field being referred to, it is different even
for multiple zero sized fields that might live at the same location but
represent different storage constructs.
Fields
slot: b256
The assigned location in storage.
offset: u64
The assigned offset based on the data structure T
.
field_id: b256
A unique identifier.
Trait Implementations
impl AbiEncode for StorageKey<T>
impl AbiEncode for StorageKey<T>
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for StorageKey<T>
impl AbiDecode for StorageKey<T>
pub fn abi_decode(refmut buffer: BufferReader) -> Self
impl StorageKey for StorageKey<T>
impl StorageKey for StorageKey<T>
pub fn new(slot: b256, offset: u64, field_id: b256) -> Self
pub fn new(slot: b256, offset: u64, field_id: b256) -> Self
Create a new StorageKey
.
Arguments
slot
: [b256] - The assigned location in storage for the newStorageKey
.offset
: [u64] - The assigned offset based on the data structureT
for the newStorageKey
.field_id
: [b256] - A unique identifier for the newStorageKey
.
Returns
- [StorageKey] - The newly created
StorageKey
.
Examples
use std::hash::sha256;
fn foo() {
let my_key = StorageKey::<u64>::new(b256::zero(), 0, sha256(b256::zero()));
assert(my_key.slot() == b256::zero());
}
pub fn slot(self) -> b256
pub fn slot(self) -> b256
Returns the storage slot address.
Returns
- [b256] - The address in storage that this storage slot points to.
Examples
use std::hash::sha256;
fn foo() {
let my_key = StorageKey::<u64>::new(b256::zero(), 0, sha256(b256::zero()));
assert(my_key.slot() == b256::zero());
}
pub fn offset(self) -> u64
pub fn offset(self) -> u64
Returns the offset on the storage slot.
Returns
- [u64] - The offset in storage that this storage slot points to.
Examples
use std::hash::sha256;
fn foo() {
let my_key = StorageKey::<u64>::new(b256::zero(), 0, sha256(b256::zero()));
assert(my_key.offset() == 0);
}
pub fn field_id(self) -> b256
pub fn field_id(self) -> b256
Returns the storage slot field id.
Additional Information
The field id is a unique identifier for the storage field being referred to, it is different even
for multiple zero sized fields that might live at the same location but
represent different storage constructs.
Returns
- [b256] - The field id for this storage slot.
Examples
use std::hash::sha256;
fn foo() {
let my_key = StorageKey::<u64>::new(b256::zero(), 0, sha256(b256::zero()));
assert(my_key.field_id() == sha256(b256::zero()));
}
impl StorageKey for StorageKey<T>
impl StorageKey for StorageKey<T>
pub fn read(self) -> T
pub fn read(self) -> T
Reads a value of type T
starting at the location specified by self
. If the value
crosses the boundary of a storage slot, reading continues at the following slot.
Returns
- [T] - Returns the value previously stored if a the storage slots read were
valid and containvalue
. Reverts otherwise.
Number of Storage Accesses
- Reads:
1
Examples
fn foo() {
let r: StorageKey<u64> = StorageKey::new(b256::zero(), 2, b256::zero());
// Reads the third word from storage slot with key 0x000...0
let x: u64 = r.read();
}
pub fn try_read(self) -> Option<T>
pub fn try_read(self) -> Option<T>
Reads a value of type T
starting at the location specified by self
. If the value
crosses the boundary of a storage slot, reading continues at the following slot.
Returns
- [Option] - Returns
Some(value)
if a the storage slots read were valid and containvalue
.
Otherwise, returnNone
.
Number of Storage Accesses
- Reads:
1
Examples
fn foo() {
let r: StorageKey<u64> = StorageKey::new(b256::zero(), 2, b256::zero());
// Reads the third word from storage slot with key 0x000...0
let x: Option<u64> = r.try_read();
}
pub fn write(self, value: T)
pub fn write(self, value: T)
Writes a value of type T
starting at the location specified by self
. If the value
crosses the boundary of a storage slot, writing continues at the following slot.
Arguments
value
: [T] - The value of typeT
to write.
Number of Storage Accesses
- Reads:
1
- Writes:
1
Examples
fn foo() {
let r: StorageKey<u64> = StorageKey::new(b256::zero(), 2, b256::zero());
// Writes 42 at the third word of storage slot with key 0x000...0
let x = r.write(42);
}
pub fn clear(self) -> bool
pub fn clear(self) -> bool
Clears the value at self
.
Number of Storage Accesses
- Clears:
1
Examples
fn foo() {
let r: StorageKey<u64> = StorageKey::new(b256::zero(), 2, b256::zero());
r.write(42);
let cleared = r.clear();
assert(cleared);
}
pub fn zero() -> Self
pub fn zero() -> Self
Returns the zero value for the StorageKey<T>
type.
Returns
- [StorageKey] -> The zero value for the
StorageKey<T>
type.
Examples
fn foo() {
let zero_storage_key: StorageKey<u64> = StorageKey::zero();
assert(zero_storage_key.slot() == b256::zero());
assert(zero_storage_key.offset() == 0);
assert(zero_storage_key.field_id() == b256::zero());
}
pub fn is_zero(self) -> bool
pub fn is_zero(self) -> bool
Returns whether a StorageKey<T>
is set to zero.
Returns
- [bool] -> True if the
StorageKey<T>
is set to zero, otherwise false.
Examples
fn foo() {
let zero_storage_key: StorageKey<u64> = StorageKey::zero();
assert(zero_storage_key.is_zero());
}
impl StorageKey for StorageKey<StorageMap<K, V>>
impl StorageKey for StorageKey<StorageMap<K, V>>
pub fn insert(
self,
key: K,
value: V,
)
pub fn insert(
self,
key: K,
value: V,
)
Inserts a key-value pair into the map.
Arguments
key
: [K] - The key to which the value is paired.value
: [V] - The value to be stored.
Number of Storage Accesses
- Reads:
1
- Writes:
1
Examples
storage {
map: StorageMap<u64, bool> = StorageMap {}
}
fn foo() {
let key = 5_u64;
let value = true;
storage.map.insert(key, value);
let retrieved_value = storage.map.get(key).read();
assert(value == retrieved_value);
}
pub fn get(self, key: K) -> StorageKey<V>
pub fn get(self, key: K) -> StorageKey<V>
Retrieves the StorageKey
that describes the raw location in storage of the value
stored at key
, regardless of whether a value is actually stored at that location or not.
Arguments
key
: [K] - The key to which the value is paired.
Returns
- [StorageKey] - Describes the raw location in storage of the value stored at
key
.
Examples
storage {
map: StorageMap<u64, bool> = StorageMap {}
}
fn foo() {
let key = 5_u64;
let value = true;
storage.map.insert(key, value);
let retrieved_value = storage.map.get(key).read();
assert(value == retrieved_value);
}
pub fn remove(self, key: K) -> bool
pub fn remove(self, key: K) -> bool
Clears a value previously stored using a key
Arguments
key
: [K] - The key to which the value is paired.
Returns
- [bool] - Indicates whether there was a value previously stored at
key
.
Number of Storage Accesses
- Clears:
1
Examples
storage {
map: StorageMap<u64, bool> = StorageMap {}
}
fn foo() {
let key = 5_u64;
let value = true;
storage.map.insert(key, value);
let removed = storage.map.remove(key);
assert(removed);
assert(storage.map.get(key).is_none());
}
pub fn try_insert(
self,
key: K,
value: V,
) -> Result<V, StorageMapError<V>>
pub fn try_insert(
self,
key: K,
value: V,
) -> Result<V, StorageMapError<V>>
Inserts a key-value pair into the map if a value does not already exist for the key.
Arguments
key
: [K] - The key to which the value is paired.value
: [V] - The value to be stored.
Returns
- [Result<V, StorageMapError>] -
Result::Ok(value)
if the value was inserted, orResult::Err(StorageMapError::OccupiedError(pre_existing_value))
if a value already existed for the key.
Number of Storage Accesses
- Reads:
1
- Writes:
1
Examples
use std::storage::storage_map::StorageMapError;
storage {
map: StorageMap<u64, bool> = StorageMap {}
}
fn foo() {
let key = 5_u64;
let value = true;
storage.map.insert(key, value);
let new_value = false;
let result = storage.map.try_insert(key, new_value);
assert(result == Result::Err(StorageMapError::OccupiedError(value))); // The old value is returned.
let retrieved_value = storage.map.get(key).read();
assert(value == retrieved_value); // New value was not inserted, as a value already existed.
let key2 = 10_u64;
let returned_value = storage.map.try_insert(key2, new_value);
assert(returned_value == Result::Ok(new_value)); // New value is returned.
}
fn get_slot_key(self, key: K) -> b256
impl StorageKey for StorageKey<StorageVec<V>>
impl StorageKey for StorageKey<StorageVec<V>>
pub fn push(self, value: V)
pub fn push(self, value: V)
Appends the value to the end of the vector.
Arguments
value
: [V] - The item being added to the end of the vector.
Number of Storage Accesses
- Reads:
3
- Writes:
2
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
let five = 5_u64;
storage.vec.push(five);
assert(five == storage.vec.get(0).unwrap().read());
}
pub fn pop(self) -> Option<V>
pub fn pop(self) -> Option<V>
Removes the last element of the vector and returns it, None
if empty.
Returns
- [Option] - The last element
V
orNone
.
Number of Storage Accesses
- Reads:
3
- Writes:
1
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
let five = 5_u64;
storage.vec.push(five);
let popped_value = storage.vec.pop().unwrap();
assert(five == popped_value);
let none_value = storage.vec.pop();
assert(none_value.is_none());
}
pub fn get(self, index: u64) -> Option<StorageKey<V>>
pub fn get(self, index: u64) -> Option<StorageKey<V>>
Gets the value in the given index, None
if index is out of bounds.
Arguments
index
: [u64] - The index of the vec to retrieve the item from.
Returns
- [Option<StorageKey>] - Describes the raw location in storage of the value stored at
key
orNone
if out of bounds.
Number of Storage Accesses
- Reads:
1
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
let five = 5_u64;
storage.vec.push(five);
assert(five == storage.vec.get(0).unwrap().read());
assert(storage.vec.get(1).is_none());
}
pub fn remove(self, index: u64) -> V
pub fn remove(self, index: u64) -> V
Removes the element in the given index and moves all the elements in the following indexes
down one index. Also returns the element.
Additional Information
WARNING: Expensive for larger vecs.
Arguments
index
: [u64] - The index of the vec to remove the item from.
Returns
- [V] - The element that has been removed at the index.
Reverts
- Reverts if index is larger or equal to length of the vec.
Number of Storage Accesses
- Reads:
3 + (2 * (self.len() - index))
- Writes:
self.len() - index
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
storage.vec.push(5);
storage.vec.push(10);
storage.vec.push(15);
let removed_value = storage.vec.remove(1);
assert(10 == removed_value);
assert(storage.vec.len() == 2);
}
pub fn swap_remove(self, index: u64) -> V
pub fn swap_remove(self, index: u64) -> V
Removes the element at the specified index and fills it with the last element.
This does not preserve ordering and returns the element.
Arguments
index
: [u64] - The index of the vec to remove the item from.
Returns
- [V] - The element that has been removed at the index.
Reverts
- Reverts if index is larger or equal to length of the vec.
Number of Storage Accesses
- Reads:
5
- Writes:
2
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
storage.vec.push(5);
storage.vec.push(10);
storage.vec.push(15);
let removed_value = storage.vec.swap_remove(0);
assert(5 == removed_value);
let swapped_value = storage.vec.get(0).unwrap().read();
assert(15 == swapped_value);
}
pub fn set(
self,
index: u64,
value: V,
)
pub fn set(
self,
index: u64,
value: V,
)
Sets or mutates the value at the given index.
Arguments
index
: [u64] - The index of the vec to set the value atvalue
: [V] - The value to be set
Reverts
- Reverts if index is larger than or equal to the length of the vec.
Number of Storage Accesses
- Reads:
2
- Writes:
1
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
storage.vec.push(5);
storage.vec.push(10);
storage.vec.push(15);
storage.vec.set(0, 20);
let set_value = storage.vec.get(0).unwrap().read();
assert(20 == set_value);
}
pub fn insert(
self,
index: u64,
value: V,
)
pub fn insert(
self,
index: u64,
value: V,
)
Inserts the value at the given index, moving the current index’s value
as well as the following index’s value up by one index.
Additional Information
WARNING: Expensive for larger vecs.
Arguments
index
: [u64] - The index of the vec to insert the item into.value
: [V] - The value to insert into the vec.
Reverts
- Reverts if index is larger than the length of the vec.
Number of Storage Accesses
- Reads:
if self.len() == index { 3 } else { 5 + (2 * (self.len() - index)) }
- Writes:
if self.len() == index { 2 } else { 2 + self.len() - index }
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
storage.vec.push(5);
storage.vec.push(15);
storage.vec.insert(1, 10);
assert(5 == storage.vec.get(0).unwrap().read());
assert(10 == storage.vec.get(1).unwrap().read());
assert(15 == storage.vec.get(2).unwrap().read());
}
pub fn len(self) -> u64
pub fn len(self) -> u64
Returns the length of the vector.
Returns
- [u64] - The stored length of the vector.
Number of Storage Accesses
- Reads:
1
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
assert(0 == storage.vec.len());
storage.vec.push(5);
assert(1 == storage.vec.len());
storage.vec.push(10);
assert(2 == storage.vec.len());
}
pub fn is_empty(self) -> bool
pub fn is_empty(self) -> bool
Checks whether the len is zero or not.
Returns
- [bool] - Indicates whether the vector is or is not empty.
Number of Storage Accesses
- Reads:
1
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
assert(true == storage.vec.is_empty());
storage.vec.push(5);
assert(false == storage.vec.is_empty());
storage.vec.clear();
assert(true == storage.vec.is_empty());
}
pub fn swap(
self,
element1_index: u64,
element2_index: u64,
)
pub fn swap(
self,
element1_index: u64,
element2_index: u64,
)
Swaps two elements.
Arguments
element1_index
: [u64] - The index of the first element.element2_index
: [u64] - The index of the second element.
Reverts
- If
element1_index
orelement2_index
is greater than the length of the vector.
Number of Storage Accesses
- Reads:
5
- Writes:
2
Examples
use std::storage::storage_vec::*;
storage {
vec: StorageVec<u64> = StorageVec {}
}
fn foo() {
storage.vec.push(5);
storage.vec.push(10);
storage.vec.push(15);
storage.vec.swap(0, 2);
assert(15 == storage.vec.get(0).unwrap().read());
assert(10 == storage.vec.get(1).unwrap().read());
assert(5 == storage.vec.get(2).unwrap().read());
pub fn first(self) -> Option<StorageKey<V>>
pub fn first(self) -> Option<StorageKey<V>>
Returns the first element of the vector, or None
if it is empty.
Returns
- [Option<StorageKey>] - Describes the raw location in storage of the value stored at
the start of the vector or zero if the vector is empty.
Number of Storage Accesses
- Reads:
1
Examples
storage {
vec: StorageVec<u64> = StorageVec {},
}
fn foo() {
assert(storage.vec.first().is_none());
storage.vec.push(5);
assert(5 == storage.vec.first().unwrap().read());
}
pub fn last(self) -> Option<StorageKey<V>>
pub fn last(self) -> Option<StorageKey<V>>
Returns the last element of the vector, or None
if it is empty.
Returns
- [Option<StorageKey>] - Describes the raw location in storage of the value stored at
the end of the vector or zero if the vector is empty.
Number of Storage Accesses
- Reads:
1
Examples
storage {
vec: StorageVec<u64> = StorageVec {},
}
fn foo() {
assert(storage.vec.last().is_none());
storage.vec.push(5);
storage.vec.push(10);
assert(10 == storage.vec.last().unwrap().read());
}
pub fn reverse(self)
pub fn reverse(self)
Reverses the order of elements in the vector, in place.
Number of Storage Accesses
- Reads:
1 + (3 * (self.len() / 2))
- Writes:
2 * (self.len() / 2)
Examples
storage {
vec: StorageVec<u64> = StorageVec {},
}
fn foo() {
storage.vec.push(5);
storage.vec.push(10);
storage.vec.push(15);
storage.vec.reverse();
assert(15 == storage.vec.get(0).unwrap().read());
assert(10 == storage.vec.get(1).unwrap().read());
assert(5 == storage.vec.get(2).unwrap().read());
}
pub fn fill(self, value: V)
pub fn fill(self, value: V)
Fills self
with elements by cloning value
.
Arguments
value
: [V] - Value to copy to each element of the vector.
Number of Storage Accesses
- Reads:
1 + self.len()
- Writes:
self.len()
Examples
storage {
vec: StorageVec<u64> = StorageVec {},
}
fn foo() {
storage.vec.push(5);
storage.vec.push(10);
storage.vec.push(15);
storage.vec.fill(20);
assert(20 == storage.vec.get(0).unwrap().read());
assert(20 == storage.vec.get(1).unwrap().read());
assert(20 == storage.vec.get(2).unwrap().read());
}
pub fn resize(
self,
new_len: u64,
value: V,
)
pub fn resize(
self,
new_len: u64,
value: V,
)
Resizes self
in place so that len
is equal to new_len
.
Additional Information
If new_len
is greater than len
, self
is extended by the difference, with each
additional slot being filled with value
. If the new_len
is less than len
, self
is
simply truncated.
Arguments
new_len
: [u64] - The new length to expand or truncate tovalue
: [V] - The value to fill into new slots if thenew_len
is greater than the current length
Number of Storage Accesses
- Reads -
if new_len > self.len() { new_len - len + 2 } else { 2 }
- Writes -
if new_len > self.len() { new_len - len + 1 } else { 1 }
Examples
storage {
vec: StorageVec<u64> = StorageVec {},
}
fn foo() {
storage.vec.push(5);
storage.vec.push(10);
storage.vec.resize(4, 20);
assert(5 == storage.vec.get(0).unwrap().read());
assert(10 == storage.vec.get(1).unwrap().read());
assert(20 == storage.vec.get(2).unwrap().read());
assert(20 == storage.vec.get(3).unwrap().read());
storage.vec.resize(2, 0);
assert(5 == storage.vec.get(0).unwrap().read());
assert(10 == storage.vec.get(1).unwrap().read());
assert(None == storage.vec.get(2));
assert(None == storage.vec.get(3));
}
pub fn store_vec(
self,
vec: Vec<V>,
)
pub fn store_vec(
self,
vec: Vec<V>,
)
Stores a Vec
as a StorageVec
.
Additional Information
This will overwrite any existing values in the StorageVec
.
Arguments
vec
: [Vec] - The vector to store in storage.
Number of Storage Accesses
- Writes -
2
Examples
storage {
vec: StorageVec<u64> = StorageVec {},
}
fn foo() {
let mut vec = Vec::<u64>::new();
vec.push(5);
vec.push(10);
vec.push(15);
storage.vec.store_vec(vec);
assert(5 == storage.vec.get(0).unwrap().read());
assert(10 == storage.vec.get(1).unwrap().read());
assert(15 == storage.vec.get(2).unwrap().read());
}
pub fn load_vec(self) -> Vec<V>
pub fn load_vec(self) -> Vec<V>
Load a Vec
from the StorageVec
.
Additional Information
This method does not work for any V
type that has a 0 size, such as StorageVec
itself. Meaning you cannot use this method on a StorageVec<StorageVec<T>>
.
Returns
- [Option<Vec>] - The vector constructed from storage or
None
.
Reverts
- If the size of type
V
is 0.
Number of Storage Accesses
- Reads -
2
Examples
storage {
vec: StorageVec<u64> = StorageVec {},
}
fn foo() {
let mut vec = Vec::<u64>::new();
vec.push(5);
vec.push(10);
vec.push(15);
storage.vec.store_vec(vec);
let returned_vec = storage.vec.load_vec();
assert(5 == returned_vec.get(0).unwrap());
assert(10 == returned_vec.get(1).unwrap());
assert(15 == returned_vec.get(2).unwrap());
}
pub fn iter(self) -> StorageVecIter<V>
pub fn iter(self) -> StorageVecIter<V>
Returns an [Iterator] to iterate over this StorageVec
.
Returns
- [StorageVecIter] - The struct which can be iterated over.
Examples
storage {
vec: StorageVec<u64> = StorageVec {},
}
fn foo() {
storage.vec.push(5);
storage.vec.push(10);
storage.vec.push(15);
// Get the iterator
let iter = storage.vec.iter();
assert_eq(5, iter.next().unwrap().read());
assert_eq(10, iter.next().unwrap().read());
assert_eq(15, iter.next().unwrap().read());
for elem in storage.vec.iter() {
let elem_value = elem.read();
log(elem_value);
}
}
impl StorableSlice<Bytes> for StorageKey<StorageBytes>
impl StorableSlice<Bytes> for StorageKey<StorageBytes>
pub fn clear(self) -> bool
pub fn clear(self) -> bool
Clears a collection of tightly packed bytes in storage.
Returns
- [bool] - Indicates whether all of the storage slots cleared were previously set.
Number of Storage Accesses
- Reads:
1
- Clears:
2
Examples
use std::{storage::storage_bytes::StorageBytes, bytes::Bytes};
storage {
stored_bytes: StorageBytes = StorageBytes {}
}
fn foo() {
let mut bytes = Bytes::new();
bytes.push(5_u8);
bytes.push(7_u8);
bytes.push(9_u8);
storage.stored_bytes.write_slice(bytes);
assert(storage.stored_bytes.read_slice().is_some());
let cleared = storage.stored_bytes.clear();
assert(cleared);
let retrieved_bytes = storage.stored_bytes.read_slice();
assert(retrieved_bytes.is_none());
}
pub fn len(self) -> u64
pub fn len(self) -> u64
Returns the length of tightly packed bytes in storage.
Returns
- [u64] - The length of the bytes in storage.
Number of Storage Accesses
- Reads:
1
Examples
use std::{storage::storage_bytes::StorageBytes, bytes::Bytes};
storage {
stored_bytes: StorageBytes = StorageBytes {}
}
fn foo() {
let mut bytes = Bytes::new();
bytes.push(5_u8);
bytes.push(7_u8);
bytes.push(9_u8);
assert(storage.stored_bytes.len() == 0)
storage.stored_bytes.write_slice(bytes);
assert(storage.stored_bytes.len() == 3);
}
pub fn read_slice(self) -> Option<Bytes>
pub fn read_slice(self) -> Option<Bytes>
Constructs a Bytes
type from a collection of tightly packed bytes in storage.
Returns
- [Option] - The valid
Bytes
stored, otherwiseNone
.
Number of Storage Accesses
- Reads:
2
Examples
use std::{storage::storage_bytes::StorageBytes, bytes::Bytes};
storage {
stored_bytes: StorageBytes = StorageBytes {}
}
fn foo() {
let mut bytes = Bytes::new();
bytes.push(5_u8);
bytes.push(7_u8);
bytes.push(9_u8);
assert(storage.stored_bytes.read_slice().is_none());
storage.stored_bytes.write_slice(bytes);
let retrieved_bytes = storage.stored_bytes.read_slice().unwrap();
assert(bytes == retrieved_bytes);
}
pub fn write_slice(
self,
bytes: Bytes,
)
pub fn write_slice(
self,
bytes: Bytes,
)
Takes a Bytes
type and stores the underlying collection of tightly packed bytes.
Arguments
bytes
: [Bytes] - The bytes which will be stored.
Number of Storage Accesses
- Writes:
2
Examples
use std::{storage::storage_bytes::StorageBytes, bytes::Bytes};
storage {
stored_bytes: StorageBytes = StorageBytes {}
}
fn foo() {
let mut bytes = Bytes::new();
bytes.push(5_u8);
bytes.push(7_u8);
bytes.push(9_u8);
storage.stored_bytes.write_slice(bytes);
}
impl StorableSlice<String> for StorageKey<StorageString>
impl StorableSlice<String> for StorageKey<StorageString>
pub fn clear(self) -> bool
pub fn clear(self) -> bool
Clears a stored String
in storage.
Returns
- [bool] - Indicates whether all of the storage slots cleared were previously set.
Number of Storage Accesses
- Reads:
1
- Clears:
2
Examples
use std::{storage::storage_string::StorageString, string::String};
storage {
stored_string: StorageString = StorageString {}
}
fn foo() {
let string = String::from_ascii_str("Fuel is blazingly fast");
storage.stored_string.write_slice(string);
assert(storage.stored_string.read_slice().is_some());
let cleared = storage.stored_string.clear();
assert(cleared);
let retrieved_string = storage.stored_string.read_slice();
assert(retrieved_string.is_none());
}
pub fn len(self) -> u64
pub fn len(self) -> u64
Returns the length of String
in storage.
Returns
- [u64] - The length of the bytes in storage.
Number of Storage Accesses
- Reads:
1
Examples
use std::{storage::storage_string::StorageString, string::String};
storage {
stored_string: StorageString = StorageString {}
}
fn foo() {
let string = String::from_ascii_str("Fuel is blazingly fast");
assert(storage.stored_string.len() == 0)
storage.stored_string.write_slice(string);
assert(storage.stored_string.len() == 3);
}
pub fn read_slice(self) -> Option<String>
pub fn read_slice(self) -> Option<String>
Constructs a String
type from storage.
Returns
- [Option] - The valid
String
stored, otherwiseNone
.
Number of Storage Accesses
- Reads:
2
Examples
use std::{storage::storage_string::StorageString, string::String};
storage {
stored_string: StorageString = StorageString {}
}
fn foo() {
let string = String::from_ascii_str("Fuel is blazingly fast");
assert(storage.stored_string.read_slice().is_none());
storage.stored_string.write_slice(string);
let retrieved_string = storage.stored_string.read_slice().unwrap();
assert(string == retrieved_string);
}
pub fn write_slice(
self,
string: String,
)
pub fn write_slice(
self,
string: String,
)
Takes a String
type and saves the underlying data in storage.
Arguments
string
: [String] - The string which will be stored.
Number of Storage Accesses
- Writes:
2
Examples
use std::{storage::storage_string::StorageString, string::String};
storage {
stored_string: StorageString = StorageString {}
}
fn foo() {
let string = String::from_ascii_str("Fuel is blazingly fast");
storage.stored_string.write_slice(string);
}