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>
impl<T: ?Sized, Token: TokenTrait> TokenCell<T, Token>
Source§impl<T: Sized, Token: TokenTrait> TokenCell<T, Token>
impl<T: Sized, Token: TokenTrait> TokenCell<T, Token>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwraps the value from the cell.
Full ownership of the cell is sufficient proof that the inner value can be recovered.
Source§impl<T, Token: TokenTrait> TokenCell<T, Token>
impl<T, Token: TokenTrait> TokenCell<T, Token>
Sourcepub fn new(value: T, token: &Token) -> Self
pub fn new(value: T, token: &Token) -> Self
All calls to UnsafeTokenCellTrait or TokenCellTrait’s methods on the returned value MUST use the same instance of token
as passed to this constructor.
Methods from Deref<Target = UnsafeCell<T>>§
1.101.0 · Sourcepub unsafe fn replace(&self, value: T) -> T
pub unsafe fn replace(&self, value: T) -> T
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
use std::cell::UnsafeCell;
let uc = UnsafeCell::new(5);
let old = unsafe { uc.replace(10) };
assert_eq!(old, 5);1.0.0 · Sourcepub fn get(&self) -> *mut T
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 (shared or mutable) references, you must uphold the aliasing rules; see the type-level docs for more discussion and caveats.
This is equivalent to casting self to a raw pointer and then casting that raw
pointer to *mut T.
§Examples
use std::cell::UnsafeCell;
let uc = UnsafeCell::new(5);
let five = uc.get();1.50.0 · Sourcepub fn get_mut(&mut self) -> &mut T
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);1.101.0 · Sourcepub unsafe fn as_ref_unchecked(&self) -> &T
pub unsafe fn as_ref_unchecked(&self) -> &T
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
use std::cell::UnsafeCell;
let uc = UnsafeCell::new(5);
let val = unsafe { uc.as_ref_unchecked() };
assert_eq!(val, &5);1.101.0 · Sourcepub unsafe fn as_mut_unchecked(&self) -> &mut T
pub unsafe fn as_mut_unchecked(&self) -> &mut T
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
use std::cell::UnsafeCell;
let uc = UnsafeCell::new(5);
unsafe { *uc.as_mut_unchecked() += 1; }
assert_eq!(uc.into_inner(), 6);