use std::error::Error;1use std::fmt;2use std::marker;34/// An error returned from the `proc_exit` host syscall.5///6/// Embedders can test if an error returned from wasm is this error, in which7/// case it may signal a non-fatal trap.8#[derive(Debug)]9pub struct I32Exit(pub i32);1011impl fmt::Display for I32Exit {12fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {13write!(f, "Exited with i32 exit status {}", self.0)14}15}1617impl std::error::Error for I32Exit {}1819/// A helper error type used by many other modules through type aliases.20///21/// This type is an `Error` itself and is intended to be a representation of22/// either:23///24/// * A custom error type `T`25/// * A trap, represented as `anyhow::Error`26///27/// This error is created through either the `::trap` constructor representing a28/// full-fledged trap or the `From<T>` constructor which is intended to be used29/// with `?`. The goal is to make normal errors `T` "automatic" but enable error30/// paths to return a `::trap` error optionally still as necessary without extra31/// boilerplate everywhere else.32///33/// Note that this type isn't used directly but instead is intended to be used34/// as:35///36/// ```rust,ignore37/// type MyError = TrappableError<bindgen::TheError>;38/// ```39///40/// where `MyError` is what you'll use throughout bindings code and41/// `bindgen::TheError` is the type that this represents as generated by the42/// `bindgen!` macro.43#[repr(transparent)]44pub struct TrappableError<T> {45err: anyhow::Error,46_marker: marker::PhantomData<T>,47}4849impl<T> TrappableError<T> {50pub fn trap(err: impl Into<anyhow::Error>) -> TrappableError<T> {51TrappableError {52err: err.into(),53_marker: marker::PhantomData,54}55}5657pub fn downcast(self) -> anyhow::Result<T>58where59T: Error + Send + Sync + 'static,60{61self.err.downcast()62}6364pub fn downcast_ref(&self) -> Option<&T>65where66T: Error + Send + Sync + 'static,67{68self.err.downcast_ref()69}70}7172impl<T> From<T> for TrappableError<T>73where74T: Error + Send + Sync + 'static,75{76fn from(error: T) -> Self {77Self {78err: error.into(),79_marker: marker::PhantomData,80}81}82}8384impl<T> fmt::Debug for TrappableError<T> {85fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {86self.err.fmt(f)87}88}8990impl<T> fmt::Display for TrappableError<T> {91fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {92self.err.fmt(f)93}94}9596impl<T> Error for TrappableError<T> {}979899