pub struct GhostCell<'brand, T: ?Sized> { /* private fields */ }
Expand description

Branded wrapper for a value, whose type is T.

A GhostCell<'x, T> owns an instance of type T:

  • Unique access to the cell allows unimpeded access to the contained value.
  • Shared access to the cell requires mediating access through the associated GhostToken<'x, T> which will enforce at compile-time the aliasing XOR mutability safety property.

Implementations§

source§

impl<'brand, T> GhostCell<'brand, T>

source

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

Wraps some T into a GhostCell with brand 'brand which associates it to one, and only one, GhostToken.

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

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

    assert_eq!(42, *cell.borrow(&token));
});
source

pub fn into_inner(self) -> T

Turns an owned GhostCell back into owned data.

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

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

    cell.into_inner()
});

assert_eq!(42, value);
source§

impl<'brand, T: ?Sized> GhostCell<'brand, T>

source

pub fn borrow<'a>(&'a self, _: &'a GhostToken<'brand>) -> &'a T

Immutably borrows the GhostCell with the same-branded token.

§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();

    let one: &i32 = vec[1].borrow(&token);
    let two: &i32 = vec[2].borrow(&token);

    *one + *two
});

assert_eq!(84, value);
source

pub fn borrow_mut<'a>(&'a self, _: &'a mut GhostToken<'brand>) -> &'a mut T

Mutably borrows the GhostCell with the same-branded token.

§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();

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

    *cell.borrow(&token)
});

assert_eq!(33, value);
source

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

Returns a raw pointer to the contained value.

source

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

Turns a mutably borrowed GhostCell into mutably borrowed data.

self is mutably borrowed for the lifetime of the result, ensuring the absence of aliasing.

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

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

    *cell.get_mut() = 33;

    *cell.borrow(&token)
});

assert_eq!(33, value);
source

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

Turns mutably borrowed data into a mutably borrowed GhostCell.

t is mutably borrowed for the lifetime of the result, ensuring the absence of aliasing.

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

let n = 12;
let mut value = 42;

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

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

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

assert_eq!(33, value);
source§

impl<'brand, T> GhostCell<'brand, T>

source

pub fn replace(&self, value: T, token: &mut GhostToken<'brand>) -> T

Returns the value, replacing it by the supplied one.

§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();

    let previous = vec[n / 2].replace(33, &mut token);
    assert_eq!(42, previous);

    *cell.borrow(&token)
});

assert_eq!(33, value);
source

pub fn take(&self, token: &mut GhostToken<'brand>) -> T
where T: Default,

Returns the value, replacing it with the default value.

§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();

    let previous = vec[n / 2].take(&mut token);
    assert_eq!(42, previous);

    *cell.borrow(&token)
});

assert_eq!(0, value);
source§

impl<'brand, T> GhostCell<'brand, [T]>

source

