pub trait HostError: 'static + Display + Debug + DowncastSync { }
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.
Types that implement this trait can automatically be converted to wasmi::Error
and wasmi::Trap
and will be represented as a boxed HostError
. You can then use the various methods on wasmi::Error
to get your custom error type back
Examples
use std::fmt;
use wasmi_core::{Trap, 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<(), Trap> {
let my_error = MyError { code: 1312 };
// Note how you can just convert your errors to `wasmi::Error`
Err(my_error.into())
}
// Get a reference to the concrete error
match failable_fn() {
Err(Trap::Host(host_error)) => {
let my_error: &MyError = host_error.downcast_ref::<MyError>().unwrap();
assert_eq!(my_error.code, 1312);
}
_ => panic!(),
}
// get the concrete error itself
match failable_fn() {
Err(err) => {
let my_error = match err {
Trap::Host(host_error) => host_error.downcast::<MyError>().unwrap(),
unexpected => panic!("expected host error but found: {}", unexpected),
};
assert_eq!(my_error.code, 1312);
}
_ => panic!(),
}
Implementations§
source§impl dyn HostError
impl dyn HostError
sourcepub fn is<__T>(&self) -> boolwhere
__T: HostError,
pub fn is<__T>(&self) -> boolwhere __T: HostError,
Returns true if the trait object wraps an object of type __T
.
sourcepub fn downcast<__T>(
self: Box<dyn HostError, Global>
) -> Result<Box<__T, Global>, Box<dyn HostError, Global>>where
__T: HostError,
pub fn downcast<__T>( self: Box<dyn HostError, Global> ) -> Result<Box<__T, Global>, Box<dyn HostError, Global>>where __T: HostError,
Returns a boxed object from a boxed trait object if the underlying object is of type
__T
. Returns the original boxed trait if it isn’t.
sourcepub fn downcast_rc<__T>(
self: Rc<dyn HostError>
) -> Result<Rc<__T>, Rc<dyn HostError>>where
__T: HostError,
pub fn downcast_rc<__T>( self: Rc<dyn HostError> ) -> Result<Rc<__T>, Rc<dyn HostError>>where __T: HostError,
Returns an Rc
-ed object from an Rc
-ed trait object if the underlying object is of
type __T
. Returns the original Rc
-ed trait if it isn’t.
sourcepub fn downcast_ref<__T>(&self) -> Option<&__T>where
__T: HostError,
pub fn downcast_ref<__T>(&self) -> Option<&__T>where __T: HostError,
Returns a reference to the object within the trait object if it is of type __T
, or
None
if it isn’t.
sourcepub fn downcast_mut<__T>(&mut self) -> Option<&mut __T>where
__T: HostError,
pub fn downcast_mut<__T>(&mut self) -> Option<&mut __T>where __T: HostError,
Returns a mutable reference to the object within the trait object if it is of type
__T
, or None
if it isn’t.