pub struct StateBuilder<S> { /* private fields */ }
Expand description

An state builder that allows the creation of StateMap, StateSet, and StateBox. It is parametrized by a parameter S that is assumed to implement HasStateApi.

The state_builder is designed to provide an abstraction over the contract state, abstracting over the exact keys (keys in the sense of key-value store, which is the low-level semantics of contract state) that are used when storing specific values.

Implementations

Open a new state_builder. Only a single instance of the state_builder should exist during contract execution, thus this should only be called at the very beginning of execution.

Create a new empty StateMap.

Create a new empty StateSet.

Create a new StateBox and insert the value into the state. This stores the serialized value in the contract state. Thus if the StateBox is dropped without calling delete then the value will remain in contract state, leading to a space leak.

Note that this dropping can happen implicitly via assignment. For example,

struct MyState<S: HasStateApi> {
    inner: StateBox<u64, S>,
}
fn incorrect_replace<S: HasStateApi>(
    state_builder: &mut StateBuilder<S>,
    state: &mut MyState<S>,
) {
    // The following is incorrect. The old value of `inner` is not properly deleted.
    // from the state.
    state.inner = state_builder.new_box(0); // ⚠️
}

Instead, the old value should be manually deleted.

fn correct_replace<S: HasStateApi>(
    state_builder: &mut StateBuilder<S>,
    state: &mut MyState<S>,
) {
    let old_box = mem::replace(&mut state.inner, state_builder.new_box(0));
    old_box.delete()
}

Create a new Self with an empty TestStateApi.

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.