better error trait design
This commit is contained in:
28
src/lib.rs
28
src/lib.rs
@@ -1,18 +1,30 @@
|
||||
pub mod test {
|
||||
// FIXME: parameterize macro on what ctx type is used here
|
||||
generate::from_witx!("test.witx");
|
||||
|
||||
pub struct WasiCtx {} // FIXME: parameterize macro on what ctx type is used here
|
||||
pub struct WasiCtx {
|
||||
mem_errors: Vec<::memory::MemoryError>,
|
||||
value_errors: Vec<::memory::GuestValueError>,
|
||||
}
|
||||
|
||||
impl types::WitxErrorConversion for WasiCtx {
|
||||
fn success_to_errno(&mut self) -> types::Errno {
|
||||
types::Errno::Ok
|
||||
// Errno is used as a first return value in the functions above, therefore
|
||||
// it must implement GuestError with type Context = WasiCtx.
|
||||
// The context type should let you do logging or debugging or whatever you need
|
||||
// with these errors. We just push them to vecs.
|
||||
impl ::memory::GuestError for types::Errno {
|
||||
type Context = WasiCtx;
|
||||
fn is_success(&self) -> bool {
|
||||
match self {
|
||||
types::Errno::Ok => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
fn memory_error_to_errno(&mut self, e: ::memory::MemoryError) -> types::Errno {
|
||||
eprintln!("memory error: {:?}", e);
|
||||
fn from_memory_error(e: ::memory::MemoryError, ctx: &mut WasiCtx) -> types::Errno {
|
||||
ctx.mem_errors.push(e);
|
||||
types::Errno::InvalidArg
|
||||
}
|
||||
fn value_error_to_errno(&mut self, e: ::memory::GuestValueError) -> types::Errno {
|
||||
eprintln!("guest value error: {:?}", e);
|
||||
fn from_value_error(e: ::memory::GuestValueError, ctx: &mut WasiCtx) -> types::Errno {
|
||||
ctx.value_errors.push(e);
|
||||
types::Errno::InvalidArg
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user