[][src]Struct display_error_chain::DisplayErrorChain

pub struct DisplayErrorChain<'a, E: ?Sized>(_);

Provides an fmt::Display implementation for an error as a chain.

use display_error_chain::DisplayErrorChain;

// Let's set up a custom error. Normally one would use `snafu` or
// something similar to avoid the boilerplate.
#[derive(Debug)]
enum CustomError {
    NoCause,
    IO { source: std::io::Error },
}

// Custom display implementation (which doesn't take error
// sources into consideration).
impl std::fmt::Display for CustomError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CustomError::NoCause => {
                write!(f, "No cause")
            }
            CustomError::IO { .. } => {
                write!(f, "Some I/O")
            }
        }
    }
}

impl std::error::Error for CustomError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            CustomError::NoCause => None,
            CustomError::IO { source } => Some(source),
        }
    }
}

// And finally let's see how `DisplayErrorChain` helps!
let io = CustomError::IO {
    source: std::io::Error::new(std::io::ErrorKind::AlreadyExists, "wow"),
};
let formatted = DisplayErrorChain::new(&io).to_string();
assert_eq!("Some I/O\nCaused by:\n  -> wow", formatted);

let no_cause = CustomError::NoCause;
let formatted = DisplayErrorChain::new(&no_cause).to_string();
assert_eq!("No cause", formatted);

Implementations

impl<'a, E: ?Sized> DisplayErrorChain<'a, E> where
    E: Error
[src]

pub fn new(error: &'a E) -> Self[src]

Initializes the formatter with the error provided.

Trait Implementations

impl<'a, E: ?Sized> Display for DisplayErrorChain<'a, E> where
    E: Error
[src]

Auto Trait Implementations

impl<'a, E: ?Sized> RefUnwindSafe for DisplayErrorChain<'a, E> where
    E: RefUnwindSafe
[src]

impl<'a, E: ?Sized> Send for DisplayErrorChain<'a, E> where
    E: Sync
[src]

impl<'a, E: ?Sized> Sync for DisplayErrorChain<'a, E> where
    E: Sync
[src]

impl<'a, E: ?Sized> Unpin for DisplayErrorChain<'a, E>[src]

impl<'a, E: ?Sized> UnwindSafe for DisplayErrorChain<'a, E> where
    E: RefUnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.