pub struct MapCell<T> { /* private fields */ }
Expand description
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
sourceimpl<T> MapCell<T>
impl<T> MapCell<T>
sourcepub fn take(&self) -> Option<T>
pub fn take(&self) -> Option<T>
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
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);
sourcepub fn replace(&self, val: T) -> Option<T>
pub fn replace(&self, val: T) -> Option<T>
Replaces the contents of the MapCell
with val
. If the cell was not
empty, the previous value is returned, otherwise None
is returned.
sourcepub fn map<F, R>(&self, closure: F) -> Option<R> where
F: FnOnce(&mut T) -> R,
pub fn map<F, R>(&self, closure: F) -> Option<R> where
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
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,
sourcepub fn and_then<F, R>(&self, closure: F) -> Option<R> where
F: FnOnce(&mut T) -> Option<R>,
pub fn and_then<F, R>(&self, closure: F) -> Option<R> where
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,
Auto Trait Implementations
impl<T> !RefUnwindSafe for MapCell<T>
impl<T> Send for MapCell<T> where
T: Send,
impl<T> !Sync for MapCell<T>
impl<T> Unpin for MapCell<T> where
T: Unpin,
impl<T> UnwindSafe for MapCell<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more