Struct TokenCell

Source
pub struct TokenCell<T: ?Sized, Token: TokenTrait> { /* private fields */ }
Expand description

A Cell that shifts the management of access permissions to its inner value onto a Token.

Implementations§

Source§

impl<T: ?Sized, Token: TokenTrait> TokenCell<T, Token>

Source

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

Source§

impl<T: Sized, Token: TokenTrait> TokenCell<T, Token>

Source

pub fn into_inner(self) -> T

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

Source

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

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

Replace the value in this UnsafeCell and return the old value.

§Safety

The caller must take care to avoid aliasing and data races.

  • It is Undefined Behavior to allow calls to race with any other access to the wrapped value.
  • It is Undefined Behavior to call this while any other reference(s) to the wrapped value are alive.
§Examples
#![feature(unsafe_cell_access)]
use std::cell::UnsafeCell;

let uc = UnsafeCell::new(5);

let old = unsafe { uc.replace(10) };
assert_eq!(old, 5);
1.0.0 · Source

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

Gets a mutable pointer to the wrapped value.

This can be cast to a pointer of any kind. When creating references, you must uphold the aliasing rules; see the type-level docs for more discussion and caveats.

§Examples
use std::cell::UnsafeCell;

let uc = UnsafeCell::new(5);

let five = uc.get();
1.50.0 · Source

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

Returns a mutable reference to the underlying data.

This call borrows the UnsafeCell mutably (at compile-time) which guarantees that we possess the only reference.

§Examples
use std::cell::UnsafeCell;

let mut c = UnsafeCell::new(5);
*c.get_mut() += 1;

assert_eq!(*c.get_mut(), 6);
Source

pub unsafe fn as_ref_unchecked(&self) -> &T

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

Get a shared reference to the value within the UnsafeCell.

§Safety
  • It is Undefined Behavior to call this while any mutable reference to the wrapped value is alive.
  • Mutating the wrapped value while the returned reference is alive is Undefined Behavior.
§Examples
#![feature(unsafe_cell_access)]
use std::cell::UnsafeCell;

let uc = UnsafeCell::new(5);

let val = unsafe { uc.as_ref_unchecked() };
assert_eq!(val, &5);
Source

pub unsafe fn as_mut_unchecked(&self) -> &mut T

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

Get an exclusive reference to the value within the UnsafeCell.

§Safety
  • It is Undefined Behavior to call this while any other reference(s) to the wrapped value are alive.
  • Mutating the wrapped value through other means while the returned reference is alive is Undefined Behavior.
§Examples
#![feature(unsafe_cell_access)]
use std::cell::UnsafeCell;

let uc = UnsafeCell::new(5);

unsafe { *uc.as_mut_unchecked() += 1; }
assert_eq!(uc.into_inner(), 6);

Trait Implementations§

Source§

impl<T: ?Sized, Token: TokenTrait> Deref for TokenCell<T, Token>

Source§

type Target = UnsafeCell<T>

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T: ?Sized, Token: TokenTrait> DerefMut for TokenCell<T, Token>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: ?Sized, Token: TokenTrait> TokenCellTrait<T, Token> for TokenCell<T, Token>

Source§

fn new(inner: T, token: &Token) -> Self
where T: Sized,

Constructs a new cell using token as its key.
Source§

fn try_guard<'l>( &'l self, token: &'l Token, ) -> Result<TokenGuard<'l, T, Token>, <Token as TokenTrait>::ComparisonError>

Attempts to construct a guard which Derefs to the inner data, but also allows recovering the Token.
Source§

fn try_borrow<'l>( &'l self, token: &'l Token, ) -> Result<&'l T, Token::ComparisonError>

Attempts to borrow the inner data. Read more
Source§

fn try_guard_mut<'l>( &'l self, token: &'l mut Token, ) -> Result<TokenGuardMut<'l, T, Token>, <Token as TokenTrait>::ComparisonError>

Attempts to construct a guard which DerefMuts to the inner data, but also allows recovering the Token.
Source§

fn try_borrow_mut<'l>( &'l self, token: &'l mut Token, ) -> Result<&'l mut T, Token::ComparisonError>

Attempts to borrow the inner data mutably. Read more
Source§

fn borrow<'l>(&'l self, token: &'l Token) -> &'l T
where Token::ComparisonError: Debug,

Borrows the inner data, panicking if the wrong token was used as key.
Source§

fn borrow_mut<'l>(&'l self, token: &'l mut Token) -> &'l mut T
where Token::ComparisonError: Debug,

Borrows the inner data mutably, panicking if the wrong token was used as key.
Source§

fn map<'a, U, F: FnOnce(TokenGuard<'a, T, Token>) -> U>( &'a self, f: F, ) -> TokenMap<'a, T, U, F, Self, Token>

Constructs a lazy computation that can then be applied using the token.
Source§

fn map_mut<'a, U, F: FnOnce(TokenGuardMut<'a, T, Token>) -> U>( &'a self, f: F, ) -> TokenMapMut<'a, T, U, F, Self, Token>

Constructs a lazy computation that can then be applied using the token.
Source§

impl<T: ?Sized, Token: TokenTrait> Sync for TokenCell<T, Token>

Auto Trait Implementations§

§

impl<T, Token> !Freeze for TokenCell<T, Token>

§

impl<T, Token> !RefUnwindSafe for TokenCell<T, Token>

§

impl<T, Token> Send for TokenCell<T, Token>
where <Token as TokenTrait>::Identifier: Send, T: Send + ?Sized,

§

impl<T, Token> Unpin for TokenCell<T, Token>
where <Token as TokenTrait>::Identifier: Unpin, T: Unpin + ?Sized,

§

impl<T, Token> UnwindSafe for TokenCell<T, Token>
where <Token as TokenTrait>::Identifier: UnwindSafe, T: UnwindSafe + ?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, 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.