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
val: UnsafeCell<T>
occupied: Cell<bool>
Methods
impl<T> MapCell<T>
[src]
pub fn empty() -> MapCell<T>
[src]
pub const fn new(value: T) -> MapCell<T>
[src]
Creates a new MapCell
containing value
pub fn is_none(&self) -> bool
[src]
pub fn is_some(&self) -> bool
[src]
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 replace
ed), 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);
pub fn put(&self, val: T)
[src]
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]
F: FnOnce(&mut T) -> R,
Allows closure
to borrow the contents of the MapCell
if-and-only-if
it is not take
n 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));
pub fn map_or<F, R>(&self, default: R, closure: F) -> R where
F: FnOnce(&mut T) -> R,
[src]
F: FnOnce(&mut T) -> R,
pub fn and_then<F, R>(&self, closure: F) -> Option<R> where
F: FnOnce(&mut T) -> Option<R>,
[src]
F: FnOnce(&mut T) -> Option<R>,
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]
F: FnOnce(&mut T),
G: FnOnce() -> T,