Skip to main content

OptionExtensions

Trait OptionExtensions 

Source
pub trait OptionExtensions<T>: Sized {
    // Required methods
    fn convert<U>(self, converter: impl FnOnce(T) -> U) -> Option<U>;
    fn convert_optional<U>(
        self,
        converter: impl FnOnce(T) -> Option<U>,
    ) -> Option<U>;
    fn convert_or<U>(
        self,
        converter: impl FnOnce(T) -> U,
        fallback_converter: impl FnOnce() -> U,
    ) -> U;
    fn if_has_value(self, action: impl FnOnce(T)) -> OptionElse;
    fn value_or(self, value_selector: impl FnOnce() -> T) -> T;
    fn value_or_default(self) -> T
       where T: Default;
    fn value_or_throw<E>(
        self,
        exception_generator: impl FnOnce() -> E,
    ) -> Result<T, E>;
}
Expand description

The extensions of an option (OptionExtensions on Optional<T>).

Required Methods§

Source

fn convert<U>(self, converter: impl FnOnce(T) -> U) -> Option<U>

The value converted by converter, or none (Convert(Func<TSource, TDestination>)).

Source

fn convert_optional<U>( self, converter: impl FnOnce(T) -> Option<U>, ) -> Option<U>

The option that converter makes of the value, or none (Convert(Func<TSource, Optional<TDestination>>)).

Source

fn convert_or<U>( self, converter: impl FnOnce(T) -> U, fallback_converter: impl FnOnce() -> U, ) -> U

The value converted by converter, or what fallback_converter makes when there is none (ConvertOr).

Source

fn if_has_value(self, action: impl FnOnce(T)) -> OptionElse

Runs action with the value; the result runs its else_ action when there is none (IfHasValue(Action<T>)).

Source

fn value_or(self, value_selector: impl FnOnce() -> T) -> T

The value, or what value_selector makes when there is none (ValueOr(Func<T>)).

Source

fn value_or_default(self) -> T
where T: Default,

The value, or the default value of T (ValueOrDefault).

Source

fn value_or_throw<E>( self, exception_generator: impl FnOnce() -> E, ) -> Result<T, E>

The value, or the error that exception_generator makes when there is none (ValueOrThrow).

Deviation (DEVIATIONS.md, DynamicData): upstream throws the exception; the port returns it as the Err of the result.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> OptionExtensions<T> for Option<T>

Source§

fn convert<U>(self, converter: impl FnOnce(T) -> U) -> Option<U>

Source§

fn convert_optional<U>( self, converter: impl FnOnce(T) -> Option<U>, ) -> Option<U>

Source§

fn convert_or<U>( self, converter: impl FnOnce(T) -> U, fallback_converter: impl FnOnce() -> U, ) -> U

Source§

fn if_has_value(self, action: impl FnOnce(T)) -> OptionElse

Source§

fn value_or(self, value_selector: impl FnOnce() -> T) -> T

Source§

fn value_or_default(self) -> T
where T: Default,

Source§

fn value_or_throw<E>( self, exception_generator: impl FnOnce() -> E, ) -> Result<T, E>

Implementors§