devela::all

Trait ExtOptRes

Source
pub trait ExtOptRes<T, E>: Sealed {
    // Required methods
    fn transpose_result(self) -> Result<Option<T>, E> ;
    fn unwrap_or_else_result<F: FnOnce() -> Result<T, E>>(
        self,
        f: F,
    ) -> Result<T, E> ;
    fn map_ok<U, F: FnOnce(T) -> U>(self, f: F) -> OptRes<U, E> ;
    fn map_err<F, G: FnOnce(E) -> F>(self, f: G) -> OptRes<T, F> ;
    fn ok_or_default_err(self) -> Result<T, E> 
       where E: Default;
}
Expand description

Extension trait providing additional methods for OptRes.

This trait is sealed and cannot be implemented for any other type.

See also ExtOption, ExtResult,

Required Methods§

Source

fn transpose_result(self) -> Result<Option<T>, E>

Transposes Option<Result<T, E>> into Result<Option<T>, E>.

§Examples
use devela::{ExtOptRes, OptRes};

let a: OptRes<u8, &str> = None;
let b: OptRes<u8, &str> = Some(Ok(1));
let c: OptRes<u8, &str> = Some(Err("err"));

assert_eq![a.transpose_result(), Ok(None)];
assert_eq![b.transpose_result(), Ok(Some(1))];
assert_eq![c.transpose_result(), Err("err")];

// Comparison with std:
// a.transpose_result()
// match a { Some(Ok(t)) => Ok(Some(t)), Some(Err(e)) => Err(e), None => Ok(None) }
Source

fn unwrap_or_else_result<F: FnOnce() -> Result<T, E>>( self, f: F, ) -> Result<T, E>

Unwraps the result if the Option is Some, otherwise calls the provided closure.

§Examples
use devela::{ExtOptRes, OptRes};

let a: OptRes<u8, &str> = None;
let b: OptRes<u8, &str> = Some(Ok(1));
let c: OptRes<u8, &str> = Some(Err("err"));

assert_eq![a.unwrap_or_else_result(|| Err("none")), Err("none")];
assert_eq![b.unwrap_or_else_result(|| Err("none")), Ok(1)];
assert_eq![c.unwrap_or_else_result(|| Err("none")), Err("err")];

// Comparison with std:
// a.unwrap_or_else_result(|| Err("none"))
// a.unwrap_or_else(|| Err("none")).unwrap_or_else(|_| handle_err())
Source

fn map_ok<U, F: FnOnce(T) -> U>(self, f: F) -> OptRes<U, E>

Applies a function to the Ok value inside Option<Result<T, E>>, if both are present.

§Examples
use devela::{ExtOptRes, OptRes};

let a: OptRes<u8, &str> = None;
let b: OptRes<u8, &str> = Some(Ok(1));
let c: OptRes<u8, &str> = Some(Err("err"));

assert_eq![a.map_ok(|v| v + 1), None];
assert_eq![b.map_ok(|v| v + 1), Some(Ok(2))];
assert_eq![c.map_ok(|v| v + 1), Some(Err("err"))];

// Comparison with std:
// a.map_ok(|v| v + 1)
// a.map(|res| res.map(|v| v + 1))
Source

fn map_err<F, G: FnOnce(E) -> F>(self, f: G) -> OptRes<T, F>

Applies a function to the Err value inside Option<Result<T, E>>, if both are present.

§Examples
use devela::{ExtOptRes, OptRes};

let a: OptRes<u8, &str> = None;
let b: OptRes<u8, &str> = Some(Ok(1));
let c: OptRes<u8, &str> = Some(Err("err"));

assert_eq![a.map_err(|_e| "new_err"), None];
assert_eq![b.map_err(|_e| "new_err"), Some(Ok(1))];
assert_eq![c.map_err(|_e| "new_err"), Some(Err("new_err"))];

// Comparison with std:
// a.map_err(|e| handle_error(e))
// a.map(|res| res.map_err(|e| handle_error(e)))
Source

fn ok_or_default_err(self) -> Result<T, E>
where E: Default,

Provides a default error if the Option is None.

§Examples
use devela::{ExtOptRes, OptRes};

let a: OptRes<u8, &str> = None;
let b: OptRes<u8, &str> = Some(Ok(1));
let c: OptRes<u8, &str> = Some(Err("err"));

assert_eq![a.ok_or_default_err(), Err("")];
assert_eq![b.ok_or_default_err(), Ok(1)];
assert_eq![c.ok_or_default_err(), Err("err")];

// Comparison with std:
// a.ok_or_default_err()
// a.ok_or_else(|| Err(Default::default()))

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, E> ExtOptRes<T, E> for OptRes<T, E>