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§
Source§impl<'a, T> TakeCell<'a, T>where
T: ?Sized,
impl<'a, T> TakeCell<'a, T>where
T: ?Sized,
pub const fn empty() -> TakeCell<'a, T>
pub fn is_none(&self) -> bool
pub fn is_some(&self) -> bool
Sourcepub fn take(&self) -> Option<&'a mut T>
pub fn take(&self) -> Option<&'a mut T>
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
replace
ed), 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);
Sourcepub fn replace(&self, val: &'a mut T) -> Option<&'a mut T>
pub fn replace(&self, val: &'a mut T) -> Option<&'a mut T>
Replaces the contents of the TakeCell
with val
. If the cell was not
empty, the previous value is returned, otherwise None
is returned.
Sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
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
.
Sourcepub fn map<F, R>(&self, closure: F) -> Option<R>
pub fn map<F, R>(&self, closure: F) -> Option<R>
Allows closure
to borrow the contents of the TakeCell
if-and-only-if
it is not take
n 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));
Sourcepub fn map_or<F, R>(&self, default: R, closure: F) -> R
pub fn map_or<F, R>(&self, default: R, closure: F) -> R
Performs a map
or returns a default value if the TakeCell
is empty
Sourcepub fn map_or_else<U, D, F>(&self, default: D, f: F) -> U
pub fn map_or_else<U, D, F>(&self, default: D, f: F) -> U
Performs a map
or generates a value with the default
closure if the TakeCell
is empty