pub struct DisplayError<E> { /* private fields */ }Expand description
Wrapper that converts a Debug + Display type (without Error impl) into
a core::error::Error.
Useful for wrapping third-party error types that don’t implement Error,
making them usable as snafu source fields.
§Usage
§Pattern A: #[suzu(from)] — auto-wraps type and generates source(from(...)) (recommended)
use suzunari_error::*;
// A third-party type that implements Debug + Display but not Error.
#[derive(Debug)]
struct LibError(String);
impl std::fmt::Display for LibError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[suzunari_error]
#[suzu(display("operation failed"))]
struct AppError {
#[suzu(from)]
source: LibError, // becomes DisplayError<LibError>
}Note: #[suzu(from)] requires the field type to be a concrete type.
Fields whose type contains a generic type parameter of the enclosing struct
or enum are rejected at compile time.
§Pattern B: Manual source(from(...)) — explicit control
Uses #[snafu(source(from(...)))] directly with DisplayError::new.
Note: DisplayError::new always returns None from source(), so this
pattern does not preserve the source chain even if LibError implements
Error. Use Pattern A (#[suzu(from)]) for automatic source chain
preservation.
use suzunari_error::*;
// A third-party type that implements Error.
#[derive(Debug)]
struct LibError(String);
impl std::fmt::Display for LibError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
// LibError implements Error, but DisplayError::new does not preserve its source chain.
impl std::error::Error for LibError {}
#[suzunari_error]
#[suzu(display("operation failed"))]
struct AppError {
#[snafu(source(from(LibError, DisplayError::new)))]
source: DisplayError<LibError>,
}§Source chain preservation
When constructed via #[suzu(from)], DisplayError automatically detects
whether the wrapped type implements Error at compile time (using autoref
specialization). If it does, source() delegates to the inner type’s
source(). If not, source() returns None.
When constructed manually via DisplayError::new(), source() always
returns None. Use #[suzu(from)] for automatic source chain preservation.
§Pattern C: map_err — direct wrapping without snafu context
use suzunari_error::DisplayError;
#[derive(Debug)]
struct LibError(String);
impl std::fmt::Display for LibError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
fn fallible() -> Result<(), LibError> {
Err(LibError("boom".into()))
}
// Wrap non-Error type into Error for use with ? or error combinators
fn do_something() -> Result<(), Box<dyn std::error::Error>> {
fallible().map_err(DisplayError::new)?;
Ok(())
}Implementations§
Source§impl<E: Debug + Display> DisplayError<E>
impl<E: Debug + Display> DisplayError<E>
Source§impl<E> DisplayError<E>
impl<E> DisplayError<E>
Sourcepub fn into_inner(self) -> E
pub fn into_inner(self) -> E
Unwraps and returns the inner value.
Trait Implementations§
Source§impl<E: Clone> Clone for DisplayError<E>
Clones the inner E value and copies the get_source function pointer,
preserving source chain delegation behavior in the clone.
impl<E: Clone> Clone for DisplayError<E>
Clones the inner E value and copies the get_source function pointer,
preserving source chain delegation behavior in the clone.
Source§impl<E: Debug> Debug for DisplayError<E>
impl<E: Debug> Debug for DisplayError<E>
Source§impl<E: Display> Display for DisplayError<E>
impl<E: Display> Display for DisplayError<E>
Source§impl<E: Debug + Display> Error for DisplayError<E>
Delegates source() to the stored get_source function pointer.
impl<E: Debug + Display> Error for DisplayError<E>
Delegates source() to the stored get_source function pointer.
When constructed via #[suzu(from)] (macro-generated code), this
automatically delegates to the inner type’s source() if it implements
Error, or returns None otherwise. When constructed via new(),
always returns None.
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl<E: Hash> Hash for DisplayError<E>
impl<E: Hash> Hash for DisplayError<E>
Source§impl<E: PartialEq> PartialEq for DisplayError<E>
Compares only the inner E value. Two DisplayError<E> instances are
considered equal if their inner values are equal, regardless of how they
were constructed. The get_source function pointer is an implementation
detail for source chain delegation and is not part of the value identity.
impl<E: PartialEq> PartialEq for DisplayError<E>
Compares only the inner E value. Two DisplayError<E> instances are
considered equal if their inner values are equal, regardless of how they
were constructed. The get_source function pointer is an implementation
detail for source chain delegation and is not part of the value identity.