pub struct DisplayErrorChain<E>(/* private fields */);
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());
Other standard traits (like Debug
, Clone
and some
others) are automatically derived for the convenience using the standard
derive macros. If you need another trait, feel free to submit a PR and/or
use the DisplayErrorChain::into_inner
method to access the wrapped
error.
Implementations§
source§impl<E> DisplayErrorChain<E>where
E: Error,
impl<E> DisplayErrorChain<E>where
E: Error,
sourcepub fn into_inner(self) -> E
pub fn into_inner(self) -> E
Deconstructs the DisplayErrorChain
and returns the wrapped error.
Trait Implementations§
source§impl<E: Clone> Clone for DisplayErrorChain<E>
impl<E: Clone> Clone for DisplayErrorChain<E>
source§fn clone(&self) -> DisplayErrorChain<E>
fn clone(&self) -> DisplayErrorChain<E>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<E: Debug> Debug for DisplayErrorChain<E>
impl<E: Debug> Debug for DisplayErrorChain<E>
source§impl<E> Display for DisplayErrorChain<E>where
E: Error,
impl<E> Display for DisplayErrorChain<E>where
E: Error,
source§impl<E: Error> From<E> for DisplayErrorChain<E>
impl<E: Error> From<E> for DisplayErrorChain<E>
source§impl<E: Hash> Hash for DisplayErrorChain<E>
impl<E: Hash> Hash for DisplayErrorChain<E>
source§impl<E: Ord> Ord for DisplayErrorChain<E>
impl<E: Ord> Ord for DisplayErrorChain<E>
source§fn cmp(&self, other: &DisplayErrorChain<E>) -> Ordering
fn cmp(&self, other: &DisplayErrorChain<E>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl<E: PartialEq> PartialEq for DisplayErrorChain<E>
impl<E: PartialEq> PartialEq for DisplayErrorChain<E>
source§impl<E: PartialOrd> PartialOrd for DisplayErrorChain<E>
impl<E: PartialOrd> PartialOrd for DisplayErrorChain<E>
impl<E: Copy> Copy for DisplayErrorChain<E>
impl<E: Eq> Eq for DisplayErrorChain<E>
impl<E> StructuralPartialEq for DisplayErrorChain<E>
Auto Trait Implementations§
impl<E> Freeze for DisplayErrorChain<E>where
E: Freeze,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)