Skip to content

Commit d2e1761

Browse files
authored
Account for concurrent resource destructors (#11350)
* Account for concurrent resource destructors This fixes a minor merge conflict between #11325 and #11328 which isn't currently exercised by in-repo WASI bindings but will be soon once wasip3-prototyping is finished merging. * Update expanded test expectations
1 parent 94c7252 commit d2e1761

File tree

5 files changed

+107
-154
lines changed

5 files changed

+107
-154
lines changed

crates/component-macro/tests/expanded/resources-export_concurrent.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -237,25 +237,16 @@ pub mod foo {
237237
#[allow(unused_imports)]
238238
use wasmtime::component::__internal::{anyhow, Box};
239239
pub enum Y {}
240-
pub trait HostYWithStore: wasmtime::component::HasData {}
241-
impl<_T: ?Sized> HostYWithStore for _T
242-
where
243-
_T: wasmtime::component::HasData,
244-
{}
245-
pub trait HostY {
246-
fn drop(
247-
&mut self,
240+
pub trait HostYWithStore: wasmtime::component::HasData {
241+
fn drop<T: 'static>(
242+
accessor: &wasmtime::component::Accessor<T, Self>,
248243
rep: wasmtime::component::Resource<Y>,
249-
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send;
250-
}
251-
impl<_T: HostY + ?Sized + Send> HostY for &mut _T {
252-
async fn drop(
253-
&mut self,
254-
rep: wasmtime::component::Resource<Y>,
255-
) -> wasmtime::Result<()> {
256-
HostY::drop(*self, rep).await
257-
}
244+
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send
245+
where
246+
Self: Sized;
258247
}
248+
pub trait HostY {}
249+
impl<_T: HostY + ?Sized + Send> HostY for &mut _T {}
259250
pub trait HostWithStore: wasmtime::component::HasData + HostYWithStore + Send {}
260251
impl<_T: ?Sized> HostWithStore for _T
261252
where
@@ -273,13 +264,14 @@ pub mod foo {
273264
T: 'static + Send,
274265
{
275266
let mut inst = linker.instance("foo:foo/transitive-import")?;
276-
inst.resource_async(
267+
inst.resource_concurrent(
277268
"y",
278269
wasmtime::component::ResourceType::host::<Y>(),
279-
move |mut store, rep| {
280-
wasmtime::component::__internal::Box::new(async move {
281-
HostY::drop(
282-
&mut host_getter(store.data_mut()),
270+
move |caller: &wasmtime::component::Accessor<T>, rep| {
271+
wasmtime::component::__internal::Box::pin(async move {
272+
let accessor = &caller.with_data(host_getter);
273+
HostYWithStore::drop(
274+
accessor,
283275
wasmtime::component::Resource::new_own(rep),
284276
)
285277
.await

crates/component-macro/tests/expanded/resources-import_concurrent.rs

Lines changed: 57 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
pub enum WorldResource {}
22
pub trait HostWorldResourceWithStore: wasmtime::component::HasData + Send {
3+
fn drop<T: 'static>(
4+
accessor: &wasmtime::component::Accessor<T, Self>,
5+
rep: wasmtime::component::Resource<WorldResource>,
6+
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send
7+
where
8+
Self: Sized;
39
fn new<T: 'static>(
410
accessor: &wasmtime::component::Accessor<T, Self>,
511
) -> impl ::core::future::Future<
@@ -13,20 +19,8 @@ pub trait HostWorldResourceWithStore: wasmtime::component::HasData + Send {
1319
accessor: &wasmtime::component::Accessor<T, Self>,
1420
) -> impl ::core::future::Future<Output = ()> + Send;
1521
}
16-
pub trait HostWorldResource: Send {
17-
fn drop(
18-
&mut self,
19-
rep: wasmtime::component::Resource<WorldResource>,
20-
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send;
21-
}
22-
impl<_T: HostWorldResource + ?Sized + Send> HostWorldResource for &mut _T {
23-
async fn drop(
24-
&mut self,
25-
rep: wasmtime::component::Resource<WorldResource>,
26-
) -> wasmtime::Result<()> {
27-
HostWorldResource::drop(*self, rep).await
28-
}
29-
}
22+
pub trait HostWorldResource: Send {}
23+
impl<_T: HostWorldResource + ?Sized + Send> HostWorldResource for &mut _T {}
3024
/// Auto-generated bindings for a pre-instantiated version of a
3125
/// component which implements the world `the-world`.
3226
///
@@ -260,13 +254,14 @@ const _: () = {
260254
{
261255
let mut linker = linker.root();
262256
linker
263-
.resource_async(
257+
.resource_concurrent(
264258
"world-resource",
265259
wasmtime::component::ResourceType::host::<WorldResource>(),
266-
move |mut store, rep| {
267-
wasmtime::component::__internal::Box::new(async move {
268-
HostWorldResource::drop(
269-
&mut host_getter(store.data_mut()),
260+
move |caller: &wasmtime::component::Accessor<T>, rep| {
261+
wasmtime::component::__internal::Box::pin(async move {
262+
let accessor = &caller.with_data(host_getter);
263+
HostWorldResourceWithStore::drop(
264+
accessor,
270265
wasmtime::component::Resource::new_own(rep),
271266
)
272267
.await
@@ -398,6 +393,12 @@ pub mod foo {
398393
use wasmtime::component::__internal::{anyhow, Box};
399394
pub enum Bar {}
400395
pub trait HostBarWithStore: wasmtime::component::HasData + Send {
396+
fn drop<T: 'static>(
397+
accessor: &wasmtime::component::Accessor<T, Self>,
398+
rep: wasmtime::component::Resource<Bar>,
399+
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send
400+
where
401+
Self: Sized;
401402
fn new<T: 'static>(
402403
accessor: &wasmtime::component::Accessor<T, Self>,
403404
) -> impl ::core::future::Future<
@@ -411,20 +412,8 @@ pub mod foo {
411412
self_: wasmtime::component::Resource<Bar>,
412413
) -> impl ::core::future::Future<Output = u32> + Send;
413414
}
414-
pub trait HostBar: Send {
415-
fn drop(
416-
&mut self,
417-
rep: wasmtime::component::Resource<Bar>,
418-
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send;
419-
}
420-
impl<_T: HostBar + ?Sized + Send> HostBar for &mut _T {
421-
async fn drop(
422-
&mut self,
423-
rep: wasmtime::component::Resource<Bar>,
424-
) -> wasmtime::Result<()> {
425-
HostBar::drop(*self, rep).await
426-
}
427-
}
415+
pub trait HostBar: Send {}
416+
impl<_T: HostBar + ?Sized + Send> HostBar for &mut _T {}
428417
#[derive(wasmtime::component::ComponentType)]
429418
#[derive(wasmtime::component::Lift)]
430419
#[derive(wasmtime::component::Lower)]
@@ -580,13 +569,14 @@ pub mod foo {
580569
T: 'static + Send,
581570
{
582571
let mut inst = linker.instance("foo:foo/resources")?;
583-
inst.resource_async(
572+
inst.resource_concurrent(
584573
"bar",
585574
wasmtime::component::ResourceType::host::<Bar>(),
586-
move |mut store, rep| {
587-
wasmtime::component::__internal::Box::new(async move {
588-
HostBar::drop(
589-
&mut host_getter(store.data_mut()),
575+
move |caller: &wasmtime::component::Accessor<T>, rep| {
576+
wasmtime::component::__internal::Box::pin(async move {
577+
let accessor = &caller.with_data(host_getter);
578+
HostBarWithStore::drop(
579+
accessor,
590580
wasmtime::component::Resource::new_own(rep),
591581
)
592582
.await
@@ -904,25 +894,16 @@ pub mod foo {
904894
#[allow(unused_imports)]
905895
use wasmtime::component::__internal::{anyhow, Box};
906896
pub enum A {}
907-
pub trait HostAWithStore: wasmtime::component::HasData {}
908-
impl<_T: ?Sized> HostAWithStore for _T
909-
where
910-
_T: wasmtime::component::HasData,
911-
{}
912-
pub trait HostA {
913-
fn drop(
914-
&mut self,
915-
rep: wasmtime::component::Resource<A>,
916-
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send;
917-
}
918-
impl<_T: HostA + ?Sized + Send> HostA for &mut _T {
919-
async fn drop(
920-
&mut self,
897+
pub trait HostAWithStore: wasmtime::component::HasData {
898+
fn drop<T: 'static>(
899+
accessor: &wasmtime::component::Accessor<T, Self>,
921900
rep: wasmtime::component::Resource<A>,
922-
) -> wasmtime::Result<()> {
923-
HostA::drop(*self, rep).await
924-
}
901+
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send
902+
where
903+
Self: Sized;
925904
}
905+
pub trait HostA {}
906+
impl<_T: HostA + ?Sized + Send> HostA for &mut _T {}
926907
pub trait HostWithStore: wasmtime::component::HasData + HostAWithStore + Send {}
927908
impl<_T: ?Sized> HostWithStore for _T
928909
where
@@ -940,13 +921,14 @@ pub mod foo {
940921
T: 'static + Send,
941922
{
942923
let mut inst = linker.instance("foo:foo/long-use-chain1")?;
943-
inst.resource_async(
924+
inst.resource_concurrent(
944925
"a",
945926
wasmtime::component::ResourceType::host::<A>(),
946-
move |mut store, rep| {
947-
wasmtime::component::__internal::Box::new(async move {
948-
HostA::drop(
949-
&mut host_getter(store.data_mut()),
927+
move |caller: &wasmtime::component::Accessor<T>, rep| {
928+
wasmtime::component::__internal::Box::pin(async move {
929+
let accessor = &caller.with_data(host_getter);
930+
HostAWithStore::drop(
931+
accessor,
950932
wasmtime::component::Resource::new_own(rep),
951933
)
952934
.await
@@ -1048,25 +1030,16 @@ pub mod foo {
10481030
#[allow(unused_imports)]
10491031
use wasmtime::component::__internal::{anyhow, Box};
10501032
pub enum Foo {}
1051-
pub trait HostFooWithStore: wasmtime::component::HasData {}
1052-
impl<_T: ?Sized> HostFooWithStore for _T
1053-
where
1054-
_T: wasmtime::component::HasData,
1055-
{}
1056-
pub trait HostFoo {
1057-
fn drop(
1058-
&mut self,
1059-
rep: wasmtime::component::Resource<Foo>,
1060-
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send;
1061-
}
1062-
impl<_T: HostFoo + ?Sized + Send> HostFoo for &mut _T {
1063-
async fn drop(
1064-
&mut self,
1033+
pub trait HostFooWithStore: wasmtime::component::HasData {
1034+
fn drop<T: 'static>(
1035+
accessor: &wasmtime::component::Accessor<T, Self>,
10651036
rep: wasmtime::component::Resource<Foo>,
1066-
) -> wasmtime::Result<()> {
1067-
HostFoo::drop(*self, rep).await
1068-
}
1037+
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send
1038+
where
1039+
Self: Sized;
10691040
}
1041+
pub trait HostFoo {}
1042+
impl<_T: HostFoo + ?Sized + Send> HostFoo for &mut _T {}
10701043
pub trait HostWithStore: wasmtime::component::HasData + HostFooWithStore + Send {}
10711044
impl<_T: ?Sized> HostWithStore for _T
10721045
where
@@ -1085,13 +1058,14 @@ pub mod foo {
10851058
{
10861059
let mut inst = linker
10871060
.instance("foo:foo/transitive-interface-with-resource")?;
1088-
inst.resource_async(
1061+
inst.resource_concurrent(
10891062
"foo",
10901063
wasmtime::component::ResourceType::host::<Foo>(),
1091-
move |mut store, rep| {
1092-
wasmtime::component::__internal::Box::new(async move {
1093-
HostFoo::drop(
1094-
&mut host_getter(store.data_mut()),
1064+
move |caller: &wasmtime::component::Accessor<T>, rep| {
1065+
wasmtime::component::__internal::Box::pin(async move {
1066+
let accessor = &caller.with_data(host_getter);
1067+
HostFooWithStore::drop(
1068+
accessor,
10951069
wasmtime::component::Resource::new_own(rep),
10961070
)
10971071
.await
@@ -1143,7 +1117,7 @@ pub mod exports {
11431117
.ok_or_else(|| {
11441118
anyhow::anyhow!(
11451119
"instance export `foo:foo/uses-resource-transitively` does \
1146-
not have export `{name}`"
1120+
not have export `{name}`"
11471121
)
11481122
})
11491123
};

crates/component-macro/tests/expanded/unstable-features_concurrent.rs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,19 @@ impl core::convert::From<&LinkOptions> for foo::foo::the_interface::LinkOptions
8080
}
8181
pub enum Baz {}
8282
pub trait HostBazWithStore: wasmtime::component::HasData + Send {
83+
fn drop<T: 'static>(
84+
accessor: &wasmtime::component::Accessor<T, Self>,
85+
rep: wasmtime::component::Resource<Baz>,
86+
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send
87+
where
88+
Self: Sized;
8389
fn foo<T: 'static>(
8490
accessor: &wasmtime::component::Accessor<T, Self>,
8591
self_: wasmtime::component::Resource<Baz>,
8692
) -> impl ::core::future::Future<Output = ()> + Send;
8793
}
88-
pub trait HostBaz: Send {
89-
fn drop(
90-
&mut self,
91-
rep: wasmtime::component::Resource<Baz>,
92-
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send;
93-
}
94-
impl<_T: HostBaz + ?Sized + Send> HostBaz for &mut _T {
95-
async fn drop(
96-
&mut self,
97-
rep: wasmtime::component::Resource<Baz>,
98-
) -> wasmtime::Result<()> {
99-
HostBaz::drop(*self, rep).await
100-
}
101-
}
94+
pub trait HostBaz: Send {}
95+
impl<_T: HostBaz + ?Sized + Send> HostBaz for &mut _T {}
10296
/// Auto-generated bindings for a pre-instantiated version of a
10397
/// component which implements the world `the-world`.
10498
///
@@ -284,13 +278,14 @@ const _: () = {
284278
if options.experimental_world {
285279
if options.experimental_world_resource {
286280
linker
287-
.resource_async(
281+
.resource_concurrent(
288282
"baz",
289283
wasmtime::component::ResourceType::host::<Baz>(),
290-
move |mut store, rep| {
291-
wasmtime::component::__internal::Box::new(async move {
292-
HostBaz::drop(
293-
&mut host_getter(store.data_mut()),
284+
move |caller: &wasmtime::component::Accessor<T>, rep| {
285+
wasmtime::component::__internal::Box::pin(async move {
286+
let accessor = &caller.with_data(host_getter);
287+
HostBazWithStore::drop(
288+
accessor,
294289
wasmtime::component::Resource::new_own(rep),
295290
)
296291
.await
@@ -401,25 +396,19 @@ pub mod foo {
401396
}
402397
pub enum Bar {}
403398
pub trait HostBarWithStore: wasmtime::component::HasData + Send {
399+
fn drop<T: 'static>(
400+
accessor: &wasmtime::component::Accessor<T, Self>,
401+
rep: wasmtime::component::Resource<Bar>,
402+
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send
403+
where
404+
Self: Sized;
404405
fn foo<T: 'static>(
405406
accessor: &wasmtime::component::Accessor<T, Self>,
406407
self_: wasmtime::component::Resource<Bar>,
407408
) -> impl ::core::future::Future<Output = ()> + Send;
408409
}
409-
pub trait HostBar: Send {
410-
fn drop(
411-
&mut self,
412-
rep: wasmtime::component::Resource<Bar>,
413-
) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send;
414-
}
415-
impl<_T: HostBar + ?Sized + Send> HostBar for &mut _T {
416-
async fn drop(
417-
&mut self,
418-
rep: wasmtime::component::Resource<Bar>,
419-
) -> wasmtime::Result<()> {
420-
HostBar::drop(*self, rep).await
421-
}
422-
}
410+
pub trait HostBar: Send {}
411+
impl<_T: HostBar + ?Sized + Send> HostBar for &mut _T {}
423412
pub trait HostWithStore: wasmtime::component::HasData + HostBarWithStore + Send {
424413
fn foo<T: 'static>(
425414
accessor: &wasmtime::component::Accessor<T, Self>,
@@ -440,13 +429,14 @@ pub mod foo {
440429
if options.experimental_interface {
441430
let mut inst = linker.instance("foo:foo/the-interface")?;
442431
if options.experimental_interface_resource {
443-
inst.resource_async(
432+
inst.resource_concurrent(
444433
"bar",
445434
wasmtime::component::ResourceType::host::<Bar>(),
446-
move |mut store, rep| {
447-
wasmtime::component::__internal::Box::new(async move {
448-
HostBar::drop(
449-
&mut host_getter(store.data_mut()),
435+
move |caller: &wasmtime::component::Accessor<T>, rep| {
436+
wasmtime::component::__internal::Box::pin(async move {
437+
let accessor = &caller.with_data(host_getter);
438+
HostBarWithStore::drop(
439+
accessor,
450440
wasmtime::component::Resource::new_own(rep),
451441
)
452442
.await

0 commit comments

Comments
 (0)