[][src]Struct kernel::common::cells::MapCell

pub struct MapCell<T> { /* fields omitted */ }

A mutable memory location that enforces borrow rules at runtime without possible panics.

A MapCell is a potential reference to mutable memory. Borrow rules are enforced by forcing clients to either move the memory out of the cell or operate on a borrow within a closure. You can think of a MapCell as an Option wrapped in a RefCell --- attempts to take the value from inside a MapCell may fail by returning None.

Implementations

impl<T> MapCell<T>[src]

pub fn empty() -> MapCell<T>[src]

Creates an empty MapCell

pub fn new(value: T) -> MapCell<T>[src]

Creates a new MapCell containing value

pub fn is_none(&self) -> bool[src]

Returns a boolean which indicates if the MapCell is unoccupied.

pub fn is_some(&self) -> bool[src]

Returns a boolean which indicates if the MapCell is occupied.

pub fn take(&self) -> Option<T>[src]

Takes the value out of the MapCell leaving it empty. If the value has already been taken elsewhere (and not replaceed), the returned Option will be None.

Examples

extern crate tock_cells;
use tock_cells::map_cell::MapCell;

let cell = MapCell::new(1234);
let x = &cell;
let y = &cell;

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

pub fn put(&self, val: T)[src]

Puts a value into the MapCell.

pub fn replace(&self, val: T) -> Option<T>[src]

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

pub fn map<F, R>(&self, closure: F) -> Option<R> where
    F: FnOnce(&mut T) -> R, 
[src]

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

Examples

extern crate tock_cells;
use tock_cells::map_cell::MapCell;

let cell = MapCell::new(1234);
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(1235));

pub fn map_or<F, R>(&self, default: R, closure: F) -> R where
    F: FnOnce(&mut T) -> R, 
[src]

pub fn and_then<F, R>(&self, closure: F) -> Option<R> where
    F: FnOnce(&mut T) -> Option<R>, 
[src]

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

pub fn modify_or_replace<F, G>(&self, modify: F, mkval: G) where
    F: FnOnce(&mut T),
    G: FnOnce() -> T, 
[src]

Auto Trait Implementations

impl<T> Send for MapCell<T> where
    T: Send

impl<T> !Sync for MapCell<T>

impl<T> Unpin for MapCell<T> where
    T: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.