Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Remove usage of `async_trait` in `wiggle` bindings (#11839)

Dustin Martinb2498a29bdaa
diff --git a/Cargo.lock b/Cargo.lock
index 5d673f458b..f99efa2e36 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4191,6 +4191,7 @@ name = "wasi-common"
version = "39.0.0"
dependencies = [
"anyhow",
+ "async-trait",
"bitflags 2.9.4",
"cap-fs-ext",
"cap-rand",
@@ -5278,7 +5279,6 @@ name = "wiggle"
version = "39.0.0"
dependencies = [
"anyhow",
- "async-trait",
"bitflags 2.9.4",
"proptest",
"thiserror 2.0.17",
diff --git a/crates/wasi-common/Cargo.toml b/crates/wasi-common/Cargo.toml
index dd08548447..c0cfd17967 100644
--- a/crates/wasi-common/Cargo.toml
+++ b/crates/wasi-common/Cargo.toml
@@ -24,6 +24,7 @@ cap-std = { workspace = true }
cap-rand = { workspace = true }
bitflags = { workspace = true }
log = { workspace = true }
+async-trait = { workspace = true }
# Optional, enabled by wasmtime feature:
wasmtime = { workspace = true, optional = true, features = ['runtime'] }
diff --git a/crates/wasi-common/src/dir.rs b/crates/wasi-common/src/dir.rs
index e861d5316f..058e0d3de8 100644
--- a/crates/wasi-common/src/dir.rs
+++ b/crates/wasi-common/src/dir.rs
@@ -9,7 +9,7 @@ pub enum OpenResult {
Dir(Box<dyn WasiDir>),
}
-#[wiggle::async_trait]
+#[async_trait::async_trait]
pub trait WasiDir: Send + Sync {
fn as_any(&self) -> &dyn Any;
diff --git a/crates/wasi-common/src/file.rs b/crates/wasi-common/src/file.rs
index f9ffff8e7a..514892e591 100644
--- a/crates/wasi-common/src/file.rs
+++ b/crates/wasi-common/src/file.rs
@@ -3,7 +3,7 @@ use bitflags::bitflags;
use std::any::Any;
use std::sync::Arc;
-#[wiggle::async_trait]
+#[async_trait::async_trait]
pub trait WasiFile: Send + Sync {
fn as_any(&self) -> &dyn Any;
async fn get_filetype(&self) -> Result<FileType, Error>;
diff --git a/crates/wasi-common/src/pipe.rs b/crates/wasi-common/src/pipe.rs
index 9e70e20bd3..a7e00242f9 100644
--- a/crates/wasi-common/src/pipe.rs
+++ b/crates/wasi-common/src/pipe.rs
@@ -98,7 +98,7 @@ impl From<&str> for ReadPipe<io::Cursor<String>> {
}
}
-#[wiggle::async_trait]
+#[async_trait::async_trait]
impl<R: Read + Any + Send + Sync> WasiFile for ReadPipe<R> {
fn as_any(&self) -> &dyn Any {
self
@@ -183,7 +183,7 @@ impl WritePipe<io::Cursor<Vec<u8>>> {
}
}
-#[wiggle::async_trait]
+#[async_trait::async_trait]
impl<W: Write + Any + Send + Sync> WasiFile for WritePipe<W> {
fn as_any(&self) -> &dyn Any {
self
diff --git a/crates/wasi-common/src/sched.rs b/crates/wasi-common/src/sched.rs
index 02609e7284..52a28e8887 100644
--- a/crates/wasi-common/src/sched.rs
+++ b/crates/wasi-common/src/sched.rs
@@ -9,7 +9,7 @@ pub use subscription::{
MonotonicClockSubscription, RwEventFlags, RwSubscription, Subscription, SubscriptionResult,
};
-#[wiggle::async_trait]
+#[async_trait::async_trait]
pub trait WasiSched: Send + Sync {
async fn poll_oneoff<'a>(&self, poll: &mut Poll<'a>) -> Result<(), Error>;
async fn sched_yield(&self) -> Result<(), Error>;
diff --git a/crates/wasi-common/src/snapshots/preview_0.rs b/crates/wasi-common/src/snapshots/preview_0.rs
index 0e8afe9744..0eda288325 100644
--- a/crates/wasi-common/src/snapshots/preview_0.rs
+++ b/crates/wasi-common/src/snapshots/preview_0.rs
@@ -383,7 +383,6 @@ convert_flags_bidirectional!(
// This implementation, wherever possible, delegates directly to the Snapshot1 implementation,
// performing the no-op type conversions along the way.
-#[wiggle::async_trait]
impl wasi_unstable::WasiUnstable for WasiCtx {
async fn args_get(
&mut self,
diff --git a/crates/wasi-common/src/snapshots/preview_1.rs b/crates/wasi-common/src/snapshots/preview_1.rs
index b0ed833738..2c57fd3068 100644
--- a/crates/wasi-common/src/snapshots/preview_1.rs
+++ b/crates/wasi-common/src/snapshots/preview_1.rs
@@ -41,7 +41,6 @@ impl wiggle::GuestErrorType for types::Errno {
}
}
-#[wiggle::async_trait]
impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
async fn args_get(
&mut self,
diff --git a/crates/wasi-common/src/sync/dir.rs b/crates/wasi-common/src/sync/dir.rs
index 35d19c0dbd..cba660e7e4 100644
--- a/crates/wasi-common/src/sync/dir.rs
+++ b/crates/wasi-common/src/sync/dir.rs
@@ -120,7 +120,7 @@ impl Dir {
}
}
-#[wiggle::async_trait]
+#[async_trait::async_trait]
impl WasiDir for Dir {
fn as_any(&self) -> &dyn Any {
self
diff --git a/crates/wasi-common/src/sync/file.rs b/crates/wasi-common/src/sync/file.rs
index d95988654c..dcb024c601 100644
--- a/crates/wasi-common/src/sync/file.rs
+++ b/crates/wasi-common/src/sync/file.rs
@@ -20,7 +20,7 @@ impl File {
}
}
-#[wiggle::async_trait]
+#[async_trait::async_trait]
impl WasiFile for File {
fn as_any(&self) -> &dyn Any {
self
diff --git a/crates/wasi-common/src/sync/net.rs b/crates/wasi-common/src/sync/net.rs
index 063d5b93ae..55602f6f76 100644
--- a/crates/wasi-common/src/sync/net.rs
+++ b/crates/wasi-common/src/sync/net.rs
@@ -76,7 +76,7 @@ impl From<Socket> for Box<dyn WasiFile> {
macro_rules! wasi_listen_write_impl {
($ty:ty, $stream:ty) => {
- #[wiggle::async_trait]
+ #[async_trait::async_trait]
impl WasiFile for $ty {
fn as_any(&self) -> &dyn Any {
self
@@ -169,7 +169,7 @@ wasi_listen_write_impl!(UnixListener, UnixStream);
macro_rules! wasi_stream_write_impl {
($ty:ty, $std_ty:ty) => {
- #[wiggle::async_trait]
+ #[async_trait::async_trait]
impl WasiFile for $ty {
fn as_any(&self) -> &dyn Any {
self
diff --git a/crates/wasi-common/src/sync/sched.rs b/crates/wasi-common/src/sync/sched.rs
index 16633c68ec..1b2fa9faa1 100644
--- a/crates/wasi-common/src/sync/sched.rs
+++ b/crates/wasi-common/src/sync/sched.rs
@@ -21,7 +21,7 @@ impl SyncSched {
Self {}
}
}
-#[wiggle::async_trait]
+#[async_trait::async_trait]
impl WasiSched for SyncSched {
async fn poll_oneoff<'a>(&self, poll: &mut Poll<'a>) -> Result<(), Error> {
poll_oneoff(poll).await
diff --git a/crates/wasi-common/src/sync/stdio.rs b/crates/wasi-common/src/sync/stdio.rs
index e783a74a10..2975a087c9 100644
--- a/crates/wasi-common/src/sync/stdio.rs
+++ b/crates/wasi-common/src/sync/stdio.rs
@@ -21,7 +21,7 @@ pub fn stdin() -> Stdin {
Stdin(std::io::stdin())
}
-#[wiggle::async_trait]
+#[async_trait::async_trait]
impl WasiFile for Stdin {
fn as_any(&self) -> &dyn Any {
self
@@ -102,7 +102,7 @@ impl AsFd for Stdin {
macro_rules! wasi_file_write_impl {
($ty:ty, $ident:ident) => {
- #[wiggle::async_trait]
+ #[async_trait::async_trait]
impl WasiFile for $ty {
fn as_any(&self) -> &dyn Any {
self
diff --git a/crates/wasi-common/src/tokio/dir.rs b/crates/wasi-common/src/tokio/dir.rs
index d71fc0e493..74b18e335e 100644
--- a/crates/wasi-common/src/tokio/dir.rs
+++ b/crates/wasi-common/src/tokio/dir.rs
@@ -15,7 +15,7 @@ impl Dir {
}
}
-#[wiggle::async_trait]
+#[async_trait::async_trait]
impl WasiDir for Dir {
fn as_any(&self) -> &dyn Any {
self
diff --git a/crates/wasi-common/src/tokio/file.rs b/crates/wasi-common/src/tokio/file.rs
index c7e6183d6b..fc13ed9b23 100644
--- a/crates/wasi-common/src/tokio/file.rs
+++ b/crates/wasi-common/src/tokio/file.rs
@@ -90,7 +90,7 @@ pub fn stderr() -> Stderr {
macro_rules! wasi_file_impl {
($ty:ty) => {
- #[wiggle::async_trait]
+ #[async_trait::async_trait]
impl WasiFile for $ty {
fn as_any(&self) -> &dyn Any {
self
diff --git a/crates/wasi-common/src/tokio/sched.rs b/crates/wasi-common/src/tokio/sched.rs
index ed107c223d..41a77b4d9e 100644
--- a/crates/wasi-common/src/tokio/sched.rs
+++ b/crates/wasi-common/src/tokio/sched.rs
@@ -16,7 +16,7 @@ use crate::{
pub fn sched_ctx() -> Box<dyn crate::WasiSched> {
struct AsyncSched;
- #[wiggle::async_trait]
+ #[async_trait::async_trait]
impl WasiSched for AsyncSched {
async fn poll_oneoff<'a>(&self, poll: &mut Poll<'a>) -> Result<(), Error> {
poll_oneoff(poll).await
diff --git a/crates/wasi/src/p0.rs b/crates/wasi/src/p0.rs
index cbff399a23..48c82a48cd 100644
--- a/crates/wasi/src/p0.rs
+++ b/crates/wasi/src/p0.rs
@@ -69,7 +69,6 @@ impl wiggle::GuestErrorType for types::Errno {
}
}
-#[wiggle::async_trait]
impl<T: Snapshot1 + Send> wasi_unstable::WasiUnstable for T {
fn args_get(
&mut self,
diff --git a/crates/wasi/src/p1.rs b/crates/wasi/src/p1.rs
index 73bc079b2c..0eea9cd897 100644
--- a/crates/wasi/src/p1.rs
+++ b/crates/wasi/src/p1.rs
@@ -1160,7 +1160,6 @@ fn first_non_empty_iovec(
Ok(GuestPtr::new((0, 0)))
}
-#[async_trait::async_trait]
// Implement the WasiSnapshotPreview1 trait using only the traits that are
// required for T, i.e., in terms of the preview 2 wit interface, and state
// stored in the WasiP1Adapter struct.
diff --git a/crates/wiggle/Cargo.toml b/crates/wiggle/Cargo.toml
index ae526252fd..ccaa918e8f 100644
--- a/crates/wiggle/Cargo.toml
+++ b/crates/wiggle/Cargo.toml
@@ -20,7 +20,6 @@ witx = { version = "0.9.1", optional = true }
wiggle-macro = { workspace = true }
tracing = { workspace = true }
bitflags = { workspace = true }
-async-trait = { workspace = true }
wasmtime = { workspace = true, optional = true }
anyhow = { workspace = true }
diff --git a/crates/wiggle/generate/src/module_trait.rs b/crates/wiggle/generate/src/module_trait.rs
index fb57919c4b..0737097308 100644
--- a/crates/wiggle/generate/src/module_trait.rs
+++ b/crates/wiggle/generate/src/module_trait.rs
@@ -28,7 +28,7 @@ pub fn define_module_trait(m: &Module, settings: &CodegenSettings) -> TokenStrea
quote!(#arg_name: #arg_type)
});
- let result = match f.results.len() {
+ let mut result = match f.results.len() {
0 if f.noreturn => quote!(wiggle::anyhow::Error),
0 => quote!(()),
1 => {
@@ -60,11 +60,9 @@ pub fn define_module_trait(m: &Module, settings: &CodegenSettings) -> TokenStrea
_ => unimplemented!(),
};
- let asyncness = if settings.get_async(&m, &f).is_sync() {
- quote!()
- } else {
- quote!(async)
- };
+ if !settings.get_async(&m, &f).is_sync() {
+ result = quote!(impl std::future::Future<Output = #result> + Send);
+ }
let self_ = if settings.mutable {
quote!(&mut self)
@@ -72,7 +70,7 @@ pub fn define_module_trait(m: &Module, settings: &CodegenSettings) -> TokenStrea
quote!(&self)
};
quote!(
- #asyncness fn #funcname(
+ fn #funcname(
#self_,
mem: &mut wiggle::GuestMemory<'_>,
#(#args),*
@@ -81,7 +79,6 @@ pub fn define_module_trait(m: &Module, settings: &CodegenSettings) -> TokenStrea
});
quote! {
- #[wiggle::async_trait]
pub trait #traitname {
#(#traitmethods)*
}
diff --git a/crates/wiggle/macro/src/lib.rs b/crates/wiggle/macro/src/lib.rs
index aa3732a5b4..8301293576 100644
--- a/crates/wiggle/macro/src/lib.rs
+++ b/crates/wiggle/macro/src/lib.rs
@@ -89,12 +89,6 @@ use syn::parse_macro_input;
///
/// /// The above witx text contains one module called `$example`. So, we must
/// /// implement this one method trait for our ctx type.
-/// #[wiggle::async_trait]
-/// /// We specified in the `async_` field that `example::double_int_return_float`
-/// /// is an asynchronous method. Therefore, we use the `async_trait` proc macro
-/// /// to define this 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 constraint.
/// impl example::Example for YourCtxType {
/// /// The arrays module has two methods, shown here.
/// /// Note that the `GuestPtr` type comes from `wiggle`,
@@ -192,16 +186,6 @@ pub fn from_witx(args: TokenStream) -> TokenStream {
TokenStream::from(ret)
}
-#[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]
- #item
- })
-}
-
/// Define the structs required to integrate a Wiggle implementation with Wasmtime.
///
/// ## Arguments
diff --git a/crates/wiggle/src/lib.rs b/crates/wiggle/src/lib.rs
index 42160cc630..ced4953979 100644
--- a/crates/wiggle/src/lib.rs
+++ b/crates/wiggle/src/lib.rs
@@ -6,7 +6,7 @@ use std::mem;
use std::ops::Range;
use std::str;
-pub use wiggle_macro::{async_trait, from_witx};
+pub use wiggle_macro::from_witx;
pub use anyhow;
pub use wiggle_macro::wasmtime_integration;
@@ -26,10 +26,6 @@ pub use error::GuestError;
pub use guest_type::{GuestErrorType, GuestType, GuestTypeTransparent};
pub use region::Region;
-pub mod async_trait_crate {
- pub use async_trait::*;
-}
-
#[cfg(feature = "wasmtime")]
pub mod wasmtime_crate {
pub use wasmtime::*;
diff --git a/crates/wiggle/tests/atoms_async.rs b/crates/wiggle/tests/atoms_async.rs
index f9c7939d2a..50132965c3 100644
--- a/crates/wiggle/tests/atoms_async.rs
+++ b/crates/wiggle/tests/atoms_async.rs
@@ -12,7 +12,6 @@ wiggle::from_witx!({
impl_errno!(types::Errno);
-#[wiggle::async_trait]
impl<'a> atoms::Atoms for WasiCtx<'a> {
async fn int_float_args(
&mut self,
diff --git a/crates/wiggle/tests/wasmtime_async.rs b/crates/wiggle/tests/wasmtime_async.rs
index 07807bc8bd..93badb82fa 100644
--- a/crates/wiggle/tests/wasmtime_async.rs
+++ b/crates/wiggle/tests/wasmtime_async.rs
@@ -15,7 +15,6 @@ impl wiggle::GuestErrorType for types::Errno {
}
}
-#[wiggle::async_trait]
impl atoms::Atoms for Ctx {
fn int_float_args(
&mut self,
diff --git a/crates/wiggle/tests/wasmtime_integration.rs b/crates/wiggle/tests/wasmtime_integration.rs
index ec99b05349..6b12d502e0 100644
--- a/crates/wiggle/tests/wasmtime_integration.rs
+++ b/crates/wiggle/tests/wasmtime_integration.rs
@@ -27,7 +27,6 @@ impl wiggle::GuestErrorType for types::Errno {
}
}
-#[wiggle::async_trait]
impl atoms::Atoms for Ctx {
fn int_float_args(
&mut self,
diff --git a/crates/wiggle/tests/wasmtime_sync.rs b/crates/wiggle/tests/wasmtime_sync.rs
index efdac5f49e..7522b5eb0c 100644
--- a/crates/wiggle/tests/wasmtime_sync.rs
+++ b/crates/wiggle/tests/wasmtime_sync.rs
@@ -17,7 +17,6 @@ impl wiggle::GuestErrorType for types::Errno {
const TRIGGER_PENDING: u32 = 0;
-#[wiggle::async_trait]
impl atoms::Atoms for Ctx {
fn int_float_args(
&mut self,