imag/libimagutil/src/info_result.rs

29 lines
666 B
Rust
Raw Normal View History

2016-07-30 09:45:31 +00:00
pub trait InfoResult<T, E> : Sized {
fn map_info<F: FnOnce(&T) -> String>(self, f: F) -> Self;
fn map_info_str(self, s: &str) -> Self {
self.map_info(|_| format!("{}", s))
}
fn map_info_err<F: FnOnce(&E) -> String>(self, f: F) -> Self;
fn map_info_err_str(self, s: &str) -> Self {
self.map_info_err(|_| format!("{}", s))
}
}
impl<T, E> InfoResult<T, E> for Result<T, E> {
fn map_info<F: FnOnce(&T) -> String>(self, f: F) -> Self {
self.map(|t| { info!("{}", f(&t)); t })
}
fn map_info_err<F: FnOnce(&E) -> String>(self, f: F) -> Self {
self.map_err(|e| { info!("{}", f(&e)); e })
}
}