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

pub struct MapCell<T> {
    val: UnsafeCell<T>,
    occupied: Cell<bool>,
}

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.

Fields

Methods

impl<T> MapCell<T>
[src]

[src]

[src]

Creates a new MapCell containing value

[src]

[src]

[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

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

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

[src]

[src]

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

[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

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

[src]

[src]

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

[src]