diff --git a/crates/wiggle/generate/src/funcs.rs b/crates/wiggle/generate/src/funcs.rs index 331f8e3c3b..a12910c914 100644 --- a/crates/wiggle/generate/src/funcs.rs +++ b/crates/wiggle/generate/src/funcs.rs @@ -60,7 +60,7 @@ pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream { let err_typename = names.type_ref(&tref, anon_lifetime()); quote! { let e = wiggle::GuestError::InFunc { funcname: #funcname, location: #location, err: Box::new(e.into()) }; - let err: #err_typename = wiggle::GuestErrorType::from_error(e, ctx); + let err: #err_typename = wiggle::GuestErrorType::from_error(e, ctx); // XXX replace with conversion method on trait! return #abi_ret::from(err); } } else { diff --git a/crates/wiggle/macro/src/lib.rs b/crates/wiggle/macro/src/lib.rs index 2cd1c27b53..f0e587ce4d 100644 --- a/crates/wiggle/macro/src/lib.rs +++ b/crates/wiggle/macro/src/lib.rs @@ -66,16 +66,11 @@ use syn::parse_macro_input; /// /// /// For all types used in the `Error` position of a `Result` in the module /// /// traits, you must implement `GuestErrorType` which tells wiggle-generated -/// /// code how to determine if a method call has been successful, as well as -/// /// how to translate a wiggle runtime error into an ABI-level error. -/// impl<'a> GuestErrorType<'a> for types::Errno { -/// type Context = YourCtxType; +/// /// code what value to return when the method returns Ok(...). +/// impl GuestErrorType for types::Errno { /// fn success() -> Self { /// unimplemented!() /// } -/// fn from_error(_e: wiggle::GuestError, _c: &Self::Context) -> Self { -/// unimplemented!() -/// } /// } /// /// # fn main() { println!("this fools doc tests into compiling the above outside a function body") diff --git a/crates/wiggle/src/guest_type.rs b/crates/wiggle/src/guest_type.rs index 534eed5489..51e003c7f6 100644 --- a/crates/wiggle/src/guest_type.rs +++ b/crates/wiggle/src/guest_type.rs @@ -1,10 +1,14 @@ use crate::{GuestError, GuestPtr}; use std::mem; -pub trait GuestErrorType<'a> { - type Context; +/// A trait for types which are used to report errors. Each type used in the +/// first result position of an interface function is used, by convention, to +/// indicate whether the function was successful and subsequent results are valid, +/// or whether an error occured. This trait allows wiggle to return the correct +/// value when the interface function's idiomatic Rust method returns +/// Ok(). +pub trait GuestErrorType { fn success() -> Self; - fn from_error(e: GuestError, ctx: &Self::Context) -> Self; } /// A trait for types that are intended to be pointees in `GuestPtr`. diff --git a/crates/wiggle/test-helpers/src/lib.rs b/crates/wiggle/test-helpers/src/lib.rs index 27766dbff9..f41515be52 100644 --- a/crates/wiggle/test-helpers/src/lib.rs +++ b/crates/wiggle/test-helpers/src/lib.rs @@ -314,16 +314,10 @@ impl<'a> WasiCtx<'a> { #[macro_export] macro_rules! impl_errno { ( $errno:ty ) => { - impl<'a> wiggle::GuestErrorType<'a> for $errno { - type Context = WasiCtx<'a>; + impl wiggle::GuestErrorType for $errno { fn success() -> $errno { <$errno>::Ok } - fn from_error(e: GuestError, ctx: &WasiCtx) -> $errno { - eprintln!("GUEST ERROR: {:?}", e); - ctx.guest_errors.borrow_mut().push(e); - types::Errno::InvalidArg - } } }; } diff --git a/crates/wiggle/tests/wasi.rs b/crates/wiggle/tests/wasi.rs index 5e2f133782..c605be9c3f 100644 --- a/crates/wiggle/tests/wasi.rs +++ b/crates/wiggle/tests/wasi.rs @@ -23,18 +23,10 @@ fn document_equivelant() { type Result = std::result::Result; -impl<'a> GuestErrorType<'a> for types::Errno { - type Context = WasiCtx<'a>; - +impl GuestErrorType for types::Errno { fn success() -> types::Errno { types::Errno::Success } - - fn from_error(e: GuestError, ctx: &Self::Context) -> types::Errno { - eprintln!("GUEST ERROR: {:?}", e); - ctx.guest_errors.borrow_mut().push(e); - types::Errno::Io - } } impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> {