Trait ResultRefMap

Source
pub trait ResultRefMap<'r, T: 'r, E: 'r> {
    // Required methods
    fn ref_map<U, F>(&'r self, f: F) -> Result<U, &E>
       where F: FnOnce(&'r T) -> U + 'r;
    fn ref_map_err<D, F>(&'r self, f: F) -> Result<&T, D>
       where F: FnOnce(&'r E) -> D + 'r;
}
Expand description

Extension trait for Result. Adds the ref_map() and ref_map_err() extension methods onto Result.

The ref_map() method borrows the internal object and passes it to a closure to be mapped in some way. This allows convenient use of as_* type methods or calculations which require the borrowed internal value.

use ref_map::*;

let answer: Result<Vec<i8>, Error> = Ok(vec![4, 7, 9, 5, 6]);

let filtered = answer.ref_map(|v| &v[3..]);
let answer = &[5, 6];

assert_eq!(filtered, Ok(&answer[..]));

The ref_map_err() provides the same, but on the Err(_) variant instead of Ok(_).


let answer: Result<(), String> = Err("file not found".into());

let error = answer.ref_map_err(|s| s.as_str());

assert_eq!(error, Err("file not found"));

See the crate-level documentation for more information.

Required Methods§

Source

fn ref_map<U, F>(&'r self, f: F) -> Result<U, &E>
where F: FnOnce(&'r T) -> U + 'r,

Borrows the value in the Ok variant and maps it using the provided closure.

Source

fn ref_map_err<D, F>(&'r self, f: F) -> Result<&T, D>
where F: FnOnce(&'r E) -> D + 'r,

Borrows the value in the Err variant and maps it using the provided closure.

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<'r, T: 'r, E: 'r> ResultRefMap<'r, T, E> for Result<T, E>

Source§

fn ref_map<U, F>(&'r self, f: F) -> Result<U, &E>
where F: FnOnce(&'r T) -> U + 'r,

Source§

fn ref_map_err<D, F>(&'r self, f: F) -> Result<&T, D>
where F: FnOnce(&'r E) -> D + 'r,

Implementors§