[go: up one dir, main page]

Source

Trait Source 

Source
pub trait Source: Trace + Error {
    // Required method
    fn new<T: Error + Send + Sync + 'static>(source: T) -> Self;
}
Expand description

An error type which can be uniformly constructed from an Error and additional trace information.

§Example

use core::{error::Error, fmt};

use rancor::{fail, Source};

#[derive(Debug)]
struct DivideByZeroError;

impl fmt::Display for DivideByZeroError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "attempted to divide by zero")
    }
}

impl Error for DivideByZeroError {}

fn try_divide<E: Source>(a: i32, b: i32) -> Result<i32, E> {
    if b == 0 {
        fail!(DivideByZeroError);
    }
    Ok(a / b)
}

Required Methods§

Source

fn new<T: Error + Send + Sync + 'static>(source: T) -> Self

Returns a new Self using the given Error.

Depending on the specific implementation, this may box the error, immediately emit a diagnostic, or discard it and only remember that some error occurred.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Source for Panic

Source§

impl Source for BoxedError

Available on crate feature alloc only.
Source§

impl Source for Error

Source§

impl Source for Failure