Struct SCell

Source
pub struct SCell<Key: ?Sized, T: ?Sized> { /* private fields */ }
Expand description

SCell, or SingletonCell provides an interface of a Ghost Cell, where the Key is allowed to be any singleton, rather than a particular token type.

As a result, the uniqueness for the key can be provided by any means, and the key type / resulting cell may also be ’static for long-lived data structures

Implementations§

Source§

impl<Key: ?Sized, T: ?Sized> SCell<Key, T>

Source

pub fn from_mut(t: &mut T) -> &mut Self

Convert a unique reference to a value to a unique reference to this cell type. These two types are equivalent when accessed uniquely.

Source

pub fn get_mut(&mut self) -> &mut T

Uniquely borrow this cell in order to access T mutably.

This requires unique access to self, rather than using a key. For shared access, see the borrow_mut method instead.

Because this requires unique access to self, and all other borrows require at least a shared reference, unique access to the underlying data is safe.

Source

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

Get a raw pointer to this cell’s data.

Source§

impl<Key: ?Sized, T> SCell<Key, T>

Source

pub fn new(t: T) -> Self

Construct a new SCell from underlying data.

See also SCell::from_mut which can be used when the usage of SCell is only within the scope of mutable borrows of the data.

Source

pub fn into_inner(self) -> T

Destruct the SCell and access the inner data.

This requires ownership of the SCell and hence guarantees no simultaneous access.

Source§

impl<Key: ?Sized, T> SCell<Key, [T]>

Source

pub fn as_slice_of_cells(&self) -> &[SCell<Key, T>]

Returns a slice of cells from a cell of a slice.

Source§

impl<Key: Singleton + ?Sized, T: ?Sized> SCell<Key, T>

Source

pub fn borrow<'a>(&'a self, _: impl Exists<&'a Key>) -> &'a T
where Key: 'a,

Borrow the data underlying this cell, using a reference to the singleton Key type.

This is safe because any mutable borrow must either come from a mutable reference to this cell, or a mutable reference to the key, which cannot exist since this takes a shared borrow of both this cell and the key.

Source

pub fn borrow_mut<'a>(&'a self, _: impl Exists<&'a mut Key>) -> &'a mut T
where Key: 'a,

Mutably borrow the data underlying this cell, using a mutable reference to the singleton Key type.

This is safe because it requires a unique borrow of the key, and a shared borrow of self, which prevents all other borrows into the data.

Source§

impl<Key: Singleton + ?Sized, T> SCell<Key, T>

Source

pub fn replace<'a>(&'a self, value: T, token: impl Exists<&'a mut Key>) -> T
where Key: 'a,

Replace the value behind this cell with a new one.

This mutates the data and hence requires unique access to the key to ensure that no borrows are invalidated.

Source

pub fn take<'a>(&'a self, token: impl Exists<&'a mut Key>) -> T
where Key: 'a, T: Default,

Replace the value behind this cell with the default one.

This mutates the data and so requires unique access to the key to ensure that no borrows are invalidated.

Trait Implementations§

Source§

impl<Key, T: Send> Send for SCell<Key, T>

An owned SCell is equivalent to its underlying value, and can be converted between them, so SCell<Key, T> is Send if and only if T is Send

Source§

impl<Key, T: Send + Sync> Sync for SCell<Key, T>

A shared reference to SCell can access the underlying value both via shared reference, and via mutable reference, so it can be Sync only if T is both Sync and Send.

SCell does not otherwise put any constraints on Sync since all shared usages must use references to the Key, which must be sent between threads as normal.

Auto Trait Implementations§

§

impl<Key, T> !Freeze for SCell<Key, T>

§

impl<Key, T> !RefUnwindSafe for SCell<Key, T>

§

impl<Key, T> Unpin for SCell<Key, T>
where T: Unpin + ?Sized, Key: ?Sized,

§

impl<Key, T> UnwindSafe for SCell<Key, T>
where T: UnwindSafe + ?Sized, Key: ?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> Exists<T> for T

Source§

fn erase(self) -> Erased<T>

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>,

Source§

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>,

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.