Trait ndless::error::Error

source ·
pub trait Error: Debug + Display {
    // Provided methods
    fn description(&self) -> &str { ... }
    fn cause(&self) -> Option<&dyn Error> { ... }
    fn source(&self) -> Option<&(dyn Error + 'static)> { ... }
}
Expand description

Error is a trait representing the basic expectations for error values, i.e., values of type E in Result<T, E>. Errors must describe themselves through the Display and Debug traits, and may provide cause chain information:

The source method is generally used when errors cross “abstraction boundaries”. If one module must report an error that is caused by an error from a lower-level module, it can allow access to that error via the source method. This makes it possible for the high-level module to provide its own errors while also revealing some of the implementation for debugging via source chains.

Provided Methods§

source

fn description(&self) -> &str

This method is soft-deprecated.

Although using it won’t cause compilation warning, new code should use Display instead and new impls can omit it.

To obtain error description as a string, use to_string().

§Examples
match "xc".parse::<u32>() {
    Err(e) => {
        // Print `e` itself, not `e.description()`.
        println!("Error: {}", e);
    }
    _ => println!("No error"),
}
source

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

👎Deprecated: replaced by Error::source, which can support downcasting

The lower-level cause of this error, if any.

§Examples
use std::error::Error;
use std::fmt;

#[derive(Debug)]
struct SuperError {
    side: SuperErrorSideKick,
}

impl fmt::Display for SuperError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SuperError is here!")
    }
}

impl Error for SuperError {
    fn description(&self) -> &str {
        "I'm the superhero of errors"
    }

    fn cause(&self) -> Option<&Error> {
        Some(&self.side)
    }
}

#[derive(Debug)]
struct SuperErrorSideKick;

impl fmt::Display for SuperErrorSideKick {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SuperErrorSideKick is here!")
    }
}

impl Error for SuperErrorSideKick {
    fn description(&self) -> &str {
        "I'm SuperError side kick"
    }
}

fn get_super_error() -> Result<(), SuperError> {
    Err(SuperError { side: SuperErrorSideKick })
}

fn main() {
    match get_super_error() {
        Err(e) => {
            println!("Error: {}", e.description());
            println!("Caused by: {}", e.cause().unwrap());
        }
        _ => println!("No error"),
    }
}
source

fn source(&self) -> Option<&(dyn Error + 'static)>

The lower-level source of this error, if any.

§Examples
use std::error::Error;
use std::fmt;

#[derive(Debug)]
struct SuperError {
    side: SuperErrorSideKick,
}

impl fmt::Display for SuperError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SuperError is here!")
    }
}

impl Error for SuperError {
    fn description(&self) -> &str {
        "I'm the superhero of errors"
    }

    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.side)
    }
}

#[derive(Debug)]
struct SuperErrorSideKick;

impl fmt::Display for SuperErrorSideKick {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "SuperErrorSideKick is here!")
    }
}

impl Error for SuperErrorSideKick {
    fn description(&self) -> &str {
        "I'm SuperError side kick"
    }
}

fn get_super_error() -> Result<(), SuperError> {
    Err(SuperError { side: SuperErrorSideKick })
}

fn main() {
    match get_super_error() {
        Err(e) => {
            println!("Error: {}", e.description());
            println!("Caused by: {}", e.source().unwrap());
        }
        _ => println!("No error"),
    }
}

Implementations§

source§

impl dyn Error + 'static

source

pub fn is<T: Error + 'static>(&self) -> bool

Returns true if the boxed type is the same as T

source

pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>

Returns some reference to the boxed value if it is of type T, or None if it isn’t.

source

pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>

Returns some mutable reference to the boxed value if it is of type T, or None if it isn’t.

source§

impl dyn Error + Send + 'static

source

pub fn is<T: Error + 'static>(&self) -> bool

Forwards to the method defined on the type Any.

source

pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>

Forwards to the method defined on the type Any.

source

pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>

Forwards to the method defined on the type Any.

source§

impl dyn Error + Send + Sync + 'static

source

pub fn is<T: Error + 'static>(&self) -> bool

Forwards to the method defined on the type Any.

source

pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>

Forwards to the method defined on the type Any.

source

pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>

Forwards to the method defined on the type Any.

source§

impl dyn Error

source

pub fn downcast<T: Error + 'static>( self: Box<Self> ) -> Result<Box<T>, Box<dyn Error>>

Attempt to downcast the box to a concrete type.

source§

impl dyn Error + Send

source

pub fn downcast<T: Error + 'static>( self: Box<Self> ) -> Result<Box<T>, Box<dyn Error + Send>>

Attempt to downcast the box to a concrete type.

