takecell

Struct TakeOwnCell

Source
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>

Source

pub const fn new(v: T) -> Self

Creates a new TakeOwnCell containing the given value.

Source

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());
Source

pub fn is_taken(&self) -> bool

Returns true if the underlying value has been already taken.

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.

Source

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 taken this function returns None.

Source

pub fn into_inner(self) -> Option<T>

Unwraps the underlying value.

Source

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));
Source

pub fn is_taken_unsync(&mut self) -> bool

Similar to is_taken, but uses unique reference instead of runtime synchronization.

Source

pub fn take_unsync(&mut self) -> Option<T>

Similar to take, but uses unique reference instead of runtime synchronization.

Source

pub unsafe fn steal(&self) -> T

Unchecked version of take.

§Safety

Call to this function must be the first call to steal or take after cell creation or heal.

Trait Implementations§

Source§

impl<T: Default> Default for TakeOwnCell<T>

Source§

fn default() -> TakeOwnCell<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Drop for TakeOwnCell<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> From<T> for TakeOwnCell<T>

Source§

fn from(v: T) -> Self

Converts to this type from the input type.
Source§

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.

Auto Trait Implementations§

§

impl<T> !Freeze for TakeOwnCell<T>

§

impl<T> !RefUnwindSafe for TakeOwnCell<T>

§

impl<T> Send for TakeOwnCell<T>
where T: Send,

§

impl<T> Unpin for TakeOwnCell<T>
where T: Unpin,

§

impl<T> UnwindSafe for TakeOwnCell<T>
where T: UnwindSafe,

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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<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.