pub trait HostError: 'static + Display + Debug + Send + Sync { }
Expand description

Trait that allows the host to return custom error.

It should be useful for representing custom traps, troubles at instantiation time or other host specific conditions.

Examples

use std::fmt;
use wasmi::{Error, HostError};

#[derive(Debug)]
struct MyError {
    code: u32,
}

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "MyError, code={}", self.code)
    }
}

impl HostError for MyError { }

fn failable_fn() -> Result<(), Error> {
    let my_error = MyError { code: 1312 };
    Err(Error::Host(Box::new(my_error)))
}

match failable_fn() {
    Err(Error::Host(host_error)) => {
        let my_error = host_error.downcast_ref::<MyError>().unwrap();
        assert_eq!(my_error.code, 1312);
    }
    _ => panic!(),
}

Implementations

Attempt to downcast this HostError to a concrete type by reference.

Attempt to downcast this HostError to a concrete type by mutable reference.

Implementors