Trait result_ext::ResultExt

source ·
pub trait ResultExt<T, E> {
    fn contains<U>(&self, x: &U) -> bool
    where
        U: PartialEq<T>
; fn contains_err<F>(&self, f: &F) -> bool
    where
        F: PartialEq<E>
; fn map_or2<U, F: FnOnce(T) -> U>(self, f: F, default: U) -> U; fn map_or_else2<U, F: FnOnce(T) -> U, D: FnOnce(E) -> U>(
        self,
        f: F,
        default: D
    ) -> U; }
Expand description

Extension trait providing additional methods for Result.

Required Methods§

Returns true if the result is an Ok value containing the given value.

Examples
use result_ext::ResultExt;

let x: Result<u32, &str> = Ok(2);
assert_eq!(x.contains(&2), true);

let x: Result<u32, &str> = Ok(3);
assert_eq!(x.contains(&2), false);

let x: Result<u32, &str> = Err("Some error message");
assert_eq!(x.contains(&2), false);

Returns true if the result is an Err value containing the given value.

Examples
use result_ext::ResultExt;

let x: Result<u32, &str> = Ok(2);
assert_eq!(x.contains_err(&"Some error message"), false);

let x: Result<u32, &str> = Err("Some error message");
assert_eq!(x.contains_err(&"Some error message"), true);

let x: Result<u32, &str> = Err("Some other error message");
assert_eq!(x.contains_err(&"Some error message"), false);

Returns the result from applying the function f to the contained value if the result is Ok, or returns provided default value if the result is Err.

The f argument of map_or2 is only evaluated if the result is Ok. The default argument of map_or2 is always evaluated – even if the result is Ok. Use map_or_else2 to avoid evaluating the default argument.

Examples
use result_ext::ResultExt;

let x: Result<_, &str> = Ok("foo");
assert_eq!(x.map_or2(|v| v.len(), 23), 3);

let x: Result<&str, _> = Err("bar");
assert_eq!(x.map_or2(|v| v.len(), 23), 23);

Returns the result from applying the function f to the contained value if the result is Ok, or returns the result from applying the function default to the contained error if the result is Err.

The f argument of map_or_else2 is only evaluated if the result is Ok. The default argument of map_or_else2 is only evaluated if the result is Err. Use map_or2 to always evaluate the default argument.

Examples
use result_ext::ResultExt;

let k = 23;

let x : Result<_, &str> = Ok("foo");
assert_eq!(x.map_or_else2(|v| v.len(), |e| k * 2), 3);

let x : Result<&str, _> = Err("bar");
assert_eq!(x.map_or_else2(|v| v.len(), |e| k * 2), 46);

Implementations on Foreign Types§

Implementors§