Struct kernel::utilities::cells::OptionalCell

source ·
pub struct OptionalCell<T> {
    value: Cell<Option<T>>,
}
Expand description

OptionalCell is a Cell that wraps an Option. This is helper type that makes keeping types that can be None a little cleaner.

Fields§

§value: Cell<Option<T>>

Implementations§

source§

impl<T> OptionalCell<T>

source

pub const fn new(val: T) -> OptionalCell<T>

Create a new OptionalCell.

source

pub const fn empty() -> OptionalCell<T>

Create an empty OptionalCell (contains just None).

source

pub fn set(&self, val: T)

Update the stored value.

source

pub fn insert(&self, opt: Option<T>)

Insert the value of the supplied Option, or None if the supplied Option is None.

source

pub fn replace(&self, val: T) -> Option<T>

Replace the contents with the supplied value. If the cell was not empty, the previous value is returned, otherwise None is returned.

source

pub fn clear(&self)

Reset the stored value to None.

source

pub fn is_some(&self) -> bool

Check if the cell contains something.

source

pub fn is_none(&self) -> bool

Check if the cell is None.

source

pub fn contains(&self, x: &T) -> bool
where T: PartialEq,

Returns true if the option is a Some value containing the given value.

source

pub fn ok_or<E>(self, err: E) -> Result<T, E>

Transforms the contained Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).

Arguments passed to ok_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use ok_or_else, which is lazily evaluated.

source

pub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>
where F: FnOnce() -> E,

Transforms the contained Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(err).

source

pub fn and<U>(self, optb: Option<U>) -> Option<U>

Returns None if the option is None, otherwise returns optb.

source

pub fn filter<P>(self, predicate: P) -> Option<T>
where P: FnOnce(&T) -> bool,

Returns None if the option is None, otherwise calls predicate with the wrapped value and returns:

  • Some(t) if predicate returns true (where t is the wrapped value), and
  • None if predicate returns false.
source

pub fn or(self, optb: Option<T>) -> Option<T>

Returns the option if it contains a value, otherwise returns optb.

Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use or_else, which is lazily evaluated.

source

pub fn or_else<F>(self, f: F) -> Option<T>
where F: FnOnce() -> Option<T>,

Returns the option if it contains a value, otherwise calls f and returns the result.

source

pub fn take(&self) -> Option<T>

Return the contained value and replace it with None.

source

pub fn unwrap_or_default(self) -> T
where T: Default,

Returns the contained value or a default

Consumes the self argument then, if Some, returns the contained value, otherwise if None, returns the default value for that type.

source§

impl<T> OptionalCell<T>
where T: Copy,

source

pub fn get(&self) -> Option<T>

Returns a copy of the contained Option.

source

pub fn unwrap_or_panic(&self) -> T

Returns the contained value or panics if contents is None. We do not use the traditional name for this function – unwrap() – because the Tock kernel discourages panicking, and this name is intended to discourage users from casually adding calls to unwrap() without careful consideration.

source

pub fn unwrap_or(&self, default: T) -> T

Returns the contained value or a default.

source

pub fn unwrap_or_else<F>(&self, default: F) -> T
where F: FnOnce() -> T,

Returns the contained value or computes a default.

source

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

Call a closure on the value if the value exists.

source

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

Call a closure on the value if the value exists, or return the default if the value is None.

source

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

If the cell contains a value, call a closure supplied with the value of the cell. If the cell contains None, call the other closure to return a default value.

source

pub fn and_then<U, F>(&self, f: F) -> Option<U>
where F: FnOnce(T) -> Option<U>,

If the cell is empty, return None. Otherwise, call a closure with the value of the cell and return the result.

Trait Implementations§

source§

impl<T> Default for OptionalCell<T>

source§

fn default() -> OptionalCell<T>

Returns an empty OptionalCell.

Auto Trait Implementations§

§

impl<T> !Freeze for OptionalCell<T>

§

impl<T> !RefUnwindSafe for OptionalCell<T>

§

impl<T> Send for OptionalCell<T>
where T: Send,

§

impl<T> !Sync for OptionalCell<T>

§

impl<T> Unpin for OptionalCell<T>
where T: Unpin,

§

impl<T> UnwindSafe for OptionalCell<T>
where T: UnwindSafe,

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, 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.