Struct TakeCell

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

Source

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

Creates a new TakeCell containing the given value.

Source

pub fn into_inner(self) -> T

Unwraps the underlying value.

Source§

impl<T: ?Sized> TakeCell<T>

Source

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

pub fn is_taken(&self) -> bool

Returns true if a reference to 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) -> &mut T

Returns a unique reference to the underlying data.

This call borrows TakeCell uniquely (at compile-time) which guarantees that we possess the only reference.

Note that this function is not affected nor affects the take. ie this function will return a reference even if take was already called

Source

pub fn heal(&mut self)

Heal this cell. After a call to this function next call to take will succeed again, even if take was called before.

§Examples
let mut cell = TakeCell::new(0);

assert!(cell.take().is_some());
assert!(cell.is_taken());

cell.heal();

assert!(!cell.is_taken());
assert!(cell.take().is_some());
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<&mut T>

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

Source

pub unsafe fn steal(&self) -> &mut 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 + ?Sized> Default for TakeCell<T>

Source§

fn default() -> TakeCell<T>

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

impl<T> From<T> for TakeCell<T>

Source§

fn from(v: T) -> Self

Converts to this type from the input type.
Source§

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.

Auto Trait Implementations§

§

impl<T> !Freeze for TakeCell<T>

§

impl<T> !RefUnwindSafe for TakeCell<T>

§

impl<T> Send for TakeCell<T>
where T: Send + ?Sized,

§

impl<T> Unpin for TakeCell<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for TakeCell<T>
where T: UnwindSafe + ?Sized,

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.