Trait ResultExt

Source
pub trait ResultExt<T, E> {
    // Required methods
    fn combine<U>(self, other: Result<U, E>) -> Result<(T, U), E>;
    fn combine_with<U, F, R>(self, other: Result<U, E>, f: F) -> Result<R, E>
       where F: FnOnce(T, U) -> R;
}
Expand description

Extension trait with useful methods for std::result::Result.

Required Methods§

Source

fn combine<U>(self, other: Result<U, E>) -> Result<(T, U), E>

Combines self and another Result.

If self is Ok(s) and other is Ok(o), this method returns Ok((s, o)). Otherwise, if the self is Ok(s) and other is Err(e), this method returns Err(e). Otherwise, self is Err(e) and this method returns Err(e) (other is not taken into account, as in short circuit calculations).

§Examples
use stdext::prelude::*;

let x = Ok(1);
let y = Ok("hi");
let z: Result<i32, &str> = Err("error");
let z2: Result<i32, &str> = Err("other_error");

assert_eq!(x.combine(y), Ok((1, "hi")));
assert_eq!(x.combine(z), Err("error"));
assert_eq!(z.combine(z2), Err("error"));
Source

fn combine_with<U, F, R>(self, other: Result<U, E>, f: F) -> Result<R, E>
where F: FnOnce(T, U) -> R,

Combines self and another Result with function f.

If self is Ok(s) and other is Ok(o), this method returns Ok(f(s, o)). Otherwise, if the self is Ok(s) and other is Err(e), this method returns Err(e). Otherwise, self is Err(e) and this method returns Err(e) (other is not taken into account, as in short circuit calculations).

§Examples
use stdext::prelude::*;

let x = Ok(1);
let y = Ok(2);
let z: Result<i32, &str> = Err("error");
let z2: Result<i32, &str> = Err("other_error");

assert_eq!(x.combine_with(y, |l, r| l + r), Ok(3));
assert_eq!(x.combine_with(z, |l, r| l + r), Err("error"));
assert_eq!(z.combine_with(z2, |l, r| l + r), Err("error"));

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.

Implementations on Foreign Types§

Source§

impl<T, E> ResultExt<T, E> for Result<T, E>

Source§

fn combine<U>(self, other: Result<U, E>) -> Result<(T, U), E>

Source§

fn combine_with<U, F, R>(self, other: Result<U, E>, f: F) -> Result<R, E>
where F: FnOnce(T, U) -> R,

Implementors§