referrerpolicy=no-referrer-when-downgrade
pub trait DefensiveOption<T> {
    // Required methods
    fn defensive_map_or_else<U, D, F>(self, default: D, f: F) -> U
       where D: FnOnce() -> U,
             F: FnOnce(T) -> U;
    fn defensive_ok_or_else<E, F>(self, err: F) -> Result<T, E>
       where E: Debug,
             F: FnOnce() -> E;
    fn defensive_ok_or<E>(self, err: E) -> Result<T, E>
       where E: Debug;
    fn defensive_map<U, F>(self, f: F) -> Option<U>
       where F: FnOnce(T) -> U;
}
Expand description

Subset of methods similar to Defensive that can only work for a Option.

Required Methods§

fn defensive_map_or_else<U, D, F>(self, default: D, f: F) -> U
where D: FnOnce() -> U, F: FnOnce(T) -> U,

Potentially map and unpack the value to something else (U), or call the default callback if None, which should never happen.

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

Defensively transform this option to a result, mapping None to the return value of an error closure.

fn defensive_ok_or<E>(self, err: E) -> Result<T, E>
where E: Debug,

Defensively transform this option to a result, mapping None to a default value.

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

Exactly the same as map, but it prints the appropriate warnings if the value being mapped is None.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

§

impl<T> DefensiveOption<T> for Option<T>

§

fn defensive_map_or_else<U, D, F>(self, default: D, f: F) -> U
where D: FnOnce() -> U, F: FnOnce(T) -> U,

§

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

§

fn defensive_ok_or<E>(self, err: E) -> Result<T, E>
where E: Debug,

§

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

Implementors§