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>
impl<T> OptionalCell<T>
Sourcepub const fn new(val: T) -> OptionalCell<T>
pub const fn new(val: T) -> OptionalCell<T>
Create a new OptionalCell.
Sourcepub const fn empty() -> OptionalCell<T>
pub const fn empty() -> OptionalCell<T>
Create an empty OptionalCell
(contains just None
).
Sourcepub fn insert(&self, opt: Option<T>)
pub fn insert(&self, opt: Option<T>)
Insert the value of the supplied Option
, or None
if the supplied
Option
is None
.
Sourcepub fn replace(&self, val: T) -> Option<T>
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.
Sourcepub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
pub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
Returns true if the option is a Some value containing the given value.
Sourcepub fn ok_or<E>(self, err: E) -> Result<T, E>
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.
Sourcepub fn ok_or_else<E, F>(self, err: F) -> Result<T, E>where
F: FnOnce() -> E,
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)
.
Sourcepub fn and<U>(self, optb: Option<U>) -> Option<U>
pub fn and<U>(self, optb: Option<U>) -> Option<U>
Returns None
if the option is None
, otherwise returns optb
.
Sourcepub fn filter<P>(self, predicate: P) -> Option<T>
pub fn filter<P>(self, predicate: P) -> Option<T>
Returns None
if the option is None
, otherwise calls predicate
with
the wrapped value and returns:
Some(t)
ifpredicate
returnstrue
(wheret
is the wrapped value), andNone
ifpredicate
returnsfalse
.
Sourcepub fn or(self, optb: Option<T>) -> Option<T>
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.
Sourcepub fn or_else<F>(self, f: F) -> Option<T>
pub fn or_else<F>(self, f: F) -> Option<T>
Returns the option if it contains a value, otherwise calls f
and
returns the result.
Sourcepub fn unwrap_or_default(self) -> Twhere
T: Default,
pub fn unwrap_or_default(self) -> Twhere
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>
impl<T: Copy> OptionalCell<T>
Sourcepub fn unwrap_or_panic(&self) -> T
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.
Sourcepub fn unwrap_or_else<F>(&self, default: F) -> Twhere
F: FnOnce() -> T,
pub fn unwrap_or_else<F>(&self, default: F) -> Twhere
F: FnOnce() -> T,
Returns the contained value or computes a default.
Sourcepub fn map<F, R>(&self, closure: F) -> Option<R>where
F: FnOnce(T) -> R,
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.
Sourcepub fn map_or<F, R>(&self, default: R, closure: F) -> Rwhere
F: FnOnce(T) -> R,
pub fn map_or<F, R>(&self, default: R, closure: F) -> Rwhere
F: FnOnce(T) -> R,
Call a closure on the value if the value exists, or return the
default if the value is None
.
Sourcepub fn map_or_else<U, D, F>(&self, default: D, closure: F) -> U
pub fn map_or_else<U, D, F>(&self, default: D, closure: F) -> 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.
Trait Implementations§
Source§impl<T> Default for OptionalCell<T>
impl<T> Default for OptionalCell<T>
Source§fn default() -> Self
fn default() -> Self
Returns an empty OptionalCell
.