[][src]Enum semeion::error::Error

pub enum Error {
    Unknown,
    Code(i32),
    Message(String),
    Any(Box<dyn Any + Send>),
}

Represents any possible error.

This enum allows to encode the errors that can be raised by this library as well as allow to encode a custom error, that will need to be propagated via the APIs exposed by this library, which originates in the user's code.

Variants

Unknown

The most generic and possibly useless type of error, to be raised when no other information is available.

Code(i32)

The Code variant allows to encode errors as simple signed integers.

Message(String)

The Message variant allows to encode the error as a string.

Any(Box<dyn Any + Send>)

The Any variant allows to encode any type of error with performance costs due to the heap allocations, and type erasure.

Example

use std::fmt;

#[derive(Debug)]
enum MyError {
    Audio,
    Game,
    InvalidArg { err: String }
}

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

impl std::error::Error for MyError {}

impl semeion::error::Any for MyError {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

// we can get a `semeion::Error` from any of the public APIs
let err = semeion::Error::with_err(MyError::Audio);

// and downcast it back to its original concrete type with
if let semeion::Error::Any(err) = err {
    let my_err = err.as_any().downcast_ref::<MyError>().unwrap();
}

Implementations

impl Error[src]

pub fn with_message(message: impl Display) -> Self[src]

Constructs a new Error with the given message.

pub fn with_err(err: impl Any + Send + 'static) -> Self[src]

Constructs a new Error with the given custom value.

Trait Implementations

impl Debug for Error[src]

impl Display for Error[src]

impl Error for Error[src]

Auto Trait Implementations

impl !RefUnwindSafe for Error

impl Send for Error

impl !Sync for Error

impl Unpin for Error

impl !UnwindSafe for Error

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.