Trait HostError

Source
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 swasmi::{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§

Source§

impl dyn HostError

Source

pub fn downcast_ref<T: HostError>(&self) -> Option<&T>

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

Source

pub fn downcast_mut<T: HostError>(&mut self) -> Option<&mut T>

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

Implementors§