pub struct TakeCell<T: ?Sized> { /* private fields */ }
Expand description
A cell type which value can be taken only once.
See crate-level documentation for more.
Implementations§
Source§impl<T> TakeCell<T>
impl<T> TakeCell<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwraps the underlying value.
Source§impl<T: ?Sized> TakeCell<T>
impl<T: ?Sized> TakeCell<T>
Sourcepub fn take(&self) -> Option<&mut T>
pub fn take(&self) -> Option<&mut T>
Returns a reference to the underlying value.
After this function once returns Some(_)
all consequtive calls before
heal
will return None
as the reference is already taken.
§Examples
let cell = TakeCell::new(0);
let uref: &mut _ = cell.take().unwrap();
*uref = 17;
// Already taken
assert!(cell.take().is_none());
let value = cell.into_inner();
assert_eq!(value, 17);
Sourcepub fn is_taken(&self) -> bool
pub fn is_taken(&self) -> bool
Returns true
if a reference to the underlying value has been already
take
n.
ie if this function returns true
, then take
will return None
.
Note however that the oposite is not true: if this function returned
false
it doesn’t guarantee that take
will return Some(_)
since
there may have been concurent calls to take
.
Sourcepub fn is_taken_unsync(&mut self) -> bool
pub fn is_taken_unsync(&mut self) -> bool
Similar to is_taken
, but uses unique reference instead of runtime
synchronization.
Sourcepub fn take_unsync(&mut self) -> Option<&mut T>
pub fn take_unsync(&mut self) -> Option<&mut T>
Similar to take
, but uses unique reference instead of runtime
synchronization.
Trait Implementations§
impl<T: ?Sized + Send> Sync for TakeCell<T>
§Safety
It is possible to pass ownership via &TakeCell
. As such, TakeCell<T>
may
be Sync
(TakeCell<T>: Send
) if and only if T
is Send
. Otherwise
there may be UB, see this example, adopted from sslab-gatech rust group.
Sync
on the other hand is not required because TakeCell
’s value is only
accesible from one thread at a time.
This is again similar to a Mutex
.