Struct kernel::utilities::cells::TakeCell

source ·
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,

source

pub const fn empty() -> TakeCell<'a, T>

source

pub fn new(value: &'a mut T) -> TakeCell<'a, T>

Creates a new TakeCell containing value

source

pub fn is_none(&self) -> bool

source

pub fn is_some(&self) -> bool

source

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 replaceed), 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);
source

pub fn put(&self, val: Option<&'a mut T>)

Stores val in the TakeCell

source

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.

source

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.

source

pub fn map<F, R>(&self, closure: F) -> Option<R>
where F: FnOnce(&mut T) -> R,

Allows closure to borrow the contents of the TakeCell if-and-only-if it is not taken 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));
source

pub fn map_or<F, R>(&self, default: R, closure: F) -> R
where F: FnOnce(&mut T) -> R,

Performs a map or returns a default value if the TakeCell is empty

source

pub fn map_or_else<U, D, F>(&self, default: D, f: F) -> U
where D: FnOnce() -> U, F: FnOnce(&mut T) -> U,

Performs a map or generates a value with the default closure if the TakeCell is empty

source

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.

source

pub fn modify_or_replace<F, G>(&self, modify: F, mkval: G)
where 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.

Auto Trait Implementations§

§

impl<'a, T> !Freeze for TakeCell<'a, T>

§

impl<'a, T> !RefUnwindSafe for TakeCell<'a, T>

§

impl<'a, T> Send for TakeCell<'a, T>
where T: Send + ?Sized,

§

impl<'a, T> !Sync for TakeCell<'a, T>

§

impl<'a, T> Unpin for TakeCell<'a, T>
where T: ?Sized,

§

impl<'a, T> !UnwindSafe for TakeCell<'a, T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> SizedTypeProperties for T

source§

#[doc(hidden)] const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.