wiggle::async_trait is defined as async_trait::async_trait(?Send)

async methods used by wiggle currently need to Not have the Send
constraint, so rather than make all use sites pass the argument
to the re-exported async_trait macro, define a new macro that
applies the argument.
This commit is contained in:
Pat Hickey
2021-03-29 10:04:42 -07:00
parent b1a3c9047f
commit e38166ac3f
7 changed files with 25 additions and 10 deletions

1
Cargo.lock generated
View File

@@ -3651,6 +3651,7 @@ dependencies = [
name = "wiggle-macro" name = "wiggle-macro"
version = "0.25.0" version = "0.25.0"
dependencies = [ dependencies = [
"proc-macro2",
"quote", "quote",
"syn", "syn",
"wiggle", "wiggle",

View File

@@ -89,8 +89,7 @@ pub fn define_module_trait(names: &Names, m: &Module, settings: &CodegenSettings
}); });
quote! { quote! {
use #rt::async_trait; #[#rt::async_trait]
#[async_trait(?Send)]
pub trait #traitname { pub trait #traitname {
#(#traitmethods)* #(#traitmethods)*
} }

View File

@@ -25,6 +25,7 @@ wiggle-generate = { path = "../generate", version = "0.25.0" }
witx = { version = "0.9.0", path = "../../wasi-common/WASI/tools/witx" } witx = { version = "0.9.0", path = "../../wasi-common/WASI/tools/witx" }
quote = "1.0" quote = "1.0"
syn = { version = "1.0", features = ["full"] } syn = { version = "1.0", features = ["full"] }
proc-macro2 = "1.0"
[dev-dependencies] [dev-dependencies]
wiggle = { path = ".." } wiggle = { path = ".." }

View File

@@ -83,11 +83,12 @@ use syn::parse_macro_input;
/// ///
/// /// The above witx text contains one module called `$example`. So, we must /// /// The above witx text contains one module called `$example`. So, we must
/// /// implement this one method trait for our ctx type. /// /// implement this one method trait for our ctx type.
/// #[wiggle::async_trait(?Send)] /// #[wiggle::async_trait]
/// /// We specified in the `async_` field that `example::double_int_return_float` /// /// We specified in the `async_` field that `example::double_int_return_float`
/// /// is an asynchronous method. Therefore, we use the `async_trait` proc macro /// /// is an asynchronous method. Therefore, we use the `async_trait` proc macro
/// /// (re-exported by wiggle from the crate of the same name) to define this /// /// to define this trait, so that `double_int_return_float` can be an `async fn`.
/// /// trait, so that `double_int_return_float` can be an `async fn`. /// /// `wiggle::async_trait` is defined as `#[async_trait::async_trait(?Send)]` -
/// /// in wiggle, async methods do not have the Send constaint.
/// impl example::Example for YourCtxType { /// impl example::Example for YourCtxType {
/// /// The arrays module has two methods, shown here. /// /// The arrays module has two methods, shown here.
/// /// Note that the `GuestPtr` type comes from `wiggle`, /// /// Note that the `GuestPtr` type comes from `wiggle`,
@@ -156,3 +157,13 @@ pub fn from_witx(args: TokenStream) -> TokenStream {
TokenStream::from(quote! { #code #metadata }) TokenStream::from(quote! { #code #metadata })
} }
#[proc_macro_attribute]
pub fn async_trait(attr: TokenStream, item: TokenStream) -> TokenStream {
let _ = parse_macro_input!(attr as syn::parse::Nothing);
let item = proc_macro2::TokenStream::from(item);
TokenStream::from(quote! {
#[wiggle::async_trait_crate::async_trait(?Send)]
#item
})
}

View File

@@ -6,9 +6,8 @@ use std::slice;
use std::str; use std::str;
use std::sync::Arc; use std::sync::Arc;
pub use wiggle_macro::from_witx; pub use wiggle_macro::{async_trait, from_witx};
// re-exports so users of wiggle don't need to track the dependency:
pub use async_trait::async_trait;
pub use bitflags; pub use bitflags;
#[cfg(feature = "wiggle_metadata")] #[cfg(feature = "wiggle_metadata")]
@@ -24,6 +23,10 @@ pub use error::GuestError;
pub use guest_type::{GuestErrorType, GuestType, GuestTypeTransparent}; pub use guest_type::{GuestErrorType, GuestType, GuestTypeTransparent};
pub use region::Region; pub use region::Region;
pub mod async_trait_crate {
pub use async_trait::*;
}
/// A trait which abstracts how to get at the region of host memory taht /// A trait which abstracts how to get at the region of host memory taht
/// contains guest memory. /// contains guest memory.
/// ///

View File

@@ -14,7 +14,7 @@ wiggle::from_witx!({
impl_errno!(types::Errno); impl_errno!(types::Errno);
#[wiggle::async_trait(?Send)] #[wiggle::async_trait]
impl<'a> atoms::Atoms for WasiCtx<'a> { impl<'a> atoms::Atoms for WasiCtx<'a> {
async fn int_float_args(&self, an_int: u32, an_float: f32) -> Result<(), types::Errno> { async fn int_float_args(&self, an_int: u32, an_float: f32) -> Result<(), types::Errno> {
println!("INT FLOAT ARGS: {} {}", an_int, an_float); println!("INT FLOAT ARGS: {} {}", an_int, an_float);

View File

@@ -28,7 +28,7 @@ impl wiggle::GuestErrorType for types::Errno {
} }
} }
#[wasmtime_wiggle::async_trait(?Send)] #[wasmtime_wiggle::async_trait]
impl atoms::Atoms for Ctx { impl atoms::Atoms for Ctx {
fn int_float_args(&self, an_int: u32, an_float: f32) -> Result<(), types::Errno> { fn int_float_args(&self, an_int: u32, an_float: f32) -> Result<(), types::Errno> {
println!("INT FLOAT ARGS: {} {}", an_int, an_float); println!("INT FLOAT ARGS: {} {}", an_int, an_float);