pub struct TakeCell<'a, T> where
    T: 'a + ?Sized
{ /* private fields */ }
Expand description

A shared reference to a mutable reference.

A TakeCell wraps potential reference to mutable memory that may be available at a given point. Rather than enforcing borrow rules at compile-time, TakeCell enables multiple clients to hold references to it, but ensures that only one referrer has access to the underlying mutable reference at a time. Clients either move the memory out of the TakeCell or operate on a borrow within a closure. Attempts to take the value from inside a TakeCell may fail by returning None.

Implementations

Creates a new TakeCell containing value

Takes the mutable reference out of the TakeCell leaving a None in it’s place. If the value has already been taken elsewhere (and not replaceed), the returned Option will be empty.

Examples
extern crate tock_cells;
use tock_cells::take_cell::TakeCell;

let mut value = 1234;
let cell = TakeCell::new(&mut value);
let x = &cell;
let y = &cell;

x.take();
assert_eq!(y.take(), None);

Stores val in the TakeCell

Replaces the contents of the TakeCell with val. If the cell was not empty, the previous value is returned, otherwise None is returned.

Retrieves a mutable reference to the inner value that only lives as long as the reference to this does.

This escapes the “take” aspect of TakeCell in a way which is guaranteed safe due to the returned reference sharing the lifetime of &mut self.

Allows closure to borrow the contents of the TakeCell if-and-only-if it is not taken already. The state of the TakeCell is unchanged after the closure completes.

Examples
extern crate tock_cells;
use tock_cells::take_cell::TakeCell;

let mut value = 1234;
let cell = TakeCell::new(&mut value);
let x = &cell;
let y = &cell;

x.map(|value| {
    // We have mutable access to the value while in the closure
    *value += 1;
});

// After the closure completes, the mutable memory is still in the cell,
// but potentially changed.
assert_eq!(y.take(), Some(&mut 1235));

Performs a map or returns a default value if the TakeCell is empty

Performs a map or generates a value with the default closure if the TakeCell is empty

Behaves the same as map, except the closure is allowed to return an Option.

Uses the first closure (modify) to modify the value in the TakeCell if it is present, otherwise, fills the TakeCell with the result of mkval.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.