A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix `bindgen!` with labelled imports and exports (#14867)
Randolf J.f031a62ff4cd
diff --git a/crates/component-macro/tests/codegen.rs b/crates/component-macro/tests/codegen.rsindex 878d1383d1..c244b6215c 100644--- a/crates/component-macro/tests/codegen.rs+++ b/crates/component-macro/tests/codegen.rs@@ -415,6 +415,43 @@ mod with_works_with_hierarchy {}}+// A labelled import reuses the remapped module of a plain import of the same+// interface.+mod implements_plain_and_labelled_with {+ mod bindings {+ wasmtime::component::bindgen!({+ inline: "+ package foo:foo;++ interface store {+ get: func() -> u32;+ }++ world w {+ import store;+ }+ ",+ });+ }++ wasmtime::component::bindgen!({+ inline: "+ package foo:foo;++ interface store {+ get: func() -> u32;+ }++ world w {+ import a: store;+ import store;+ import b: store;+ }+ ",+ with: { "foo:foo/store": bindings::foo::foo::store },+ });+}+mod trappable_imports {mod none {wasmtime::component::bindgen!({@@ -1162,6 +1199,95 @@ mod named_imports {},});}++ // An unversioned key applies to every imported version of the interface.+ mod unversioned_key_multiple_versions {+ wasmtime::component::bindgen!({+ inline: "+ package foo:root;++ package foo:[email protected] {+ interface store {+ get: func() -> u32;+ }+ }++ package foo:[email protected] {+ interface store {+ get: func() -> u32;+ put: func(v: u32);+ }+ }++ world the-world {+ import a: foo:foo/[email protected];+ import b: foo:foo/[email protected];+ }+ ",+ named_imports: {+ "foo:foo/store": u32,+ },+ });++ struct MyHost;++ impl named_imports::foo::foo0_1_0::store::Host for MyHost {+ fn get(&mut self, id: u32) -> u32 {+ id+ }+ }++ impl named_imports::foo::foo0_2_0::store::Host for MyHost {+ fn get(&mut self, id: u32) -> u32 {+ id+ }+ fn put(&mut self, _id: u32, _v: u32) {}+ }+ }++ // A versioned key takes precedence over an unversioned one.+ mod versioned_key_precedence {+ wasmtime::component::bindgen!({+ inline: "+ package foo:root;++ package foo:[email protected] {+ interface store {+ get: func() -> u32;+ }+ }++ package foo:[email protected] {+ interface store {+ get: func() -> u32;+ }+ }++ world the-world {+ import foo:foo/[email protected];+ import foo:foo/[email protected];+ }+ ",+ named_imports: {+ "foo:foo/store": u32,+ "foo:foo/[email protected]": String,+ },+ });++ struct MyHost;++ impl named_imports::foo::foo0_1_0::store::Host for MyHost {+ fn get(&mut self, id: u32) -> u32 {+ id+ }+ }++ impl named_imports::foo::foo0_2_0::store::Host for MyHost {+ fn get(&mut self, id: String) -> u32 {+ id.len() as u32+ }+ }+ }}mod include_component_type {diff --git a/crates/component-macro/tests/codegen/implements-mixed.wit b/crates/component-macro/tests/codegen/implements-mixed.witnew file mode 100644index 0000000000..1aac6a0143--- /dev/null+++ b/crates/component-macro/tests/codegen/implements-mixed.wit@@ -0,0 +1,33 @@+package foo:foo;++interface store {+ get: func(key: string) -> option<string>;+}++interface cache {+ get: func(key: string) -> option<string>;+}++interface types {+ resource r;+ make: func() -> r;+}++interface consumer {+ use types.{r};+ take: func(x: r);+}++world mixed {+ // A plain import followed by a labelled import of the same interface.+ import store;+ import a: store;++ // A labelled import followed by a plain import of the same interface.+ import b: cache;+ import cache;++ // `types` is only imported plainly through `consumer`'s `use`.+ import consumer;+ import c: types;+}diff --git a/crates/component-macro/tests/codegen/implements-unstable.wit b/crates/component-macro/tests/codegen/implements-unstable.witnew file mode 100644index 0000000000..458a6ecb42--- /dev/null+++ b/crates/component-macro/tests/codegen/implements-unstable.wit@@ -0,0 +1,21 @@+package foo:foo;++interface store {+ get: func() -> u32;+}++interface cache {+ get: func() -> u32;+}++world w {+ // Each labelled import has its own feature gate, independent of which+ // label comes first.+ import a: store;+ @unstable(feature = experimental)+ import b: store;++ @unstable(feature = experimental)+ import c: cache;+ import d: cache;+}diff --git a/crates/component-macro/tests/expanded/implements-mixed.rs b/crates/component-macro/tests/expanded/implements-mixed.rsnew file mode 100644index 0000000000..d09b8716ff--- /dev/null+++ b/crates/component-macro/tests/expanded/implements-mixed.rs@@ -0,0 +1,453 @@+/// Auto-generated bindings for a pre-instantiated version of a+/// component which implements the world `mixed`.+///+/// This structure is created through [`MixedPre::new`] which+/// takes a [`InstancePre`](wasmtime::component::InstancePre) that+/// has been created through a [`Linker`](wasmtime::component::Linker).+///+/// For more information see [`Mixed`] as well.+pub struct MixedPre<T: 'static> {+ instance_pre: wasmtime::component::InstancePre<T>,+ indices: MixedIndices,+}+impl<T: 'static> Clone for MixedPre<T> {+ fn clone(&self) -> Self {+ Self {+ instance_pre: self.instance_pre.clone(),+ indices: self.indices.clone(),+ }+ }+}+impl<_T: 'static> MixedPre<_T> {+ /// Creates a new copy of `MixedPre` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component behind `instance_pre`+ /// does not have the required exports.+ pub fn new(+ instance_pre: wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let indices = MixedIndices::new(&instance_pre)?;+ Ok(Self { instance_pre, indices })+ }+ pub fn engine(&self) -> &wasmtime::Engine {+ self.instance_pre.engine()+ }+ pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {+ &self.instance_pre+ }+ /// Instantiates a new instance of [`Mixed`] within the+ /// `store` provided.+ ///+ /// This function will use `self` as the pre-instantiated+ /// instance to perform instantiation. Afterwards the preloaded+ /// indices in `self` are used to lookup all exports on the+ /// resulting instance.+ pub fn instantiate(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<Mixed> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate(&mut store)?;+ self.indices.load(&mut store, &instance)+ }+}+impl<_T: Send + 'static> MixedPre<_T> {+ /// Same as [`Self::instantiate`], except with `async`.+ pub async fn instantiate_async(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<Mixed> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate_async(&mut store).await?;+ self.indices.load(&mut store, &instance)+ }+}+/// Auto-generated bindings for index of the exports of+/// `mixed`.+///+/// This is an implementation detail of [`MixedPre`] and can+/// be constructed if needed as well.+///+/// For more information see [`Mixed`] as well.+#[derive(Clone)]+pub struct MixedIndices {}+/// Auto-generated bindings for an instance a component which+/// implements the world `mixed`.+///+/// This structure can be created through a number of means+/// depending on your requirements and what you have on hand:+///+/// * The most convenient way is to use+/// [`Mixed::instantiate`] which only needs a+/// [`Store`], [`Component`], and [`Linker`].+///+/// * Alternatively you can create a [`MixedPre`] ahead of+/// time with a [`Component`] to front-load string lookups+/// of exports once instead of per-instantiation. This+/// method then uses [`MixedPre::instantiate`] to+/// create a [`Mixed`].+///+/// * If you've instantiated the instance yourself already+/// then you can use [`Mixed::new`].+///+/// These methods are all equivalent to one another and move+/// around the tradeoff of what work is performed when.+///+/// [`Store`]: wasmtime::Store+/// [`Component`]: wasmtime::component::Component+/// [`Linker`]: wasmtime::component::Linker+pub struct Mixed {}+const _: () = {+ impl MixedIndices {+ /// Creates a new copy of `MixedIndices` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component does not have the+ /// required exports.+ pub fn new<_T>(+ _instance_pre: &wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let _component = _instance_pre.component();+ let _instance_type = _instance_pre.instance_type();+ Ok(MixedIndices {})+ }+ /// Uses the indices stored in `self` to load an instance+ /// of [`Mixed`] from the instance provided.+ ///+ /// Note that at this time this method will additionally+ /// perform type-checks of all exports.+ pub fn load(+ &self,+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<Mixed> {+ let _ = &mut store;+ let _instance = instance;+ Ok(Mixed {})+ }+ }+ impl Mixed {+ /// Convenience wrapper around [`MixedPre::new`] and+ /// [`MixedPre::instantiate`].+ pub fn instantiate<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<Mixed> {+ let pre = linker.instantiate_pre(component)?;+ MixedPre::new(pre)?.instantiate(store)+ }+ /// Convenience wrapper around [`MixedIndices::new`] and+ /// [`MixedIndices::load`].+ pub fn new(+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<Mixed> {+ let indices = MixedIndices::new(&instance.instance_pre(&store))?;+ indices.load(&mut store, instance)+ }+ /// Convenience wrapper around [`MixedPre::new`] and+ /// [`MixedPre::instantiate_async`].+ pub async fn instantiate_async<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<Mixed>+ where+ _T: Send,+ {+ let pre = linker.instantiate_pre(component)?;+ MixedPre::new(pre)?.instantiate_async(store).await+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: foo::foo::store::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + foo::foo::types::HostWithStore<T>+ + foo::foo::consumer::HostWithStore<T>+ + foo::foo::store::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + foo::foo::types::HostWithStore<T>,+ for<'a> D::Data<+ 'a,+ >: foo::foo::store::Host + foo::foo::cache::Host + foo::foo::types::Host+ + foo::foo::consumer::Host + foo::foo::store::Host+ + foo::foo::cache::Host + foo::foo::types::Host,+ T: 'static,+ {+ foo::foo::store::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::cache::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::types::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::consumer::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("a")?, host_getter)?;+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("b")?, host_getter)?;+ foo::foo::types::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("c")?, host_getter)?;+ Ok(())+ }+ }+};+pub mod foo {+ pub mod foo {+ #[allow(clippy::all)]+ pub mod store {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData,+ {}+ pub trait Host {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> Option<wasmtime::component::__internal::String>;+ }+ impl<_T: Host + ?Sized> Host for &mut _T {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> Option<wasmtime::component::__internal::String> {+ Host::get(*self, key)+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ inst.func_wrap(+ "get",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::__internal::String,)|+ {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host, arg0);+ Ok((r,))+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ let mut inst = linker.instance("foo:foo/store")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod cache {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData,+ {}+ pub trait Host {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> Option<wasmtime::component::__internal::String>;+ }+ impl<_T: Host + ?Sized> Host for &mut _T {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> Option<wasmtime::component::__internal::String> {+ Host::get(*self, key)+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ inst.func_wrap(+ "get",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::__internal::String,)|+ {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host, arg0);+ Ok((r,))+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ let mut inst = linker.instance("foo:foo/cache")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod types {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub enum R {}+ pub trait HostRWithStore<T>: wasmtime::component::HasData {}+ impl<H: ?Sized, T> HostRWithStore<T> for H+ where+ H: wasmtime::component::HasData,+ {}+ pub trait HostR {+ fn drop(+ &mut self,+ rep: wasmtime::component::Resource<R>,+ ) -> wasmtime::Result<()>;+ }+ impl<_T: HostR + ?Sized> HostR for &mut _T {+ fn drop(+ &mut self,+ rep: wasmtime::component::Resource<R>,+ ) -> wasmtime::Result<()> {+ HostR::drop(*self, rep)+ }+ }+ pub trait HostWithStore<+ T,+ >: wasmtime::component::HasData + HostRWithStore<T> {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + HostRWithStore<T>,+ {}+ pub trait Host: HostR {+ fn make(&mut self) -> wasmtime::component::Resource<R>;+ }+ impl<_T: Host + ?Sized> Host for &mut _T {+ fn make(&mut self) -> wasmtime::component::Resource<R> {+ Host::make(*self)+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ inst.resource(+ "r",+ wasmtime::component::ResourceType::host::<R>(),+ move |mut store, rep| -> wasmtime::Result<()> {+ let resource = wasmtime::component::Resource::new_own(rep);+ wasmtime::ToWasmtimeResult::to_wasmtime_result(+ HostR::drop(&mut host_getter(store.data_mut()), resource),+ )+ },+ )?;+ inst.func_wrap(+ "make",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::make(host);+ Ok((r,))+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ let mut inst = linker.instance("foo:foo/types")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod consumer {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub type R = super::super::super::foo::foo::types::R;+ pub trait HostWithStore<T>: wasmtime::component::HasData {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData,+ {}+ pub trait Host {+ fn take(&mut self, x: wasmtime::component::Resource<R>) -> ();+ }+ impl<_T: Host + ?Sized> Host for &mut _T {+ fn take(&mut self, x: wasmtime::component::Resource<R>) -> () {+ Host::take(*self, x)+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ inst.func_wrap(+ "take",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::Resource<R>,)|+ {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::take(host, arg0);+ Ok(r)+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ let mut inst = linker.instance("foo:foo/consumer")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ }+}diff --git a/crates/component-macro/tests/expanded/implements-mixed_async.rs b/crates/component-macro/tests/expanded/implements-mixed_async.rsnew file mode 100644index 0000000000..92626ecaa1--- /dev/null+++ b/crates/component-macro/tests/expanded/implements-mixed_async.rs@@ -0,0 +1,488 @@+/// Auto-generated bindings for a pre-instantiated version of a+/// component which implements the world `mixed`.+///+/// This structure is created through [`MixedPre::new`] which+/// takes a [`InstancePre`](wasmtime::component::InstancePre) that+/// has been created through a [`Linker`](wasmtime::component::Linker).+///+/// For more information see [`Mixed`] as well.+pub struct MixedPre<T: 'static> {+ instance_pre: wasmtime::component::InstancePre<T>,+ indices: MixedIndices,+}+impl<T: 'static> Clone for MixedPre<T> {+ fn clone(&self) -> Self {+ Self {+ instance_pre: self.instance_pre.clone(),+ indices: self.indices.clone(),+ }+ }+}+impl<_T: 'static> MixedPre<_T> {+ /// Creates a new copy of `MixedPre` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component behind `instance_pre`+ /// does not have the required exports.+ pub fn new(+ instance_pre: wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let indices = MixedIndices::new(&instance_pre)?;+ Ok(Self { instance_pre, indices })+ }+ pub fn engine(&self) -> &wasmtime::Engine {+ self.instance_pre.engine()+ }+ pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {+ &self.instance_pre+ }+ /// Instantiates a new instance of [`Mixed`] within the+ /// `store` provided.+ ///+ /// This function will use `self` as the pre-instantiated+ /// instance to perform instantiation. Afterwards the preloaded+ /// indices in `self` are used to lookup all exports on the+ /// resulting instance.+ pub fn instantiate(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<Mixed> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate(&mut store)?;+ self.indices.load(&mut store, &instance)+ }+}+impl<_T: Send + 'static> MixedPre<_T> {+ /// Same as [`Self::instantiate`], except with `async`.+ pub async fn instantiate_async(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<Mixed> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate_async(&mut store).await?;+ self.indices.load(&mut store, &instance)+ }+}+/// Auto-generated bindings for index of the exports of+/// `mixed`.+///+/// This is an implementation detail of [`MixedPre`] and can+/// be constructed if needed as well.+///+/// For more information see [`Mixed`] as well.+#[derive(Clone)]+pub struct MixedIndices {}+/// Auto-generated bindings for an instance a component which+/// implements the world `mixed`.+///+/// This structure can be created through a number of means+/// depending on your requirements and what you have on hand:+///+/// * The most convenient way is to use+/// [`Mixed::instantiate`] which only needs a+/// [`Store`], [`Component`], and [`Linker`].+///+/// * Alternatively you can create a [`MixedPre`] ahead of+/// time with a [`Component`] to front-load string lookups+/// of exports once instead of per-instantiation. This+/// method then uses [`MixedPre::instantiate`] to+/// create a [`Mixed`].+///+/// * If you've instantiated the instance yourself already+/// then you can use [`Mixed::new`].+///+/// These methods are all equivalent to one another and move+/// around the tradeoff of what work is performed when.+///+/// [`Store`]: wasmtime::Store+/// [`Component`]: wasmtime::component::Component+/// [`Linker`]: wasmtime::component::Linker+pub struct Mixed {}+const _: () = {+ impl MixedIndices {+ /// Creates a new copy of `MixedIndices` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component does not have the+ /// required exports.+ pub fn new<_T>(+ _instance_pre: &wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let _component = _instance_pre.component();+ let _instance_type = _instance_pre.instance_type();+ Ok(MixedIndices {})+ }+ /// Uses the indices stored in `self` to load an instance+ /// of [`Mixed`] from the instance provided.+ ///+ /// Note that at this time this method will additionally+ /// perform type-checks of all exports.+ pub fn load(+ &self,+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<Mixed> {+ let _ = &mut store;+ let _instance = instance;+ Ok(Mixed {})+ }+ }+ impl Mixed {+ /// Convenience wrapper around [`MixedPre::new`] and+ /// [`MixedPre::instantiate`].+ pub fn instantiate<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<Mixed> {+ let pre = linker.instantiate_pre(component)?;+ MixedPre::new(pre)?.instantiate(store)+ }+ /// Convenience wrapper around [`MixedIndices::new`] and+ /// [`MixedIndices::load`].+ pub fn new(+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<Mixed> {+ let indices = MixedIndices::new(&instance.instance_pre(&store))?;+ indices.load(&mut store, instance)+ }+ /// Convenience wrapper around [`MixedPre::new`] and+ /// [`MixedPre::instantiate_async`].+ pub async fn instantiate_async<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<Mixed>+ where+ _T: Send,+ {+ let pre = linker.instantiate_pre(component)?;+ MixedPre::new(pre)?.instantiate_async(store).await+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: foo::foo::store::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + foo::foo::types::HostWithStore<T>+ + foo::foo::consumer::HostWithStore<T>+ + foo::foo::store::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + foo::foo::types::HostWithStore<T> + Send,+ for<'a> D::Data<+ 'a,+ >: foo::foo::store::Host + foo::foo::cache::Host + foo::foo::types::Host+ + foo::foo::consumer::Host + foo::foo::store::Host+ + foo::foo::cache::Host + foo::foo::types::Host + Send,+ T: 'static + Send,+ {+ foo::foo::store::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::cache::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::types::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::consumer::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("a")?, host_getter)?;+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("b")?, host_getter)?;+ foo::foo::types::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("c")?, host_getter)?;+ Ok(())+ }+ }+};+pub mod foo {+ pub mod foo {+ #[allow(clippy::all)]+ pub mod store {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send {+ async move { Host::get(*self, key).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::__internal::String,)|+ {+ wasmtime::component::__internal::Box::new(async move {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host, arg0).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/store")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod cache {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send {+ async move { Host::get(*self, key).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::__internal::String,)|+ {+ wasmtime::component::__internal::Box::new(async move {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host, arg0).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/cache")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod types {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub enum R {}+ pub trait HostRWithStore<T>: wasmtime::component::HasData {}+ impl<H: ?Sized, T> HostRWithStore<T> for H+ where+ H: wasmtime::component::HasData,+ {}+ pub trait HostR {+ fn drop(+ &mut self,+ rep: wasmtime::component::Resource<R>,+ ) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send;+ }+ impl<_T: HostR + ?Sized + Send> HostR for &mut _T {+ async fn drop(+ &mut self,+ rep: wasmtime::component::Resource<R>,+ ) -> wasmtime::Result<()> {+ HostR::drop(*self, rep).await+ }+ }+ pub trait HostWithStore<+ T,+ >: wasmtime::component::HasData + HostRWithStore<T> + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + HostRWithStore<T> + Send,+ {}+ pub trait Host: HostR + Send {+ fn make(+ &mut self,+ ) -> impl ::core::future::Future<+ Output = wasmtime::component::Resource<R>,+ > + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn make(+ &mut self,+ ) -> impl ::core::future::Future<+ Output = wasmtime::component::Resource<R>,+ > + Send {+ async move { Host::make(*self).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.resource_async(+ "r",+ wasmtime::component::ResourceType::host::<R>(),+ move |mut store, rep| {+ wasmtime::component::__internal::Box::new(async move {+ wasmtime::ToWasmtimeResult::to_wasmtime_result(+ HostR::drop(+ &mut host_getter(store.data_mut()),+ wasmtime::component::Resource::new_own(rep),+ )+ .await,+ )+ })+ },+ )?;+ inst.func_wrap_async(+ "make",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ wasmtime::component::__internal::Box::new(async move {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::make(host).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/types")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod consumer {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub type R = super::super::super::foo::foo::types::R;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn take(+ &mut self,+ x: wasmtime::component::Resource<R>,+ ) -> impl ::core::future::Future<Output = ()> + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn take(+ &mut self,+ x: wasmtime::component::Resource<R>,+ ) -> impl ::core::future::Future<Output = ()> + Send {+ async move { Host::take(*self, x).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "take",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::Resource<R>,)|+ {+ wasmtime::component::__internal::Box::new(async move {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::take(host, arg0).await;+ Ok(r)+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/consumer")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ }+}diff --git a/crates/component-macro/tests/expanded/implements-mixed_concurrent.rs b/crates/component-macro/tests/expanded/implements-mixed_concurrent.rsnew file mode 100644index 0000000000..cc51b3fbd4--- /dev/null+++ b/crates/component-macro/tests/expanded/implements-mixed_concurrent.rs@@ -0,0 +1,457 @@+/// Auto-generated bindings for a pre-instantiated version of a+/// component which implements the world `mixed`.+///+/// This structure is created through [`MixedPre::new`] which+/// takes a [`InstancePre`](wasmtime::component::InstancePre) that+/// has been created through a [`Linker`](wasmtime::component::Linker).+///+/// For more information see [`Mixed`] as well.+pub struct MixedPre<T: 'static> {+ instance_pre: wasmtime::component::InstancePre<T>,+ indices: MixedIndices,+}+impl<T: 'static> Clone for MixedPre<T> {+ fn clone(&self) -> Self {+ Self {+ instance_pre: self.instance_pre.clone(),+ indices: self.indices.clone(),+ }+ }+}+impl<_T: 'static> MixedPre<_T> {+ /// Creates a new copy of `MixedPre` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component behind `instance_pre`+ /// does not have the required exports.+ pub fn new(+ instance_pre: wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let indices = MixedIndices::new(&instance_pre)?;+ Ok(Self { instance_pre, indices })+ }+ pub fn engine(&self) -> &wasmtime::Engine {+ self.instance_pre.engine()+ }+ pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {+ &self.instance_pre+ }+ /// Instantiates a new instance of [`Mixed`] within the+ /// `store` provided.+ ///+ /// This function will use `self` as the pre-instantiated+ /// instance to perform instantiation. Afterwards the preloaded+ /// indices in `self` are used to lookup all exports on the+ /// resulting instance.+ pub fn instantiate(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<Mixed> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate(&mut store)?;+ self.indices.load(&mut store, &instance)+ }+}+impl<_T: Send + 'static> MixedPre<_T> {+ /// Same as [`Self::instantiate`], except with `async`.+ pub async fn instantiate_async(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<Mixed> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate_async(&mut store).await?;+ self.indices.load(&mut store, &instance)+ }+}+/// Auto-generated bindings for index of the exports of+/// `mixed`.+///+/// This is an implementation detail of [`MixedPre`] and can+/// be constructed if needed as well.+///+/// For more information see [`Mixed`] as well.+#[derive(Clone)]+pub struct MixedIndices {}+/// Auto-generated bindings for an instance a component which+/// implements the world `mixed`.+///+/// This structure can be created through a number of means+/// depending on your requirements and what you have on hand:+///+/// * The most convenient way is to use+/// [`Mixed::instantiate`] which only needs a+/// [`Store`], [`Component`], and [`Linker`].+///+/// * Alternatively you can create a [`MixedPre`] ahead of+/// time with a [`Component`] to front-load string lookups+/// of exports once instead of per-instantiation. This+/// method then uses [`MixedPre::instantiate`] to+/// create a [`Mixed`].+///+/// * If you've instantiated the instance yourself already+/// then you can use [`Mixed::new`].+///+/// These methods are all equivalent to one another and move+/// around the tradeoff of what work is performed when.+///+/// [`Store`]: wasmtime::Store+/// [`Component`]: wasmtime::component::Component+/// [`Linker`]: wasmtime::component::Linker+pub struct Mixed {}+const _: () = {+ impl MixedIndices {+ /// Creates a new copy of `MixedIndices` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component does not have the+ /// required exports.+ pub fn new<_T>(+ _instance_pre: &wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let _component = _instance_pre.component();+ let _instance_type = _instance_pre.instance_type();+ Ok(MixedIndices {})+ }+ /// Uses the indices stored in `self` to load an instance+ /// of [`Mixed`] from the instance provided.+ ///+ /// Note that at this time this method will additionally+ /// perform type-checks of all exports.+ pub fn load(+ &self,+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<Mixed> {+ let _ = &mut store;+ let _instance = instance;+ Ok(Mixed {})+ }+ }+ impl Mixed {+ /// Convenience wrapper around [`MixedPre::new`] and+ /// [`MixedPre::instantiate`].+ pub fn instantiate<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<Mixed> {+ let pre = linker.instantiate_pre(component)?;+ MixedPre::new(pre)?.instantiate(store)+ }+ /// Convenience wrapper around [`MixedIndices::new`] and+ /// [`MixedIndices::load`].+ pub fn new(+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<Mixed> {+ let indices = MixedIndices::new(&instance.instance_pre(&store))?;+ indices.load(&mut store, instance)+ }+ /// Convenience wrapper around [`MixedPre::new`] and+ /// [`MixedPre::instantiate_async`].+ pub async fn instantiate_async<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<Mixed>+ where+ _T: Send,+ {+ let pre = linker.instantiate_pre(component)?;+ MixedPre::new(pre)?.instantiate_async(store).await+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: foo::foo::store::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + foo::foo::types::HostWithStore<T>+ + foo::foo::consumer::HostWithStore<T>+ + foo::foo::store::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + foo::foo::types::HostWithStore<T> + Send,+ for<'a> D::Data<+ 'a,+ >: foo::foo::store::Host + foo::foo::cache::Host + foo::foo::types::Host+ + foo::foo::consumer::Host + foo::foo::store::Host+ + foo::foo::cache::Host + foo::foo::types::Host + Send,+ T: 'static + Send,+ {+ foo::foo::store::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::cache::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::types::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::consumer::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("a")?, host_getter)?;+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("b")?, host_getter)?;+ foo::foo::types::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("c")?, host_getter)?;+ Ok(())+ }+ }+};+pub mod foo {+ pub mod foo {+ #[allow(clippy::all)]+ pub mod store {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {+ fn get(+ host: wasmtime::component::Access<T, Self>,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send;+ }+ pub trait Host: Send {}+ impl<_T: Host + ?Sized + Send> Host for &mut _T {}+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::__internal::String,)|+ {+ wasmtime::component::__internal::Box::new(async move {+ let access_cx = wasmtime::AsContextMut::as_context_mut(+ &mut caller,+ );+ let host = wasmtime::component::Access::<+ T,+ D,+ >::new(access_cx, host_getter);+ let r = <D as HostWithStore<T>>::get(host, arg0).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/store")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod cache {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {+ fn get(+ host: wasmtime::component::Access<T, Self>,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send;+ }+ pub trait Host: Send {}+ impl<_T: Host + ?Sized + Send> Host for &mut _T {}+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::__internal::String,)|+ {+ wasmtime::component::__internal::Box::new(async move {+ let access_cx = wasmtime::AsContextMut::as_context_mut(+ &mut caller,+ );+ let host = wasmtime::component::Access::<+ T,+ D,+ >::new(access_cx, host_getter);+ let r = <D as HostWithStore<T>>::get(host, arg0).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/cache")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod types {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub enum R {}+ pub trait HostRWithStore<T>: wasmtime::component::HasData {+ fn drop(+ accessor: &wasmtime::component::Accessor<T, Self>,+ rep: wasmtime::component::Resource<R>,+ ) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send+ where+ Self: Sized;+ }+ pub trait HostR {}+ impl<_T: HostR + ?Sized + Send> HostR for &mut _T {}+ pub trait HostWithStore<+ T,+ >: wasmtime::component::HasData + HostRWithStore<T> + Send {+ fn make(+ host: wasmtime::component::Access<T, Self>,+ ) -> impl ::core::future::Future<+ Output = wasmtime::component::Resource<R>,+ > + Send;+ }+ pub trait Host: HostR + Send {}+ impl<_T: Host + ?Sized + Send> Host for &mut _T {}+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.resource_concurrent(+ "r",+ wasmtime::component::ResourceType::host::<R>(),+ move |caller: &wasmtime::component::Accessor<T>, rep| {+ wasmtime::component::__internal::Box::pin(async move {+ let accessor = &caller.with_getter::<D>(host_getter);+ wasmtime::ToWasmtimeResult::to_wasmtime_result(+ HostRWithStore::<+ T,+ >::drop(+ accessor,+ wasmtime::component::Resource::new_own(rep),+ )+ .await,+ )+ })+ },+ )?;+ inst.func_wrap_async(+ "make",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ wasmtime::component::__internal::Box::new(async move {+ let access_cx = wasmtime::AsContextMut::as_context_mut(+ &mut caller,+ );+ let host = wasmtime::component::Access::<+ T,+ D,+ >::new(access_cx, host_getter);+ let r = <D as HostWithStore<T>>::make(host).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/types")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod consumer {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub type R = super::super::super::foo::foo::types::R;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {+ fn take(+ host: wasmtime::component::Access<T, Self>,+ x: wasmtime::component::Resource<R>,+ ) -> impl ::core::future::Future<Output = ()> + Send;+ }+ pub trait Host: Send {}+ impl<_T: Host + ?Sized + Send> Host for &mut _T {}+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "take",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::Resource<R>,)|+ {+ wasmtime::component::__internal::Box::new(async move {+ let access_cx = wasmtime::AsContextMut::as_context_mut(+ &mut caller,+ );+ let host = wasmtime::component::Access::<+ T,+ D,+ >::new(access_cx, host_getter);+ let r = <D as HostWithStore<T>>::take(host, arg0).await;+ Ok(r)+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/consumer")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ }+}diff --git a/crates/component-macro/tests/expanded/implements-mixed_tracing_async.rs b/crates/component-macro/tests/expanded/implements-mixed_tracing_async.rsnew file mode 100644index 0000000000..0c4f1a0e5e--- /dev/null+++ b/crates/component-macro/tests/expanded/implements-mixed_tracing_async.rs@@ -0,0 +1,549 @@+/// Auto-generated bindings for a pre-instantiated version of a+/// component which implements the world `mixed`.+///+/// This structure is created through [`MixedPre::new`] which+/// takes a [`InstancePre`](wasmtime::component::InstancePre) that+/// has been created through a [`Linker`](wasmtime::component::Linker).+///+/// For more information see [`Mixed`] as well.+pub struct MixedPre<T: 'static> {+ instance_pre: wasmtime::component::InstancePre<T>,+ indices: MixedIndices,+}+impl<T: 'static> Clone for MixedPre<T> {+ fn clone(&self) -> Self {+ Self {+ instance_pre: self.instance_pre.clone(),+ indices: self.indices.clone(),+ }+ }+}+impl<_T: 'static> MixedPre<_T> {+ /// Creates a new copy of `MixedPre` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component behind `instance_pre`+ /// does not have the required exports.+ pub fn new(+ instance_pre: wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let indices = MixedIndices::new(&instance_pre)?;+ Ok(Self { instance_pre, indices })+ }+ pub fn engine(&self) -> &wasmtime::Engine {+ self.instance_pre.engine()+ }+ pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {+ &self.instance_pre+ }+ /// Instantiates a new instance of [`Mixed`] within the+ /// `store` provided.+ ///+ /// This function will use `self` as the pre-instantiated+ /// instance to perform instantiation. Afterwards the preloaded+ /// indices in `self` are used to lookup all exports on the+ /// resulting instance.+ pub fn instantiate(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<Mixed> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate(&mut store)?;+ self.indices.load(&mut store, &instance)+ }+}+impl<_T: Send + 'static> MixedPre<_T> {+ /// Same as [`Self::instantiate`], except with `async`.+ pub async fn instantiate_async(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<Mixed> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate_async(&mut store).await?;+ self.indices.load(&mut store, &instance)+ }+}+/// Auto-generated bindings for index of the exports of+/// `mixed`.+///+/// This is an implementation detail of [`MixedPre`] and can+/// be constructed if needed as well.+///+/// For more information see [`Mixed`] as well.+#[derive(Clone)]+pub struct MixedIndices {}+/// Auto-generated bindings for an instance a component which+/// implements the world `mixed`.+///+/// This structure can be created through a number of means+/// depending on your requirements and what you have on hand:+///+/// * The most convenient way is to use+/// [`Mixed::instantiate`] which only needs a+/// [`Store`], [`Component`], and [`Linker`].+///+/// * Alternatively you can create a [`MixedPre`] ahead of+/// time with a [`Component`] to front-load string lookups+/// of exports once instead of per-instantiation. This+/// method then uses [`MixedPre::instantiate`] to+/// create a [`Mixed`].+///+/// * If you've instantiated the instance yourself already+/// then you can use [`Mixed::new`].+///+/// These methods are all equivalent to one another and move+/// around the tradeoff of what work is performed when.+///+/// [`Store`]: wasmtime::Store+/// [`Component`]: wasmtime::component::Component+/// [`Linker`]: wasmtime::component::Linker+pub struct Mixed {}+const _: () = {+ impl MixedIndices {+ /// Creates a new copy of `MixedIndices` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component does not have the+ /// required exports.+ pub fn new<_T>(+ _instance_pre: &wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let _component = _instance_pre.component();+ let _instance_type = _instance_pre.instance_type();+ Ok(MixedIndices {})+ }+ /// Uses the indices stored in `self` to load an instance+ /// of [`Mixed`] from the instance provided.+ ///+ /// Note that at this time this method will additionally+ /// perform type-checks of all exports.+ pub fn load(+ &self,+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<Mixed> {+ let _ = &mut store;+ let _instance = instance;+ Ok(Mixed {})+ }+ }+ impl Mixed {+ /// Convenience wrapper around [`MixedPre::new`] and+ /// [`MixedPre::instantiate`].+ pub fn instantiate<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<Mixed> {+ let pre = linker.instantiate_pre(component)?;+ MixedPre::new(pre)?.instantiate(store)+ }+ /// Convenience wrapper around [`MixedIndices::new`] and+ /// [`MixedIndices::load`].+ pub fn new(+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<Mixed> {+ let indices = MixedIndices::new(&instance.instance_pre(&store))?;+ indices.load(&mut store, instance)+ }+ /// Convenience wrapper around [`MixedPre::new`] and+ /// [`MixedPre::instantiate_async`].+ pub async fn instantiate_async<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<Mixed>+ where+ _T: Send,+ {+ let pre = linker.instantiate_pre(component)?;+ MixedPre::new(pre)?.instantiate_async(store).await+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: foo::foo::store::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + foo::foo::types::HostWithStore<T>+ + foo::foo::consumer::HostWithStore<T>+ + foo::foo::store::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + foo::foo::types::HostWithStore<T> + Send,+ for<'a> D::Data<+ 'a,+ >: foo::foo::store::Host + foo::foo::cache::Host + foo::foo::types::Host+ + foo::foo::consumer::Host + foo::foo::store::Host+ + foo::foo::cache::Host + foo::foo::types::Host + Send,+ T: 'static + Send,+ {+ foo::foo::store::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::cache::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::types::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::consumer::add_to_linker::<T, D>(linker, host_getter)?;+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("a")?, host_getter)?;+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("b")?, host_getter)?;+ foo::foo::types::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("c")?, host_getter)?;+ Ok(())+ }+ }+};+pub mod foo {+ pub mod foo {+ #[allow(clippy::all)]+ pub mod store {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send {+ async move { Host::get(*self, key).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::__internal::String,)|+ {+ use tracing::Instrument;+ let span = tracing::span!(+ tracing::Level::TRACE, "wit-bindgen import", module =+ "store", function = "get",+ );+ wasmtime::component::__internal::Box::new(+ async move {+ tracing::event!(+ tracing::Level::TRACE, key = tracing::field::debug(& arg0),+ "call"+ );+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host, arg0).await;+ tracing::event!(+ tracing::Level::TRACE, result = tracing::field::debug(& r),+ "return"+ );+ Ok((r,))+ }+ .instrument(span),+ )+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/store")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod cache {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn get(+ &mut self,+ key: wasmtime::component::__internal::String,+ ) -> impl ::core::future::Future<+ Output = Option<wasmtime::component::__internal::String>,+ > + Send {+ async move { Host::get(*self, key).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::__internal::String,)|+ {+ use tracing::Instrument;+ let span = tracing::span!(+ tracing::Level::TRACE, "wit-bindgen import", module =+ "cache", function = "get",+ );+ wasmtime::component::__internal::Box::new(+ async move {+ tracing::event!(+ tracing::Level::TRACE, key = tracing::field::debug(& arg0),+ "call"+ );+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host, arg0).await;+ tracing::event!(+ tracing::Level::TRACE, result = tracing::field::debug(& r),+ "return"+ );+ Ok((r,))+ }+ .instrument(span),+ )+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/cache")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod types {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub enum R {}+ pub trait HostRWithStore<T>: wasmtime::component::HasData {}+ impl<H: ?Sized, T> HostRWithStore<T> for H+ where+ H: wasmtime::component::HasData,+ {}+ pub trait HostR {+ fn drop(+ &mut self,+ rep: wasmtime::component::Resource<R>,+ ) -> impl ::core::future::Future<Output = wasmtime::Result<()>> + Send;+ }+ impl<_T: HostR + ?Sized + Send> HostR for &mut _T {+ async fn drop(+ &mut self,+ rep: wasmtime::component::Resource<R>,+ ) -> wasmtime::Result<()> {+ HostR::drop(*self, rep).await+ }+ }+ pub trait HostWithStore<+ T,+ >: wasmtime::component::HasData + HostRWithStore<T> + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + HostRWithStore<T> + Send,+ {}+ pub trait Host: HostR + Send {+ fn make(+ &mut self,+ ) -> impl ::core::future::Future<+ Output = wasmtime::component::Resource<R>,+ > + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn make(+ &mut self,+ ) -> impl ::core::future::Future<+ Output = wasmtime::component::Resource<R>,+ > + Send {+ async move { Host::make(*self).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.resource_async(+ "r",+ wasmtime::component::ResourceType::host::<R>(),+ move |mut store, rep| {+ wasmtime::component::__internal::Box::new(async move {+ wasmtime::ToWasmtimeResult::to_wasmtime_result(+ HostR::drop(+ &mut host_getter(store.data_mut()),+ wasmtime::component::Resource::new_own(rep),+ )+ .await,+ )+ })+ },+ )?;+ inst.func_wrap_async(+ "make",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ use tracing::Instrument;+ let span = tracing::span!(+ tracing::Level::TRACE, "wit-bindgen import", module =+ "types", function = "make",+ );+ wasmtime::component::__internal::Box::new(+ async move {+ tracing::event!(tracing::Level::TRACE, "call");+ let host = &mut host_getter(caller.data_mut());+ let r = Host::make(host).await;+ tracing::event!(+ tracing::Level::TRACE, result = tracing::field::debug(& r),+ "return"+ );+ Ok((r,))+ }+ .instrument(span),+ )+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/types")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod consumer {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub type R = super::super::super::foo::foo::types::R;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn take(+ &mut self,+ x: wasmtime::component::Resource<R>,+ ) -> impl ::core::future::Future<Output = ()> + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn take(+ &mut self,+ x: wasmtime::component::Resource<R>,+ ) -> impl ::core::future::Future<Output = ()> + Send {+ async move { Host::take(*self, x).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "take",+ move |+ mut caller: wasmtime::StoreContextMut<'_, T>,+ (arg0,): (wasmtime::component::Resource<R>,)|+ {+ use tracing::Instrument;+ let span = tracing::span!(+ tracing::Level::TRACE, "wit-bindgen import", module =+ "consumer", function = "take",+ );+ wasmtime::component::__internal::Box::new(+ async move {+ tracing::event!(+ tracing::Level::TRACE, x = tracing::field::debug(& arg0),+ "call"+ );+ let host = &mut host_getter(caller.data_mut());+ let r = Host::take(host, arg0).await;+ tracing::event!(+ tracing::Level::TRACE, result = tracing::field::debug(& r),+ "return"+ );+ Ok(r)+ }+ .instrument(span),+ )+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/consumer")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ }+}diff --git a/crates/component-macro/tests/expanded/implements-unstable.rs b/crates/component-macro/tests/expanded/implements-unstable.rsnew file mode 100644index 0000000000..86089920be--- /dev/null+++ b/crates/component-macro/tests/expanded/implements-unstable.rs@@ -0,0 +1,314 @@+/// Link-time configurations.+#[derive(Clone, Debug, Default)]+pub struct LinkOptions {+ pub(crate) experimental: bool,+}+impl LinkOptions {+ /// Enable members marked as `@unstable(feature = experimental)`+ pub fn experimental(&mut self, enabled: bool) -> &mut Self {+ self.experimental = enabled;+ self+ }+}+/// Auto-generated bindings for a pre-instantiated version of a+/// component which implements the world `w`.+///+/// This structure is created through [`WPre::new`] which+/// takes a [`InstancePre`](wasmtime::component::InstancePre) that+/// has been created through a [`Linker`](wasmtime::component::Linker).+///+/// For more information see [`W`] as well.+pub struct WPre<T: 'static> {+ instance_pre: wasmtime::component::InstancePre<T>,+ indices: WIndices,+}+impl<T: 'static> Clone for WPre<T> {+ fn clone(&self) -> Self {+ Self {+ instance_pre: self.instance_pre.clone(),+ indices: self.indices.clone(),+ }+ }+}+impl<_T: 'static> WPre<_T> {+ /// Creates a new copy of `WPre` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component behind `instance_pre`+ /// does not have the required exports.+ pub fn new(+ instance_pre: wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let indices = WIndices::new(&instance_pre)?;+ Ok(Self { instance_pre, indices })+ }+ pub fn engine(&self) -> &wasmtime::Engine {+ self.instance_pre.engine()+ }+ pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {+ &self.instance_pre+ }+ /// Instantiates a new instance of [`W`] within the+ /// `store` provided.+ ///+ /// This function will use `self` as the pre-instantiated+ /// instance to perform instantiation. Afterwards the preloaded+ /// indices in `self` are used to lookup all exports on the+ /// resulting instance.+ pub fn instantiate(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<W> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate(&mut store)?;+ self.indices.load(&mut store, &instance)+ }+}+impl<_T: Send + 'static> WPre<_T> {+ /// Same as [`Self::instantiate`], except with `async`.+ pub async fn instantiate_async(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<W> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate_async(&mut store).await?;+ self.indices.load(&mut store, &instance)+ }+}+/// Auto-generated bindings for index of the exports of+/// `w`.+///+/// This is an implementation detail of [`WPre`] and can+/// be constructed if needed as well.+///+/// For more information see [`W`] as well.+#[derive(Clone)]+pub struct WIndices {}+/// Auto-generated bindings for an instance a component which+/// implements the world `w`.+///+/// This structure can be created through a number of means+/// depending on your requirements and what you have on hand:+///+/// * The most convenient way is to use+/// [`W::instantiate`] which only needs a+/// [`Store`], [`Component`], and [`Linker`].+///+/// * Alternatively you can create a [`WPre`] ahead of+/// time with a [`Component`] to front-load string lookups+/// of exports once instead of per-instantiation. This+/// method then uses [`WPre::instantiate`] to+/// create a [`W`].+///+/// * If you've instantiated the instance yourself already+/// then you can use [`W::new`].+///+/// These methods are all equivalent to one another and move+/// around the tradeoff of what work is performed when.+///+/// [`Store`]: wasmtime::Store+/// [`Component`]: wasmtime::component::Component+/// [`Linker`]: wasmtime::component::Linker+pub struct W {}+const _: () = {+ impl WIndices {+ /// Creates a new copy of `WIndices` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component does not have the+ /// required exports.+ pub fn new<_T>(+ _instance_pre: &wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let _component = _instance_pre.component();+ let _instance_type = _instance_pre.instance_type();+ Ok(WIndices {})+ }+ /// Uses the indices stored in `self` to load an instance+ /// of [`W`] from the instance provided.+ ///+ /// Note that at this time this method will additionally+ /// perform type-checks of all exports.+ pub fn load(+ &self,+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<W> {+ let _ = &mut store;+ let _instance = instance;+ Ok(W {})+ }+ }+ impl W {+ /// Convenience wrapper around [`WPre::new`] and+ /// [`WPre::instantiate`].+ pub fn instantiate<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<W> {+ let pre = linker.instantiate_pre(component)?;+ WPre::new(pre)?.instantiate(store)+ }+ /// Convenience wrapper around [`WIndices::new`] and+ /// [`WIndices::load`].+ pub fn new(+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<W> {+ let indices = WIndices::new(&instance.instance_pre(&store))?;+ indices.load(&mut store, instance)+ }+ /// Convenience wrapper around [`WPre::new`] and+ /// [`WPre::instantiate_async`].+ pub async fn instantiate_async<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<W>+ where+ _T: Send,+ {+ let pre = linker.instantiate_pre(component)?;+ WPre::new(pre)?.instantiate_async(store).await+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ options: &LinkOptions,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: foo::foo::store::HostWithStore<T> + foo::foo::store::HostWithStore<T>+ + foo::foo::cache::HostWithStore<T> + foo::foo::cache::HostWithStore<T>,+ for<'a> D::Data<+ 'a,+ >: foo::foo::store::Host + foo::foo::store::Host + foo::foo::cache::Host+ + foo::foo::cache::Host,+ T: 'static,+ {+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("a")?, host_getter)?;+ if options.experimental {+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("b")?, host_getter)?;+ }+ if options.experimental {+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("c")?, host_getter)?;+ }+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("d")?, host_getter)?;+ Ok(())+ }+ }+};+pub mod foo {+ pub mod foo {+ #[allow(clippy::all)]+ pub mod store {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData,+ {}+ pub trait Host {+ fn get(&mut self) -> u32;+ }+ impl<_T: Host + ?Sized> Host for &mut _T {+ fn get(&mut self) -> u32 {+ Host::get(*self)+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ inst.func_wrap(+ "get",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host);+ Ok((r,))+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ let mut inst = linker.instance("foo:foo/store")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod cache {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData,+ {}+ pub trait Host {+ fn get(&mut self) -> u32;+ }+ impl<_T: Host + ?Sized> Host for &mut _T {+ fn get(&mut self) -> u32 {+ Host::get(*self)+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ inst.func_wrap(+ "get",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host);+ Ok((r,))+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static,+ {+ let mut inst = linker.instance("foo:foo/cache")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ }+}diff --git a/crates/component-macro/tests/expanded/implements-unstable_async.rs b/crates/component-macro/tests/expanded/implements-unstable_async.rsnew file mode 100644index 0000000000..cc83e0c206--- /dev/null+++ b/crates/component-macro/tests/expanded/implements-unstable_async.rs@@ -0,0 +1,319 @@+/// Link-time configurations.+#[derive(Clone, Debug, Default)]+pub struct LinkOptions {+ pub(crate) experimental: bool,+}+impl LinkOptions {+ /// Enable members marked as `@unstable(feature = experimental)`+ pub fn experimental(&mut self, enabled: bool) -> &mut Self {+ self.experimental = enabled;+ self+ }+}+/// Auto-generated bindings for a pre-instantiated version of a+/// component which implements the world `w`.+///+/// This structure is created through [`WPre::new`] which+/// takes a [`InstancePre`](wasmtime::component::InstancePre) that+/// has been created through a [`Linker`](wasmtime::component::Linker).+///+/// For more information see [`W`] as well.+pub struct WPre<T: 'static> {+ instance_pre: wasmtime::component::InstancePre<T>,+ indices: WIndices,+}+impl<T: 'static> Clone for WPre<T> {+ fn clone(&self) -> Self {+ Self {+ instance_pre: self.instance_pre.clone(),+ indices: self.indices.clone(),+ }+ }+}+impl<_T: 'static> WPre<_T> {+ /// Creates a new copy of `WPre` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component behind `instance_pre`+ /// does not have the required exports.+ pub fn new(+ instance_pre: wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let indices = WIndices::new(&instance_pre)?;+ Ok(Self { instance_pre, indices })+ }+ pub fn engine(&self) -> &wasmtime::Engine {+ self.instance_pre.engine()+ }+ pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {+ &self.instance_pre+ }+ /// Instantiates a new instance of [`W`] within the+ /// `store` provided.+ ///+ /// This function will use `self` as the pre-instantiated+ /// instance to perform instantiation. Afterwards the preloaded+ /// indices in `self` are used to lookup all exports on the+ /// resulting instance.+ pub fn instantiate(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<W> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate(&mut store)?;+ self.indices.load(&mut store, &instance)+ }+}+impl<_T: Send + 'static> WPre<_T> {+ /// Same as [`Self::instantiate`], except with `async`.+ pub async fn instantiate_async(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<W> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate_async(&mut store).await?;+ self.indices.load(&mut store, &instance)+ }+}+/// Auto-generated bindings for index of the exports of+/// `w`.+///+/// This is an implementation detail of [`WPre`] and can+/// be constructed if needed as well.+///+/// For more information see [`W`] as well.+#[derive(Clone)]+pub struct WIndices {}+/// Auto-generated bindings for an instance a component which+/// implements the world `w`.+///+/// This structure can be created through a number of means+/// depending on your requirements and what you have on hand:+///+/// * The most convenient way is to use+/// [`W::instantiate`] which only needs a+/// [`Store`], [`Component`], and [`Linker`].+///+/// * Alternatively you can create a [`WPre`] ahead of+/// time with a [`Component`] to front-load string lookups+/// of exports once instead of per-instantiation. This+/// method then uses [`WPre::instantiate`] to+/// create a [`W`].+///+/// * If you've instantiated the instance yourself already+/// then you can use [`W::new`].+///+/// These methods are all equivalent to one another and move+/// around the tradeoff of what work is performed when.+///+/// [`Store`]: wasmtime::Store+/// [`Component`]: wasmtime::component::Component+/// [`Linker`]: wasmtime::component::Linker+pub struct W {}+const _: () = {+ impl WIndices {+ /// Creates a new copy of `WIndices` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component does not have the+ /// required exports.+ pub fn new<_T>(+ _instance_pre: &wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let _component = _instance_pre.component();+ let _instance_type = _instance_pre.instance_type();+ Ok(WIndices {})+ }+ /// Uses the indices stored in `self` to load an instance+ /// of [`W`] from the instance provided.+ ///+ /// Note that at this time this method will additionally+ /// perform type-checks of all exports.+ pub fn load(+ &self,+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<W> {+ let _ = &mut store;+ let _instance = instance;+ Ok(W {})+ }+ }+ impl W {+ /// Convenience wrapper around [`WPre::new`] and+ /// [`WPre::instantiate`].+ pub fn instantiate<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<W> {+ let pre = linker.instantiate_pre(component)?;+ WPre::new(pre)?.instantiate(store)+ }+ /// Convenience wrapper around [`WIndices::new`] and+ /// [`WIndices::load`].+ pub fn new(+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<W> {+ let indices = WIndices::new(&instance.instance_pre(&store))?;+ indices.load(&mut store, instance)+ }+ /// Convenience wrapper around [`WPre::new`] and+ /// [`WPre::instantiate_async`].+ pub async fn instantiate_async<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<W>+ where+ _T: Send,+ {+ let pre = linker.instantiate_pre(component)?;+ WPre::new(pre)?.instantiate_async(store).await+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ options: &LinkOptions,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: foo::foo::store::HostWithStore<T> + foo::foo::store::HostWithStore<T>+ + foo::foo::cache::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + Send,+ for<'a> D::Data<+ 'a,+ >: foo::foo::store::Host + foo::foo::store::Host + foo::foo::cache::Host+ + foo::foo::cache::Host + Send,+ T: 'static + Send,+ {+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("a")?, host_getter)?;+ if options.experimental {+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("b")?, host_getter)?;+ }+ if options.experimental {+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("c")?, host_getter)?;+ }+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("d")?, host_getter)?;+ Ok(())+ }+ }+};+pub mod foo {+ pub mod foo {+ #[allow(clippy::all)]+ pub mod store {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn get(&mut self) -> impl ::core::future::Future<Output = u32> + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn get(&mut self) -> impl ::core::future::Future<Output = u32> + Send {+ async move { Host::get(*self).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ wasmtime::component::__internal::Box::new(async move {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/store")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod cache {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn get(&mut self) -> impl ::core::future::Future<Output = u32> + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn get(&mut self) -> impl ::core::future::Future<Output = u32> + Send {+ async move { Host::get(*self).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ wasmtime::component::__internal::Box::new(async move {+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/cache")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ }+}diff --git a/crates/component-macro/tests/expanded/implements-unstable_concurrent.rs b/crates/component-macro/tests/expanded/implements-unstable_concurrent.rsnew file mode 100644index 0000000000..7843f35b85--- /dev/null+++ b/crates/component-macro/tests/expanded/implements-unstable_concurrent.rs@@ -0,0 +1,319 @@+/// Link-time configurations.+#[derive(Clone, Debug, Default)]+pub struct LinkOptions {+ pub(crate) experimental: bool,+}+impl LinkOptions {+ /// Enable members marked as `@unstable(feature = experimental)`+ pub fn experimental(&mut self, enabled: bool) -> &mut Self {+ self.experimental = enabled;+ self+ }+}+/// Auto-generated bindings for a pre-instantiated version of a+/// component which implements the world `w`.+///+/// This structure is created through [`WPre::new`] which+/// takes a [`InstancePre`](wasmtime::component::InstancePre) that+/// has been created through a [`Linker`](wasmtime::component::Linker).+///+/// For more information see [`W`] as well.+pub struct WPre<T: 'static> {+ instance_pre: wasmtime::component::InstancePre<T>,+ indices: WIndices,+}+impl<T: 'static> Clone for WPre<T> {+ fn clone(&self) -> Self {+ Self {+ instance_pre: self.instance_pre.clone(),+ indices: self.indices.clone(),+ }+ }+}+impl<_T: 'static> WPre<_T> {+ /// Creates a new copy of `WPre` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component behind `instance_pre`+ /// does not have the required exports.+ pub fn new(+ instance_pre: wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let indices = WIndices::new(&instance_pre)?;+ Ok(Self { instance_pre, indices })+ }+ pub fn engine(&self) -> &wasmtime::Engine {+ self.instance_pre.engine()+ }+ pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {+ &self.instance_pre+ }+ /// Instantiates a new instance of [`W`] within the+ /// `store` provided.+ ///+ /// This function will use `self` as the pre-instantiated+ /// instance to perform instantiation. Afterwards the preloaded+ /// indices in `self` are used to lookup all exports on the+ /// resulting instance.+ pub fn instantiate(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<W> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate(&mut store)?;+ self.indices.load(&mut store, &instance)+ }+}+impl<_T: Send + 'static> WPre<_T> {+ /// Same as [`Self::instantiate`], except with `async`.+ pub async fn instantiate_async(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<W> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate_async(&mut store).await?;+ self.indices.load(&mut store, &instance)+ }+}+/// Auto-generated bindings for index of the exports of+/// `w`.+///+/// This is an implementation detail of [`WPre`] and can+/// be constructed if needed as well.+///+/// For more information see [`W`] as well.+#[derive(Clone)]+pub struct WIndices {}+/// Auto-generated bindings for an instance a component which+/// implements the world `w`.+///+/// This structure can be created through a number of means+/// depending on your requirements and what you have on hand:+///+/// * The most convenient way is to use+/// [`W::instantiate`] which only needs a+/// [`Store`], [`Component`], and [`Linker`].+///+/// * Alternatively you can create a [`WPre`] ahead of+/// time with a [`Component`] to front-load string lookups+/// of exports once instead of per-instantiation. This+/// method then uses [`WPre::instantiate`] to+/// create a [`W`].+///+/// * If you've instantiated the instance yourself already+/// then you can use [`W::new`].+///+/// These methods are all equivalent to one another and move+/// around the tradeoff of what work is performed when.+///+/// [`Store`]: wasmtime::Store+/// [`Component`]: wasmtime::component::Component+/// [`Linker`]: wasmtime::component::Linker+pub struct W {}+const _: () = {+ impl WIndices {+ /// Creates a new copy of `WIndices` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component does not have the+ /// required exports.+ pub fn new<_T>(+ _instance_pre: &wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let _component = _instance_pre.component();+ let _instance_type = _instance_pre.instance_type();+ Ok(WIndices {})+ }+ /// Uses the indices stored in `self` to load an instance+ /// of [`W`] from the instance provided.+ ///+ /// Note that at this time this method will additionally+ /// perform type-checks of all exports.+ pub fn load(+ &self,+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<W> {+ let _ = &mut store;+ let _instance = instance;+ Ok(W {})+ }+ }+ impl W {+ /// Convenience wrapper around [`WPre::new`] and+ /// [`WPre::instantiate`].+ pub fn instantiate<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<W> {+ let pre = linker.instantiate_pre(component)?;+ WPre::new(pre)?.instantiate(store)+ }+ /// Convenience wrapper around [`WIndices::new`] and+ /// [`WIndices::load`].+ pub fn new(+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<W> {+ let indices = WIndices::new(&instance.instance_pre(&store))?;+ indices.load(&mut store, instance)+ }+ /// Convenience wrapper around [`WPre::new`] and+ /// [`WPre::instantiate_async`].+ pub async fn instantiate_async<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<W>+ where+ _T: Send,+ {+ let pre = linker.instantiate_pre(component)?;+ WPre::new(pre)?.instantiate_async(store).await+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ options: &LinkOptions,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: foo::foo::store::HostWithStore<T> + foo::foo::store::HostWithStore<T>+ + foo::foo::cache::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + Send,+ for<'a> D::Data<+ 'a,+ >: foo::foo::store::Host + foo::foo::store::Host + foo::foo::cache::Host+ + foo::foo::cache::Host + Send,+ T: 'static + Send,+ {+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("a")?, host_getter)?;+ if options.experimental {+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("b")?, host_getter)?;+ }+ if options.experimental {+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("c")?, host_getter)?;+ }+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("d")?, host_getter)?;+ Ok(())+ }+ }+};+pub mod foo {+ pub mod foo {+ #[allow(clippy::all)]+ pub mod store {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {+ fn get(+ host: wasmtime::component::Access<T, Self>,+ ) -> impl ::core::future::Future<Output = u32> + Send;+ }+ pub trait Host: Send {}+ impl<_T: Host + ?Sized + Send> Host for &mut _T {}+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ wasmtime::component::__internal::Box::new(async move {+ let access_cx = wasmtime::AsContextMut::as_context_mut(+ &mut caller,+ );+ let host = wasmtime::component::Access::<+ T,+ D,+ >::new(access_cx, host_getter);+ let r = <D as HostWithStore<T>>::get(host).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/store")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod cache {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {+ fn get(+ host: wasmtime::component::Access<T, Self>,+ ) -> impl ::core::future::Future<Output = u32> + Send;+ }+ pub trait Host: Send {}+ impl<_T: Host + ?Sized + Send> Host for &mut _T {}+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ wasmtime::component::__internal::Box::new(async move {+ let access_cx = wasmtime::AsContextMut::as_context_mut(+ &mut caller,+ );+ let host = wasmtime::component::Access::<+ T,+ D,+ >::new(access_cx, host_getter);+ let r = <D as HostWithStore<T>>::get(host).await;+ Ok((r,))+ })+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/cache")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ }+}diff --git a/crates/component-macro/tests/expanded/implements-unstable_tracing_async.rs b/crates/component-macro/tests/expanded/implements-unstable_tracing_async.rsnew file mode 100644index 0000000000..e8e3ff73c8--- /dev/null+++ b/crates/component-macro/tests/expanded/implements-unstable_tracing_async.rs@@ -0,0 +1,345 @@+/// Link-time configurations.+#[derive(Clone, Debug, Default)]+pub struct LinkOptions {+ pub(crate) experimental: bool,+}+impl LinkOptions {+ /// Enable members marked as `@unstable(feature = experimental)`+ pub fn experimental(&mut self, enabled: bool) -> &mut Self {+ self.experimental = enabled;+ self+ }+}+/// Auto-generated bindings for a pre-instantiated version of a+/// component which implements the world `w`.+///+/// This structure is created through [`WPre::new`] which+/// takes a [`InstancePre`](wasmtime::component::InstancePre) that+/// has been created through a [`Linker`](wasmtime::component::Linker).+///+/// For more information see [`W`] as well.+pub struct WPre<T: 'static> {+ instance_pre: wasmtime::component::InstancePre<T>,+ indices: WIndices,+}+impl<T: 'static> Clone for WPre<T> {+ fn clone(&self) -> Self {+ Self {+ instance_pre: self.instance_pre.clone(),+ indices: self.indices.clone(),+ }+ }+}+impl<_T: 'static> WPre<_T> {+ /// Creates a new copy of `WPre` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component behind `instance_pre`+ /// does not have the required exports.+ pub fn new(+ instance_pre: wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let indices = WIndices::new(&instance_pre)?;+ Ok(Self { instance_pre, indices })+ }+ pub fn engine(&self) -> &wasmtime::Engine {+ self.instance_pre.engine()+ }+ pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {+ &self.instance_pre+ }+ /// Instantiates a new instance of [`W`] within the+ /// `store` provided.+ ///+ /// This function will use `self` as the pre-instantiated+ /// instance to perform instantiation. Afterwards the preloaded+ /// indices in `self` are used to lookup all exports on the+ /// resulting instance.+ pub fn instantiate(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<W> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate(&mut store)?;+ self.indices.load(&mut store, &instance)+ }+}+impl<_T: Send + 'static> WPre<_T> {+ /// Same as [`Self::instantiate`], except with `async`.+ pub async fn instantiate_async(+ &self,+ mut store: impl wasmtime::AsContextMut<Data = _T>,+ ) -> wasmtime::Result<W> {+ let mut store = store.as_context_mut();+ let instance = self.instance_pre.instantiate_async(&mut store).await?;+ self.indices.load(&mut store, &instance)+ }+}+/// Auto-generated bindings for index of the exports of+/// `w`.+///+/// This is an implementation detail of [`WPre`] and can+/// be constructed if needed as well.+///+/// For more information see [`W`] as well.+#[derive(Clone)]+pub struct WIndices {}+/// Auto-generated bindings for an instance a component which+/// implements the world `w`.+///+/// This structure can be created through a number of means+/// depending on your requirements and what you have on hand:+///+/// * The most convenient way is to use+/// [`W::instantiate`] which only needs a+/// [`Store`], [`Component`], and [`Linker`].+///+/// * Alternatively you can create a [`WPre`] ahead of+/// time with a [`Component`] to front-load string lookups+/// of exports once instead of per-instantiation. This+/// method then uses [`WPre::instantiate`] to+/// create a [`W`].+///+/// * If you've instantiated the instance yourself already+/// then you can use [`W::new`].+///+/// These methods are all equivalent to one another and move+/// around the tradeoff of what work is performed when.+///+/// [`Store`]: wasmtime::Store+/// [`Component`]: wasmtime::component::Component+/// [`Linker`]: wasmtime::component::Linker+pub struct W {}+const _: () = {+ impl WIndices {+ /// Creates a new copy of `WIndices` bindings which can then+ /// be used to instantiate into a particular store.+ ///+ /// This method may fail if the component does not have the+ /// required exports.+ pub fn new<_T>(+ _instance_pre: &wasmtime::component::InstancePre<_T>,+ ) -> wasmtime::Result<Self> {+ let _component = _instance_pre.component();+ let _instance_type = _instance_pre.instance_type();+ Ok(WIndices {})+ }+ /// Uses the indices stored in `self` to load an instance+ /// of [`W`] from the instance provided.+ ///+ /// Note that at this time this method will additionally+ /// perform type-checks of all exports.+ pub fn load(+ &self,+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<W> {+ let _ = &mut store;+ let _instance = instance;+ Ok(W {})+ }+ }+ impl W {+ /// Convenience wrapper around [`WPre::new`] and+ /// [`WPre::instantiate`].+ pub fn instantiate<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<W> {+ let pre = linker.instantiate_pre(component)?;+ WPre::new(pre)?.instantiate(store)+ }+ /// Convenience wrapper around [`WIndices::new`] and+ /// [`WIndices::load`].+ pub fn new(+ mut store: impl wasmtime::AsContextMut,+ instance: &wasmtime::component::Instance,+ ) -> wasmtime::Result<W> {+ let indices = WIndices::new(&instance.instance_pre(&store))?;+ indices.load(&mut store, instance)+ }+ /// Convenience wrapper around [`WPre::new`] and+ /// [`WPre::instantiate_async`].+ pub async fn instantiate_async<_T>(+ store: impl wasmtime::AsContextMut<Data = _T>,+ component: &wasmtime::component::Component,+ linker: &wasmtime::component::Linker<_T>,+ ) -> wasmtime::Result<W>+ where+ _T: Send,+ {+ let pre = linker.instantiate_pre(component)?;+ WPre::new(pre)?.instantiate_async(store).await+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ options: &LinkOptions,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: foo::foo::store::HostWithStore<T> + foo::foo::store::HostWithStore<T>+ + foo::foo::cache::HostWithStore<T> + foo::foo::cache::HostWithStore<T>+ + Send,+ for<'a> D::Data<+ 'a,+ >: foo::foo::store::Host + foo::foo::store::Host + foo::foo::cache::Host+ + foo::foo::cache::Host + Send,+ T: 'static + Send,+ {+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("a")?, host_getter)?;+ if options.experimental {+ foo::foo::store::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("b")?, host_getter)?;+ }+ if options.experimental {+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("c")?, host_getter)?;+ }+ foo::foo::cache::add_to_linker_instance::<+ T,+ D,+ >(&mut linker.instance("d")?, host_getter)?;+ Ok(())+ }+ }+};+pub mod foo {+ pub mod foo {+ #[allow(clippy::all)]+ pub mod store {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn get(&mut self) -> impl ::core::future::Future<Output = u32> + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn get(&mut self) -> impl ::core::future::Future<Output = u32> + Send {+ async move { Host::get(*self).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ use tracing::Instrument;+ let span = tracing::span!(+ tracing::Level::TRACE, "wit-bindgen import", module =+ "store", function = "get",+ );+ wasmtime::component::__internal::Box::new(+ async move {+ tracing::event!(tracing::Level::TRACE, "call");+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host).await;+ tracing::event!(+ tracing::Level::TRACE, result = tracing::field::debug(& r),+ "return"+ );+ Ok((r,))+ }+ .instrument(span),+ )+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/store")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ #[allow(clippy::all)]+ pub mod cache {+ #[allow(unused_imports)]+ use wasmtime::component::__internal::Box;+ pub trait HostWithStore<T>: wasmtime::component::HasData + Send {}+ impl<H: ?Sized, T> HostWithStore<T> for H+ where+ H: wasmtime::component::HasData + Send,+ {}+ pub trait Host: Send {+ fn get(&mut self) -> impl ::core::future::Future<Output = u32> + Send;+ }+ impl<_T: Host + ?Sized + Send> Host for &mut _T {+ fn get(&mut self) -> impl ::core::future::Future<Output = u32> + Send {+ async move { Host::get(*self).await }+ }+ }+ pub fn add_to_linker_instance<T, D>(+ inst: &mut wasmtime::component::LinkerInstance<'_, T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ inst.func_wrap_async(+ "get",+ move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {+ use tracing::Instrument;+ let span = tracing::span!(+ tracing::Level::TRACE, "wit-bindgen import", module =+ "cache", function = "get",+ );+ wasmtime::component::__internal::Box::new(+ async move {+ tracing::event!(tracing::Level::TRACE, "call");+ let host = &mut host_getter(caller.data_mut());+ let r = Host::get(host).await;+ tracing::event!(+ tracing::Level::TRACE, result = tracing::field::debug(& r),+ "return"+ );+ Ok((r,))+ }+ .instrument(span),+ )+ },+ )?;+ Ok(())+ }+ pub fn add_to_linker<T, D>(+ linker: &mut wasmtime::component::Linker<T>,+ host_getter: fn(&mut T) -> D::Data<'_>,+ ) -> wasmtime::Result<()>+ where+ D: HostWithStore<T>,+ for<'a> D::Data<'a>: Host,+ T: 'static + Send,+ {+ let mut inst = linker.instance("foo:foo/cache")?;+ add_to_linker_instance::<T, D>(&mut inst, host_getter)+ }+ }+ }+}diff --git a/crates/wasmtime/src/runtime/component/mod.rs b/crates/wasmtime/src/runtime/component/mod.rsindex b1fef26809..c0a0dfa20d 100644--- a/crates/wasmtime/src/runtime/component/mod.rs+++ b/crates/wasmtime/src/runtime/component/mod.rs@@ -481,6 +481,10 @@ pub(crate) use self::store::ComponentStoreData;/// ///// // The id type must be `Clone + Send + Sync + 'static`./// //+/// // As with `with`, a key without a version applies to every version of+/// // the interface imported by the world, and a key with a version takes+/// // precedence over one without.+/// ///// // Without this option the host can only load components whose/// // imports match the shape of the world given to `bindgen!`. With it,/// // the host can also load components which import the listeddiff --git a/crates/wit-bindgen/src/lib.rs b/crates/wit-bindgen/src/lib.rsindex c11f3c3ed3..4be6ee0581 100644--- a/crates/wit-bindgen/src/lib.rs+++ b/crates/wit-bindgen/src/lib.rs@@ -71,7 +71,10 @@ struct Wasmtime {import_interfaces: IndexMap<InterfaceId, ImportInterface>,world_import_functions: Vec<Function>,world_implements_interfaces: Vec<(String, InterfaceId)>,- interfaces_for_implements: HashMap<InterfaceId, InterfaceId>,+ /// For each named interface imported by the world, the import whose+ /// bindings are shared by all of the world's imports of the interface, and+ /// whether that's a plain (rather than labelled) import.+ interfaces_for_implements: HashMap<InterfaceId, (InterfaceId, bool)>,exports: Exports,types: Types,sizes: SizeAlign,@@ -437,6 +440,32 @@ impl Wasmtime {}let world = &resolve.worlds[id];++ // All imports of a named interface, plain or labelled (`import a: foo;`),+ // share one set of bindings: those of the plain import if there is+ // one, and otherwise those of the first labelled import. Choose these+ // up front, since a plain import may come after the labelled ones.+ for (name, import) in world.imports.iter() {+ let WorldItem::Interface { id, .. } = import else {+ continue;+ };+ if resolve.interfaces[*id].name.is_none() {+ continue;+ }+ let og_interface = resolve.interfaces[*id].clone_of.unwrap_or(*id);+ match name {+ WorldKey::Interface(_) => {+ self.interfaces_for_implements+ .insert(og_interface, (*id, true));+ }+ WorldKey::Name(_) => {+ self.interfaces_for_implements+ .entry(og_interface)+ .or_insert((*id, false));+ }+ }+ }+for (name, import) in world.imports.iter() {if !self.opts.only_interfaces || matches!(import, WorldItem::Interface { .. }) {self.import(resolve, name, import)?;@@ -462,24 +491,62 @@ impl Wasmtime {/// These are emitted underneath a top-level `named_imports` module by/// `finish`.fn generate_named_imports(&mut self, resolve: &Resolve) -> anyhow::Result<()> {- // For all named imports see what interface that lines up with in the- // `Resolve` which will have bindings generated.- 'outer: for (interface_name, id_type) in self.opts.named_imports.clone() {- for (id, _iface) in resolve.interfaces.iter() {- for (key, projection) in lookup_keys(- resolve,- &WorldKey::Interface(id),- LookupItem::InterfaceNoPop,- ) {- assert!(projection.is_empty());- if key == interface_name {- self.generate_named_import(resolve, id, &id_type)?;- continue 'outer;- }+ // Like `with`, each imported interface uses the most specific key that+ // matches it, so a key without a version applies to every version of+ // the interface imported by the world.+ let mut used_keys = HashSet::new();+ let mut seen = HashSet::new();+ let imports = self.import_interfaces.keys().copied().collect::<Vec<_>>();+ for id in imports {+ let iface = &resolve.interfaces[id];+ // Skip anonymous interfaces, which can't be named by a key. Also,+ // wit-parser gives each labelled import (`import a: foo;`) its own+ // copy of the interface, with `clone_of` pointing at the original,+ // but named-import bindings are generated once per interface, so+ // skip copies of an interface that's already been handled.+ if iface.name.is_none() || !seen.insert(iface.clone_of.unwrap_or(id)) {+ continue;+ }+ let mut id_type = None;+ for (key, projection) in lookup_keys(+ resolve,+ &WorldKey::Interface(id),+ LookupItem::InterfaceNoPop,+ ) {+ assert!(projection.is_empty());+ if let Some(ty) = self.opts.named_imports.get(&key) {+ id_type = Some(ty.clone());+ used_keys.insert(key);+ break;}}+ if let Some(id_type) = id_type {+ self.generate_named_import(resolve, id, &id_type)?;+ }+ }- bail!("named imports key {interface_name:?} not found")+ let mut unused_keys = self+ .opts+ .named_imports+ .keys()+ .filter(|key| !used_keys.contains(*key))+ .collect::<Vec<_>>();+ unused_keys.sort();+ if let Some(key) = unused_keys.first() {+ let exists = resolve.interfaces.iter().any(|(id, iface)| {+ iface.name.is_some()+ && lookup_keys(+ resolve,+ &WorldKey::Interface(id),+ LookupItem::InterfaceNoPop,+ )+ .iter()+ .any(|(k, _)| k == *key)+ });+ if exists {+ bail!("named imports key {key:?} is not imported by the world")+ }+ bail!("named imports key {key:?} not found")}Ok(())@@ -521,14 +588,12 @@ impl Wasmtime {&& resolve.interfaces[*id].name.is_some(){let og_interface = resolve.interfaces[*id].clone_of.unwrap_or(*id);- let implements = match self.interfaces_for_implements.get(&og_interface) {- Some(id) => *id,- None => {- self.interfaces_for_implements.insert(og_interface, *id);- self.import_interface(resolve, &WorldKey::Interface(*id), *id, false)?;- *id- }- };+ let (implements, plain) = self.interfaces_for_implements[&og_interface];+ // Bindings shared with a plain import are generated for+ // that import instead.+ if !plain && implements == *id {+ self.import_interface(resolve, &WorldKey::Interface(*id), *id, false)?;+ }self.world_implements_interfaces.push((kebab.to_string(), implements));} else {@@ -1647,17 +1712,19 @@ impl Wasmtime {""};- let import_stability = resolve.worlds[world]- .imports- .iter()- .filter_map(|(_, i)| match i {- WorldItem::Interface { id, stability, .. } if *id == interface_id => {- Some(stability.clone())- }- _ => None,- })- .next()- .unwrap_or(Stability::Unknown);+ let imports = &resolve.worlds[world].imports;+ let import = match &name_override {+ // Labelled imports of the same interface share an+ // `interface_id`, so find these by their label instead.+ Some(name) => imports.get(&WorldKey::Name(name.clone())),+ None => imports+ .values()+ .find(|i| matches!(i, WorldItem::Interface { id, .. } if *id == interface_id)),+ };+ let import_stability = match import {+ Some(WorldItem::Interface { stability, .. }) => stability.clone(),+ _ => Stability::Unknown,+ };let gate = FeatureGate::open(&mut self.src, &import_stability);match &name_override {diff --git a/tests/all/component_model/bindgen.rs b/tests/all/component_model/bindgen.rsindex 81cad90e9e..673b678ef5 100644--- a/tests/all/component_model/bindgen.rs+++ b/tests/all/component_model/bindgen.rs@@ -909,6 +909,90 @@ mod unstable_import {}}+mod implements_unstable {+ use super::*;+ use wasmtime::component::HasSelf;++ wasmtime::component::bindgen!({+ inline: "+ package demo:pkg;++ interface store {+ get: func() -> u32;+ }++ interface cache {+ get: func() -> u32;+ }++ world w {+ import a: store;+ @unstable(feature = experimental)+ import b: store;++ @unstable(feature = experimental)+ import c: cache;+ import d: cache;+ }+ ",+ });++ struct MyHost;++ impl demo::pkg::store::Host for MyHost {+ fn get(&mut self) -> u32 {+ 0+ }+ }++ impl demo::pkg::cache::Host for MyHost {+ fn get(&mut self) -> u32 {+ 0+ }+ }++ /// Returns whether a component importing just `name`, implementing+ /// `interface`, can be instantiated with `options`.+ fn links(options: &LinkOptions, name: &str, interface: &str) -> Result<bool> {+ let mut config = Config::new();+ config.wasm_component_model(true);+ config.wasm_component_model_implements(true);+ let engine = Engine::new(&config)?;+ let component = Component::new(+ &engine,+ format!(+ r#"+ (component+ (import "{name}" (implements "{interface}") (instance+ (export "get" (func (result u32)))+ ))+ )+ "#+ ),+ )?;+ let mut linker = Linker::<MyHost>::new(&engine);+ W::add_to_linker::<_, HasSelf<_>>(&mut linker, options, |h| h)?;+ Ok(linker.instantiate_pre(&component).is_ok())+ }++ #[test]+ fn labels_have_their_own_feature_gates() -> Result<()> {+ let default = LinkOptions::default();+ assert!(links(&default, "a", "demo:pkg/store")?);+ assert!(!links(&default, "b", "demo:pkg/store")?);+ assert!(!links(&default, "c", "demo:pkg/cache")?);+ assert!(links(&default, "d", "demo:pkg/cache")?);++ let mut experimental = LinkOptions::default();+ experimental.experimental(true);+ assert!(links(&experimental, "a", "demo:pkg/store")?);+ assert!(links(&experimental, "b", "demo:pkg/store")?);+ assert!(links(&experimental, "c", "demo:pkg/cache")?);+ assert!(links(&experimental, "d", "demo:pkg/cache")?);+ Ok(())+ }+}+mod anyhow_errors {use super::*;use crate::ErrorExt;@@ -1143,6 +1227,118 @@ mod implements {Ok(())}}++mod implements_plain_and_labelled {+ use super::*;+ use wasmtime::component::HasSelf;++ wasmtime::component::bindgen!({+ inline: "+ package demo:pkg;++ interface store {+ get: func() -> u32;+ }++ world w {+ import store;+ import a: store;++ export get-plain: func() -> u32;+ export get-a: func() -> u32;+ }+ ",+ });++ // A component importing `demo:pkg/store` both plainly and under the label+ // `a`, exporting a function which forwards to each import.+ const COMPONENT: &str = r#"+ (component+ (import "demo:pkg/store" (instance $plain+ (export "get" (func (result u32)))+ ))+ (import "a" (implements "demo:pkg/store") (instance $a+ (export "get" (func (result u32)))+ ))++ (core func $plain-get (canon lower (func $plain "get")))+ (core func $a-get (canon lower (func $a "get")))++ (core module $m+ (import "" "plain-get" (func $plain-get (result i32)))+ (import "" "a-get" (func $a-get (result i32)))+ (func (export "get-plain") (result i32) call $plain-get)+ (func (export "get-a") (result i32) call $a-get)+ )+ (core instance $i (instantiate $m+ (with "" (instance+ (export "plain-get" (func $plain-get))+ (export "a-get" (func $a-get))+ ))+ ))++ (func (export "get-plain") (result u32) (canon lift (core func $i "get-plain")))+ (func (export "get-a") (result u32) (canon lift (core func $i "get-a")))+ )+ "#;++ fn implements_engine() -> Engine {+ let mut config = Config::new();+ config.wasm_component_model(true);+ config.wasm_component_model_implements(true);+ Engine::new(&config).unwrap()+ }++ #[derive(Default)]+ struct Counter(u32);++ impl demo::pkg::store::Host for Counter {+ fn get(&mut self) -> u32 {+ self.0 += 1;+ self.0+ }+ }++ #[test]+ fn world_add_to_linker_links_both() -> Result<()> {+ let engine = implements_engine();+ let component = Component::new(&engine, COMPONENT)?;+ let mut linker = Linker::new(&engine);+ W::add_to_linker::<_, HasSelf<Counter>>(&mut linker, |s| s)?;+ let mut store = Store::new(&engine, Counter::default());+ let w = W::instantiate(&mut store, &component, &linker)?;+ assert_eq!(w.call_get_plain(&mut store)?, 1);+ assert_eq!(w.call_get_a(&mut store)?, 2);+ Ok(())+ }++ #[test]+ fn imports_can_be_linked_separately() -> Result<()> {+ #[derive(Default)]+ struct MyHost {+ plain: Counter,+ a: Counter,+ }++ let engine = implements_engine();+ let component = Component::new(&engine, COMPONENT)?;+ let mut linker = Linker::new(&engine);+ demo::pkg::store::add_to_linker::<MyHost, HasSelf<Counter>>(&mut linker, |s| &mut s.plain)?;+ demo::pkg::store::add_to_linker_instance::<MyHost, HasSelf<Counter>>(+ &mut linker.instance("a")?,+ |s| &mut s.a,+ )?;+ let mut store = Store::new(&engine, MyHost::default());+ let w = W::instantiate(&mut store, &component, &linker)?;+ assert_eq!(w.call_get_plain(&mut store)?, 1);+ assert_eq!(w.call_get_plain(&mut store)?, 2);+ assert_eq!(w.call_get_a(&mut store)?, 1);+ assert_eq!(store.data().plain.0, 2);+ assert_eq!(store.data().a.0, 1);+ Ok(())+ }+}+mod named_imports {use super::*;use std::collections::HashMap;