Skip to main content

DisplayError

Struct DisplayError 

Source
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

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>

Source

pub fn new(error: E) -> Self

Wraps error in a DisplayError, making it usable as a source field.

source() will always return None. For automatic source chain preservation, use #[suzu(from)] instead.

Source§

impl<E> DisplayError<E>

Source

pub fn inner(&self) -> &E

Returns a reference to the wrapped value.

Source

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.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E: Debug> Debug for DisplayError<E>

Source§

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

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

impl<E: Display> Display for DisplayError<E>

Source§

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

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

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)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<E: Hash> Hash for DisplayError<E>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
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.

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<E: Eq> Eq for DisplayError<E>

Auto Trait Implementations§

§

impl<E> Freeze for DisplayError<E>
where E: Freeze,

§

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

§

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

§

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

§

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

§

impl<E> UnsafeUnpin for DisplayError<E>
where E: UnsafeUnpin,

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsErrorSource for T
where T: Error + 'static,

Source§

fn as_error_source(&self) -> &(dyn Error + 'static)

For maximum effectiveness, this needs to be called as a method to benefit from Rust’s automatic dereferencing of method receivers.
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

fn to_string(&self) -> String

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

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.