Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ humantime = { workspace = true }
tempfile = { workspace = true, optional = true }

async-trait = { workspace = true }
trait-variant = { workspace = true }
bytes = { workspace = true }
cfg-if = { workspace = true }
tokio = { workspace = true, optional = true, features = [ "signal", "macros" ] }
Expand All @@ -96,6 +97,7 @@ criterion = { workspace = true }
num_cpus = "1.13.0"
memchr = "2.4"
async-trait = { workspace = true }
trait-variant = { workspace = true }
wat = { workspace = true }
rayon = "1.5.0"
wasmtime-wast = { workspace = true, features = ['component-model'] }
Expand Down Expand Up @@ -313,6 +315,7 @@ tracing = "0.1.26"
bitflags = "2.0"
thiserror = "1.0.43"
async-trait = "0.1.71"
trait-variant = "0.1.2"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per this section of the contributing docs, the bar for adding new dependencies to Wasmtime is a bit higher than for other projects (hence the cargo vet CI failure), so we need to make sure this one is worth including.

From what I can tell, it's only being used to annotate wasmtime-wit-bindgen-generated traits. Given that we're generating those traits anyway, I wonder if we should modify the generator to emit a -> impl Future<Output = T> + Send return type directly rather than rely on trait-variant to transform the code it just generated. That would avoid the extra dependency and remove a layer of abstraction. What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would strongly recommend adding trait-variant because:

  1. it is maintained officially by the Rust project, so the code quality is reassured.
  2. this is the recommended way to add Send bound by the Rust announcement about async fn in traits
  3. in the announcement, it's said future version of trait-variant will support dyn trait object. Using trait-variant will enable dyn object support in the future without the need to modify bindgen
  4. compared to writing -> impl Future<Output = T> + Send ourselves, it's better to leave async fn signatures for users to debug when users have IDEs like RustRover that can expand proc_macro layer by layer. So when they want to debug, they actually see #[trait_variant::make] and async fn instead of fn ... -> impl Future<Output = T> + Send
  5. from my perspective, the use of #[trait_variant::make] is sort of a marker indicating better support of async fn in traits is needed, which may benefit contributions.

In general, I think trait-variant is a better solution than async-trait when we take the future evolution of Rust into account.

PS: replacing async-trait with trait-variant is a breaking change because:

  • traits by async-trait support dyn trait object while traits with trait-variant for now do not
  • when implementing async traits, users need to remove #[async_trait::async_trait] on the impl blocks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining! I hadn't realized trait-variant was a rust-lang project and that it has official status. I'll add a commit to tell cargo vet to trust that crate.

And yes, I realize the removal of async-trait will be a breaking change in any case.

