Browse Source

GuestErrorType only needs to have a success constructor

pull/1470/head
Pat Hickey 5 years ago
parent
commit
167a040ea5
  1. 2
      crates/wiggle/generate/src/funcs.rs
  2. 9
      crates/wiggle/macro/src/lib.rs
  3. 10
      crates/wiggle/src/guest_type.rs
  4. 8
      crates/wiggle/test-helpers/src/lib.rs
  5. 10
      crates/wiggle/tests/wasi.rs

2
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 {

9
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")

10
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(<rest of return values>).
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<T>`.

8
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
}
}
};
}

10
crates/wiggle/tests/wasi.rs

@ -23,18 +23,10 @@ fn document_equivelant() {
type Result<T> = std::result::Result<T, types::Errno>;
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> {

Loading…
Cancel
Save