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>
pub fn into_inner(self) -> T
Methods from Deref<Target = UnsafeCell<T>>§
Sourcepub unsafe fn replace(&self, value: T) -> T
🔬This is a nightly-only experimental API. (unsafe_cell_access
)
pub unsafe fn replace(&self, value: T) -> T
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 · 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 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 · 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);
Sourcepub unsafe fn as_ref_unchecked(&self) -> &T
🔬This is a nightly-only experimental API. (unsafe_cell_access
)
pub unsafe fn as_ref_unchecked(&self) -> &T
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);
Sourcepub unsafe fn as_mut_unchecked(&self) -> &mut T
🔬This is a nightly-only experimental API. (unsafe_cell_access
)
pub unsafe fn as_mut_unchecked(&self) -> &mut T
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> TokenCellTrait<T, Token> for TokenCell<T, Token>
impl<T: ?Sized, Token: TokenTrait> TokenCellTrait<T, Token> for TokenCell<T, Token>
Source§fn new(inner: T, token: &Token) -> Selfwhere
T: Sized,
fn new(inner: T, token: &Token) -> Selfwhere
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>
fn try_guard<'l>( &'l self, token: &'l Token, ) -> Result<TokenGuard<'l, T, Token>, <Token as TokenTrait>::ComparisonError>
Attempts to construct a guard which
Deref
s 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>
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>
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
DerefMut
s 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>
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 Twhere
Token::ComparisonError: Debug,
fn borrow<'l>(&'l self, token: &'l Token) -> &'l Twhere
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 Twhere
Token::ComparisonError: Debug,
fn borrow_mut<'l>(&'l self, token: &'l mut Token) -> &'l mut Twhere
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>
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>
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.
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>
impl<T, Token> Unpin for TokenCell<T, Token>
impl<T, Token> UnwindSafe for TokenCell<T, Token>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more