pub fn write<T>(slot: b256, offset: u64, value: T) 
Expand description

Stores value in storage, in slots of 32 bytes, starting at slot and offset given in words.

Deprecation Notice

This function is deprecated in favor of write_quads, write_slot, and update_slot.
To preserve exactly the same behavior as write, use write_quads. To store the value into
a single dynamic slot of a variable size, use write_slot. To update a portion of a dynamic slot,
or append to it, use update_slot.

Additional Information

The value can be stored in the slot or the following slots depending on the offset and size of value.
If the value crosses the boundary of a storage slot, writing continues at the following slot.

The offset is given in words and can be outside of the slot boundary. For example, offset 4 means
the beginning of the next slot, offset 5 means the second word of the next slot, and so on.

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.

The value is memory-copied into the storage slots. If it contains any pointers or references,
the data they point to will not be stored in storage.

To store dynamic types like Vec, String, or Bytes, use the dedicated storage types provided in the storage module,
like StorageVec, StorageString, and StorageBytes.

Arguments

  • slot: [b256] - The storage slot from which to count the offset. The value can be stored in this or the following slots.
  • offset: [u64] - An offset, in words, starting at the beginning of slot at which value should be stored.
  • value: [T] - The value to be stored.

Number of Storage Accesses

  • Reads: 0 if the value occupies full slots, 1 otherwise (to read the existing data that will be partially overwritten)
  • Writes: 1

Reverts

  • If the currently existing storage slots being read before writing the value have size different than 32 bytes.

Examples

use std::storage::storage_api::{read_quads, write_quads};

fn foo() {
    let five = 5_u64;
    write_quads(b256::zero(), 2, five);
    let stored_five = read_quads::<u64>(b256::zero(), 2).unwrap();
    assert_eq(five, stored_five);
}