diff --git a/crates/component-macro/tests/expanded/resources-export_concurrent.rs b/crates/component-macro/tests/expanded/resources-export_concurrent.rs index d15ca49a6592..4f1121af72d0 100644 --- a/crates/component-macro/tests/expanded/resources-export_concurrent.rs +++ b/crates/component-macro/tests/expanded/resources-export_concurrent.rs @@ -237,25 +237,16 @@ pub mod foo { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; pub enum Y {} - pub trait HostYWithStore: wasmtime::component::HasData {} - impl<_T: ?Sized> HostYWithStore for _T - where - _T: wasmtime::component::HasData, - {} - pub trait HostY { - fn drop( - &mut self, + pub trait HostYWithStore: wasmtime::component::HasData { + fn drop( + accessor: &wasmtime::component::Accessor, rep: wasmtime::component::Resource, - ) -> impl ::core::future::Future> + Send; - } - impl<_T: HostY + ?Sized + Send> HostY for &mut _T { - async fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostY::drop(*self, rep).await - } + ) -> impl ::core::future::Future> + Send + where + Self: Sized; } + pub trait HostY {} + impl<_T: HostY + ?Sized + Send> HostY for &mut _T {} pub trait HostWithStore: wasmtime::component::HasData + HostYWithStore + Send {} impl<_T: ?Sized> HostWithStore for _T where @@ -273,13 +264,14 @@ pub mod foo { T: 'static + Send, { let mut inst = linker.instance("foo:foo/transitive-import")?; - inst.resource_async( + inst.resource_concurrent( "y", wasmtime::component::ResourceType::host::(), - move |mut store, rep| { - wasmtime::component::__internal::Box::new(async move { - HostY::drop( - &mut host_getter(store.data_mut()), + move |caller: &wasmtime::component::Accessor, rep| { + wasmtime::component::__internal::Box::pin(async move { + let accessor = &caller.with_data(host_getter); + HostYWithStore::drop( + accessor, wasmtime::component::Resource::new_own(rep), ) .await diff --git a/crates/component-macro/tests/expanded/resources-import_concurrent.rs b/crates/component-macro/tests/expanded/resources-import_concurrent.rs index c88d75287b20..9567377ad244 100644 --- a/crates/component-macro/tests/expanded/resources-import_concurrent.rs +++ b/crates/component-macro/tests/expanded/resources-import_concurrent.rs @@ -1,5 +1,11 @@ pub enum WorldResource {} pub trait HostWorldResourceWithStore: wasmtime::component::HasData + Send { + fn drop( + accessor: &wasmtime::component::Accessor, + rep: wasmtime::component::Resource, + ) -> impl ::core::future::Future> + Send + where + Self: Sized; fn new( accessor: &wasmtime::component::Accessor, ) -> impl ::core::future::Future< @@ -13,20 +19,8 @@ pub trait HostWorldResourceWithStore: wasmtime::component::HasData + Send { accessor: &wasmtime::component::Accessor, ) -> impl ::core::future::Future + Send; } -pub trait HostWorldResource: Send { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> impl ::core::future::Future> + Send; -} -impl<_T: HostWorldResource + ?Sized + Send> HostWorldResource for &mut _T { - async fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostWorldResource::drop(*self, rep).await - } -} +pub trait HostWorldResource: Send {} +impl<_T: HostWorldResource + ?Sized + Send> HostWorldResource for &mut _T {} /// Auto-generated bindings for a pre-instantiated version of a /// component which implements the world `the-world`. /// @@ -260,13 +254,14 @@ const _: () = { { let mut linker = linker.root(); linker - .resource_async( + .resource_concurrent( "world-resource", wasmtime::component::ResourceType::host::(), - move |mut store, rep| { - wasmtime::component::__internal::Box::new(async move { - HostWorldResource::drop( - &mut host_getter(store.data_mut()), + move |caller: &wasmtime::component::Accessor, rep| { + wasmtime::component::__internal::Box::pin(async move { + let accessor = &caller.with_data(host_getter); + HostWorldResourceWithStore::drop( + accessor, wasmtime::component::Resource::new_own(rep), ) .await @@ -398,6 +393,12 @@ pub mod foo { use wasmtime::component::__internal::{anyhow, Box}; pub enum Bar {} pub trait HostBarWithStore: wasmtime::component::HasData + Send { + fn drop( + accessor: &wasmtime::component::Accessor, + rep: wasmtime::component::Resource, + ) -> impl ::core::future::Future> + Send + where + Self: Sized; fn new( accessor: &wasmtime::component::Accessor, ) -> impl ::core::future::Future< @@ -411,20 +412,8 @@ pub mod foo { self_: wasmtime::component::Resource, ) -> impl ::core::future::Future + Send; } - pub trait HostBar: Send { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> impl ::core::future::Future> + Send; - } - impl<_T: HostBar + ?Sized + Send> HostBar for &mut _T { - async fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostBar::drop(*self, rep).await - } - } + pub trait HostBar: Send {} + impl<_T: HostBar + ?Sized + Send> HostBar for &mut _T {} #[derive(wasmtime::component::ComponentType)] #[derive(wasmtime::component::Lift)] #[derive(wasmtime::component::Lower)] @@ -580,13 +569,14 @@ pub mod foo { T: 'static + Send, { let mut inst = linker.instance("foo:foo/resources")?; - inst.resource_async( + inst.resource_concurrent( "bar", wasmtime::component::ResourceType::host::(), - move |mut store, rep| { - wasmtime::component::__internal::Box::new(async move { - HostBar::drop( - &mut host_getter(store.data_mut()), + move |caller: &wasmtime::component::Accessor, rep| { + wasmtime::component::__internal::Box::pin(async move { + let accessor = &caller.with_data(host_getter); + HostBarWithStore::drop( + accessor, wasmtime::component::Resource::new_own(rep), ) .await @@ -904,25 +894,16 @@ pub mod foo { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; pub enum A {} - pub trait HostAWithStore: wasmtime::component::HasData {} - impl<_T: ?Sized> HostAWithStore for _T - where - _T: wasmtime::component::HasData, - {} - pub trait HostA { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> impl ::core::future::Future> + Send; - } - impl<_T: HostA + ?Sized + Send> HostA for &mut _T { - async fn drop( - &mut self, + pub trait HostAWithStore: wasmtime::component::HasData { + fn drop( + accessor: &wasmtime::component::Accessor, rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostA::drop(*self, rep).await - } + ) -> impl ::core::future::Future> + Send + where + Self: Sized; } + pub trait HostA {} + impl<_T: HostA + ?Sized + Send> HostA for &mut _T {} pub trait HostWithStore: wasmtime::component::HasData + HostAWithStore + Send {} impl<_T: ?Sized> HostWithStore for _T where @@ -940,13 +921,14 @@ pub mod foo { T: 'static + Send, { let mut inst = linker.instance("foo:foo/long-use-chain1")?; - inst.resource_async( + inst.resource_concurrent( "a", wasmtime::component::ResourceType::host::(), - move |mut store, rep| { - wasmtime::component::__internal::Box::new(async move { - HostA::drop( - &mut host_getter(store.data_mut()), + move |caller: &wasmtime::component::Accessor, rep| { + wasmtime::component::__internal::Box::pin(async move { + let accessor = &caller.with_data(host_getter); + HostAWithStore::drop( + accessor, wasmtime::component::Resource::new_own(rep), ) .await @@ -1048,25 +1030,16 @@ pub mod foo { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; pub enum Foo {} - pub trait HostFooWithStore: wasmtime::component::HasData {} - impl<_T: ?Sized> HostFooWithStore for _T - where - _T: wasmtime::component::HasData, - {} - pub trait HostFoo { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> impl ::core::future::Future> + Send; - } - impl<_T: HostFoo + ?Sized + Send> HostFoo for &mut _T { - async fn drop( - &mut self, + pub trait HostFooWithStore: wasmtime::component::HasData { + fn drop( + accessor: &wasmtime::component::Accessor, rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostFoo::drop(*self, rep).await - } + ) -> impl ::core::future::Future> + Send + where + Self: Sized; } + pub trait HostFoo {} + impl<_T: HostFoo + ?Sized + Send> HostFoo for &mut _T {} pub trait HostWithStore: wasmtime::component::HasData + HostFooWithStore + Send {} impl<_T: ?Sized> HostWithStore for _T where @@ -1085,13 +1058,14 @@ pub mod foo { { let mut inst = linker .instance("foo:foo/transitive-interface-with-resource")?; - inst.resource_async( + inst.resource_concurrent( "foo", wasmtime::component::ResourceType::host::(), - move |mut store, rep| { - wasmtime::component::__internal::Box::new(async move { - HostFoo::drop( - &mut host_getter(store.data_mut()), + move |caller: &wasmtime::component::Accessor, rep| { + wasmtime::component::__internal::Box::pin(async move { + let accessor = &caller.with_data(host_getter); + HostFooWithStore::drop( + accessor, wasmtime::component::Resource::new_own(rep), ) .await @@ -1143,7 +1117,7 @@ pub mod exports { .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/uses-resource-transitively` does \ - not have export `{name}`" + not have export `{name}`" ) }) }; diff --git a/crates/component-macro/tests/expanded/unstable-features_concurrent.rs b/crates/component-macro/tests/expanded/unstable-features_concurrent.rs index d39ffb9b12b2..a45fe341cd0a 100644 --- a/crates/component-macro/tests/expanded/unstable-features_concurrent.rs +++ b/crates/component-macro/tests/expanded/unstable-features_concurrent.rs @@ -80,25 +80,19 @@ impl core::convert::From<&LinkOptions> for foo::foo::the_interface::LinkOptions } pub enum Baz {} pub trait HostBazWithStore: wasmtime::component::HasData + Send { + fn drop( + accessor: &wasmtime::component::Accessor, + rep: wasmtime::component::Resource, + ) -> impl ::core::future::Future> + Send + where + Self: Sized; fn foo( accessor: &wasmtime::component::Accessor, self_: wasmtime::component::Resource, ) -> impl ::core::future::Future + Send; } -pub trait HostBaz: Send { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> impl ::core::future::Future> + Send; -} -impl<_T: HostBaz + ?Sized + Send> HostBaz for &mut _T { - async fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostBaz::drop(*self, rep).await - } -} +pub trait HostBaz: Send {} +impl<_T: HostBaz + ?Sized + Send> HostBaz for &mut _T {} /// Auto-generated bindings for a pre-instantiated version of a /// component which implements the world `the-world`. /// @@ -284,13 +278,14 @@ const _: () = { if options.experimental_world { if options.experimental_world_resource { linker - .resource_async( + .resource_concurrent( "baz", wasmtime::component::ResourceType::host::(), - move |mut store, rep| { - wasmtime::component::__internal::Box::new(async move { - HostBaz::drop( - &mut host_getter(store.data_mut()), + move |caller: &wasmtime::component::Accessor, rep| { + wasmtime::component::__internal::Box::pin(async move { + let accessor = &caller.with_data(host_getter); + HostBazWithStore::drop( + accessor, wasmtime::component::Resource::new_own(rep), ) .await @@ -401,25 +396,19 @@ pub mod foo { } pub enum Bar {} pub trait HostBarWithStore: wasmtime::component::HasData + Send { + fn drop( + accessor: &wasmtime::component::Accessor, + rep: wasmtime::component::Resource, + ) -> impl ::core::future::Future> + Send + where + Self: Sized; fn foo( accessor: &wasmtime::component::Accessor, self_: wasmtime::component::Resource, ) -> impl ::core::future::Future + Send; } - pub trait HostBar: Send { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> impl ::core::future::Future> + Send; - } - impl<_T: HostBar + ?Sized + Send> HostBar for &mut _T { - async fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostBar::drop(*self, rep).await - } - } + pub trait HostBar: Send {} + impl<_T: HostBar + ?Sized + Send> HostBar for &mut _T {} pub trait HostWithStore: wasmtime::component::HasData + HostBarWithStore + Send { fn foo( accessor: &wasmtime::component::Accessor, @@ -440,13 +429,14 @@ pub mod foo { if options.experimental_interface { let mut inst = linker.instance("foo:foo/the-interface")?; if options.experimental_interface_resource { - inst.resource_async( + inst.resource_concurrent( "bar", wasmtime::component::ResourceType::host::(), - move |mut store, rep| { - wasmtime::component::__internal::Box::new(async move { - HostBar::drop( - &mut host_getter(store.data_mut()), + move |caller: &wasmtime::component::Accessor, rep| { + wasmtime::component::__internal::Box::pin(async move { + let accessor = &caller.with_data(host_getter); + HostBarWithStore::drop( + accessor, wasmtime::component::Resource::new_own(rep), ) .await diff --git a/crates/wit-bindgen/src/config.rs b/crates/wit-bindgen/src/config.rs index 0b2d512c46d3..d4aeaec86727 100644 --- a/crates/wit-bindgen/src/config.rs +++ b/crates/wit-bindgen/src/config.rs @@ -102,14 +102,7 @@ impl FunctionConfig { resource_name: &str, ) -> FunctionFlags { let mut ret = FunctionFlags::empty(); - self.add_function_flags(resolve, ns, &format!("[drop]{resource_name}"), &mut ret); - - // FIXME: this currently isn't supported and fools the rest of the - // bindings generation to thinking a `*WithStore` trait is needed when - // it isn't, so forcibly remove it. This'll need updating after #11325. - ret.remove(FunctionFlags::STORE); - ret } diff --git a/crates/wit-bindgen/src/lib.rs b/crates/wit-bindgen/src/lib.rs index d4811aa9499e..1882ed5da84b 100644 --- a/crates/wit-bindgen/src/lib.rs +++ b/crates/wit-bindgen/src/lib.rs @@ -1528,7 +1528,7 @@ impl Wasmtime { move |caller: &{wt}::component::Accessor::, rep| {{ {wt}::component::__internal::Box::pin(async move {{ let accessor = &caller.with_data(host_getter); - Host{camel}Concurrent::drop(accessor, {wt}::component::Resource::new_own(rep)).await + Host{camel}WithStore::drop(accessor, {wt}::component::Resource::new_own(rep)).await }}) }}, )?;" @@ -2892,6 +2892,7 @@ impl<'a> InterfaceGenerator<'a> { ); ret.with_store_name = Some(format!("{trait_name}WithStore")); + let mut extra_with_store_function = false; for extra in extra_functions { match extra { ExtraTraitMethod::ResourceDrop { name } => { @@ -2907,6 +2908,7 @@ impl<'a> InterfaceGenerator<'a> { self.src, "fn drop(accessor: &{wt}::component::Accessor, rep: {wt}::component::Resource<{camel}>) -> impl ::core::future::Future> + Send where Self: Sized;" ); + extra_with_store_function = true; } ExtraTraitMethod::ErrorConvert { .. } => {} } @@ -2920,7 +2922,7 @@ impl<'a> InterfaceGenerator<'a> { // If `*WithStore` is empty, generate a blanket impl for the trait since // it's otherwise not necessary to implement it manually. - if partition.with_store.is_empty() { + if partition.with_store.is_empty() && !extra_with_store_function { uwriteln!(self.src, "impl<_T: ?Sized> {trait_name}WithStore for _T"); uwriteln!( self.src, @@ -2945,13 +2947,12 @@ impl<'a> InterfaceGenerator<'a> { for extra in extra_functions { match extra { ExtraTraitMethod::ResourceDrop { name } => { - let camel = name.to_upper_camel_case(); - let flags = self.import_resource_drop_flags(name); ret.all_func_flags |= flags; if flags.contains(FunctionFlags::STORE) { continue; } + let camel = name.to_upper_camel_case(); uwrite!( self.src, "fn drop(&mut self, rep: {wt}::component::Resource<{camel}>) -> " @@ -3020,6 +3021,9 @@ fn convert_{snake}(&mut self, err: {root}{custom_name}) -> {wt}::Result<{camel}> match extra { ExtraTraitMethod::ResourceDrop { name } => { let flags = self.import_resource_drop_flags(name); + if flags.contains(FunctionFlags::STORE) { + continue; + } let camel = name.to_upper_camel_case(); let mut await_ = ""; if flags.contains(FunctionFlags::ASYNC) {