pub struct TakeOwnCell<T>(/* private fields */);
Expand description
A cell type which value can be taken only once.
In difference with TakeCell
, this type provides
ownership, and not a reference to the inner value. Because of this it can’t
contain unsized values.
See crate-level documentation for more.
Implementations§
Source§impl<T> TakeOwnCell<T>
impl<T> TakeOwnCell<T>
Sourcepub fn take(&self) -> Option<T>
pub fn take(&self) -> Option<T>
Returns the underlying value.
After this function once returns Some(_)
all consequtive calls before
heal
will return None
as the value is already taken.
§Examples
let cell = TakeOwnCell::new(17);
let value: i32 = cell.take().unwrap();
assert_eq!(value, 17);
// Already taken
assert!(cell.take().is_none());
assert!(cell.into_inner().is_none());
Sourcepub fn get(&mut self) -> Option<&mut T>
pub fn get(&mut self) -> Option<&mut T>
Returns a unique reference to the underlying data.
This call borrows TakeOwnCell
uniquely (at compile-time) which
guarantees that we possess the only reference.
Note that this function does not affect the take
. ie take
may
still return Some(_)
after a call to this function. The oposite is not
true, after the value is take
n this function returns None
.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Unwraps the underlying value.
Sourcepub fn heal(&mut self, v: T) -> (&mut T, Result<(), T>)
pub fn heal(&mut self, v: T) -> (&mut T, Result<(), T>)
Heal this cell. After a call to this function next call to take
will
succeed again, even if take
was called before.
Returns a reference to the underlying value and Err(v)
if this cell
was not taken before the call to this function.
§Examples
let mut cell = TakeOwnCell::new(17);
let (uref, res) = cell.heal(12);
assert_eq!(res, Err(12));
assert_eq!(*uref, 17);
*uref = 0xAA;
assert_eq!(cell.take(), Some(0xAA));
let (uref, res) = cell.heal(12);
assert!(res.is_ok());
assert_eq!(*uref, 12);
*uref = 0xBB;
assert_eq!(cell.into_inner(), Some(0xBB));
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<T>
pub fn take_unsync(&mut self) -> Option<T>
Similar to take
, but uses unique reference instead of runtime
synchronization.
Trait Implementations§
Source§impl<T: Default> Default for TakeOwnCell<T>
impl<T: Default> Default for TakeOwnCell<T>
Source§fn default() -> TakeOwnCell<T>
fn default() -> TakeOwnCell<T>
Source§impl<T> Drop for TakeOwnCell<T>
impl<T> Drop for TakeOwnCell<T>
Source§impl<T> From<T> for TakeOwnCell<T>
impl<T> From<T> for TakeOwnCell<T>
impl<T: Send> Sync for TakeOwnCell<T>
§Safety
It is possible to pass ownership via &TakeOwnCell
. As such,
TakeOwnCell<T>
may be Sync
(TakeOwnCell<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 TakeOwnCell
’s value is
only accesible from one thread at a time.
This is again similar to a Mutex
.