[][src]Struct kernel::common::cells::OptionalCell

pub struct OptionalCell<T> where
    T: Copy
{ value: Cell<Option<T>>, }

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

Methods

impl<T> OptionalCell<T> where
    T: Copy
[src]

pub fn new(val: T) -> OptionalCell<T>[src]

Create a new OptionalCell.

pub fn empty() -> OptionalCell<T>[src]

Create an empty OptionalCell (contains just None).

pub fn set(&self, val: T)[src]

Update the stored value.

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

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

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

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

pub fn clear(&self)[src]

Reset the stored value to None.

pub fn is_some(&self) -> bool[src]

Check if the cell contains something.

pub fn is_none(&self) -> bool[src]

Check if the cell is None.

pub fn expect(&self, msg: &str) -> T[src]

Returns the contained value or panics if contents is None.

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

Returns the contained value or a default.

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

Returns the contained value or computes a default.

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

Call a closure on the value if the value exists.

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

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

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

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.

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

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.

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

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

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

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

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

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

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

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.

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

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.

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

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

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

Return the contained value and replace it with None.

pub fn unwrap_or_default(self) -> T where
    T: Default
[src]

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.

Trait Implementations

impl<T> Default for OptionalCell<T> where
    T: Copy + Default
[src]

Auto Trait Implementations

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

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

impl<T> !Sync for OptionalCell<T>

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]