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
sourceimpl dyn HostError
impl dyn HostError
sourcepub fn downcast_ref<T: HostError>(&self) -> Option<&T>
pub fn downcast_ref<T: HostError>(&self) -> Option<&T>
Attempt to downcast this HostError
to a concrete type by reference.
sourcepub fn downcast_mut<T: HostError>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: HostError>(&mut self) -> Option<&mut T>
Attempt to downcast this HostError
to a concrete type by mutable
reference.