Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Module 0x1::storage_slot

use 0x1::features;
use 0x1::object;
use 0x1::signer;

Resource StorageSlotResource

struct StorageSlotResource<T> has key
Fields
val: T

Struct StorageSlot

struct StorageSlot<T> has store
Fields
addr: address

Constants

Storage slot natives are not enabled.

const ESTORAGE_SLOT_NATIVES_NOT_ENABLED: u64 = 1;

Resource under storage slot not found, this should never happen. Emitted by native functions.

const ESTORAGE_SLOT_NOT_FOUND: u64 = 2;

Function new

public fun new<T: store>(value: T): storage_slot::StorageSlot<T>
Implementation
public fun new<T: store>(value: T): StorageSlot<T> {
    let unique_signer = object::create_unique_onchain_signer().generate_signer_for_extending();
    move_to(&unique_signer, StorageSlotResource { val: value });
    StorageSlot { addr: unique_signer.address_of() }
}

Function borrow_storage_slot_resource

fun borrow_storage_slot_resource<T: store, BR>(self: &storage_slot::StorageSlot<T>): &BR
Implementation
native fun borrow_storage_slot_resource<T: store, BR>(self: &StorageSlot<T>): &BR;

Function borrow_storage_slot_resource_mut

fun borrow_storage_slot_resource_mut<T: store, BR>(self: &mut storage_slot::StorageSlot<T>): &mut BR
Implementation
native fun borrow_storage_slot_resource_mut<T: store, BR>(self: &mut StorageSlot<T>): &mut BR;

Function borrow

public fun borrow<T: store>(self: &storage_slot::StorageSlot<T>): &T
Implementation
public fun borrow<T: store>(self: &StorageSlot<T>): &T {
    assert!(std::features::is_storage_slot_natives_enabled(), ESTORAGE_SLOT_NATIVES_NOT_ENABLED);
    &self.borrow_storage_slot_resource<T, StorageSlotResource<T>>().val
}

Function borrow_mut

public fun borrow_mut<T: store>(self: &mut storage_slot::StorageSlot<T>): &mut T
Implementation
public fun borrow_mut<T: store>(self: &mut StorageSlot<T>): &mut T {
    assert!(std::features::is_storage_slot_natives_enabled(), ESTORAGE_SLOT_NATIVES_NOT_ENABLED);
    &mut self.borrow_storage_slot_resource_mut<T, StorageSlotResource<T>>().val
}

Function copy_storage_slot

public fun copy_storage_slot<T: copy, store>(self: &storage_slot::StorageSlot<T>): storage_slot::StorageSlot<T>
Implementation
public fun copy_storage_slot<T: store + copy>(self: &StorageSlot<T>): StorageSlot<T> {
    new(*self.borrow())
}

Function destroy

public fun destroy<T: store>(self: storage_slot::StorageSlot<T>): T
Implementation
public fun destroy<T: store>(self: StorageSlot<T>): T {
    let StorageSlot { addr } = self;
    let StorageSlotResource { val } = move_from<StorageSlotResource<T>>(addr);
    val
}

Specification

pragma verify = false;

Function new

public fun new<T: store>(value: T): storage_slot::StorageSlot<T>
pragma verify = false;

Function borrow

public fun borrow<T: store>(self: &storage_slot::StorageSlot<T>): &T
pragma opaque;
pragma verify = false;
aborts_if [abstract] false;

Function borrow_mut

public fun borrow_mut<T: store>(self: &mut storage_slot::StorageSlot<T>): &mut T
pragma opaque;
pragma verify = false;
aborts_if [abstract] false;

Function copy_storage_slot

public fun copy_storage_slot<T: copy, store>(self: &storage_slot::StorageSlot<T>): storage_slot::StorageSlot<T>
pragma verify = false;

Function destroy

public fun destroy<T: store>(self: storage_slot::StorageSlot<T>): T
pragma verify = false;