pub fn read_slot<T>(slot: b256, offset: u64) -> Option<T> 
Expand description

Reads a value of type T from a single dynamic slot, starting at the offset given in bytes.

Additional Information

If T is a zero-sized type, no storage access will occur. Storage API does not store zero-sized types in storage,
so reading from the slot and offset where a zero-sized type would be stored will return None.

Arguments

  • slot: [b256] - The storage slot from which to read a value.
  • offset: [u64] - An offset, in bytes, from the start of slot, from which the value should be read.

Returns

  • [Option] - Option(value) if the storage slot read was valid and contain value. Otherwise, None.

Number of Storage Accesses

  • Reads: 1

Reverts

  • If the offset is out of bounds of the currently used portion of the slot, if the slot is not empty.
  • If the storage slot is not large enough to contain a value of size of T at the given offset.

Examples

use std::storage::storage_api::{read_slot, append_slot};

fn foo() {
    let five = 5_u64;
    append_slot(b256::zero(), five);
    append_slot(b256::zero(), five + 1);
    let stored_five = read_slot::<u64>(b256::zero(), 0).unwrap();
    assert_eq(five, stored_five);
    let stored_six = read_slot::<u64>(b256::zero(), 1 * 8).unwrap();
    assert_eq(five + 1, stored_six);
}