Trait stdext::result::ResultExt[][src]

pub trait ResultExt<T, E> {
    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

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"));

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"));

Implementations on Foreign Types

Implementors