source§

impl dyn Error + Send + Sync

source

pub fn downcast<T: Error + 'static>( self: Box<Self> ) -> Result<Box<T>, Box<Self>>

Attempt to downcast the box to a concrete type.

Trait Implementations§

source§

impl From<&str> for Box<dyn Error>

source§

fn from(err: &str) -> Box<dyn Error>

Converts a [str] into a box of dyn Error.

§Examples
use std::error::Error;
use std::mem;

let a_str_error = "a str error";
let a_boxed_error = Box::<Error>::from(a_str_error);
assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
source§

impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a>

source§

fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a>

Converts a [str] into a box of dyn Error + Send + Sync.

§Examples
use std::error::Error;
use std::mem;

let a_str_error = "a str error";
let a_boxed_error = Box::<Error + Send + Sync>::from(a_str_error);
assert!(
    mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
source§

impl<'a> From<Cow<'a, str>> for Box<dyn Error>

source§

fn from(err: Cow<'a, str>) -> Box<dyn Error>

Converts a Cow into a box of dyn Error.

§Examples
use std::error::Error;
use std::mem;
use std::borrow::Cow;

let a_cow_str_error = Cow::from("a str error");
let a_boxed_error = Box::<Error>::from(a_cow_str_error);
assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
source§

impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a>

source§

fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a>

Converts a Cow into a box of dyn Error + Send + Sync.

§Examples
use std::error::Error;
use std::mem;
use std::borrow::Cow;

let a_cow_str_error = Cow::from("a str error");
let a_boxed_error = Box::<Error + Send + Sync>::from(a_cow_str_error);
assert!(
    mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
source§

impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a>

source§

fn from(err: E) -> Box<dyn Error + 'a>

Converts a type of Error into a box of dyn Error.

§Examples
use std::error::Error;
use std::fmt;
use std::mem;

#[derive(Debug)]
struct AnError;

impl fmt::Display for AnError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f , "An error")
    }
}

impl Error for AnError {
    fn description(&self) -> &str {
        "Description of an error"
    }
}

let an_error = AnError;
assert!(0 == mem::size_of_val(&an_error));
let a_boxed_error = Box::<Error>::from(an_error);
assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
source§

impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a>

source§

fn from(err: E) -> Box<dyn Error + Send + Sync + 'a>

Converts a type of Error + Send + Sync into a box of dyn Error + Send + Sync.

§Examples
use std::error::Error;
use std::fmt;
use std::mem;

#[derive(Debug)]
struct AnError;

impl fmt::Display for AnError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f , "An error")
    }
}

impl Error for AnError {
    fn description(&self) -> &str {
        "Description of an error"
    }
}

unsafe impl Send for AnError {}

unsafe impl Sync for AnError {}

let an_error = AnError;
assert!(0 == mem::size_of_val(&an_error));
let a_boxed_error = Box::<Error + Send + Sync>::from(an_error);
assert!(
    mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
source§

impl From<String> for Box<dyn Error>

source§

fn from(str_err: String) -> Box<dyn Error>

Converts a String into a box of dyn Error.

§Examples
use std::error::Error;
use std::mem;

let a_string_error = "a string error".to_string();
let a_boxed_error = Box::<Error>::from(a_string_error);
assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
source§

impl From<String> for Box<dyn Error + Send + Sync>

source§

fn from(err: String) -> Box<dyn Error + Send + Sync>

Converts a String into a box of dyn Error + Send + Sync.

§Examples
use std::error::Error;
use std::mem;

let a_string_error = "a string error".to_string();
let a_boxed_error = Box::<Error + Send + Sync>::from(a_string_error);
assert!(
    mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))

Implementations on Foreign Types§

source§

impl Error for TryFromSliceError

source§

fn description(&self) -> &str

source§

impl Error for BorrowError

source§

fn description(&self) -> &str

source§

impl Error for BorrowMutError

source§

fn description(&self) -> &str

source§

impl Error for CharTryFromError

source§

fn description(&self) -> &str

source§

impl Error for ParseCharError

source§

fn description(&self) -> &str

source§

impl Error for DecodeUtf16Error

source§

fn description(&self) -> &str

source§

impl Error for Error

source§

fn description(&self) -> &str

source§

impl Error for ParseFloatError

source§

fn description(&self) -> &str

source§

impl Error for ParseIntError

source§

fn description(&self) -> &str

source§

impl Error for TryFromIntError

source§

fn description(&self) -> &str

source§

impl Error for ParseBoolError

source§

fn description(&self) -> &str

source§

impl Error for Utf8Error

source§

fn description(&self) -> &str

Implementors§