Struct ic_stable_structures::cell::Cell

source ·
pub struct Cell<T: Storable, M: Memory> { /* private fields */ }
Expand description

Represents a serializable value stored in the stable memory. It has semantics similar to “stable variables” in Motoko and share the same limitations. The main difference is that Cell writes its value to the memory on each assignment, not just in upgrade hooks. You should use cells only for small (up to a few MiB) values to keep upgrades safe.

Cell is a good choice for small read-only configuration values set once on canister installation and rarely updated.

Implementations§

source§

impl<T: Storable, M: Memory> Cell<T, M>

source

pub fn new(memory: M, value: T) -> Result<Self, ValueError>

Creates a new cell in the specified memory, overwriting the previous contents of the memory.

source

pub fn init(memory: M, default_value: T) -> Result<Self, InitError>

Initializes the value of the cell based on the contents of the memory. If the memory already contains a cell, initializes the cell with the decoded value. Otherwise, sets the cell value to default_value and writes it to the memory.

source

pub fn get(&self) -> &T

Returns the current value in the cell.

source

pub fn into_memory(self) -> M

Returns the underlying memory.

source

pub fn set(&mut self, value: T) -> Result<T, ValueError>

Updates the current value in the cell. If the new value is too large to fit into the memory, the value in the cell does not change.

Auto Trait Implementations§

§

impl<T, M> Freeze for Cell<T, M>
where M: Freeze, T: Freeze,

§

impl<T, M> RefUnwindSafe for Cell<T, M>

§

impl<T, M> Send for Cell<T, M>
where M: Send, T: Send,

§

impl<T, M> Sync for Cell<T, M>
where M: Sync, T: Sync,

§

impl<T, M> Unpin for Cell<T, M>
where M: Unpin, T: Unpin,

§

impl<T, M> UnwindSafe for Cell<T, M>
where M: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.