pub fn as_slice_of_cells(&self) -> &[GhostCell<'brand, T>]

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

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

let n = 12;

let value = GhostToken::new(|mut token| {
    let mut vec: Vec<_> = (0..n).collect();
    let cell = GhostCell::from_mut(&mut vec[..]);

    let slice = cell.as_slice_of_cells();

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

    vec[n / 2]
});

assert_eq!(33, value);
source

pub fn from_slice_of_cells<'slice>( slice: &'slice [GhostCell<'brand, T>] ) -> &'slice Self

Returns a cell containing a slice from slice of cells.

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

let n = 12;

let value = GhostToken::new(|mut token| {
    let mut vec: Vec<_> = (0..n).collect();
    let cell = GhostCell::from_mut(&mut vec[..]);
    let all_elements = cell.as_slice_of_cells();

    let quad = GhostCell::from_slice_of_cells(&all_elements[4..][..4]);
    quad.borrow_mut(&mut token).copy_from_slice(&33u32.to_be_bytes());

    vec.iter().position(|&v| v == 33)
});

assert_eq!(Some(7), value);
source§

impl<'brand, T, const N: usize> GhostCell<'brand, [T; N]>

source

pub fn as_array_of_cells(&self) -> &[GhostCell<'brand, T>; N]

Returns a reference to an array of cells from a cell containing an array.

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

let value = GhostToken::new(|mut token| {
    let mut array = [0, 1, 2, 3, 5];
    let n = array.len();

    let cell = GhostCell::from_mut(&mut array);

    let inner = cell.as_array_of_cells();

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

    array[n / 2]
});

assert_eq!(33, value);
source

pub fn from_array_of_cells<'a>(array: &'a [GhostCell<'brand, T>; N]) -> &'a Self

Returns a cell containing an array from an array of cells.

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

let value = GhostToken::new(|mut token| {
    let mut array = [0, 1, 2, 3, 5];
    let n = array.len();

    let cell = GhostCell::from_mut(&mut array);

    let inner = cell.as_array_of_cells();

    let inner = GhostCell::from_array_of_cells(&inner);

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

    array[n / 2]
});

assert_eq!(33, value);
source§

impl<'brand> GhostCell<'brand, ()>

source

pub fn as_tuple_of_cells(&self) -> &()

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>(tuple: &'a ()) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0> GhostCell<'brand, (T0,)>

source

pub fn as_tuple_of_cells(&self) -> &(GhostCell<'brand, T0>,)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>(tuple: &'a (GhostCell<'brand, T0>,)) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1> GhostCell<'brand, (T0, T1)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2> GhostCell<'brand, (T0, T1, T2)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2, T3> GhostCell<'brand, (T0, T1, T2, T3)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2, T3, T4> GhostCell<'brand, (T0, T1, T2, T3, T4)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2, T3, T4, T5> GhostCell<'brand, (T0, T1, T2, T3, T4, T5)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2, T3, T4, T5, T6> GhostCell<'brand, (T0, T1, T2, T3, T4, T5, T6)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2, T3, T4, T5, T6, T7> GhostCell<'brand, (T0, T1, T2, T3, T4, T5, T6, T7)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2, T3, T4, T5, T6, T7, T8> GhostCell<'brand, (T0, T1, T2, T3, T4, T5, T6, T7, T8)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>, GhostCell<'brand, T8>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>, GhostCell<'brand, T8>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> GhostCell<'brand, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>, GhostCell<'brand, T8>, GhostCell<'brand, T9>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>, GhostCell<'brand, T8>, GhostCell<'brand, T9>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> GhostCell<'brand, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>, GhostCell<'brand, T8>, GhostCell<'brand, T9>, GhostCell<'brand, T10>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>, GhostCell<'brand, T8>, GhostCell<'brand, T9>, GhostCell<'brand, T10>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

source§

impl<'brand, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> GhostCell<'brand, (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>

source

pub fn as_tuple_of_cells( &self ) -> &(GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>, GhostCell<'brand, T8>, GhostCell<'brand, T9>, GhostCell<'brand, T10>, GhostCell<'brand, T11>)

Returns a reference to a tuple of cells from a cell containing a tuple.

source

pub fn from_tuple_of_cells<'a>( tuple: &'a (GhostCell<'brand, T0>, GhostCell<'brand, T1>, GhostCell<'brand, T2>, GhostCell<'brand, T3>, GhostCell<'brand, T4>, GhostCell<'brand, T5>, GhostCell<'brand, T6>, GhostCell<'brand, T7>, GhostCell<'brand, T8>, GhostCell<'brand, T9>, GhostCell<'brand, T10>, GhostCell<'brand, T11>) ) -> &'a Self

Returns a cell containing a tuple from a tuple of cells.

Trait Implementations§

source§

impl<'brand, T: ?Sized> AsMut<T> for GhostCell<'brand, T>

source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<'brand, T: Default> Default for GhostCell<'brand, T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'brand, T> From<T> for GhostCell<'brand, T>

source§

fn from(t: T) -> Self

Converts to this type from the input type.
source§

impl<'brand, T: ?Sized + Send> Send for GhostCell<'brand, T>

A GhostCell<'_, T> owns a T, so it cannot be sent across threads if T cannot.

Conversely, a GhostCell does not add any state on top of T, so if T can be sent across threads, so can GhostCell<'_, T>

source§

impl<'brand, T: ?Sized + Send + Sync> Sync for GhostCell<'brand, T>

A GhostCell<'_, T> owns a T, so it cannot be accessed from different threads if T cannot.

Conversely, a GhostCell does not add any state on top of T, so if T can be accessed from different threads, so can GhostCell<'_, T>. T also needs to be sendable across threads, because a T can be extracted from a &GhostCell<'brand, T> via GhostCell::replace.

Auto Trait Implementations§

§

impl<'brand, T> !RefUnwindSafe for GhostCell<'brand, T>

§

impl<'brand, T: ?Sized> Unpin for GhostCell<'brand, T>
where T: Unpin,

§

impl<'brand, T: ?Sized> UnwindSafe for GhostCell<'brand, T>
where 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<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
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.