Skip to main content

GhostToken

Struct GhostToken 

Source
pub struct GhostToken<'brand> { /* private fields */ }
Expand description

A GhostToken<'x> is the key to access the content of any &GhostCell<'x, _> sharing the same brand.

Each GhostToken<'x> is created alongside a unique brand (its lifetime), and each GhostCell<'x, T> is associated to one, and only one, GhostToken at a time via this brand. The entire set of GhostCell<'x, T> associated to a given GhostToken<'x> creates a pool of cells all being accessible solely through the one token they are associated to.

The pool of GhostCell associated to a token need not be homogeneous, each may own a value of a different type.

Implementations§

Source§

impl<'brand> GhostToken<'brand>

Source

pub fn new<R, F>(fun: F) -> R
where for<'new_brand> F: FnOnce(GhostToken<'new_brand>) -> R,

Creates a fresh token to which GhostCells can be tied to later.

Due to the use of a lifetime, the GhostCells tied to a given token can only live within the confines of the invocation of the fun closure.

§Example
use ghost_cell::{GhostToken, GhostCell};

let n = 12;

let value = GhostToken::new(|mut token| {
    let cell = GhostCell::new(42);

    let vec: Vec<_> = (0..n).map(|_| &cell).collect();

    *vec[n / 2].borrow_mut(&mut token) = 33;

    *cell.borrow(&token)
});

assert_eq!(33, value);

Trait Implementations§

Source§

impl<'brand> Send for GhostToken<'brand>

A GhostToken is stateless, therefore it can safely be passed across threads.

Source§

impl<'brand> Sync for GhostToken<'brand>

A GhostToken is stateless, therefore it can safely be accessed from different threads.

Auto Trait Implementations§

§

impl<'brand> Freeze for GhostToken<'brand>

§

impl<'brand> RefUnwindSafe for GhostToken<'brand>

§

impl<'brand> Unpin for GhostToken<'brand>

§

impl<'brand> UnsafeUnpin for GhostToken<'brand>

§

impl<'brand> UnwindSafe for GhostToken<'brand>

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

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.