Struct display_error_chain::DisplayErrorChain
source · [−]pub struct DisplayErrorChain<'a, E: ?Sized>(_);
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);
Implementations
Trait Implementations
Auto Trait Implementations
impl<'a, E: ?Sized> RefUnwindSafe for DisplayErrorChain<'a, E> where
E: RefUnwindSafe,
impl<'a, E: ?Sized> Send for DisplayErrorChain<'a, E> where
E: Sync,
impl<'a, E: ?Sized> Sync for DisplayErrorChain<'a, E> where
E: Sync,
impl<'a, E: ?Sized> Unpin for DisplayErrorChain<'a, E>
impl<'a, E: ?Sized> UnwindSafe for DisplayErrorChain<'a, E> where
E: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more