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>
impl<Key: ?Sized, T: ?Sized> SCell<Key, T>
Sourcepub fn from_mut(t: &mut T) -> &mut Self
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.
Sourcepub fn get_mut(&mut self) -> &mut T
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§impl<Key: ?Sized, T> SCell<Key, T>
impl<Key: ?Sized, T> SCell<Key, T>
Sourcepub fn new(t: T) -> Self
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.
Sourcepub fn into_inner(self) -> T
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]>
impl<Key: ?Sized, T> SCell<Key, [T]>
Sourcepub fn as_slice_of_cells(&self) -> &[SCell<Key, T>]
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>
impl<Key: Singleton + ?Sized, T: ?Sized> SCell<Key, T>
Sourcepub fn borrow<'a>(&'a self, _: impl Exists<&'a Key>) -> &'a Twhere
Key: 'a,
pub fn borrow<'a>(&'a self, _: impl Exists<&'a Key>) -> &'a Twhere
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.
Sourcepub fn borrow_mut<'a>(&'a self, _: impl Exists<&'a mut Key>) -> &'a mut Twhere
Key: 'a,
pub fn borrow_mut<'a>(&'a self, _: impl Exists<&'a mut Key>) -> &'a mut Twhere
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>
impl<Key: Singleton + ?Sized, T> SCell<Key, T>
Sourcepub fn replace<'a>(&'a self, value: T, token: impl Exists<&'a mut Key>) -> Twhere
Key: 'a,
pub fn replace<'a>(&'a self, value: T, token: impl Exists<&'a mut Key>) -> Twhere
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.
Sourcepub fn take<'a>(&'a self, token: impl Exists<&'a mut Key>) -> Twhere
Key: 'a,
T: Default,
pub fn take<'a>(&'a self, token: impl Exists<&'a mut Key>) -> Twhere
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§
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
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.