pub struct DisplayErrorChain<E>(_);
Expand description

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

use display_error_chain::{DisplayErrorChain, ErrorChainExt as _};

// 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;
// You can also use a `.chain()` shortcut from the `ErrorChainExt` trait.
let formatted = no_cause.chain().to_string();
assert_eq!("No cause", formatted);

// or `.into_chain()` to make the `DisplayErrorChain` to consume the error.
let formatted = no_cause.into_chain().to_string();
assert_eq!("No cause", formatted);

// `from` or `into` will also work with both owned and referenced errors:
let chain: DisplayErrorChain<_> = CustomError::NoCause.into();
assert_eq!("No cause", chain.to_string());

let chain: DisplayErrorChain<_> = (&CustomError::NoCause).into();
assert_eq!("No cause", chain.to_string());

Implementations§

source§

impl<E> DisplayErrorChain<E>where E: Error,

source

pub fn new(error: E) -> Self

Initializes the formatter with the error provided.

source

pub fn into_inner(self) -> E

Deconstructs the DisplayErrorChain and returns the wrapped error.

Trait Implementations§

source§

impl<E> Display for DisplayErrorChain<E>where E: Error,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<E: Error> From<E> for DisplayErrorChain<E>

source§

fn from(value: E) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<E> RefUnwindSafe for DisplayErrorChain<E>where E: RefUnwindSafe,

§

impl<E> Send for DisplayErrorChain<E>where E: Send,

§

impl<E> Sync for DisplayErrorChain<E>where E: Sync,

§

impl<E> Unpin for DisplayErrorChain<E>where E: Unpin,

§

impl<E> UnwindSafe for DisplayErrorChain<E>where E: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.