use std::fmt;
pub struct DisplayResult<'a, T: fmt::Display, E: fmt::Display>(pub &'a Result<T, E>);
impl<T: fmt::Display, E: fmt::Display> fmt::Display for DisplayResult<'_, T, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
Ok(t) => write!(f, "Ok({})", t),
Err(e) => write!(f, "Err({})", e),
}
}
}
pub trait DisplayResultExt<'a, T: fmt::Display, E: fmt::Display> {
fn display(&'a self) -> DisplayResult<'a, T, E>;
}
impl<T: fmt::Display, E: fmt::Display> DisplayResultExt<'_, T, E> for Result<T, E> {
fn display(&self) -> DisplayResult<'_, T, E> {
DisplayResult(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display_result() {
let result = Result::<i32, i32>::Ok(1);
assert_eq!(result.display().to_string(), "Ok(1)");
let result = Result::<i32, i32>::Err(2);
assert_eq!(result.display().to_string(), "Err(2)");
}
}