Struct kernel::common::take_cell::TakeCell
[−]
[src]
pub struct TakeCell<'a, T: 'a + ?Sized> { val: UnsafeCell<Option<&'a mut T>>, }
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
.
Fields
val: UnsafeCell<Option<&'a mut T>>
Methods
impl<'a, T: ?Sized> TakeCell<'a, T>
[src]
pub const fn empty() -> TakeCell<'a, T>
[src]
pub fn new(value: &'a mut T) -> TakeCell<'a, T>
[src]
Creates a new TakeCell
containing value
pub fn is_none(&self) -> bool
[src]
pub fn is_some(&self) -> bool
[src]
pub fn take(&self) -> Option<&'a mut T>
[src]
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
let cell = TakeCell::new(1234); let x = &cell; let y = &cell; x.take(); assert_eq!(y.take(), None);
pub fn put(&self, val: Option<&'a mut T>)
[src]
Stores val
in the TakeCell
pub fn replace(&self, val: &'a mut T) -> Option<&'a mut T>
[src]
Replaces the contents of the TakeCell
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 TakeCell
if-and-only-if
it is not take
n already. The state of the TakeCell
is unchanged
after the closure completes.
Examples
let cell = TakeCell::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,
Performs a map
or returns a default value if the TakeCell
is empty
pub fn map_or_else<U, D, F>(&self, default: D, f: F) -> U where
D: FnOnce() -> U,
F: FnOnce(&mut T) -> U,
[src]
D: FnOnce() -> U,
F: FnOnce(&mut T) -> U,
Performs a map
or generates a value with the default
closure if the TakeCell
is empty
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() -> &'a mut T,
[src]
F: FnOnce(&mut T),
G: FnOnce() -> &'a mut T,
Uses the first closure (modify
) to modify the value in the TakeCell
if it is present, otherwise, fills the TakeCell
with the result of
mkval
.