Skip to main content

ContractCell

Struct ContractCell 

Source
pub struct ContractCell<T: ?Sized>(/* private fields */);
Expand description

A wrapped core::cell::Cell that implements Sync so it can be used in statics. Methods of the inner Cell are available via this wrapper’s Deref implementation. The wrapper adds no costs to Cell.

Note: For static storage that works in the test environment, use the contract_local! macro, e.g.:

contract_local! {
    static VAR: ContractCell<u32> = const { ContractCell::new(0) };
}

(This macro is a no-op in the guest environment.)

Implementations§

Source§

impl<T> ContractCell<T>

Source

pub const fn new(value: T) -> Self

Methods from Deref<Target = Cell<T>>§

1.0.0 · Source

pub fn set(&self, val: T)

Sets the contained value.

§Examples
use std::cell::Cell;

let c = Cell::new(5);

c.set(10);
1.17.0 · Source

pub fn swap(&self, other: &Cell<T>)

Swaps the values of two Cells.

The difference with std::mem::swap is that this function doesn’t require a &mut reference.

§Panics

This function will panic if self and other are different Cells that partially overlap. (Using just standard library methods, it is impossible to create such partially overlapping Cells. However, unsafe code is allowed to e.g. create two &Cell<[i32; 2]> that partially overlap.)

§Examples
use std::cell::Cell;

let c1 = Cell::new(5i32);
let c2 = Cell::new(10i32);
c1.swap(&c2);
assert_eq!(10, c1.get());
assert_eq!(5, c2.get());
1.17.0 · Source

pub fn replace(&self, val: T) -> T

Replaces the contained value with val, and returns the old contained value.

§Examples
use std::cell::Cell;

let cell = Cell::new(5);
assert_eq!(cell.get(), 5);
assert_eq!(cell.replace(10), 5);
assert_eq!(cell.get(), 10);
1.0.0 · Source

pub fn get(&self) -> T

Returns a copy of the contained value.

§Examples
use std::cell::Cell;

let c = Cell::new(5);

let five = c.get();
1.88.0 · Source

pub fn update(&self, f: impl FnOnce(T) -> T)

Updates the contained value using a function.

§Examples
use std::cell::Cell;

let c = Cell::new(5);
c.update(|x| x + 1);
assert_eq!(c.get(), 6);
1.12.0 · Source

pub fn as_ptr(&self) -> *mut T

Returns a raw pointer to the underlying data in this cell.

§Examples
use std::cell::Cell;

let c = Cell::new(5);

let ptr = c.as_ptr();
1.17.0 · Source

pub fn take(&self) -> T
where T: Default,

Takes the value of the cell, leaving Default::default() in its place.

§Examples
use std::cell::Cell;

let c = Cell::new(5);
let five = c.take();

assert_eq!(five, 5);
assert_eq!(c.into_inner(), 0);
Source

pub fn get_cloned(&self) -> Cell<T>

🔬This is a nightly-only experimental API. (cell_get_cloned)

Get a clone of the Cell that contains a copy of the original value.

This allows a cheaply Clone-able type like an Rc to be stored in a Cell, exposing the cheaper clone() method.

§Examples
#![feature(cell_get_cloned)]

use core::cell::Cell;
use std::rc::Rc;

let rc = Rc::new(1usize);
let c1 = Cell::new(rc);
let c2 = c1.get_cloned();
assert_eq!(*c2.into_inner(), 1);

Trait Implementations§

Source§

impl<T: ?Sized> Deref for ContractCell<T>

Source§

type Target = Cell<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: ?Sized> Sync for ContractCell<T>

Auto Trait Implementations§

§

impl<T> !Freeze for ContractCell<T>

§

impl<T> !RefUnwindSafe for ContractCell<T>

§

impl<T> Send for ContractCell<T>
where Cell<T>: Send, T: ?Sized,

§

impl<T> Unpin for ContractCell<T>
where Cell<T>: Unpin, T: ?Sized,

§

impl<T> UnsafeUnpin for ContractCell<T>
where Cell<T>: UnsafeUnpin, T: ?Sized,

§

impl<T> UnwindSafe for ContractCell<T>
where Cell<T>: UnwindSafe, T: ?Sized,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> StaticIfNoAlloc for T

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.