heck = "0.5"
similar = "2.1.0"
toml = "0.8.10"
Expand Down
3 changes: 1 addition & 2 deletions crates/component-macro/tests/expanded/char_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub mod foo {
pub mod chars {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
/// A function that accepts a character
async fn take_char(&mut self, x: char) -> ();
Expand Down Expand Up @@ -255,7 +255,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
/// A function that accepts a character
async fn take_char(&mut self, x: char) -> () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub mod foo {
pub mod chars {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
/// A function that accepts a character
async fn take_char(&mut self, x: char) -> ();
Expand Down Expand Up @@ -284,7 +284,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
/// A function that accepts a character
async fn take_char(&mut self, x: char) -> () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub mod foo {
>::ALIGN32
);
};
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn kebab_case(&mut self) -> ();
async fn foo(&mut self, x: LudicrousSpeed) -> ();
Expand Down Expand Up @@ -403,7 +403,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn kebab_case(&mut self) -> () {
Host::kebab_case(*self).await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub mod foo {
>::ALIGN32
);
};
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn kebab_case(&mut self) -> ();
async fn foo(&mut self, x: LudicrousSpeed) -> ();
Expand Down Expand Up @@ -563,7 +563,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn kebab_case(&mut self) -> () {
Host::kebab_case(*self).await
Expand Down
6 changes: 2 additions & 4 deletions crates/component-macro/tests/expanded/dead-code_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub mod a {
4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
);
};
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn f(&mut self) -> LiveType;
}
Expand Down Expand Up @@ -248,7 +248,6 @@ pub mod a {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn f(&mut self) -> LiveType {
Host::f(*self).await
Expand All @@ -259,7 +258,7 @@ pub mod a {
pub mod interface_with_dead_type {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {}
pub trait GetHost<
T,
Expand Down Expand Up @@ -293,7 +292,6 @@ pub mod a {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub mod a {
4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32
);
};
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn f(&mut self) -> LiveType;
}
Expand Down Expand Up @@ -261,7 +261,6 @@ pub mod a {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn f(&mut self) -> LiveType {
Host::f(*self).await
Expand All @@ -272,7 +271,7 @@ pub mod a {
pub mod interface_with_dead_type {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {}
pub trait GetHost<
T,
Expand Down Expand Up @@ -306,7 +305,6 @@ pub mod a {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct FooIndices {}
/// [`Component`]: wasmtime::component::Component
/// [`Linker`]: wasmtime::component::Linker
pub struct Foo {}
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait FooImports: Send {
async fn foo(&mut self) -> ();
}
Expand All @@ -111,7 +111,6 @@ where
{
type Host = O;
}
#[wasmtime::component::__internal::async_trait]
impl<_T: FooImports + ?Sized + Send> FooImports for &mut _T {
async fn foo(&mut self) -> () {
FooImports::foo(*self).await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct FooIndices {}
/// [`Component`]: wasmtime::component::Component
/// [`Linker`]: wasmtime::component::Linker
pub struct Foo {}
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait FooImports: Send {
async fn foo(&mut self) -> ();
}
Expand All @@ -111,7 +111,6 @@ where
{
type Host = O;
}
#[wasmtime::component::__internal::async_trait]
impl<_T: FooImports + ?Sized + Send> FooImports for &mut _T {
async fn foo(&mut self) -> () {
FooImports::foo(*self).await
Expand Down
3 changes: 1 addition & 2 deletions crates/component-macro/tests/expanded/flags_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ pub mod foo {
assert!(8 == < Flag64 as wasmtime::component::ComponentType >::SIZE32);
assert!(4 == < Flag64 as wasmtime::component::ComponentType >::ALIGN32);
};
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn roundtrip_flag1(&mut self, x: Flag1) -> Flag1;
async fn roundtrip_flag2(&mut self, x: Flag2) -> Flag2;
Expand Down Expand Up @@ -440,7 +440,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn roundtrip_flag1(&mut self, x: Flag1) -> Flag1 {
Host::roundtrip_flag1(*self, x).await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ pub mod foo {
assert!(8 == < Flag64 as wasmtime::component::ComponentType >::SIZE32);
assert!(4 == < Flag64 as wasmtime::component::ComponentType >::ALIGN32);
};
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn roundtrip_flag1(&mut self, x: Flag1) -> Flag1;
async fn roundtrip_flag2(&mut self, x: Flag2) -> Flag2;
Expand Down Expand Up @@ -552,7 +552,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn roundtrip_flag1(&mut self, x: Flag1) -> Flag1 {
Host::roundtrip_flag1(*self, x).await
Expand Down
3 changes: 1 addition & 2 deletions crates/component-macro/tests/expanded/floats_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub mod foo {
pub mod floats {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn f32_param(&mut self, x: f32) -> ();
async fn f64_param(&mut self, x: f64) -> ();
Expand Down Expand Up @@ -272,7 +272,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn f32_param(&mut self, x: f32) -> () {
Host::f32_param(*self, x).await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub mod foo {
pub mod floats {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn f32_param(&mut self, x: f32) -> ();
async fn f64_param(&mut self, x: f64) -> ();
Expand Down Expand Up @@ -330,7 +330,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn f32_param(&mut self, x: f32) -> () {
Host::f32_param(*self, x).await
Expand Down
3 changes: 1 addition & 2 deletions crates/component-macro/tests/expanded/host-world_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct Host_Indices {}
/// [`Component`]: wasmtime::component::Component
/// [`Linker`]: wasmtime::component::Linker
pub struct Host_ {}
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host_Imports: Send {
async fn foo(&mut self) -> ();
}
Expand All @@ -111,7 +111,6 @@ where
{
type Host = O;
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host_Imports + ?Sized + Send> Host_Imports for &mut _T {
async fn foo(&mut self) -> () {
Host_Imports::foo(*self).await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct Host_Indices {}
/// [`Component`]: wasmtime::component::Component
/// [`Linker`]: wasmtime::component::Linker
pub struct Host_ {}
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host_Imports: Send {
async fn foo(&mut self) -> ();
}
Expand All @@ -111,7 +111,6 @@ where
{
type Host = O;
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host_Imports + ?Sized + Send> Host_Imports for &mut _T {
async fn foo(&mut self) -> () {
Host_Imports::foo(*self).await
Expand Down
3 changes: 1 addition & 2 deletions crates/component-macro/tests/expanded/integers_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub mod foo {
pub mod integers {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn a1(&mut self, x: u8) -> ();
async fn a2(&mut self, x: i8) -> ();
Expand Down Expand Up @@ -459,7 +459,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn a1(&mut self, x: u8) -> () {
Host::a1(*self, x).await
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub mod foo {
pub mod integers {
#[allow(unused_imports)]
use wasmtime::component::__internal::anyhow;
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn a1(&mut self, x: u8) -> ();
async fn a2(&mut self, x: i8) -> ();
Expand Down Expand Up @@ -724,7 +724,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn a1(&mut self, x: u8) -> () {
Host::a1(*self, x).await
Expand Down
3 changes: 1 addition & 2 deletions crates/component-macro/tests/expanded/lists_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ pub mod foo {
>::ALIGN32
);
};
#[wasmtime::component::__internal::async_trait]
#[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)]
pub trait Host: Send {
async fn list_u8_param(
&mut self,
Expand Down Expand Up @@ -887,7 +887,6 @@ pub mod foo {
{
add_to_linker_get_host(linker, get)
}
#[wasmtime::component::__internal::async_trait]
impl<_T: Host + ?Sized + Send> Host for &mut _T {
async fn list_u8_param(
&mut self,
Expand Down
Loading