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

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
; }

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

Required methods

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

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"));
Loading content...

Implementations on Foreign Types

impl<T, E> ResultExt<T, E> for Result<T, E>[src]

Loading content...

Implementors

Loading content...