tock_cells::optional_cell

Struct OptionalCell

Source
pub struct OptionalCell<T> { /* private fields */ }
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.

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: Copy> OptionalCell<T>

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: FnOnce(T) -> Option<U>>(&self, f: F) -> 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() -> Self

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

Source§

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

Source§

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.