radicle_std_ext::result

Trait ResultExt

Source
pub trait ResultExt<T, E> {
    // Required method
    fn or_matches<E2, P, F>(self, pred: P, f: F) -> Result<T, E2>
       where E2: From<E>,
             P: FnOnce(&E) -> bool,
             F: FnOnce() -> Result<T, E2>;
}

Required Methods§

Source

fn or_matches<E2, P, F>(self, pred: P, f: F) -> Result<T, E2>
where E2: From<E>, P: FnOnce(&E) -> bool, F: FnOnce() -> Result<T, E2>,

Calls f if the result is Err, and the predicate pred on the error value returns true. Otherwise returns the Ok value of self. Note that f may change the error type, so as long as the target type can be converted from the original one.

§Examples
use std::io;
use radicle_std_ext::result::ResultExt as _;

let res = Err(io::Error::new(io::ErrorKind::Other, "crashbug"))
    .or_matches::<io::Error, _, _>(|e| matches!(e.kind(), io::ErrorKind::Other), || Ok(()))
    .unwrap();

assert_eq!((), res)

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 or_matches<E2, P, F>(self, pred: P, f: F) -> Result<T, E2>
where E2: From<E>, P: FnOnce(&E) -> bool, F: FnOnce() -> Result<T, E2>,

Implementors§