From 79741982ebc93824123e1ce9b3e0972fe50cfcff Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 8 May 2025 13:46:28 -0700 Subject: [PATCH 1/2] Replace `GetHost` with a function pointer, add `HasData` This commit is a refactoring to the fundamentals of the `bindgen!` macro and the functions that it generates. Prior to this change the fundamental entrypoint generated by `bindgen!` was a function `add_to_linker_get_host` which takes a value of type `G: GetHost`. This `GetHost` implementation is effectively an alias for a closure whose return value is able to close over the parameter given lfietime-wise. The `GetHost` abstraction was added to Wasmtime originally to enable using any type that implements `Host` traits, not just `&mut U` as was originally supported. The definition of `GetHost` was _just_ right to enable a type such as `MyThing<&mut T>` to implement `Host` and a closure could be provided that could return it. At the time that `GetHost` was added it was known to be problematic from an understandability point of view, namely: * It has a non-obvious definition. * It's pretty advanced Rust voodoo to understand what it's actually doing * Using `GetHost` required lots of `for<'a> ...` in places which is unfamiliar syntax for many. * `GetHost` values couldn't be type-erased (e.g. put in a trait object) as we couldn't figure out the lifetime syntax to do so. Despite these issues it was the only known solution at hand so we landed it and kept the previous `add_to_linker` style (`&mut T -> &mut U`) as a convenience. While this has worked reasonable well (most folks just try to not look at `GetHost`) it has reached a breaking point in the WASIp3 work. In the WASIp3 work it's effectively now going to be required that the `G: GetHost` value is packaged up and actually stored inside of accessors provided to host functions. This means that `GetHost` values now need to not only be taken in `add_to_linker` but additionally provided to the rest of the system through an "accessor". This was made possible in #10746 by moving the `GetHost` type into Wasmtime itself (as opposed to generated code where it lived prior). While this worked with WASIp3 and it was possible to plumb `G: GetHost` safely around, this ended up surfacing more issues. Namely all "concurrent" host functions started getting significantly more complicated `where` clauses and type signatures. At the end of the day I felt that we had reached the end of the road to `GetHost` and wanted to search for alternatives, hence this change. The fundamental purpose of `GetHost` was to be able to express, in a generic fashion: * Give me a closure that takes `&mut T` and returns `D`. * The `D` type can close over the lifetime in `&mut T`. * The `D` type must implement `bindgen!`-generated traits. A realization I had was that we could model this with a generic associated type in Rust. Rust support for generic associated types is relatively new and not something I've used much before, but it ended up being a perfect model for this. The definition of the new `HasData` trait is deceptively simple: trait HasData { type Data<'a>; } What this enables us to do though is to generate `add_to_linker` functions that look like this: fn add_to_linker( linker: &mut Linker, getter: fn(&mut T) -> D::Data<'_>, ) -> Result<()> where D: HasData, for<'a> D::Data<'a>: Host; This definition here models `G: GetHost` as a literal function pointer, and the ability to close over the `&mut T` lifetime with type (not just `&mut U`) is expressed through the type constructor `type Data<'a>`). Ideally we could take a generic generic associated type (I'm not even sure what to call that), but that's not something Rust has today. Overall this felt like a much simpler way of modeling `GetHost` and its requirements. This plumbed well throughout the WASIp3 work and the signatures for concurrent functions felt much more appropriate in terms of complexity after this change. Taking this change to the limit means that `GetHost` in its entirety could be purged since all usages of it could be replaced with `fn(&mut T) -> D::Data<'a>`, a hopefully much more understandable type. This change is not all rainbows however, there are some gotchas that remain: * One is that all `add_to_linker` generated functions have a `D: HasData` type parameter. This type parameter cannot be inferred and must always be explicitly specified, and it's not easy to know what to supply here without reading documentation. Actually supplying the type parameter is quite easy once you know what to do (and what to fill in), but it may involve defining a small struct with a custom `HasData` implementation which can be non-obvious. * Another is that the `G: GetHost` value was previously a full Rust closure, but now it's transitioning to a function pointer. This is done in preparation for WASIp3 work where the function needs to be passed around, and doing that behind a generic parameter is more effort than it's worth. This means that embedders relying on the true closure-like nature here will have to update to using a function pointer instead. * The function pointer is stored in locations that require `'static`, and while `fn(T)` might be expected to be `'static` regardless of `T` is is, in fact, not. This means that practically `add_to_linker` requires `T: 'static`. Relative to just before this change this is a possible regression in functionality, but there orthogonal reasons beyond just this that we want to start requiring `T: 'static` anyway. That means that this isn't actually a regression relative to #10760, a related change. The first point is partially ameliorated with WASIp3 work insofar that the `D` type parameter will start serving as a location to specify where concurrent implementations are found. These concurrent methods don't take `&mut self` but instead are implemented for `T: HasData` types. In that sense it's more justified to have this weird type parameter, but in the meantime without this support it'll feel a bit odd to have this little type parameter hanging off the side. This change has been integrated into the WASIp3 prototyping repository with success. This has additionally been integrated into the Spin embedding which has one of the more complicated reliances on `*_get_host` functions known. Given that it's expected that while this is not necessarily a trivial change to rebase over it should at least be possible. Finally the `HasData` trait here has been included with what I'm hoping is a sufficient amount of documentation to at least give folks a spring board to understand it. If folks have confusion about this `D` type parameter my hope is they'll make their way to `HasData` which showcases various patterns for "librarifying" host implementations of WIT interfaces. These patterns are all used throughout Wasmtime and WASI currently in crates and tests and such. --- Cargo.lock | 1 + crates/component-macro/tests/codegen.rs | 17 +- crates/component-macro/tests/expanded.rs | 15 +- crates/wasi-config/src/lib.rs | 13 +- crates/wasi-http/Cargo.toml | 1 + crates/wasi-http/src/lib.rs | 96 ++---- crates/wasi-io/src/lib.rs | 22 +- crates/wasi-keyvalue/src/lib.rs | 18 +- crates/wasi-nn/src/wit.rs | 20 +- crates/wasi-tls/src/lib.rs | 14 +- crates/wasi/src/p2/bindings.rs | 8 +- crates/wasi/src/p2/mod.rs | 192 ++++++----- .../runtime/component/bindgen_examples/mod.rs | 18 +- .../src/runtime/component/get_host.rs | 255 --------------- .../src/runtime/component/has_data.rs | 305 ++++++++++++++++++ crates/wasmtime/src/runtime/component/mod.rs | 4 +- crates/wit-bindgen/src/lib.rs | 146 ++++----- examples/component/main.rs | 21 +- examples/min-platform/embedding/src/wasi.rs | 22 +- tests/all/component_model/bindgen.rs | 18 +- tests/all/component_model/bindgen/results.rs | 23 +- 21 files changed, 646 insertions(+), 583 deletions(-) delete mode 100644 crates/wasmtime/src/runtime/component/get_host.rs create mode 100644 crates/wasmtime/src/runtime/component/has_data.rs diff --git a/Cargo.lock b/Cargo.lock index fb70128d3c27..157cda3b360d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4659,6 +4659,7 @@ dependencies = [ "tracing-subscriber", "wasmtime", "wasmtime-wasi", + "wasmtime-wasi-io", "webpki-roots", ] diff --git a/crates/component-macro/tests/codegen.rs b/crates/component-macro/tests/codegen.rs index 0846ff8c7cf1..67fa758f6b72 100644 --- a/crates/component-macro/tests/codegen.rs +++ b/crates/component-macro/tests/codegen.rs @@ -12,14 +12,15 @@ macro_rules! gentest { async: true, }); } - mod concurrent { - wasmtime::component::bindgen!({ - path: $path, - async: true, - concurrent_imports: true, - concurrent_exports: true, - }); - } + // TODO: re-enable this when wasip3 is merged back into this repo + // mod concurrent { + // wasmtime::component::bindgen!({ + // path: $path, + // async: true, + // concurrent_imports: true, + // concurrent_exports: true, + // }); + // } mod tracing { wasmtime::component::bindgen!({ path: $path, diff --git a/crates/component-macro/tests/expanded.rs b/crates/component-macro/tests/expanded.rs index 216e4cb47434..7ca66796ac9b 100644 --- a/crates/component-macro/tests/expanded.rs +++ b/crates/component-macro/tests/expanded.rs @@ -15,13 +15,14 @@ macro_rules! genexpand { stringify: true, }))?; - process_expanded($path, "_concurrent", wasmtime::component::bindgen!({ - path: $path, - async: true, - concurrent_imports: true, - concurrent_exports: true, - stringify: true, - }))?; + // TODO: re-enable this when wasip3 is merged back into this repo + // process_expanded($path, "_concurrent", wasmtime::component::bindgen!({ + // path: $path, + // async: true, + // concurrent_imports: true, + // concurrent_exports: true, + // stringify: true, + // }))?; process_expanded($path, "_tracing_async", wasmtime::component::bindgen!({ path: $path, diff --git a/crates/wasi-config/src/lib.rs b/crates/wasi-config/src/lib.rs index 47994a3254e5..a7161d850153 100644 --- a/crates/wasi-config/src/lib.rs +++ b/crates/wasi-config/src/lib.rs @@ -69,6 +69,7 @@ use anyhow::Result; use std::collections::HashMap; +use wasmtime::component::HasData; mod gen_ { wasmtime::component::bindgen!({ @@ -140,10 +141,16 @@ impl generated::Host for WasiConfig<'_> { } /// Add all the `wasi-config` world's interfaces to a [`wasmtime::component::Linker`]. -pub fn add_to_linker( +pub fn add_to_linker( l: &mut wasmtime::component::Linker, - f: impl Fn(&mut T) -> WasiConfig<'_> + Send + Sync + Copy + 'static, + f: fn(&mut T) -> WasiConfig<'_>, ) -> Result<()> { - generated::add_to_linker_get_host(l, f)?; + generated::add_to_linker::(l, f)?; Ok(()) } + +struct HasWasiConfig; + +impl HasData for HasWasiConfig { + type Data<'a> = WasiConfig<'a>; +} diff --git a/crates/wasi-http/Cargo.toml b/crates/wasi-http/Cargo.toml index 624e69773b80..3c88729458c5 100644 --- a/crates/wasi-http/Cargo.toml +++ b/crates/wasi-http/Cargo.toml @@ -27,6 +27,7 @@ http-body = { workspace = true } http-body-util = { workspace = true } tracing = { workspace = true } wasmtime-wasi = { workspace = true } +wasmtime-wasi-io = { workspace = true } wasmtime = { workspace = true, features = ['component-model'] } tokio-rustls = { workspace = true } rustls = { workspace = true } diff --git a/crates/wasi-http/src/lib.rs b/crates/wasi-http/src/lib.rs index aa54e2db5a24..218172a772ff 100644 --- a/crates/wasi-http/src/lib.rs +++ b/crates/wasi-http/src/lib.rs @@ -236,7 +236,9 @@ pub use crate::types::{ WasiHttpCtx, WasiHttpImpl, WasiHttpView, DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS, DEFAULT_OUTGOING_BODY_CHUNK_SIZE, }; +use wasmtime::component::{HasData, Linker}; use wasmtime_wasi::p2::IoImpl; + /// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`]. /// /// This function will add the `async` variant of all interfaces into the @@ -284,46 +286,12 @@ use wasmtime_wasi::p2::IoImpl; /// ``` pub fn add_to_linker_async(l: &mut wasmtime::component::Linker) -> anyhow::Result<()> where - T: WasiHttpView + wasmtime_wasi::p2::WasiView, + T: WasiHttpView + wasmtime_wasi::p2::WasiView + 'static, { - let io_closure = type_annotate_io::(|t| wasmtime_wasi::p2::IoImpl(t)); - wasmtime_wasi::p2::bindings::io::poll::add_to_linker_get_host(l, io_closure)?; - wasmtime_wasi::p2::bindings::io::error::add_to_linker_get_host(l, io_closure)?; - wasmtime_wasi::p2::bindings::io::streams::add_to_linker_get_host(l, io_closure)?; - - let closure = - type_annotate_wasi::(|t| wasmtime_wasi::p2::WasiImpl(wasmtime_wasi::p2::IoImpl(t))); - wasmtime_wasi::p2::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::cli::stdin::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::cli::stdout::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::cli::stderr::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::random::random::add_to_linker_get_host(l, closure)?; - + wasmtime_wasi::p2::add_to_linker_proxy_interfaces_async(l)?; add_only_http_to_linker_async(l) } -// NB: workaround some rustc inference - a future refactoring may make this -// obsolete. -fn type_annotate_http(val: F) -> F -where - F: Fn(&mut T) -> WasiHttpImpl<&mut T>, -{ - val -} -fn type_annotate_wasi(val: F) -> F -where - F: Fn(&mut T) -> wasmtime_wasi::p2::WasiImpl<&mut T>, -{ - val -} -fn type_annotate_io(val: F) -> F -where - F: Fn(&mut T) -> wasmtime_wasi::p2::IoImpl<&mut T>, -{ - val -} - /// A slimmed down version of [`add_to_linker_async`] which only adds /// `wasi:http` interfaces to the linker. /// @@ -333,15 +301,22 @@ pub fn add_only_http_to_linker_async( l: &mut wasmtime::component::Linker, ) -> anyhow::Result<()> where - T: WasiHttpView, + T: WasiHttpView + 'static, { - let closure = type_annotate_http::(|t| WasiHttpImpl(IoImpl(t))); - crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?; - crate::bindings::http::types::add_to_linker_get_host(l, closure)?; + crate::bindings::http::outgoing_handler::add_to_linker::<_, WasiHttp>(l, |x| { + WasiHttpImpl(IoImpl(x)) + })?; + crate::bindings::http::types::add_to_linker::<_, WasiHttp>(l, |x| WasiHttpImpl(IoImpl(x)))?; Ok(()) } +struct WasiHttp(T); + +impl HasData for WasiHttp { + type Data<'a> = WasiHttpImpl<&'a mut T>; +} + /// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`]. /// /// This function will add the `sync` variant of all interfaces into the @@ -382,31 +357,12 @@ where /// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx } /// } /// ``` -pub fn add_to_linker_sync(l: &mut wasmtime::component::Linker) -> anyhow::Result<()> +pub fn add_to_linker_sync(l: &mut Linker) -> anyhow::Result<()> where - T: WasiHttpView + wasmtime_wasi::p2::WasiView, + T: WasiHttpView + wasmtime_wasi::p2::WasiView + 'static, { - let io_closure = type_annotate_io::(|t| wasmtime_wasi::p2::IoImpl(t)); - // For the sync linker, use the definitions of poll and streams from the - // wasmtime_wasi::p2::bindings::sync space because those are defined using in_tokio. - wasmtime_wasi::p2::bindings::sync::io::poll::add_to_linker_get_host(l, io_closure)?; - wasmtime_wasi::p2::bindings::sync::io::streams::add_to_linker_get_host(l, io_closure)?; - // The error interface in the wasmtime_wasi is synchronous - wasmtime_wasi::p2::bindings::io::error::add_to_linker_get_host(l, io_closure)?; - - let closure = - type_annotate_wasi::(|t| wasmtime_wasi::p2::WasiImpl(wasmtime_wasi::p2::IoImpl(t))); - - wasmtime_wasi::p2::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::cli::stdin::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::cli::stdout::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::cli::stderr::add_to_linker_get_host(l, closure)?; - wasmtime_wasi::p2::bindings::random::random::add_to_linker_get_host(l, closure)?; - - add_only_http_to_linker_sync(l)?; - - Ok(()) + wasmtime_wasi::p2::add_to_linker_proxy_interfaces_sync(l)?; + add_only_http_to_linker_sync(l) } /// A slimmed down version of [`add_to_linker_sync`] which only adds @@ -414,14 +370,16 @@ where /// /// This is useful when using [`wasmtime_wasi::p2::add_to_linker_sync`] for /// example to avoid re-adding the same interfaces twice. -pub fn add_only_http_to_linker_sync(l: &mut wasmtime::component::Linker) -> anyhow::Result<()> +pub fn add_only_http_to_linker_sync(l: &mut Linker) -> anyhow::Result<()> where - T: WasiHttpView, + T: WasiHttpView + 'static, { - let closure = type_annotate_http::(|t| WasiHttpImpl(IoImpl(t))); - - crate::bindings::http::outgoing_handler::add_to_linker_get_host(l, closure)?; - crate::bindings::http::types::add_to_linker_get_host(l, closure)?; + crate::bindings::sync::http::outgoing_handler::add_to_linker::<_, WasiHttp>(l, |x| { + WasiHttpImpl(IoImpl(x)) + })?; + crate::bindings::sync::http::types::add_to_linker::<_, WasiHttp>(l, |x| { + WasiHttpImpl(IoImpl(x)) + })?; Ok(()) } diff --git a/crates/wasi-io/src/lib.rs b/crates/wasi-io/src/lib.rs index dfab99894d0b..a88375f79503 100644 --- a/crates/wasi-io/src/lib.rs +++ b/crates/wasi-io/src/lib.rs @@ -36,7 +36,7 @@ pub use async_trait::async_trait; pub use ::bytes; use alloc::boxed::Box; -use wasmtime::component::ResourceTable; +use wasmtime::component::{HasData, ResourceTable}; /// A trait which provides access to the [`ResourceTable`] inside the /// embedder's `T` of [`Store`][`Store`]. @@ -168,21 +168,17 @@ impl IoView for IoImpl { /// fn table(&mut self) -> &mut ResourceTable { &mut self.table } /// } /// ``` -pub fn add_to_linker_async( +pub fn add_to_linker_async( l: &mut wasmtime::component::Linker, ) -> wasmtime::Result<()> { - let closure = io_type_annotate::(|t| IoImpl(t)); - crate::bindings::wasi::io::error::add_to_linker_get_host(l, closure)?; - crate::bindings::wasi::io::poll::add_to_linker_get_host(l, closure)?; - crate::bindings::wasi::io::streams::add_to_linker_get_host(l, closure)?; + crate::bindings::wasi::io::error::add_to_linker::>(l, |x| IoImpl(x))?; + crate::bindings::wasi::io::poll::add_to_linker::>(l, |x| IoImpl(x))?; + crate::bindings::wasi::io::streams::add_to_linker::>(l, |x| IoImpl(x))?; Ok(()) } -// NB: workaround some rustc inference - a future refactoring may make this -// obsolete. -fn io_type_annotate(val: F) -> F -where - F: Fn(&mut T) -> IoImpl<&mut T>, -{ - val +struct HasIo(T); + +impl HasData for HasIo { + type Data<'a> = IoImpl<&'a mut T>; } diff --git a/crates/wasi-keyvalue/src/lib.rs b/crates/wasi-keyvalue/src/lib.rs index d4d69cad51bd..9dab4b7a34c6 100644 --- a/crates/wasi-keyvalue/src/lib.rs +++ b/crates/wasi-keyvalue/src/lib.rs @@ -84,7 +84,7 @@ mod generated { use self::generated::wasi::keyvalue; use anyhow::Result; use std::collections::HashMap; -use wasmtime::component::{Resource, ResourceTable, ResourceTableError}; +use wasmtime::component::{HasData, Resource, ResourceTable, ResourceTableError}; #[doc(hidden)] pub enum Error { @@ -288,12 +288,18 @@ impl keyvalue::batch::Host for WasiKeyValue<'_> { } /// Add all the `wasi-keyvalue` world's interfaces to a [`wasmtime::component::Linker`]. -pub fn add_to_linker( +pub fn add_to_linker( l: &mut wasmtime::component::Linker, - f: impl Fn(&mut T) -> WasiKeyValue<'_> + Send + Sync + Copy + 'static, + f: fn(&mut T) -> WasiKeyValue<'_>, ) -> Result<()> { - keyvalue::store::add_to_linker_get_host(l, f)?; - keyvalue::atomics::add_to_linker_get_host(l, f)?; - keyvalue::batch::add_to_linker_get_host(l, f)?; + keyvalue::store::add_to_linker::<_, HasWasiKeyValue>(l, f)?; + keyvalue::atomics::add_to_linker::<_, HasWasiKeyValue>(l, f)?; + keyvalue::batch::add_to_linker::<_, HasWasiKeyValue>(l, f)?; Ok(()) } + +struct HasWasiKeyValue; + +impl HasData for HasWasiKeyValue { + type Data<'a> = WasiKeyValue<'a>; +} diff --git a/crates/wasi-nn/src/wit.rs b/crates/wasi-nn/src/wit.rs index 34d9cd789863..cb51542c5ee7 100644 --- a/crates/wasi-nn/src/wit.rs +++ b/crates/wasi-nn/src/wit.rs @@ -21,7 +21,7 @@ use anyhow::anyhow; use std::collections::HashMap; use std::hash::Hash; use std::{fmt, str::FromStr}; -use wasmtime::component::{Resource, ResourceTable}; +use wasmtime::component::{HasData, Resource, ResourceTable}; /// Capture the state necessary for calling into the backend ML libraries. pub struct WasiNnCtx { @@ -150,17 +150,23 @@ pub use generated_::Ml as ML; /// Add the WIT-based version of the `wasi-nn` API to a /// [`wasmtime::component::Linker`]. -pub fn add_to_linker( +pub fn add_to_linker( l: &mut wasmtime::component::Linker, - f: impl Fn(&mut T) -> WasiNnView<'_> + Send + Sync + Copy + 'static, + f: fn(&mut T) -> WasiNnView<'_>, ) -> anyhow::Result<()> { - generated::graph::add_to_linker_get_host(l, f)?; - generated::tensor::add_to_linker_get_host(l, f)?; - generated::inference::add_to_linker_get_host(l, f)?; - generated::errors::add_to_linker_get_host(l, f)?; + generated::graph::add_to_linker::<_, HasWasiNnView>(l, f)?; + generated::tensor::add_to_linker::<_, HasWasiNnView>(l, f)?; + generated::inference::add_to_linker::<_, HasWasiNnView>(l, f)?; + generated::errors::add_to_linker::<_, HasWasiNnView>(l, f)?; Ok(()) } +struct HasWasiNnView; + +impl HasData for HasWasiNnView { + type Data<'a> = WasiNnView<'a>; +} + impl generated::graph::Host for WasiNnView<'_> { fn load( &mut self, diff --git a/crates/wasi-tls/src/lib.rs b/crates/wasi-tls/src/lib.rs index 36fdb535d7ec..e43a3f207f73 100644 --- a/crates/wasi-tls/src/lib.rs +++ b/crates/wasi-tls/src/lib.rs @@ -81,7 +81,7 @@ use std::{future::Future, mem, pin::Pin, sync::LazyLock}; use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt}; use tokio::sync::Mutex; use tokio_rustls::client::TlsStream; -use wasmtime::component::{Resource, ResourceTable}; +use wasmtime::component::{HasData, Resource, ResourceTable}; use wasmtime_wasi::async_trait; use wasmtime_wasi::p2::bindings::io::{ error::Error as HostIoError, @@ -139,15 +139,21 @@ impl<'a> WasiTlsCtx<'a> { impl<'a> generated::types::Host for WasiTlsCtx<'a> {} /// Add the `wasi-tls` world's types to a [`wasmtime::component::Linker`]. -pub fn add_to_linker( +pub fn add_to_linker( l: &mut wasmtime::component::Linker, opts: &mut LinkOptions, - f: impl Fn(&mut T) -> WasiTlsCtx + Send + Sync + Copy + 'static, + f: fn(&mut T) -> WasiTlsCtx<'_>, ) -> Result<()> { - generated::types::add_to_linker_get_host(l, &opts, f)?; + generated::types::add_to_linker::<_, WasiTls>(l, &opts, f)?; Ok(()) } +struct WasiTls; + +impl HasData for WasiTls { + type Data<'a> = WasiTlsCtx<'a>; +} + enum TlsError { /// The component should trap. Under normal circumstances, this only occurs /// when the underlying transport stream returns [`StreamError::Trap`]. diff --git a/crates/wasi/src/p2/bindings.rs b/crates/wasi/src/p2/bindings.rs index f3d24e73057e..035e4c71a495 100644 --- a/crates/wasi/src/p2/bindings.rs +++ b/crates/wasi/src/p2/bindings.rs @@ -17,7 +17,7 @@ //! ```rust //! use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView}; //! use wasmtime::{Result, Engine, Config}; -//! use wasmtime::component::{Linker, ResourceTable}; +//! use wasmtime::component::{Linker, ResourceTable, HasSelf}; //! //! wasmtime::component::bindgen!({ //! inline: " @@ -66,7 +66,7 @@ //! let engine = Engine::new(&config)?; //! let mut linker: Linker = Linker::new(&engine); //! wasmtime_wasi::p2::add_to_linker_async(&mut linker)?; -//! example::wasi::custom_host::add_to_linker(&mut linker, |state| state)?; +//! example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?; //! //! // .. use `Linker` to instantiate component ... //! @@ -88,7 +88,7 @@ /// ```rust /// use wasmtime_wasi::p2::{IoView, WasiCtx, WasiView}; /// use wasmtime::{Result, Engine}; -/// use wasmtime::component::{Linker, ResourceTable}; +/// use wasmtime::component::{Linker, ResourceTable, HasSelf}; /// /// wasmtime::component::bindgen!({ /// inline: " @@ -137,7 +137,7 @@ /// let engine = Engine::default(); /// let mut linker: Linker = Linker::new(&engine); /// wasmtime_wasi::p2::add_to_linker_sync(&mut linker)?; -/// example::wasi::custom_host::add_to_linker(&mut linker, |state| state)?; +/// example::wasi::custom_host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?; /// /// // .. use `Linker` to instantiate component ... /// diff --git a/crates/wasi/src/p2/mod.rs b/crates/wasi/src/p2/mod.rs index 150350bd2da6..2ec9a9a822bd 100644 --- a/crates/wasi/src/p2/mod.rs +++ b/crates/wasi/src/p2/mod.rs @@ -226,7 +226,7 @@ //! [async]: https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.async_support //! [`ResourceTable`]: wasmtime::component::ResourceTable -use wasmtime::component::Linker; +use wasmtime::component::{HasData, Linker}; pub mod bindings; mod ctx; @@ -318,45 +318,96 @@ pub use wasmtime_wasi_io::{IoImpl, IoView}; /// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx } /// } /// ``` -pub fn add_to_linker_async(linker: &mut Linker) -> anyhow::Result<()> { - let options = crate::p2::bindings::LinkOptions::default(); +pub fn add_to_linker_async(linker: &mut Linker) -> anyhow::Result<()> { + let options = bindings::LinkOptions::default(); add_to_linker_with_options_async(linker, &options) } /// Similar to [`add_to_linker_async`], but with the ability to enable unstable features. -pub fn add_to_linker_with_options_async( +pub fn add_to_linker_with_options_async( linker: &mut Linker, - options: &crate::p2::bindings::LinkOptions, + options: &bindings::LinkOptions, ) -> anyhow::Result<()> { + wasmtime_wasi_io::add_to_linker_async(linker)?; + add_nonblocking_to_linker(linker, options)?; + let l = linker; - wasmtime_wasi_io::add_to_linker_async(l)?; + let f: fn(&mut T) -> WasiImpl<&mut T> = |t| WasiImpl(IoImpl(t)); + bindings::filesystem::types::add_to_linker::>(l, f)?; + bindings::sockets::tcp::add_to_linker::>(l, f)?; + bindings::sockets::udp::add_to_linker::>(l, f)?; + Ok(()) +} - let closure = type_annotate::(|t| WasiImpl(IoImpl(t))); +/// Shared functionality for [`add_to_linker_async`] and [`add_to_linker_sync`]. +fn add_nonblocking_to_linker<'a, T: WasiView + 'static, O>( + linker: &mut Linker, + options: &'a O, +) -> anyhow::Result<()> +where + bindings::sockets::network::LinkOptions: From<&'a O>, + bindings::cli::exit::LinkOptions: From<&'a O>, +{ + use crate::p2::bindings::{cli, clocks, filesystem, random, sockets}; - crate::p2::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::filesystem::types::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::filesystem::preopens::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::random::random::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::random::insecure::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::random::insecure_seed::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::exit::add_to_linker_get_host(l, &options.into(), closure)?; - crate::p2::bindings::cli::environment::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::stdin::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::stdout::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::stderr::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_input::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_output::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_stdin::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_stdout::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_stderr::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::tcp::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::tcp_create_socket::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::udp::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::udp_create_socket::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::instance_network::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::network::add_to_linker_get_host(l, &options.into(), closure)?; - crate::p2::bindings::sockets::ip_name_lookup::add_to_linker_get_host(l, closure)?; + let l = linker; + let f: fn(&mut T) -> WasiImpl<&mut T> = |t| WasiImpl(IoImpl(t)); + clocks::wall_clock::add_to_linker::>(l, f)?; + clocks::monotonic_clock::add_to_linker::>(l, f)?; + filesystem::preopens::add_to_linker::>(l, f)?; + random::random::add_to_linker::>(l, f)?; + random::insecure::add_to_linker::>(l, f)?; + random::insecure_seed::add_to_linker::>(l, f)?; + cli::exit::add_to_linker::>(l, &options.into(), f)?; + cli::environment::add_to_linker::>(l, f)?; + cli::stdin::add_to_linker::>(l, f)?; + cli::stdout::add_to_linker::>(l, f)?; + cli::stderr::add_to_linker::>(l, f)?; + cli::terminal_input::add_to_linker::>(l, f)?; + cli::terminal_output::add_to_linker::>(l, f)?; + cli::terminal_stdin::add_to_linker::>(l, f)?; + cli::terminal_stdout::add_to_linker::>(l, f)?; + cli::terminal_stderr::add_to_linker::>(l, f)?; + sockets::tcp_create_socket::add_to_linker::>(l, f)?; + sockets::udp_create_socket::add_to_linker::>(l, f)?; + sockets::instance_network::add_to_linker::>(l, f)?; + sockets::network::add_to_linker::>(l, &options.into(), f)?; + sockets::ip_name_lookup::add_to_linker::>(l, f)?; + Ok(()) +} + +/// Same as [`add_to_linker_async`] except that this only adds interfaces +/// present in the `wasi:http/proxy` world. +pub fn add_to_linker_proxy_interfaces_async( + linker: &mut Linker, +) -> anyhow::Result<()> { + wasmtime_wasi_io::add_to_linker_async(linker)?; + add_proxy_interfaces_nonblocking(linker) +} + +/// Same as [`add_to_linker_sync`] except that this only adds interfaces +/// present in the `wasi:http/proxy` world. +#[doc(hidden)] +pub fn add_to_linker_proxy_interfaces_sync( + linker: &mut Linker, +) -> anyhow::Result<()> { + add_sync_wasi_io(linker)?; + add_proxy_interfaces_nonblocking(linker) +} + +fn add_proxy_interfaces_nonblocking( + linker: &mut Linker, +) -> anyhow::Result<()> { + use crate::p2::bindings::{cli, clocks, random}; + + let l = linker; + let f: fn(&mut T) -> WasiImpl<&mut T> = |t| WasiImpl(IoImpl(t)); + clocks::wall_clock::add_to_linker::>(l, f)?; + clocks::monotonic_clock::add_to_linker::>(l, f)?; + random::random::add_to_linker::>(l, f)?; + cli::stdin::add_to_linker::>(l, f)?; + cli::stdout::add_to_linker::>(l, f)?; + cli::stderr::add_to_linker::>(l, f)?; Ok(()) } @@ -415,65 +466,50 @@ pub fn add_to_linker_with_options_async( /// fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx } /// } /// ``` -pub fn add_to_linker_sync( +pub fn add_to_linker_sync( linker: &mut wasmtime::component::Linker, ) -> anyhow::Result<()> { - let options = crate::p2::bindings::sync::LinkOptions::default(); + let options = bindings::sync::LinkOptions::default(); add_to_linker_with_options_sync(linker, &options) } /// Similar to [`add_to_linker_sync`], but with the ability to enable unstable features. -pub fn add_to_linker_with_options_sync( +pub fn add_to_linker_with_options_sync( linker: &mut wasmtime::component::Linker, - options: &crate::p2::bindings::sync::LinkOptions, + options: &bindings::sync::LinkOptions, ) -> anyhow::Result<()> { - let l = linker; - let io_closure = io_type_annotate::(|t| IoImpl(t)); - wasmtime_wasi_io::bindings::wasi::io::error::add_to_linker_get_host(l, io_closure)?; - - crate::p2::bindings::sync::io::poll::add_to_linker_get_host(l, io_closure)?; - crate::p2::bindings::sync::io::streams::add_to_linker_get_host(l, io_closure)?; + add_nonblocking_to_linker(linker, options)?; + add_sync_wasi_io(linker)?; - let closure = type_annotate::(|t| WasiImpl(IoImpl(t))); + let l = linker; + let f: fn(&mut T) -> WasiImpl<&mut T> = |t| WasiImpl(IoImpl(t)); + bindings::sync::filesystem::types::add_to_linker::>(l, f)?; + bindings::sync::sockets::tcp::add_to_linker::>(l, f)?; + bindings::sync::sockets::udp::add_to_linker::>(l, f)?; + Ok(()) +} - crate::p2::bindings::clocks::wall_clock::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::clocks::monotonic_clock::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sync::filesystem::types::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::filesystem::preopens::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::random::random::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::random::insecure::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::random::insecure_seed::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::exit::add_to_linker_get_host(l, &options.into(), closure)?; - crate::p2::bindings::cli::environment::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::stdin::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::stdout::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::stderr::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_input::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_output::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_stdin::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_stdout::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::cli::terminal_stderr::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sync::sockets::tcp::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::tcp_create_socket::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sync::sockets::udp::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::udp_create_socket::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::instance_network::add_to_linker_get_host(l, closure)?; - crate::p2::bindings::sockets::network::add_to_linker_get_host(l, &options.into(), closure)?; - crate::p2::bindings::sockets::ip_name_lookup::add_to_linker_get_host(l, closure)?; +/// Shared functionality of [`add_to_linker_sync`]` and +/// [`add_to_linker_proxy_interfaces_sync`]. +fn add_sync_wasi_io( + linker: &mut wasmtime::component::Linker, +) -> anyhow::Result<()> { + let l = linker; + let f: fn(&mut T) -> IoImpl<&mut T> = |t| IoImpl(t); + wasmtime_wasi_io::bindings::wasi::io::error::add_to_linker::>(l, f)?; + bindings::sync::io::poll::add_to_linker::>(l, f)?; + bindings::sync::io::streams::add_to_linker::>(l, f)?; Ok(()) } -// NB: workaround some rustc inference - a future refactoring may make this -// obsolete. -fn io_type_annotate(val: F) -> F -where - F: Fn(&mut T) -> IoImpl<&mut T>, -{ - val +struct HasIo(T); + +impl HasData for HasIo { + type Data<'a> = IoImpl<&'a mut T>; } -fn type_annotate(val: F) -> F -where - F: Fn(&mut T) -> WasiImpl<&mut T>, -{ - val + +struct HasWasi(T); + +impl HasData for HasWasi { + type Data<'a> = WasiImpl<&'a mut T>; } diff --git a/crates/wasmtime/src/runtime/component/bindgen_examples/mod.rs b/crates/wasmtime/src/runtime/component/bindgen_examples/mod.rs index e77ce276edcd..2b49a850e489 100644 --- a/crates/wasmtime/src/runtime/component/bindgen_examples/mod.rs +++ b/crates/wasmtime/src/runtime/component/bindgen_examples/mod.rs @@ -102,12 +102,18 @@ macro_rules! bindgen { /// // Configuration of the linker is done through a generated `add_to_linker` /// // method on the bindings structure. /// // -/// // Note that the closure provided here is a projection from `T` in +/// // Note that the function provided here is a projection from `T` in /// // `Store` to `&mut U` where `U` implements the `HelloWorldImports` /// // trait. In this case the `T`, `MyState`, is stored directly in the /// // structure so no projection is necessary here. +/// // +/// // Note that the second type parameter of `add_to_linker` is chosen here +/// // as the built-in `HasSelf` type in Wasmtime. This effectively says +/// // that our function isn't actually projecting, it's returning the +/// // input, so `HasSelf<_>` is a convenience to avoid writing a custom +/// // `HasData` implementation. /// let mut linker = Linker::new(&engine); -/// HelloWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?; +/// HelloWorld::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?; /// /// // As with the core wasm API of Wasmtime instantiation occurs within a /// // `Store`. The bindings structure contains an `instantiate` method which @@ -167,7 +173,7 @@ pub mod _0_hello_world; /// let component = Component::from_file(&engine, "./your-component.wasm")?; /// /// let mut linker = Linker::new(&engine); -/// MyWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?; +/// MyWorld::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?; /// /// let mut store = Store::new( /// &engine, @@ -224,7 +230,7 @@ pub mod _1_world_imports; /// let component = Component::from_file(&engine, "./your-component.wasm")?; /// /// let mut linker = Linker::new(&engine); -/// HelloWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?; +/// HelloWorld::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?; /// /// let mut store = Store::new( /// &engine, @@ -384,7 +390,7 @@ pub mod _4_imported_resources; /// /// ```rust /// use wasmtime::{Result, Engine, Store}; -/// use wasmtime::component::{bindgen, Component, Linker}; +/// use wasmtime::component::{bindgen, Component, Linker, HasSelf}; /// #[doc = include_str!("./_5_all_world_export_kinds.rs")] /// @@ -402,7 +408,7 @@ pub mod _4_imported_resources; /// let component = Component::from_file(&engine, "./your-component.wasm")?; /// /// let mut linker = Linker::new(&engine); -/// WithExports::add_to_linker(&mut linker, |state: &mut MyState| state)?; +/// WithExports::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?; /// /// let mut store = Store::new(&engine, MyState); /// let bindings = WithExports::instantiate(&mut store, &component, &linker)?; diff --git a/crates/wasmtime/src/runtime/component/get_host.rs b/crates/wasmtime/src/runtime/component/get_host.rs deleted file mode 100644 index df54110083ce..000000000000 --- a/crates/wasmtime/src/runtime/component/get_host.rs +++ /dev/null @@ -1,255 +0,0 @@ -/// An "alias trait" used for [`bindgen!`](super::bindgen)-generated -/// `add_to_linker_get_host`-style functions. -/// -/// This trait is used as a trait bound for closures that "project" from a -/// store's `&mut T` internals to `U` where `U` is allowed to close over the -/// lifetime of `&mut T`. This is a somewhat tricky operation to write down in a -/// trait bound which requires a somewhat special definition of this trait. -/// -/// This trait has a supertrait of `Fn(T) -> U` where `U` here is specified as -/// an associated type `Host` for this trait. There is then additionally a -/// blanket implementation for all `Fn(T) -> U` closures as well meaning no -/// embedders should need to implement this trait manually. -/// -/// Note that this trait additionally requires that closures are `Send + Sync + -/// Copy + 'static` which means they effectively need to behave like function -/// pointers. -/// -/// Also note that using this trait requires type-annotations at this time. -/// Refer to the example below for more information. -/// -/// # Examples -/// -/// Embedders should generally not need to write `GetHost` bounds themselves but -/// they will need to interact with functions that have a parameter bounded by -/// `GetHost` generated by [`bindgen!`](super::bindgen). -/// -/// ``` -/// use wasmtime::component::{Linker, GetHost}; -/// -/// // generated by `bindgen!` -/// trait Host { /* ... */ } -/// // generated by `bindgen!` -/// impl<'a, T: ?Sized + Host> Host for &'a mut T { /* ... */ } -/// -/// // generated by `bindgen!` -/// fn add_to_linker_get_host(linker: &mut Linker, get_host: G) -/// where G: for<'a> GetHost<&'a mut T, Host: Host>, -/// { -/// // ... -/// # let _ = (linker, get_host); -/// } -/// -/// // In some cases you'll implement `Host` for the `T` in the store directly. -/// struct MyType; -/// -/// impl Host for MyType { -/// // ... -/// } -/// -/// fn my_add_to_linker(linker: &mut Linker) { -/// let get_host = type_annotate(|x| x); -/// add_to_linker_get_host(linker, get_host); -/// -/// // Type annotating a closure is not the most intuitive thing to do in -/// // Rust, but that's all this function is doing. This is taking a `F` -/// // and then simply returning it, but the purpose of this function is to -/// // synthetically place a bound on `F` here with a `where` clause. This -/// // `where` clause is what annotates the type of `F` manually and is how -/// // we accurately describe the lifetimes associated with this closure. -/// fn type_annotate(f: F) -> F -/// where F: Fn(&mut MyType) -> &mut MyType -/// { -/// f -/// } -/// } -/// -/// // In other cases you might store the structure implementing `Host` -/// // elsewhere in the store -/// struct MyContainerType { -/// the_impl: MyType, -/// } -/// -/// fn my_add_to_linker_with_container(linker: &mut Linker) { -/// let get_host = type_annotate(|x| &mut x.the_impl); -/// add_to_linker_get_host(linker, get_host); -/// -/// fn type_annotate(f: F) -> F -/// where F: Fn(&mut MyContainerType) -> &mut MyType -/// { -/// f -/// } -/// } -/// -/// // And in some cases you might want to combine pieces of state within a `T` -/// // of a `Store` into one type that implements this state. This is, for -/// // example, how WASI impls work in Wasmtime. -/// struct MyStoreState { -/// the_table: MyTable, -/// the_impl: MyType, -/// } -/// -/// struct MyTable { /* ... */ } -/// -/// struct MyBorrowedState<'a> { -/// the_table: &'a mut MyTable, -/// the_impl: &'a mut MyType, -/// } -/// -/// impl Host for MyBorrowedState<'_> { -/// // ... -/// } -/// -/// fn my_add_to_linker_with_more_lifetimes(linker: &mut Linker) { -/// let get_host = type_annotate(|x| MyBorrowedState { -/// the_table: &mut x.the_table, -/// the_impl: &mut x.the_impl, -/// }); -/// add_to_linker_get_host(linker, get_host); -/// -/// fn type_annotate(f: F) -> F -/// where F: Fn(&mut MyStoreState) -> MyBorrowedState<'_> -/// { -/// f -/// } -/// } -/// ``` -/// -/// # Too Much Detail -/// -/// The general idea behind this trait is that it expresses the ability to take -/// a closure as a parameter which projects from a store's state, `T`, to a `U` -/// which implements the bindgen-generated traits. The actual operation is -/// provided a `&'a mut T` at runtime and `U` must then also be able to close over -/// the `'a` too. -/// -/// This is surprisingly subtle to do in Rust and there are many variations of -/// writing this down which don't achieve the desired result. For example the -/// most naive way to possibly write this does not work. -/// -/// ```compile_fail -/// use wasmtime::component::Linker; -/// -/// # trait Host { /* ... */ } -/// # impl<'a, T: ?Sized + Host> Host for &'a mut T { /* ... */ } -/// // Naively let's just take `F: FnMut...` -/// fn add_to_linker_get_host(linker: &mut Linker, get_host: F) -/// where F: FnMut(&mut T) -> U, -/// { -/// // ... -/// # let _ = (linker, get_host); -/// } -/// -/// struct MyStoreType { the_impl: MyType, } -/// struct MyType { /* ... */ } -/// impl Host for MyType { /* ... */ } -/// -/// fn my_add_to_linker(linker: &mut Linker) { -/// let get_host = type_annotate(|x| x); -/// add_to_linker_get_host(linker, get_host); -/// //~^ ERROR: one type is more general than the other -/// -/// fn type_annotate(f: F) -> F -/// where F: Fn(&mut MyType) -> &mut MyType -/// { -/// f -/// } -/// } -/// ``` -/// -/// The problem with this version is that the exact definition of the trait -/// bound is: `where F: for<'a> FnMut(&'a mut T) -> U`, and `U` doesn't have -/// access to the lifetime `'a` that's only defined under the trait bound of `F` -/// itself. So this doesn't work but how about lifting the lifetime up? -/// -/// ```compile_fail -/// use wasmtime::component::Linker; -/// -/// # trait Host { /* ... */ } -/// # impl<'a, T: ?Sized + Host> Host for &'a mut T { /* ... */ } -/// fn add_to_linker_get_host<'a, F, T: 'a, U>(linker: &mut Linker, get_host: F) -/// where F: FnMut(&'a mut T) -> U + Send + Sync + 'static, -/// { -/// linker.root().func_wrap("f", move |mut store, ()| { -/// let mut data = store.data_mut(); -/// let the_u = get_host(&mut data); -/// //~^ ERROR: borrowed data escapes outside of closure -/// Ok(()) -/// }); -/// } -/// ``` -/// -/// The problem with this invocation is the meaning is still wrong. While the -/// above example will compile with `'a` defined here the actual usage of -/// `get_host` doesn't work. The root of the issue is that the lifetime can't -/// actually be named when `add_to_linker_get_host` is called, instead it *must* -/// be part of the `for<'a>` part. -/// -/// Rust doesn't have higher-ranked definitions such as `for<'a, U> Fn(&'a mut -/// T) -> U` where `U` would have access to the `'a` lifetime here, meaning that -/// at this point we've exhausted the various possibilities of using `Fn` trait -/// bounds directly. They all mean subtly different things than the original -/// problem we're trying to solve here. -/// -/// This trait definition exists to accurately express in the trait system what -/// we're trying to solve which is that the return value of a closure is allowed -/// to close over its arguments. This is achieved where the trait here is -/// defined with just `T`, notably not `&'a mut T`, meaning that whatever is in -/// scope for `T` is also in-scope for the returned value from the closure. -/// Usage of this trait then looks like `G: for<'a> GetHost<&'a mut T>` meaning -/// that the `G::Host` associated type has access to `'a`. -/// -/// This then gives rise to the second issue of this trait which is actually -/// defining a closure that implements this trait. Ideally what we want to write -/// is something that looks like this: -/// -/// ```ignore -/// # struct MyStoreType { the_impl: MyType, } -/// # struct MyType { /* ... */ } -/// let my_closure = for<'a> |x: &'a mut MyStoreType| -> &'a mut MyType { -/// &mut x.the_impl -/// }; -/// ``` -/// -/// This syntax technically compiles but [is -/// unstable](https://github.com/rust-lang/rust/issues/97362). Alternatively one -/// might want to write: -/// -/// ```ignore -/// # struct MyStoreType { the_impl: MyType, } -/// # struct MyType { /* ... */ } -/// let my_closure: impl FnMut(&mut MyStoreType) -> &mut MyType = |x| { -/// &mut x.the_impl -/// }; -/// ``` -/// -/// but this syntax is [also -/// unstable](https://github.com/rust-lang/rust/issues/63065). Thus what we're -/// left with is the sort of kludge workaround in the examples above: -/// -/// ``` -/// # struct MyStoreType { the_impl: MyType, } -/// # struct MyType { /* ... */ } -/// let my_closure = type_annotate(|x| &mut x.the_impl); -/// -/// fn type_annotate(f: F) -> F -/// where F: Fn(&mut MyStoreType) -> &mut MyType -/// { -/// f -/// } -/// ``` -/// -/// Our hope is that this can get more ergonomic over time, but this is -/// hopefully at least illuminating how we have ended up here. -pub trait GetHost: Fn(T) -> >::Host + Send + Sync + Copy + 'static { - /// The type that this accessor returns. Notably this has access to the - /// lifetimes of `T`. - type Host; -} - -impl GetHost for F -where - F: Fn(T) -> U + Send + Sync + Copy + 'static, -{ - type Host = U; -} diff --git a/crates/wasmtime/src/runtime/component/has_data.rs b/crates/wasmtime/src/runtime/component/has_data.rs new file mode 100644 index 000000000000..ce115bc181b3 --- /dev/null +++ b/crates/wasmtime/src/runtime/component/has_data.rs @@ -0,0 +1,305 @@ +/// A trait used as part of [`bindgen!`] to indicate a `Data<'_>` payload that +/// implements some host bindings traits. +/// +/// The purpose of the [`bindgen!`] macro is to define Rust traits that the host +/// can implement to fulfill WIT functions imported from the host into a +/// component. The [`bindgen!`] macro then additionally generates a function +/// which takes a [`Linker`] and an implementation of the traits and fills out +/// the [`Linker`]. This trait, [`HasData`], is used in this process of filling +/// out the [`Linker`] for some WIT interfaces. +/// +/// Wasmtime's [`Store`] type is the home for all per-instance state. +/// Notably the `T` here is generic (the Wasmtime library allows any type to be +/// placed here) and it's also instance-specific as a [`Store`] is typically +/// allocated one-per-instance. Implementations of host APIs, however, often +/// want to live in a library and not be tied to any particular `T`. For example +/// Wasmtime provides the `wasmtime-wasi` crates as an implementation of +/// standard WASI APIs as a library, but they don't want to fix a particular `T` +/// in [`Store`] as embedders should be able to fill out their own `T` for +/// their needs. The purpose of this trait is to enable this situation. +/// +/// This trait is used in `add_to_linker` functions generated by [`bindgen!`] in +/// conjunction with a function pointer. It looks something along the lines of: +/// +/// ``` +/// use wasmtime::component::{Linker, HasData}; +/// +/// // generated by bindgen! +/// trait Host { +/// // .. +/// } +/// +/// fn add_to_linker(linker: &mut Linker, getter: fn(&mut T) -> D::Data<'_>) +/// where D: HasData, +/// for<'a> D::Data<'a>: Host, +/// { +/// // ... +/// # let _ = (linker, getter); +/// } +/// ``` +/// +/// Here the `D` type parameter, bounded by [`HasData`], is used to specify +/// the return type of the `getter` function provided here. The `getter` +/// "projects" from `&mut T` to `D::Data<'_>`, notably enabling it to capture +/// the lifetime of the `&mut T` passed in as well. +/// +/// The `Data` associated type here is further bounded in `add_to_linker` above +/// as it must implement the traits generated by [`bindgen!`]. This means that +/// `linker` is filled out with functions that, when called, first `getter` is +/// invoked and then the actual function delegates to the `Host` trait +/// implementation in this case. +/// +/// The `D` type parameter here isn't actually a runtime value, nor is it stored +/// anywhere. It's purely used as a means of projecting a "generic associated +/// type", here where `<'a>` is the generic argument, a lifetime. This means +/// that the choice of `D` is disconnected from both `T` and `D::Data<'_>` and +/// can be whatever you like. Sometimes you might need to define an empty struct +/// in your project to use this, but Wasmtime also provides a convenience +/// [`HasSelf`] type to avoid the need for this in some common use cases. +/// +/// # Example: `Host for T` using `Store` +/// +/// Let's say you wanted to invoke the above `add_to_linker` function where the +/// `T` in [`Store`] directly implements the `Host` trait itself: +/// +/// ``` +/// use wasmtime::component::{Linker, HasSelf}; +/// # use wasmtime::component::HasData; +/// # +/// # trait Host { } +/// # impl Host for &mut T {} +/// # +/// # fn add_to_linker(linker: &mut Linker, getter: fn(&mut T) -> D::Data<'_>) +/// # where D: HasData, +/// # for<'a> D::Data<'a>: Host, +/// # { +/// # let _ = (linker, getter); +/// # } +/// +/// struct MyStoreState { /* ... */ } +/// +/// impl Host for MyStoreState { +/// // .. +/// } +/// +/// fn fill_out_my_linker(linker: &mut Linker) { +/// add_to_linker::<_, HasSelf<_>>(linker, |x| x) +/// } +/// ``` +/// +/// Here the `add_to_linker` invocation is annotated with `<_, HasSelf<_>>`. The +/// first argument gets inferred to `MyStoreState`, and the second argument gets +/// inferred to `HasSelf` This means that the projection function +/// in this case, here the identity `|x| x`, is typed as +/// `fn(&mut MyStoreState) -> &mut MyStoreState`. This is because the `HasData` +/// implementation for `HasSelf` means that we have +/// `type Data<'a> = &'a mut T`. +/// +/// # Example: `Host for MyLibraryState` using `Store` +/// +/// Let's say though that you instead are writing a library like WASI where you +/// don't know the `T` of [`Store`] ahead of time. In such a case you might +/// hand-write your own `add_to_linker` wrapper that looks like this: +/// +/// ``` +/// use wasmtime::component::{Linker, HasData}; +/// # +/// # trait Host { } +/// # impl Host for &mut T {} +/// # +/// # fn add_to_linker(linker: &mut Linker, getter: fn(&mut T) -> D::Data<'_>) +/// # where D: HasData, +/// # for<'a> D::Data<'a>: Host, +/// # { +/// # let _ = (linker, getter); +/// # } +/// +/// // publicly exposed per-instance state for your library, users will +/// // construct this and store it within the `T` in `Store` +/// pub struct MyLibraryState { /* ... */ } +/// +/// impl Host for MyLibraryState { +/// // .. +/// } +/// +/// // hand-written publicly exposed convenience function to add this crate's +/// // component functionality to the provided linker. +/// pub fn add_my_library_to_linker( +/// linker: &mut Linker, +/// f: fn(&mut T) -> &mut MyLibraryState, +/// ) { +/// // invoke the bindgen!-generated `add_to_linker` +/// add_to_linker::<_, MyLibrary>(linker, f); +/// } +/// +/// // Note this need not be publicly exposed, it's just a private internal +/// // detail. +/// struct MyLibrary; +/// +/// impl HasData for MyLibrary { +/// type Data<'a> = &'a mut MyLibraryState; +/// } +/// ``` +/// +/// Here the `MyLibrary` type, private to this crate, has a `HasData` +/// implementation which indicates that implementations of the +/// `bindgen!`-generated APIs are done in terms of `MyLibraryState` +/// specifically. The `add_my_library_to_linker` takes an externally-provided +/// `f` closure which projects from `&mut T`, whatever data the store is using, +/// to `MyLibraryState` which must be stored within the store itself. +/// +/// # Example: `Host for MyLibraryState<'_>` using `Store` +/// +/// Let's say you're like the above scenario where you're writing a library but +/// instead of being able to wrap up all your state for each trait +/// implementation in a structure it's spread across a few structures. These +/// structures may be stored in different locations inside of `Store` which +/// makes it difficult to wrap up in a single type and return that. Here you can +/// make use of "view" types to collect a number of disjoint borrows into one +/// structure: +/// +/// ``` +/// use wasmtime::component::{Linker, HasData}; +/// # +/// # trait Host { } +/// # impl Host for &mut T {} +/// # +/// # fn add_to_linker(linker: &mut Linker, getter: fn(&mut T) -> D::Data<'_>) +/// # where D: HasData, +/// # for<'a> D::Data<'a>: Host, +/// # { +/// # let _ = (linker, getter); +/// # } +/// # struct StateA; +/// # struct StateB; +/// +/// // Like before, this is publicly exposed, and this is a "bag of pointers" to +/// // all the pieces of state necessary to implement `Host` below. +/// pub struct MyLibraryState<'a> { +/// pub state_a: &'a mut StateA, +/// pub state_b: &'a mut StateB, +/// } +/// +/// impl Host for MyLibraryState<'_> { +/// // .. +/// } +/// +/// pub fn add_my_library_to_linker( +/// linker: &mut Linker, +/// f: fn(&mut T) -> MyLibraryState<'_>, +/// ) { +/// // invoke the bindgen!-generated `add_to_linker` +/// add_to_linker::<_, MyLibrary>(linker, f); +/// } +/// +/// struct MyLibrary; +/// +/// impl HasData for MyLibrary { +/// type Data<'a> = MyLibraryState<'a>; +/// } +/// ``` +/// +/// This is similar to the above example but shows using a lifetime parameter on +/// a structure instead of using a pointer-with-a-lifetime only. Otherwise +/// though this'll end up working out the same way. +/// +/// # Example: `Host for U: MyLibrary` using `Store` +/// +/// Let's say you're in a situation where you're a library which wants to create +/// a "simpler" trait than the `Host`-generated traits from [`bindgen!`] and +/// then you want to implement `Host` in terms of this trait. This requires a +/// bit more boilerplate as well as a new custom structure, but doing so looks +/// like this: +/// +/// ``` +/// use wasmtime::component::{Linker, HasData}; +/// # +/// # trait Host { } +/// # impl Host for &mut T {} +/// # +/// # fn add_to_linker(linker: &mut Linker, getter: fn(&mut T) -> D::Data<'_>) +/// # where D: HasData, +/// # for<'a> D::Data<'a>: Host, +/// # { +/// # let _ = (linker, getter); +/// # } +/// +/// // This is your public trait which external users need to implement. This +/// // can encapsulate any "core" functionality needed to implement +/// // bindgen!-generated traits such as `Host` below. +/// pub trait MyLibraryTrait { +/// // ... +/// } +/// +/// // You'll need to provide a "forwarding" implementation of this trait from +/// // `&mut T` to `T` to get the below implementation to compile. +/// impl MyLibraryTrait for &mut T { +/// // ... +/// } +/// +/// // This is a bit of a "hack" and an unfortunate workaround, but is currently +/// // used to work within the confines of orphan rules and such. This is +/// // publicly exposed as the function provided below must provide access to +/// // this. +/// pub struct MyLibraryImpl(pub T); +/// +/// // Here you'd implement all of `Host` in terms of `MyLibraryTrait`. +/// // Functions with `&mut self` would use `self.0` to access the functionality +/// // in `MyLibraryTrait`. +/// impl Host for MyLibraryImpl { +/// // .. +/// } +/// +/// // optional: this avoids the need for `self.0` accessors in `Host` above, +/// // but this otherwise isn't required. +/// impl MyLibraryTrait for MyLibraryImpl { +/// // .. +/// } +/// +/// // Note the second type parameter on this method, `U`, which is the user's +/// // implementation of `MyLibraryTrait`. Note that this additionally must +/// // be bounded with `'static` here. +/// pub fn add_my_library_to_linker( +/// linker: &mut Linker, +/// f: fn(&mut T) -> MyLibraryImpl<&mut U>, +/// ) +/// where U: MyLibraryTrait + 'static, +/// { +/// add_to_linker::<_, MyLibrary>(linker, f); +/// } +/// +/// // An adjusted definition of `MyLibrary` relative to the previous example, +/// // still private, which hooks up all the types used here. +/// struct MyLibrary(U); +/// +/// impl HasData for MyLibrary { +/// type Data<'a> = MyLibraryImpl<&'a mut U>; +/// } +/// ``` +/// +/// This iteration of implementing component-interfaces-as-a-library is +/// unfortunately relatively verbose and unintuitive at this time. We're always +/// keen on improving this though, so if you've got ideas of how to improve this +/// please let us know! +/// +/// [`bindgen!`]: super::bindgen +/// [`Linker`]: super::Linker +/// [`Store`]: crate::Store +pub trait HasData: 'static { + /// The data associated with this trait implementation, chiefly used as a + /// generic associated type to allow plumbing the `'a` lifetime into the + /// definition here. + /// + /// See the trait documentation for more examples. + type Data<'a>; +} + +/// A convenience implementation of the [`HasData`] trait when the data +/// associated with an implementation is `&mut T`. +/// +/// For more examples on using this see the [`HasData`] trait. +pub struct HasSelf(core::marker::PhantomData); + +impl HasData for HasSelf { + type Data<'a> = &'a mut T; +} diff --git a/crates/wasmtime/src/runtime/component/mod.rs b/crates/wasmtime/src/runtime/component/mod.rs index 78ce898ebd6d..84cd4f61b42d 100644 --- a/crates/wasmtime/src/runtime/component/mod.rs +++ b/crates/wasmtime/src/runtime/component/mod.rs @@ -104,7 +104,7 @@ mod component; #[cfg(feature = "component-model-async")] pub(crate) mod concurrent; mod func; -mod get_host; +mod has_data; mod instance; mod linker; mod matching; @@ -122,7 +122,7 @@ pub use self::concurrent::{ pub use self::func::{ ComponentNamedList, ComponentType, Func, Lift, Lower, TypedFunc, WasmList, WasmStr, }; -pub use self::get_host::GetHost; +pub use self::has_data::*; pub use self::instance::{Instance, InstanceExportLookup, InstancePre}; pub use self::linker::{Linker, LinkerInstance}; pub use self::resource_table::{ResourceTable, ResourceTableError}; diff --git a/crates/wit-bindgen/src/lib.rs b/crates/wit-bindgen/src/lib.rs index bd9da493f6ff..551fe59cd937 100644 --- a/crates/wit-bindgen/src/lib.rs +++ b/crates/wit-bindgen/src/lib.rs @@ -1532,9 +1532,9 @@ impl Wasmtime { let camel = to_rust_upper_camel_case(&resolve.worlds[world].name); let data_bounds = if self.opts.is_store_data_send() { - "T: Send," + "Send + 'static" } else { - "" + "'static" }; let wt = self.wasmtime_path(); if has_world_imports_trait { @@ -1549,14 +1549,15 @@ impl Wasmtime { uwrite!( self.src, " - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut {wt}::component::Linker, {options_param} - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> {wt}::Result<()> where - G: for<'a> {wt}::component::GetHost<&'a mut T, Host: {host_bounds}>, - {data_bounds} + D: {wt}::component::HasData, + for<'a> D::Data<'a>: {host_bounds}, + T: {data_bounds} {{ let mut linker = linker.root(); " @@ -1617,66 +1618,64 @@ impl Wasmtime { .collect::>() .concat(); - (format!("U: Send{bounds}"), format!("T: Send{bounds},")) + (format!("Send{bounds}"), format!("Send{bounds} + 'static,")) } else { ( - format!("U: {}", self.world_host_traits(resolve, world).join(" + ")), + format!("{}", self.world_host_traits(resolve, world).join(" + ")), data_bounds.to_string(), ) }; - if !self.opts.skip_mut_forwarding_impls { + uwriteln!( + self.src, + " + pub fn add_to_linker( + linker: &mut {wt}::component::Linker, + {options_param} + get: fn(&mut T) -> D::Data<'_>, + ) -> {wt}::Result<()> + where + D: {wt}::component::HasData, + for<'a> D::Data<'a>: {host_bounds}, + T: {data_bounds} + {{ + " + ); + let gate = FeatureGate::open(&mut self.src, &resolve.worlds[world].stability); + if has_world_imports_trait { uwriteln!( self.src, - " - pub fn add_to_linker( - linker: &mut {wt}::component::Linker, - {options_param} - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> {wt}::Result<()> - where - T: 'static, - {data_bounds} - {host_bounds} - {{ - " + "Self::add_to_linker_imports::(linker {options_arg}, get)?;" ); - let gate = FeatureGate::open(&mut self.src, &resolve.worlds[world].stability); - if has_world_imports_trait { - uwriteln!( - self.src, - "Self::add_to_linker_imports_get_host(linker {options_arg}, get)?;" - ); - } - for (interface_id, path) in self.import_interface_paths() { - let options_arg = if self.interface_link_options[&interface_id].has_any() { - ", &options.into()" - } else { - "" - }; + } + for (interface_id, path) in self.import_interface_paths() { + let options_arg = if self.interface_link_options[&interface_id].has_any() { + ", &options.into()" + } else { + "" + }; - 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 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 gate = FeatureGate::open(&mut self.src, &import_stability); - uwriteln!( - self.src, - "{path}::add_to_linker(linker {options_arg}, get)?;" - ); - gate.close(&mut self.src); - } + let gate = FeatureGate::open(&mut self.src, &import_stability); + uwriteln!( + self.src, + "{path}::add_to_linker::(linker {options_arg}, get)?;" + ); gate.close(&mut self.src); - uwriteln!(self.src, "Ok(())\n}}"); } + gate.close(&mut self.src); + uwriteln!(self.src, "Ok(())\n}}"); } fn generate_add_resource_to_linker( @@ -2626,7 +2625,7 @@ impl<'a> InterfaceGenerator<'a> { let (data_bounds, mut host_bounds, mut get_host_bounds) = match self.generator.opts.call_style() { CallStyle::Async => ( - "T: Send,".to_string(), + "Send + 'static,", "Host + Send".to_string(), "Host + Send".to_string(), ), @@ -2639,12 +2638,12 @@ impl<'a> InterfaceGenerator<'a> { ); ( - "T: Send + 'static,".to_string(), + "Send + 'static,", format!("Host{} + Send", constraints("T")), format!("Host{} + Send", constraints("D")), ) } - CallStyle::Sync => (String::new(), "Host".to_string(), "Host".to_string()), + CallStyle::Sync => ("'static", "Host".to_string(), "Host".to_string()), }; for ty in required_conversion_traits { @@ -2652,24 +2651,24 @@ impl<'a> InterfaceGenerator<'a> { uwrite!(get_host_bounds, " + {ty}"); } - let (options_param, options_arg) = if self.generator.interface_link_options[&id].has_any() { - ("options: &LinkOptions,", ", options") + let options_param = if self.generator.interface_link_options[&id].has_any() { + "options: &LinkOptions," } else { - ("", "") + "" }; uwriteln!( self.src, " - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut {wt}::component::Linker, {options_param} - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> {wt}::Result<()> where - T: 'static, - G: for<'a> {wt}::component::GetHost<&'a mut T, Host: {host_bounds}>, - {data_bounds} + D: {wt}::component::HasData, + for<'a> D::Data<'a>: {host_bounds}, + T: {data_bounds} {{ " ); @@ -2696,23 +2695,6 @@ impl<'a> InterfaceGenerator<'a> { uwriteln!(self.src, "}}"); if !self.generator.opts.skip_mut_forwarding_impls { - // Generate add_to_linker (with closure) - uwriteln!( - self.src, - " - pub fn add_to_linker( - linker: &mut {wt}::component::Linker, - {options_param} - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> {wt}::Result<()> - where - T: 'static, U: {host_bounds}, {data_bounds} - {{ - add_to_linker_get_host(linker {options_arg}, get) - }} - " - ); - // Generate impl Host for &mut Host let maybe_send = if is_maybe_async { "+ Send" } else { "" }; @@ -2914,7 +2896,7 @@ impl<'a> InterfaceGenerator<'a> { if let CallStyle::Concurrent = &style { uwrite!( self.src, - "let r = ::{func_name}(host, " + "let r = as {host_trait}>::{func_name}(host, " ); } else { uwrite!(self.src, "let r = {host_trait}::{func_name}(host, "); diff --git a/examples/component/main.rs b/examples/component/main.rs index c26e70b57607..b456db837c4e 100644 --- a/examples/component/main.rs +++ b/examples/component/main.rs @@ -2,26 +2,24 @@ use anyhow::Context; use std::{fs, path::Path}; use wasmtime::{ - component::{bindgen, Component, Linker}, + component::{bindgen, Component, HasSelf, Linker}, Config, Engine, Result, Store, }; // Generate bindings of the guest and host components. bindgen!("convert" in "./examples/component/convert.wit"); -struct HostComponent; +struct MyState { + // .. +} // Implementation of the host interface defined in the wit file. -impl host::Host for HostComponent { +impl host::Host for MyState { fn multiply(&mut self, a: f32, b: f32) -> f32 { a * b } } -struct MyState { - host: HostComponent, -} - /// This function is only needed until rust can natively output a component. /// /// Generally embeddings should not be expected to do this programmatically, but instead @@ -47,14 +45,9 @@ fn main() -> Result<()> { // Create our component and call our generated host function. let component = Component::from_binary(&engine, &component)?; - let mut store = Store::new( - &engine, - MyState { - host: HostComponent {}, - }, - ); + let mut store = Store::new(&engine, MyState {}); let mut linker = Linker::new(&engine); - host::add_to_linker(&mut linker, |state: &mut MyState| &mut state.host)?; + host::add_to_linker::<_, HasSelf<_>>(&mut linker, |state| state)?; let convert = Convert::instantiate(&mut store, &component, &linker)?; let result = convert.call_convert_celsius_to_fahrenheit(&mut store, 23.4)?; println!("Converted to: {result:?}"); diff --git a/examples/min-platform/embedding/src/wasi.rs b/examples/min-platform/embedding/src/wasi.rs index b92365a09b89..eec7e120f037 100644 --- a/examples/min-platform/embedding/src/wasi.rs +++ b/examples/min-platform/embedding/src/wasi.rs @@ -212,16 +212,18 @@ impl ExampleCtx { // wasi:cli/command world. Many of these impls are bare-bones and some are // intentionally broken, see notes below. pub fn add_to_linker_async(linker: &mut Linker) -> Result<()> { - wasi::clocks::monotonic_clock::add_to_linker(linker, |t| t)?; - wasi::clocks::wall_clock::add_to_linker(linker, |t| t)?; - wasi::cli::environment::add_to_linker(linker, |t| t)?; - wasi::cli::exit::add_to_linker(linker, &wasi::cli::exit::LinkOptions::default(), |t| t)?; - wasi::cli::stdin::add_to_linker(linker, |t| t)?; - wasi::cli::stdout::add_to_linker(linker, |t| t)?; - wasi::cli::stderr::add_to_linker(linker, |t| t)?; - wasi::random::random::add_to_linker(linker, |t| t)?; - wasi::filesystem::preopens::add_to_linker(linker, |t| t)?; - wasi::filesystem::types::add_to_linker(linker, |t| t)?; + type Data = wasmtime::component::HasSelf; + + wasi::clocks::monotonic_clock::add_to_linker::<_, Data>(linker, |t| t)?; + wasi::clocks::wall_clock::add_to_linker::<_, Data>(linker, |t| t)?; + wasi::cli::environment::add_to_linker::<_, Data>(linker, |t| t)?; + wasi::cli::exit::add_to_linker::<_, Data>(linker, &Default::default(), |t| t)?; + wasi::cli::stdin::add_to_linker::<_, Data>(linker, |t| t)?; + wasi::cli::stdout::add_to_linker::<_, Data>(linker, |t| t)?; + wasi::cli::stderr::add_to_linker::<_, Data>(linker, |t| t)?; + wasi::random::random::add_to_linker::<_, Data>(linker, |t| t)?; + wasi::filesystem::preopens::add_to_linker::<_, Data>(linker, |t| t)?; + wasi::filesystem::types::add_to_linker::<_, Data>(linker, |t| t)?; Ok(()) } diff --git a/tests/all/component_model/bindgen.rs b/tests/all/component_model/bindgen.rs index 0c36dd1966d8..4001a7814953 100644 --- a/tests/all/component_model/bindgen.rs +++ b/tests/all/component_model/bindgen.rs @@ -66,6 +66,7 @@ mod no_imports { mod one_import { use super::*; + use wasmtime::component::HasSelf; wasmtime::component::bindgen!({ inline: " @@ -118,7 +119,7 @@ mod one_import { } let mut linker = Linker::new(&engine); - foo::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + foo::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let one_import = OneImport::instantiate(&mut store, &component, &linker)?; one_import.call_bar(&mut store)?; @@ -129,7 +130,7 @@ mod one_import { mod resources_at_world_level { use super::*; - use wasmtime::component::Resource; + use wasmtime::component::{HasSelf, Resource}; wasmtime::component::bindgen!({ inline: " @@ -206,7 +207,7 @@ mod resources_at_world_level { impl ResourcesImports for MyImports {} let mut linker = Linker::new(&engine); - Resources::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + Resources::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let one_import = Resources::instantiate(&mut store, &component, &linker)?; one_import.call_y(&mut store, Resource::new_own(40))?; @@ -218,7 +219,7 @@ mod resources_at_world_level { mod resources_at_interface_level { use super::*; - use wasmtime::component::Resource; + use wasmtime::component::{HasSelf, Resource}; wasmtime::component::bindgen!({ inline: " @@ -311,7 +312,7 @@ mod resources_at_interface_level { impl foo::foo::def::Host for MyImports {} let mut linker = Linker::new(&engine); - Resources::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + Resources::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let one_import = Resources::instantiate(&mut store, &component, &linker)?; one_import @@ -409,7 +410,7 @@ mod async_config { mod exported_resources { use super::*; use std::mem; - use wasmtime::component::Resource; + use wasmtime::component::{HasSelf, Resource}; wasmtime::component::bindgen!({ inline: " @@ -598,7 +599,7 @@ mod exported_resources { )?; let mut linker = Linker::new(&engine); - Resources::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + Resources::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let i = Resources::instantiate(&mut store, &component, &linker)?; @@ -636,6 +637,7 @@ mod exported_resources { mod unstable_import { use super::*; + use wasmtime::component::HasSelf; wasmtime::component::bindgen!({ inline: " @@ -730,7 +732,7 @@ mod unstable_import { } let mut linker = Linker::new(&engine); - MyWorld::add_to_linker(&mut linker, link_options, |h: &mut MyHost| h)?; + MyWorld::add_to_linker::<_, HasSelf<_>>(&mut linker, link_options, |h| h)?; let mut store = Store::new(&engine, MyHost::default()); let one_import = MyWorld::instantiate(&mut store, &component, &linker)?; one_import.call_bar(&mut store)?; diff --git a/tests/all/component_model/bindgen/results.rs b/tests/all/component_model/bindgen/results.rs index b330a1269e76..c7181d9588c0 100644 --- a/tests/all/component_model/bindgen/results.rs +++ b/tests/all/component_model/bindgen/results.rs @@ -7,6 +7,8 @@ use wasmtime::{ mod empty_error { use super::*; + use wasmtime::component::HasSelf; + wasmtime::component::bindgen!({ inline: " package inline:inline; @@ -75,7 +77,7 @@ mod empty_error { } let mut linker = Linker::new(&engine); - imports::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + imports::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let results = ResultPlayground::instantiate(&mut store, &component, &linker)?; @@ -109,6 +111,8 @@ mod empty_error { mod string_error { use super::*; + use wasmtime::component::HasSelf; + wasmtime::component::bindgen!({ inline: " package inline:inline; @@ -188,7 +192,7 @@ mod string_error { } let mut linker = Linker::new(&engine); - imports::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + imports::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let results = ResultPlayground::instantiate(&mut store, &component, &linker)?; @@ -225,6 +229,7 @@ mod enum_error { use super::*; use exports::foo; use inline::inline::imports; + use wasmtime::component::HasSelf; wasmtime::component::bindgen!({ inline: " @@ -364,7 +369,7 @@ mod enum_error { } let mut linker = Linker::new(&engine); - imports::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + imports::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let results = ResultPlayground::instantiate(&mut store, &component, &linker)?; @@ -406,6 +411,7 @@ mod record_error { use super::*; use exports::foo; use inline::inline::imports; + use wasmtime::component::HasSelf; wasmtime::component::bindgen!({ inline: " @@ -528,7 +534,7 @@ mod record_error { } let mut linker = Linker::new(&engine); - imports::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + imports::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let results = ResultPlayground::instantiate(&mut store, &component, &linker)?; @@ -574,6 +580,7 @@ mod variant_error { use super::*; use exports::foo; use inline::inline::imports; + use wasmtime::component::HasSelf; wasmtime::component::bindgen!({ inline: " @@ -723,7 +730,7 @@ mod variant_error { } let mut linker = Linker::new(&engine); - imports::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + imports::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let results = ResultPlayground::instantiate(&mut store, &component, &linker)?; @@ -770,6 +777,7 @@ mod multiple_interfaces_error { use exports::foo; use inline::inline::imports; use inline::inline::types; + use wasmtime::component::HasSelf; wasmtime::component::bindgen!({ inline: " @@ -926,7 +934,7 @@ mod multiple_interfaces_error { } let mut linker = Linker::new(&engine); - imports::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + imports::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let results = ResultPlayground::instantiate(&mut store, &component, &linker)?; @@ -966,6 +974,7 @@ mod multiple_interfaces_error { mod with_remapping { use super::*; + use wasmtime::component::HasSelf; mod interfaces { wasmtime::component::bindgen!({ @@ -1048,7 +1057,7 @@ mod with_remapping { } let mut linker = Linker::new(&engine); - interfaces::imports::add_to_linker(&mut linker, |f: &mut MyImports| f)?; + interfaces::imports::add_to_linker::<_, HasSelf<_>>(&mut linker, |f| f)?; let mut store = Store::new(&engine, MyImports::default()); let results = ResultPlayground::instantiate(&mut store, &component, &linker)?; From 46d408a83c14b08930304a5923f4044fe6549bb6 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 11 May 2025 16:28:55 -0700 Subject: [PATCH 2/2] Update expanded test expectations --- crates/component-macro/tests/expanded/char.rs | 26 +- .../tests/expanded/char_async.rs | 33 +- .../tests/expanded/char_tracing_async.rs | 33 +- .../tests/expanded/conventions.rs | 26 +- .../tests/expanded/conventions_async.rs | 33 +- .../expanded/conventions_tracing_async.rs | 33 +- .../tests/expanded/dead-code.rs | 49 +- .../tests/expanded/dead-code_async.rs | 58 +- .../tests/expanded/dead-code_tracing_async.rs | 58 +- .../tests/expanded/direct-import.rs | 17 +- .../tests/expanded/direct-import_async.rs | 21 +- .../expanded/direct-import_concurrent.rs | 232 -- .../expanded/direct-import_tracing_async.rs | 21 +- .../tests/expanded/empty_concurrent.rs | 148 -- .../component-macro/tests/expanded/flags.rs | 26 +- .../tests/expanded/flags_async.rs | 33 +- .../tests/expanded/flags_tracing_async.rs | 33 +- .../component-macro/tests/expanded/floats.rs | 26 +- .../tests/expanded/floats_async.rs | 33 +- .../tests/expanded/floats_tracing_async.rs | 33 +- .../tests/expanded/host-world.rs | 17 +- .../tests/expanded/host-world_async.rs | 21 +- .../tests/expanded/host-world_concurrent.rs | 232 -- .../expanded/host-world_tracing_async.rs | 21 +- .../tests/expanded/integers.rs | 26 +- .../tests/expanded/integers_async.rs | 33 +- .../tests/expanded/integers_tracing_async.rs | 33 +- .../component-macro/tests/expanded/lists.rs | 26 +- .../tests/expanded/lists_async.rs | 33 +- .../tests/expanded/lists_tracing_async.rs | 33 +- .../tests/expanded/many-arguments.rs | 26 +- .../tests/expanded/many-arguments_async.rs | 33 +- .../expanded/many-arguments_tracing_async.rs | 33 +- .../tests/expanded/multi-return_concurrent.rs | 707 ------ .../tests/expanded/multiversion.rs | 45 +- .../tests/expanded/multiversion_async.rs | 56 +- .../tests/expanded/multiversion_concurrent.rs | 532 ---- .../expanded/multiversion_tracing_async.rs | 56 +- .../component-macro/tests/expanded/path1.rs | 26 +- .../tests/expanded/path1_async.rs | 33 +- .../tests/expanded/path1_concurrent.rs | 194 -- .../tests/expanded/path1_tracing_async.rs | 33 +- .../component-macro/tests/expanded/path2.rs | 26 +- .../tests/expanded/path2_async.rs | 33 +- .../tests/expanded/path2_tracing_async.rs | 33 +- .../component-macro/tests/expanded/records.rs | 26 +- .../tests/expanded/records_async.rs | 33 +- .../tests/expanded/records_concurrent.rs | 1529 ----------- .../tests/expanded/records_tracing_async.rs | 33 +- .../component-macro/tests/expanded/rename.rs | 45 +- .../tests/expanded/rename_async.rs | 56 +- .../tests/expanded/rename_concurrent.rs | 293 --- .../tests/expanded/rename_tracing_async.rs | 56 +- .../tests/expanded/resources-export.rs | 26 +- .../tests/expanded/resources-export_async.rs | 33 +- .../expanded/resources-export_concurrent.rs | 804 ------ .../resources-export_tracing_async.rs | 33 +- .../tests/expanded/resources-import.rs | 138 +- .../tests/expanded/resources-import_async.rs | 164 +- .../expanded/resources-import_concurrent.rs | 2262 ----------------- .../resources-import_tracing_async.rs | 164 +- .../tests/expanded/share-types.rs | 45 +- .../tests/expanded/share-types_async.rs | 56 +- .../tests/expanded/share-types_concurrent.rs | 434 ---- .../expanded/share-types_tracing_async.rs | 56 +- .../tests/expanded/simple-functions.rs | 26 +- .../tests/expanded/simple-functions_async.rs | 33 +- .../simple-functions_tracing_async.rs | 33 +- .../tests/expanded/simple-lists.rs | 26 +- .../tests/expanded/simple-lists_async.rs | 33 +- .../tests/expanded/simple-lists_concurrent.rs | 722 ------ .../expanded/simple-lists_tracing_async.rs | 33 +- .../tests/expanded/simple-wasi.rs | 47 +- .../tests/expanded/simple-wasi_async.rs | 58 +- .../expanded/simple-wasi_tracing_async.rs | 58 +- .../tests/expanded/small-anonymous.rs | 26 +- .../tests/expanded/small-anonymous_async.rs | 33 +- .../expanded/small-anonymous_tracing_async.rs | 33 +- .../expanded/smoke-default_concurrent.rs | 181 -- .../component-macro/tests/expanded/smoke.rs | 26 +- .../tests/expanded/smoke_async.rs | 33 +- .../tests/expanded/smoke_tracing_async.rs | 33 +- .../component-macro/tests/expanded/strings.rs | 26 +- .../tests/expanded/strings_async.rs | 33 +- .../tests/expanded/strings_tracing_async.rs | 33 +- .../tests/expanded/unstable-features.rs | 44 +- .../tests/expanded/unstable-features_async.rs | 52 +- .../expanded/unstable-features_concurrent.rs | 644 ----- .../unstable-features_tracing_async.rs | 52 +- .../tests/expanded/unversioned-foo.rs | 26 +- .../tests/expanded/unversioned-foo_async.rs | 33 +- .../expanded/unversioned-foo_concurrent.rs | 280 -- .../expanded/unversioned-foo_tracing_async.rs | 33 +- .../tests/expanded/use-paths.rs | 85 +- .../tests/expanded/use-paths_async.rs | 104 +- .../tests/expanded/use-paths_tracing_async.rs | 104 +- .../tests/expanded/variants.rs | 26 +- .../tests/expanded/variants_async.rs | 33 +- .../tests/expanded/variants_tracing_async.rs | 33 +- .../tests/expanded/wat_concurrent.rs | 225 -- .../tests/expanded/worlds-with-types.rs | 26 +- .../tests/expanded/worlds-with-types_async.rs | 33 +- .../expanded/worlds-with-types_concurrent.rs | 259 -- .../worlds-with-types_tracing_async.rs | 33 +- 104 files changed, 1202 insertions(+), 12048 deletions(-) delete mode 100644 crates/component-macro/tests/expanded/direct-import_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/empty_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/host-world_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/multi-return_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/multiversion_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/path1_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/records_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/rename_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/resources-export_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/resources-import_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/share-types_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/simple-lists_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/smoke-default_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/unstable-features_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/wat_concurrent.rs delete mode 100644 crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs diff --git a/crates/component-macro/tests/expanded/char.rs b/crates/component-macro/tests/expanded/char.rs index 23ae9775ca88..3a8b3df9db15 100644 --- a/crates/component-macro/tests/expanded/char.rs +++ b/crates/component-macro/tests/expanded/char.rs @@ -144,15 +144,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::chars::Host, T: 'static, - U: foo::foo::chars::Host, { - foo::foo::chars::add_to_linker(linker, get)?; + foo::foo::chars::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest { @@ -172,13 +173,14 @@ pub mod foo { /// A function that returns a character fn return_char(&mut self) -> char; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/chars")?; inst.func_wrap( @@ -202,16 +204,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { /// A function that accepts a character fn take_char(&mut self, x: char) -> () { diff --git a/crates/component-macro/tests/expanded/char_async.rs b/crates/component-macro/tests/expanded/char_async.rs index 324d1bc60826..1fc506670e8b 100644 --- a/crates/component-macro/tests/expanded/char_async.rs +++ b/crates/component-macro/tests/expanded/char_async.rs @@ -150,16 +150,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::chars::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::chars::Host + Send, + T: Send + 'static, { - foo::foo::chars::add_to_linker(linker, get)?; + foo::foo::chars::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest { @@ -180,14 +180,14 @@ pub mod foo { /// A function that returns a character async fn return_char(&mut self) -> char; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/chars")?; inst.func_wrap_async( @@ -215,17 +215,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { /// A function that accepts a character async fn take_char(&mut self, x: char) -> () { diff --git a/crates/component-macro/tests/expanded/char_tracing_async.rs b/crates/component-macro/tests/expanded/char_tracing_async.rs index 5d7d1a14881a..f471550b953c 100644 --- a/crates/component-macro/tests/expanded/char_tracing_async.rs +++ b/crates/component-macro/tests/expanded/char_tracing_async.rs @@ -150,16 +150,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::chars::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::chars::Host + Send, + T: Send + 'static, { - foo::foo::chars::add_to_linker(linker, get)?; + foo::foo::chars::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest { @@ -180,14 +180,14 @@ pub mod foo { /// A function that returns a character async fn return_char(&mut self) -> char; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/chars")?; inst.func_wrap_async( @@ -244,17 +244,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { /// A function that accepts a character async fn take_char(&mut self, x: char) -> () { diff --git a/crates/component-macro/tests/expanded/conventions.rs b/crates/component-macro/tests/expanded/conventions.rs index cad5a33e5fbc..e22daa351495 100644 --- a/crates/component-macro/tests/expanded/conventions.rs +++ b/crates/component-macro/tests/expanded/conventions.rs @@ -146,15 +146,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::conventions::Host, T: 'static, - U: foo::foo::conventions::Host, { - foo::foo::conventions::add_to_linker(linker, get)?; + foo::foo::conventions::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_conventions(&self) -> &exports::foo::foo::conventions::Guest { @@ -220,13 +221,14 @@ pub mod foo { /// Identifiers with the same name as keywords are quoted. fn bool(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/conventions")?; inst.func_wrap( @@ -330,16 +332,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn kebab_case(&mut self) -> () { Host::kebab_case(*self) diff --git a/crates/component-macro/tests/expanded/conventions_async.rs b/crates/component-macro/tests/expanded/conventions_async.rs index c4e6c851842f..39ae2e1e17d8 100644 --- a/crates/component-macro/tests/expanded/conventions_async.rs +++ b/crates/component-macro/tests/expanded/conventions_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::conventions::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::conventions::Host + Send, + T: Send + 'static, { - foo::foo::conventions::add_to_linker(linker, get)?; + foo::foo::conventions::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_conventions(&self) -> &exports::foo::foo::conventions::Guest { @@ -228,14 +228,14 @@ pub mod foo { /// Identifiers with the same name as keywords are quoted. async fn bool(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/conventions")?; inst.func_wrap_async( @@ -363,17 +363,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn kebab_case(&mut self) -> () { Host::kebab_case(*self).await diff --git a/crates/component-macro/tests/expanded/conventions_tracing_async.rs b/crates/component-macro/tests/expanded/conventions_tracing_async.rs index 91c64ac6a1f0..15d7fcdfe2d2 100644 --- a/crates/component-macro/tests/expanded/conventions_tracing_async.rs +++ b/crates/component-macro/tests/expanded/conventions_tracing_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::conventions::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::conventions::Host + Send, + T: Send + 'static, { - foo::foo::conventions::add_to_linker(linker, get)?; + foo::foo::conventions::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_conventions(&self) -> &exports::foo::foo::conventions::Guest { @@ -228,14 +228,14 @@ pub mod foo { /// Identifiers with the same name as keywords are quoted. async fn bool(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/conventions")?; inst.func_wrap_async( @@ -523,17 +523,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn kebab_case(&mut self) -> () { Host::kebab_case(*self).await diff --git a/crates/component-macro/tests/expanded/dead-code.rs b/crates/component-macro/tests/expanded/dead-code.rs index 9aaa2e621172..c3a5b6cd1fd4 100644 --- a/crates/component-macro/tests/expanded/dead-code.rs +++ b/crates/component-macro/tests/expanded/dead-code.rs @@ -138,17 +138,20 @@ const _: () = { let indices = ImportsIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - U: a::b::interface_with_live_type::Host + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: a::b::interface_with_live_type::Host + a::b::interface_with_dead_type::Host, + T: 'static, { - a::b::interface_with_live_type::add_to_linker(linker, get)?; - a::b::interface_with_dead_type::add_to_linker(linker, get)?; + a::b::interface_with_live_type::add_to_linker::(linker, get)?; + a::b::interface_with_dead_type::add_to_linker::(linker, get)?; Ok(()) } } @@ -182,13 +185,14 @@ pub mod a { pub trait Host { fn f(&mut self) -> LiveType; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("a:b/interface-with-live-type")?; inst.func_wrap( @@ -201,16 +205,6 @@ pub mod a { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn f(&mut self) -> LiveType { Host::f(*self) @@ -272,27 +266,18 @@ pub mod a { assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32); }; pub trait Host {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("a:b/interface-with-dead-type")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/dead-code_async.rs b/crates/component-macro/tests/expanded/dead-code_async.rs index 906b58499c7c..db914bb5c4cf 100644 --- a/crates/component-macro/tests/expanded/dead-code_async.rs +++ b/crates/component-macro/tests/expanded/dead-code_async.rs @@ -144,18 +144,20 @@ const _: () = { let indices = ImportsIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: a::b::interface_with_live_type::Host + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: a::b::interface_with_live_type::Host + a::b::interface_with_dead_type::Host + Send, + T: Send + 'static, { - a::b::interface_with_live_type::add_to_linker(linker, get)?; - a::b::interface_with_dead_type::add_to_linker(linker, get)?; + a::b::interface_with_live_type::add_to_linker::(linker, get)?; + a::b::interface_with_dead_type::add_to_linker::(linker, get)?; Ok(()) } } @@ -190,14 +192,14 @@ pub mod a { pub trait Host: Send { async fn f(&mut self) -> LiveType; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("a:b/interface-with-live-type")?; inst.func_wrap_async( @@ -212,17 +214,6 @@ pub mod a { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn f(&mut self) -> LiveType { Host::f(*self).await @@ -285,29 +276,18 @@ pub mod a { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("a:b/interface-with-dead-type")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/dead-code_tracing_async.rs b/crates/component-macro/tests/expanded/dead-code_tracing_async.rs index fc7df1492de9..90654d1a9c78 100644 --- a/crates/component-macro/tests/expanded/dead-code_tracing_async.rs +++ b/crates/component-macro/tests/expanded/dead-code_tracing_async.rs @@ -144,18 +144,20 @@ const _: () = { let indices = ImportsIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: a::b::interface_with_live_type::Host + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: a::b::interface_with_live_type::Host + a::b::interface_with_dead_type::Host + Send, + T: Send + 'static, { - a::b::interface_with_live_type::add_to_linker(linker, get)?; - a::b::interface_with_dead_type::add_to_linker(linker, get)?; + a::b::interface_with_live_type::add_to_linker::(linker, get)?; + a::b::interface_with_dead_type::add_to_linker::(linker, get)?; Ok(()) } } @@ -190,14 +192,14 @@ pub mod a { pub trait Host: Send { async fn f(&mut self) -> LiveType; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("a:b/interface-with-live-type")?; inst.func_wrap_async( @@ -225,17 +227,6 @@ pub mod a { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn f(&mut self) -> LiveType { Host::f(*self).await @@ -298,29 +289,18 @@ pub mod a { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("a:b/interface-with-dead-type")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/direct-import.rs b/crates/component-macro/tests/expanded/direct-import.rs index 37cdac572e04..26d8772f05e2 100644 --- a/crates/component-macro/tests/expanded/direct-import.rs +++ b/crates/component-macro/tests/expanded/direct-import.rs @@ -146,12 +146,14 @@ const _: () = { let indices = FooIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: FooImports>, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: FooImports, + T: 'static, { let mut linker = linker.root(); linker @@ -165,15 +167,16 @@ const _: () = { )?; Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: FooImports, T: 'static, - U: FooImports, { - Self::add_to_linker_imports_get_host(linker, get)?; + Self::add_to_linker_imports::(linker, get)?; Ok(()) } } diff --git a/crates/component-macro/tests/expanded/direct-import_async.rs b/crates/component-macro/tests/expanded/direct-import_async.rs index 1b1c0a01f503..c96d73bec88d 100644 --- a/crates/component-macro/tests/expanded/direct-import_async.rs +++ b/crates/component-macro/tests/expanded/direct-import_async.rs @@ -153,13 +153,14 @@ const _: () = { let indices = FooIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: FooImports>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: FooImports, + T: Send + 'static, { let mut linker = linker.root(); linker @@ -175,16 +176,16 @@ const _: () = { )?; Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: FooImports + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: FooImports + Send, + T: Send + 'static, { - Self::add_to_linker_imports_get_host(linker, get)?; + Self::add_to_linker_imports::(linker, get)?; Ok(()) } } diff --git a/crates/component-macro/tests/expanded/direct-import_concurrent.rs b/crates/component-macro/tests/expanded/direct-import_concurrent.rs deleted file mode 100644 index fff4b35c3ba9..000000000000 --- a/crates/component-macro/tests/expanded/direct-import_concurrent.rs +++ /dev/null @@ -1,232 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `foo`. -/// -/// This structure is created through [`FooPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`Foo`] as well. -pub struct FooPre { - instance_pre: wasmtime::component::InstancePre, - indices: FooIndices, -} -impl Clone for FooPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> FooPre<_T> { - /// Creates a new copy of `FooPre` 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 { - let indices = FooIndices::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 [`Foo`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `foo`. -/// -/// This is an implementation detail of [`FooPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`Foo`] as well. -#[derive(Clone)] -pub struct FooIndices {} -/// Auto-generated bindings for an instance a component which -/// implements the world `foo`. -/// -/// 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 -/// [`Foo::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`FooPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`FooPre::instantiate_async`] to -/// create a [`Foo`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`Foo::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 Foo {} -pub trait FooImports { - type Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; -} -impl<_T: FooImports> FooImports for &mut _T { - type Data = _T::Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as FooImports>::foo(store) - } -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl FooIndices { - /// Creates a new copy of `FooIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - Ok(FooIndices {}) - } - /// Uses the indices stored in `self` to load an instance - /// of [`Foo`] 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 { - let _ = &mut store; - let _instance = instance; - Ok(Foo {}) - } - } - impl Foo { - /// Convenience wrapper around [`FooPre::new`] and - /// [`FooPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - FooPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`FooIndices::new`] and - /// [`FooIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = FooIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker_imports_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: FooImports, - >, - T: Send, - { - let mut linker = linker.root(); - linker - .func_wrap_concurrent( - "foo", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::foo(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + FooImports, - U: Send + FooImports, - { - Self::add_to_linker_imports_get_host(linker, get)?; - Ok(()) - } - } -}; diff --git a/crates/component-macro/tests/expanded/direct-import_tracing_async.rs b/crates/component-macro/tests/expanded/direct-import_tracing_async.rs index a4f9dd9c5163..38a33791f738 100644 --- a/crates/component-macro/tests/expanded/direct-import_tracing_async.rs +++ b/crates/component-macro/tests/expanded/direct-import_tracing_async.rs @@ -153,13 +153,14 @@ const _: () = { let indices = FooIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: FooImports>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: FooImports, + T: Send + 'static, { let mut linker = linker.root(); linker @@ -188,16 +189,16 @@ const _: () = { )?; Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: FooImports + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: FooImports + Send, + T: Send + 'static, { - Self::add_to_linker_imports_get_host(linker, get)?; + Self::add_to_linker_imports::(linker, get)?; Ok(()) } } diff --git a/crates/component-macro/tests/expanded/empty_concurrent.rs b/crates/component-macro/tests/expanded/empty_concurrent.rs deleted file mode 100644 index 004890450fc6..000000000000 --- a/crates/component-macro/tests/expanded/empty_concurrent.rs +++ /dev/null @@ -1,148 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `empty`. -/// -/// This structure is created through [`EmptyPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`Empty`] as well. -pub struct EmptyPre { - instance_pre: wasmtime::component::InstancePre, - indices: EmptyIndices, -} -impl Clone for EmptyPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> EmptyPre<_T> { - /// Creates a new copy of `EmptyPre` 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 { - let indices = EmptyIndices::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 [`Empty`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `empty`. -/// -/// This is an implementation detail of [`EmptyPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`Empty`] as well. -#[derive(Clone)] -pub struct EmptyIndices {} -/// Auto-generated bindings for an instance a component which -/// implements the world `empty`. -/// -/// 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 -/// [`Empty::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`EmptyPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`EmptyPre::instantiate_async`] to -/// create a [`Empty`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`Empty::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 Empty {} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl EmptyIndices { - /// Creates a new copy of `EmptyIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - Ok(EmptyIndices {}) - } - /// Uses the indices stored in `self` to load an instance - /// of [`Empty`] 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 { - let _ = &mut store; - let _instance = instance; - Ok(Empty {}) - } - } - impl Empty { - /// Convenience wrapper around [`EmptyPre::new`] and - /// [`EmptyPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - EmptyPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`EmptyIndices::new`] and - /// [`EmptyIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = EmptyIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - } -}; diff --git a/crates/component-macro/tests/expanded/flags.rs b/crates/component-macro/tests/expanded/flags.rs index d8366434526e..17308c02fd7a 100644 --- a/crates/component-macro/tests/expanded/flags.rs +++ b/crates/component-macro/tests/expanded/flags.rs @@ -144,15 +144,16 @@ const _: () = { let indices = TheFlagsIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::flegs::Host, T: 'static, - U: foo::foo::flegs::Host, { - foo::foo::flegs::add_to_linker(linker, get)?; + foo::foo::flegs::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_flegs(&self) -> &exports::foo::foo::flegs::Guest { @@ -289,13 +290,14 @@ pub mod foo { fn roundtrip_flag32(&mut self, x: Flag32) -> Flag32; fn roundtrip_flag64(&mut self, x: Flag64) -> Flag64; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/flegs")?; inst.func_wrap( @@ -377,16 +379,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn roundtrip_flag1(&mut self, x: Flag1) -> Flag1 { Host::roundtrip_flag1(*self, x) diff --git a/crates/component-macro/tests/expanded/flags_async.rs b/crates/component-macro/tests/expanded/flags_async.rs index b0412c6a9ce4..acb88e81598a 100644 --- a/crates/component-macro/tests/expanded/flags_async.rs +++ b/crates/component-macro/tests/expanded/flags_async.rs @@ -150,16 +150,16 @@ const _: () = { let indices = TheFlagsIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::flegs::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::flegs::Host + Send, + T: Send + 'static, { - foo::foo::flegs::add_to_linker(linker, get)?; + foo::foo::flegs::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_flegs(&self) -> &exports::foo::foo::flegs::Guest { @@ -297,14 +297,14 @@ pub mod foo { async fn roundtrip_flag32(&mut self, x: Flag32) -> Flag32; async fn roundtrip_flag64(&mut self, x: Flag64) -> Flag64; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/flegs")?; inst.func_wrap_async( @@ -400,17 +400,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn roundtrip_flag1(&mut self, x: Flag1) -> Flag1 { Host::roundtrip_flag1(*self, x).await diff --git a/crates/component-macro/tests/expanded/flags_tracing_async.rs b/crates/component-macro/tests/expanded/flags_tracing_async.rs index 57323e2c8824..09aea26c6175 100644 --- a/crates/component-macro/tests/expanded/flags_tracing_async.rs +++ b/crates/component-macro/tests/expanded/flags_tracing_async.rs @@ -150,16 +150,16 @@ const _: () = { let indices = TheFlagsIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::flegs::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::flegs::Host + Send, + T: Send + 'static, { - foo::foo::flegs::add_to_linker(linker, get)?; + foo::foo::flegs::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_flegs(&self) -> &exports::foo::foo::flegs::Guest { @@ -297,14 +297,14 @@ pub mod foo { async fn roundtrip_flag32(&mut self, x: Flag32) -> Flag32; async fn roundtrip_flag64(&mut self, x: Flag64) -> Flag64; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/flegs")?; inst.func_wrap_async( @@ -512,17 +512,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn roundtrip_flag1(&mut self, x: Flag1) -> Flag1 { Host::roundtrip_flag1(*self, x).await diff --git a/crates/component-macro/tests/expanded/floats.rs b/crates/component-macro/tests/expanded/floats.rs index a43ea889daa8..70e160867136 100644 --- a/crates/component-macro/tests/expanded/floats.rs +++ b/crates/component-macro/tests/expanded/floats.rs @@ -146,15 +146,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::floats::Host, T: 'static, - U: foo::foo::floats::Host, { - foo::foo::floats::add_to_linker(linker, get)?; + foo::foo::floats::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_floats(&self) -> &exports::foo::foo::floats::Guest { @@ -174,13 +175,14 @@ pub mod foo { fn f32_result(&mut self) -> f32; fn f64_result(&mut self) -> f64; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/floats")?; inst.func_wrap( @@ -217,16 +219,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn f32_param(&mut self, x: f32) -> () { Host::f32_param(*self, x) diff --git a/crates/component-macro/tests/expanded/floats_async.rs b/crates/component-macro/tests/expanded/floats_async.rs index e155bf523c24..b3dc4b67c4fd 100644 --- a/crates/component-macro/tests/expanded/floats_async.rs +++ b/crates/component-macro/tests/expanded/floats_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::floats::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::floats::Host + Send, + T: Send + 'static, { - foo::foo::floats::add_to_linker(linker, get)?; + foo::foo::floats::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_floats(&self) -> &exports::foo::foo::floats::Guest { @@ -182,14 +182,14 @@ pub mod foo { async fn f32_result(&mut self) -> f32; async fn f64_result(&mut self) -> f64; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/floats")?; inst.func_wrap_async( @@ -234,17 +234,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn f32_param(&mut self, x: f32) -> () { Host::f32_param(*self, x).await diff --git a/crates/component-macro/tests/expanded/floats_tracing_async.rs b/crates/component-macro/tests/expanded/floats_tracing_async.rs index b2277e5d2ddb..816d17d79ac1 100644 --- a/crates/component-macro/tests/expanded/floats_tracing_async.rs +++ b/crates/component-macro/tests/expanded/floats_tracing_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::floats::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::floats::Host + Send, + T: Send + 'static, { - foo::foo::floats::add_to_linker(linker, get)?; + foo::foo::floats::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_floats(&self) -> &exports::foo::foo::floats::Guest { @@ -182,14 +182,14 @@ pub mod foo { async fn f32_result(&mut self) -> f32; async fn f64_result(&mut self) -> f64; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/floats")?; inst.func_wrap_async( @@ -292,17 +292,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn f32_param(&mut self, x: f32) -> () { Host::f32_param(*self, x).await diff --git a/crates/component-macro/tests/expanded/host-world.rs b/crates/component-macro/tests/expanded/host-world.rs index aec33d8486ae..627dc49fa099 100644 --- a/crates/component-macro/tests/expanded/host-world.rs +++ b/crates/component-macro/tests/expanded/host-world.rs @@ -146,12 +146,14 @@ const _: () = { let indices = Host_Indices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host_Imports>, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host_Imports, + T: 'static, { let mut linker = linker.root(); linker @@ -165,15 +167,16 @@ const _: () = { )?; Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host_Imports, T: 'static, - U: Host_Imports, { - Self::add_to_linker_imports_get_host(linker, get)?; + Self::add_to_linker_imports::(linker, get)?; Ok(()) } } diff --git a/crates/component-macro/tests/expanded/host-world_async.rs b/crates/component-macro/tests/expanded/host-world_async.rs index fe8d2ff9f4ab..98344028f30d 100644 --- a/crates/component-macro/tests/expanded/host-world_async.rs +++ b/crates/component-macro/tests/expanded/host-world_async.rs @@ -153,13 +153,14 @@ const _: () = { let indices = Host_Indices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host_Imports>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host_Imports, + T: Send + 'static, { let mut linker = linker.root(); linker @@ -175,16 +176,16 @@ const _: () = { )?; Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: Host_Imports + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host_Imports + Send, + T: Send + 'static, { - Self::add_to_linker_imports_get_host(linker, get)?; + Self::add_to_linker_imports::(linker, get)?; Ok(()) } } diff --git a/crates/component-macro/tests/expanded/host-world_concurrent.rs b/crates/component-macro/tests/expanded/host-world_concurrent.rs deleted file mode 100644 index 64aafda7af78..000000000000 --- a/crates/component-macro/tests/expanded/host-world_concurrent.rs +++ /dev/null @@ -1,232 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `host`. -/// -/// This structure is created through [`Host_Pre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`Host_`] as well. -pub struct Host_Pre { - instance_pre: wasmtime::component::InstancePre, - indices: Host_Indices, -} -impl Clone for Host_Pre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> Host_Pre<_T> { - /// Creates a new copy of `Host_Pre` 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 { - let indices = Host_Indices::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 [`Host_`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `host`. -/// -/// This is an implementation detail of [`Host_Pre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`Host_`] as well. -#[derive(Clone)] -pub struct Host_Indices {} -/// Auto-generated bindings for an instance a component which -/// implements the world `host`. -/// -/// 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 -/// [`Host_::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`Host_Pre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`Host_Pre::instantiate_async`] to -/// create a [`Host_`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`Host_::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 Host_ {} -pub trait Host_Imports { - type Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; -} -impl<_T: Host_Imports> Host_Imports for &mut _T { - type Data = _T::Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host_Imports>::foo(store) - } -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl Host_Indices { - /// Creates a new copy of `Host_Indices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - Ok(Host_Indices {}) - } - /// Uses the indices stored in `self` to load an instance - /// of [`Host_`] 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 { - let _ = &mut store; - let _instance = instance; - Ok(Host_ {}) - } - } - impl Host_ { - /// Convenience wrapper around [`Host_Pre::new`] and - /// [`Host_Pre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - Host_Pre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`Host_Indices::new`] and - /// [`Host_Indices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = Host_Indices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker_imports_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host_Imports, - >, - T: Send, - { - let mut linker = linker.root(); - linker - .func_wrap_concurrent( - "foo", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::foo(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + Host_Imports, - U: Send + Host_Imports, - { - Self::add_to_linker_imports_get_host(linker, get)?; - Ok(()) - } - } -}; diff --git a/crates/component-macro/tests/expanded/host-world_tracing_async.rs b/crates/component-macro/tests/expanded/host-world_tracing_async.rs index 047aae0be6f3..883cee5b5df4 100644 --- a/crates/component-macro/tests/expanded/host-world_tracing_async.rs +++ b/crates/component-macro/tests/expanded/host-world_tracing_async.rs @@ -153,13 +153,14 @@ const _: () = { let indices = Host_Indices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host_Imports>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host_Imports, + T: Send + 'static, { let mut linker = linker.root(); linker @@ -188,16 +189,16 @@ const _: () = { )?; Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: Host_Imports + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host_Imports + Send, + T: Send + 'static, { - Self::add_to_linker_imports_get_host(linker, get)?; + Self::add_to_linker_imports::(linker, get)?; Ok(()) } } diff --git a/crates/component-macro/tests/expanded/integers.rs b/crates/component-macro/tests/expanded/integers.rs index 34dd60f2c0a1..7e998938c809 100644 --- a/crates/component-macro/tests/expanded/integers.rs +++ b/crates/component-macro/tests/expanded/integers.rs @@ -146,15 +146,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::integers::Host, T: 'static, - U: foo::foo::integers::Host, { - foo::foo::integers::add_to_linker(linker, get)?; + foo::foo::integers::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_integers(&self) -> &exports::foo::foo::integers::Guest { @@ -198,13 +199,14 @@ pub mod foo { fn r8(&mut self) -> i64; fn pair_ret(&mut self) -> (i64, u8); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/integers")?; inst.func_wrap( @@ -375,16 +377,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn a1(&mut self, x: u8) -> () { Host::a1(*self, x) diff --git a/crates/component-macro/tests/expanded/integers_async.rs b/crates/component-macro/tests/expanded/integers_async.rs index 661cb2a69b28..85fed06bd7f7 100644 --- a/crates/component-macro/tests/expanded/integers_async.rs +++ b/crates/component-macro/tests/expanded/integers_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::integers::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::integers::Host + Send, + T: Send + 'static, { - foo::foo::integers::add_to_linker(linker, get)?; + foo::foo::integers::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_integers(&self) -> &exports::foo::foo::integers::Guest { @@ -206,14 +206,14 @@ pub mod foo { async fn r8(&mut self) -> i64; async fn pair_ret(&mut self) -> (i64, u8); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/integers")?; inst.func_wrap_async( @@ -421,17 +421,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a1(&mut self, x: u8) -> () { Host::a1(*self, x).await diff --git a/crates/component-macro/tests/expanded/integers_tracing_async.rs b/crates/component-macro/tests/expanded/integers_tracing_async.rs index e4e3d3ded9e7..af209278533a 100644 --- a/crates/component-macro/tests/expanded/integers_tracing_async.rs +++ b/crates/component-macro/tests/expanded/integers_tracing_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::integers::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::integers::Host + Send, + T: Send + 'static, { - foo::foo::integers::add_to_linker(linker, get)?; + foo::foo::integers::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_integers(&self) -> &exports::foo::foo::integers::Guest { @@ -206,14 +206,14 @@ pub mod foo { async fn r8(&mut self) -> i64; async fn pair_ret(&mut self) -> (i64, u8); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/integers")?; inst.func_wrap_async( @@ -686,17 +686,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a1(&mut self, x: u8) -> () { Host::a1(*self, x).await diff --git a/crates/component-macro/tests/expanded/lists.rs b/crates/component-macro/tests/expanded/lists.rs index 2c59d1f7f64a..779e70bd64fa 100644 --- a/crates/component-macro/tests/expanded/lists.rs +++ b/crates/component-macro/tests/expanded/lists.rs @@ -144,15 +144,16 @@ const _: () = { let indices = TheListsIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::lists::Host, T: 'static, - U: foo::foo::lists::Host, { - foo::foo::lists::add_to_linker(linker, get)?; + foo::foo::lists::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_lists(&self) -> &exports::foo::foo::lists::Guest { @@ -445,13 +446,14 @@ pub mod foo { a: LoadStoreAllSizes, ) -> LoadStoreAllSizes; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/lists")?; inst.func_wrap( @@ -760,16 +762,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn list_u8_param( &mut self, diff --git a/crates/component-macro/tests/expanded/lists_async.rs b/crates/component-macro/tests/expanded/lists_async.rs index 0246562e44b8..bc313dcd4da6 100644 --- a/crates/component-macro/tests/expanded/lists_async.rs +++ b/crates/component-macro/tests/expanded/lists_async.rs @@ -150,16 +150,16 @@ const _: () = { let indices = TheListsIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::lists::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::lists::Host + Send, + T: Send + 'static, { - foo::foo::lists::add_to_linker(linker, get)?; + foo::foo::lists::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_lists(&self) -> &exports::foo::foo::lists::Guest { @@ -473,14 +473,14 @@ pub mod foo { a: LoadStoreAllSizes, ) -> LoadStoreAllSizes; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/lists")?; inst.func_wrap_async( @@ -847,17 +847,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn list_u8_param( &mut self, diff --git a/crates/component-macro/tests/expanded/lists_tracing_async.rs b/crates/component-macro/tests/expanded/lists_tracing_async.rs index 209ff0099354..fb5dfbedd75a 100644 --- a/crates/component-macro/tests/expanded/lists_tracing_async.rs +++ b/crates/component-macro/tests/expanded/lists_tracing_async.rs @@ -150,16 +150,16 @@ const _: () = { let indices = TheListsIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::lists::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::lists::Host + Send, + T: Send + 'static, { - foo::foo::lists::add_to_linker(linker, get)?; + foo::foo::lists::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_lists(&self) -> &exports::foo::foo::lists::Guest { @@ -473,14 +473,14 @@ pub mod foo { a: LoadStoreAllSizes, ) -> LoadStoreAllSizes; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/lists")?; inst.func_wrap_async( @@ -1278,17 +1278,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn list_u8_param( &mut self, diff --git a/crates/component-macro/tests/expanded/many-arguments.rs b/crates/component-macro/tests/expanded/many-arguments.rs index 4c238bdb6d3b..87e45dc13f9a 100644 --- a/crates/component-macro/tests/expanded/many-arguments.rs +++ b/crates/component-macro/tests/expanded/many-arguments.rs @@ -146,15 +146,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::manyarg::Host, T: 'static, - U: foo::foo::manyarg::Host, { - foo::foo::manyarg::add_to_linker(linker, get)?; + foo::foo::manyarg::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_manyarg(&self) -> &exports::foo::foo::manyarg::Guest { @@ -271,13 +272,14 @@ pub mod foo { ) -> (); fn big_argument(&mut self, x: BigStruct) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/manyarg")?; inst.func_wrap( @@ -356,16 +358,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn many_args( &mut self, diff --git a/crates/component-macro/tests/expanded/many-arguments_async.rs b/crates/component-macro/tests/expanded/many-arguments_async.rs index b2f3a87db971..49ee28dd53a4 100644 --- a/crates/component-macro/tests/expanded/many-arguments_async.rs +++ b/crates/component-macro/tests/expanded/many-arguments_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::manyarg::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::manyarg::Host + Send, + T: Send + 'static, { - foo::foo::manyarg::add_to_linker(linker, get)?; + foo::foo::manyarg::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_manyarg(&self) -> &exports::foo::foo::manyarg::Guest { @@ -279,14 +279,14 @@ pub mod foo { ) -> (); async fn big_argument(&mut self, x: BigStruct) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/manyarg")?; inst.func_wrap_async( @@ -370,17 +370,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn many_args( &mut self, diff --git a/crates/component-macro/tests/expanded/many-arguments_tracing_async.rs b/crates/component-macro/tests/expanded/many-arguments_tracing_async.rs index 49b1711a640b..02dfe411bb2e 100644 --- a/crates/component-macro/tests/expanded/many-arguments_tracing_async.rs +++ b/crates/component-macro/tests/expanded/many-arguments_tracing_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::manyarg::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::manyarg::Host + Send, + T: Send + 'static, { - foo::foo::manyarg::add_to_linker(linker, get)?; + foo::foo::manyarg::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_manyarg(&self) -> &exports::foo::foo::manyarg::Guest { @@ -279,14 +279,14 @@ pub mod foo { ) -> (); async fn big_argument(&mut self, x: BigStruct) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/manyarg")?; inst.func_wrap_async( @@ -413,17 +413,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn many_args( &mut self, diff --git a/crates/component-macro/tests/expanded/multi-return_concurrent.rs b/crates/component-macro/tests/expanded/multi-return_concurrent.rs deleted file mode 100644 index 89789883a15a..000000000000 --- a/crates/component-macro/tests/expanded/multi-return_concurrent.rs +++ /dev/null @@ -1,707 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `the-world`. -/// -/// This structure is created through [`TheWorldPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`TheWorld`] as well. -pub struct TheWorldPre { - instance_pre: wasmtime::component::InstancePre, - indices: TheWorldIndices, -} -impl Clone for TheWorldPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T> TheWorldPre<_T> { - /// Creates a new copy of `TheWorldPre` 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 { - let indices = TheWorldIndices::new(instance_pre.component())?; - 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 [`TheWorld`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `the-world`. -/// -/// This is an implementation detail of [`TheWorldPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`TheWorld`] as well. -#[derive(Clone)] -pub struct TheWorldIndices { - interface0: exports::foo::foo::multi_return::GuestIndices, -} -/// Auto-generated bindings for an instance a component which -/// implements the world `the-world`. -/// -/// 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 -/// [`TheWorld::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`TheWorldPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`TheWorldPre::instantiate_async`] to -/// create a [`TheWorld`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`TheWorld::new`]. -/// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// -/// 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 TheWorld { - interface0: exports::foo::foo::multi_return::Guest, -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl TheWorldIndices { - /// Creates a new copy of `TheWorldIndices` 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( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::multi_return::GuestIndices::new( - _component, - )?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::multi_return::GuestIndices::new_instance( - &mut store, - _instance, - )?; - Ok(TheWorldIndices { interface0 }) - } - /// Uses the indices stored in `self` to load an instance - /// of [`TheWorld`] 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 { - let _instance = instance; - let interface0 = self.interface0.load(&mut store, &_instance)?; - Ok(TheWorld { interface0 }) - } - } - impl TheWorld { - /// Convenience wrapper around [`TheWorldPre::new`] and - /// [`TheWorldPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - TheWorldPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and - /// [`TheWorldIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: Send + foo::foo::multi_return::Host + 'static, - U: Send + foo::foo::multi_return::Host, - { - foo::foo::multi_return::add_to_linker(linker, get)?; - Ok(()) - } - pub fn foo_foo_multi_return(&self) -> &exports::foo::foo::multi_return::Guest { - &self.interface0 - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod multi_return { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub trait Host { - type Data; - fn mra( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn mrb( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn mrc( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> u32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn mrd( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> u32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn mre( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> (u32, f32) + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub trait GetHost< - T, - D, - >: Fn(T) -> >::Host + Send + Sync + Copy + 'static { - type Host: Host + Send; - } - impl GetHost for F - where - F: Fn(T) -> O + Send + Sync + Copy + 'static, - O: Host + Send, - { - type Host = O; - } - pub fn add_to_linker_get_host< - T, - G: for<'a> GetHost<&'a mut T, T, Host: Host + Send>, - >( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/multi-return")?; - inst.func_wrap_concurrent( - "mra", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::mra(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "mrb", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::mrb(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "mrc", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::mrc(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "mrd", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::mrd(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "mre", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::mre(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32, f32)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32, f32)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn mra( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::mra(store) - } - fn mrb( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::mrb(store) - } - fn mrc( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> u32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::mrc(store) - } - fn mrd( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> u32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::mrd(store) - } - fn mre( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> (u32, f32) + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::mre(store) - } - } - } - } -} -pub mod exports { - pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod multi_return { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub struct Guest { - mra: wasmtime::component::Func, - mrb: wasmtime::component::Func, - mrc: wasmtime::component::Func, - mrd: wasmtime::component::Func, - mre: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - mra: wasmtime::component::ComponentExportIndex, - mrb: wasmtime::component::ComponentExportIndex, - mrc: wasmtime::component::ComponentExportIndex, - mrd: wasmtime::component::ComponentExportIndex, - mre: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let (_, instance) = component - .export_index(None, "foo:foo/multi-return") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/multi-return`" - ) - })?; - Self::_new(|name| { - component.export_index(Some(&instance), name).map(|p| p.1) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export(&mut store, None, "foo:foo/multi-return") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/multi-return`" - ) - })?; - Self::_new(|name| { - instance.get_export(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { - let mut lookup = move |name| { - lookup(name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `foo:foo/multi-return` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let mra = lookup("mra")?; - let mrb = lookup("mrb")?; - let mrc = lookup("mrc")?; - let mrd = lookup("mrd")?; - let mre = lookup("mre")?; - Ok(GuestIndices { - mra, - mrb, - mrc, - mrd, - mre, - }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let mut store = store.as_context_mut(); - let _ = &mut store; - let _instance = instance; - let mra = *_instance - .get_typed_func::<(), ()>(&mut store, &self.mra)? - .func(); - let mrb = *_instance - .get_typed_func::<(), ()>(&mut store, &self.mrb)? - .func(); - let mrc = *_instance - .get_typed_func::<(), (u32,)>(&mut store, &self.mrc)? - .func(); - let mrd = *_instance - .get_typed_func::<(), (u32,)>(&mut store, &self.mrd)? - .func(); - let mre = *_instance - .get_typed_func::<(), (u32, f32)>(&mut store, &self.mre)? - .func(); - Ok(Guest { mra, mrb, mrc, mrd, mre }) - } - } - impl Guest { - pub async fn call_mra( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (), - >::new_unchecked(self.mra) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise) - } - pub async fn call_mrb( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (), - >::new_unchecked(self.mrb) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise) - } - pub async fn call_mrc( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (u32,), - >::new_unchecked(self.mrc) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_mrd( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (u32,), - >::new_unchecked(self.mrd) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_mre( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (u32, f32), - >::new_unchecked(self.mre) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise) - } - } - } - } - } -} diff --git a/crates/component-macro/tests/expanded/multiversion.rs b/crates/component-macro/tests/expanded/multiversion.rs index cc24f23ee700..684438a7932a 100644 --- a/crates/component-macro/tests/expanded/multiversion.rs +++ b/crates/component-macro/tests/expanded/multiversion.rs @@ -151,16 +151,17 @@ const _: () = { let indices = FooIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host, T: 'static, - U: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host, { - my::dep0_1_0::a::add_to_linker(linker, get)?; - my::dep0_2_0::a::add_to_linker(linker, get)?; + my::dep0_1_0::a::add_to_linker::(linker, get)?; + my::dep0_2_0::a::add_to_linker::(linker, get)?; Ok(()) } pub fn my_dep0_1_0_a(&self) -> &exports::my::dep0_1_0::a::Guest { @@ -180,13 +181,14 @@ pub mod my { pub trait Host { fn x(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("my:dep/a@0.1.0")?; inst.func_wrap( @@ -199,16 +201,6 @@ pub mod my { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn x(&mut self) -> () { Host::x(*self) @@ -224,13 +216,14 @@ pub mod my { pub trait Host { fn x(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("my:dep/a@0.2.0")?; inst.func_wrap( @@ -243,16 +236,6 @@ pub mod my { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn x(&mut self) -> () { Host::x(*self) diff --git a/crates/component-macro/tests/expanded/multiversion_async.rs b/crates/component-macro/tests/expanded/multiversion_async.rs index d65a80eeaa8d..a11f18b64c1b 100644 --- a/crates/component-macro/tests/expanded/multiversion_async.rs +++ b/crates/component-macro/tests/expanded/multiversion_async.rs @@ -157,17 +157,17 @@ const _: () = { let indices = FooIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host + Send, + T: Send + 'static, { - my::dep0_1_0::a::add_to_linker(linker, get)?; - my::dep0_2_0::a::add_to_linker(linker, get)?; + my::dep0_1_0::a::add_to_linker::(linker, get)?; + my::dep0_2_0::a::add_to_linker::(linker, get)?; Ok(()) } pub fn my_dep0_1_0_a(&self) -> &exports::my::dep0_1_0::a::Guest { @@ -188,14 +188,14 @@ pub mod my { pub trait Host: Send { async fn x(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("my:dep/a@0.1.0")?; inst.func_wrap_async( @@ -210,17 +210,6 @@ pub mod my { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn x(&mut self) -> () { Host::x(*self).await @@ -237,14 +226,14 @@ pub mod my { pub trait Host: Send { async fn x(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("my:dep/a@0.2.0")?; inst.func_wrap_async( @@ -259,17 +248,6 @@ pub mod my { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn x(&mut self) -> () { Host::x(*self).await diff --git a/crates/component-macro/tests/expanded/multiversion_concurrent.rs b/crates/component-macro/tests/expanded/multiversion_concurrent.rs deleted file mode 100644 index b26f35005144..000000000000 --- a/crates/component-macro/tests/expanded/multiversion_concurrent.rs +++ /dev/null @@ -1,532 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `foo`. -/// -/// This structure is created through [`FooPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`Foo`] as well. -pub struct FooPre { - instance_pre: wasmtime::component::InstancePre, - indices: FooIndices, -} -impl Clone for FooPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> FooPre<_T> { - /// Creates a new copy of `FooPre` 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 { - let indices = FooIndices::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 [`Foo`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `foo`. -/// -/// This is an implementation detail of [`FooPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`Foo`] as well. -#[derive(Clone)] -pub struct FooIndices { - interface0: exports::my::dep0_1_0::a::GuestIndices, - interface1: exports::my::dep0_2_0::a::GuestIndices, -} -/// Auto-generated bindings for an instance a component which -/// implements the world `foo`. -/// -/// 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 -/// [`Foo::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`FooPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`FooPre::instantiate_async`] to -/// create a [`Foo`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`Foo::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 Foo { - interface0: exports::my::dep0_1_0::a::Guest, - interface1: exports::my::dep0_2_0::a::Guest, -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl FooIndices { - /// Creates a new copy of `FooIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_instance_pre)?; - let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_instance_pre)?; - Ok(FooIndices { - interface0, - interface1, - }) - } - /// Uses the indices stored in `self` to load an instance - /// of [`Foo`] 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 { - let _ = &mut store; - let _instance = instance; - let interface0 = self.interface0.load(&mut store, &_instance)?; - let interface1 = self.interface1.load(&mut store, &_instance)?; - Ok(Foo { interface0, interface1 }) - } - } - impl Foo { - /// Convenience wrapper around [`FooPre::new`] and - /// [`FooPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - FooPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`FooIndices::new`] and - /// [`FooIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = FooIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + my::dep0_1_0::a::Host + my::dep0_2_0::a::Host, - U: Send + my::dep0_1_0::a::Host + my::dep0_2_0::a::Host, - { - my::dep0_1_0::a::add_to_linker(linker, get)?; - my::dep0_2_0::a::add_to_linker(linker, get)?; - Ok(()) - } - pub fn my_dep0_1_0_a(&self) -> &exports::my::dep0_1_0::a::Guest { - &self.interface0 - } - pub fn my_dep0_2_0_a(&self) -> &exports::my::dep0_2_0::a::Guest { - &self.interface1 - } - } -}; -pub mod my { - pub mod dep0_1_0 { - #[allow(clippy::all)] - pub mod a { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub trait Host { - type Data; - fn x( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host + Send, - >, - T: Send + 'static, - { - let mut inst = linker.instance("my:dep/a@0.1.0")?; - inst.func_wrap_concurrent( - "x", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::x(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn x( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::x(store) - } - } - } - } - pub mod dep0_2_0 { - #[allow(clippy::all)] - pub mod a { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub trait Host { - type Data; - fn x( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host + Send, - >, - T: Send + 'static, - { - let mut inst = linker.instance("my:dep/a@0.2.0")?; - inst.func_wrap_concurrent( - "x", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::x(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn x( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::x(store) - } - } - } - } -} -pub mod exports { - pub mod my { - pub mod dep0_1_0 { - #[allow(clippy::all)] - pub mod a { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub struct Guest { - x: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - x: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "my:dep/a@0.1.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.1.0`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `my:dep/a@0.1.0` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let x = lookup("x")?; - Ok(GuestIndices { x }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let x = *_instance - .get_typed_func::<(), ()>(&mut store, &self.x)? - .func(); - Ok(Guest { x }) - } - } - impl Guest { - pub async fn call_x( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (), - >::new_unchecked(self.x) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise) - } - } - } - } - pub mod dep0_2_0 { - #[allow(clippy::all)] - pub mod a { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub struct Guest { - x: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - x: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "my:dep/a@0.2.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.2.0`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `my:dep/a@0.2.0` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let x = lookup("x")?; - Ok(GuestIndices { x }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let x = *_instance - .get_typed_func::<(), ()>(&mut store, &self.x)? - .func(); - Ok(Guest { x }) - } - } - impl Guest { - pub async fn call_x( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (), - >::new_unchecked(self.x) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise) - } - } - } - } - } -} diff --git a/crates/component-macro/tests/expanded/multiversion_tracing_async.rs b/crates/component-macro/tests/expanded/multiversion_tracing_async.rs index 1d0e95761282..34b15358fc90 100644 --- a/crates/component-macro/tests/expanded/multiversion_tracing_async.rs +++ b/crates/component-macro/tests/expanded/multiversion_tracing_async.rs @@ -157,17 +157,17 @@ const _: () = { let indices = FooIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: my::dep0_1_0::a::Host + my::dep0_2_0::a::Host + Send, + T: Send + 'static, { - my::dep0_1_0::a::add_to_linker(linker, get)?; - my::dep0_2_0::a::add_to_linker(linker, get)?; + my::dep0_1_0::a::add_to_linker::(linker, get)?; + my::dep0_2_0::a::add_to_linker::(linker, get)?; Ok(()) } pub fn my_dep0_1_0_a(&self) -> &exports::my::dep0_1_0::a::Guest { @@ -188,14 +188,14 @@ pub mod my { pub trait Host: Send { async fn x(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("my:dep/a@0.1.0")?; inst.func_wrap_async( @@ -223,17 +223,6 @@ pub mod my { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn x(&mut self) -> () { Host::x(*self).await @@ -250,14 +239,14 @@ pub mod my { pub trait Host: Send { async fn x(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("my:dep/a@0.2.0")?; inst.func_wrap_async( @@ -285,17 +274,6 @@ pub mod my { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn x(&mut self) -> () { Host::x(*self).await diff --git a/crates/component-macro/tests/expanded/path1.rs b/crates/component-macro/tests/expanded/path1.rs index 16aa025c1ca2..ffe080385ea5 100644 --- a/crates/component-macro/tests/expanded/path1.rs +++ b/crates/component-macro/tests/expanded/path1.rs @@ -138,15 +138,16 @@ const _: () = { let indices = Path1Indices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: paths::path1::test::Host, T: 'static, - U: paths::path1::test::Host, { - paths::path1::test::add_to_linker(linker, get)?; + paths::path1::test::add_to_linker::(linker, get)?; Ok(()) } } @@ -158,27 +159,18 @@ pub mod paths { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; pub trait Host {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("paths:path1/test")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/path1_async.rs b/crates/component-macro/tests/expanded/path1_async.rs index ed817f6c2bc4..fa468c5f0aeb 100644 --- a/crates/component-macro/tests/expanded/path1_async.rs +++ b/crates/component-macro/tests/expanded/path1_async.rs @@ -144,16 +144,16 @@ const _: () = { let indices = Path1Indices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: paths::path1::test::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: paths::path1::test::Host + Send, + T: Send + 'static, { - paths::path1::test::add_to_linker(linker, get)?; + paths::path1::test::add_to_linker::(linker, get)?; Ok(()) } } @@ -166,29 +166,18 @@ pub mod paths { use wasmtime::component::__internal::{anyhow, Box}; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("paths:path1/test")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/path1_concurrent.rs b/crates/component-macro/tests/expanded/path1_concurrent.rs deleted file mode 100644 index 46ec37c4680f..000000000000 --- a/crates/component-macro/tests/expanded/path1_concurrent.rs +++ /dev/null @@ -1,194 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `path1`. -/// -/// This structure is created through [`Path1Pre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`Path1`] as well. -pub struct Path1Pre { - instance_pre: wasmtime::component::InstancePre, - indices: Path1Indices, -} -impl Clone for Path1Pre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> Path1Pre<_T> { - /// Creates a new copy of `Path1Pre` 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 { - let indices = Path1Indices::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 [`Path1`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `path1`. -/// -/// This is an implementation detail of [`Path1Pre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`Path1`] as well. -#[derive(Clone)] -pub struct Path1Indices {} -/// Auto-generated bindings for an instance a component which -/// implements the world `path1`. -/// -/// 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 -/// [`Path1::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`Path1Pre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`Path1Pre::instantiate_async`] to -/// create a [`Path1`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`Path1::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 Path1 {} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl Path1Indices { - /// Creates a new copy of `Path1Indices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - Ok(Path1Indices {}) - } - /// Uses the indices stored in `self` to load an instance - /// of [`Path1`] 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 { - let _ = &mut store; - let _instance = instance; - Ok(Path1 {}) - } - } - impl Path1 { - /// Convenience wrapper around [`Path1Pre::new`] and - /// [`Path1Pre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - Path1Pre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`Path1Indices::new`] and - /// [`Path1Indices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = Path1Indices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + paths::path1::test::Host, - U: Send + paths::path1::test::Host, - { - paths::path1::test::add_to_linker(linker, get)?; - Ok(()) - } - } -}; -pub mod paths { - pub mod path1 { - #[allow(clippy::all)] - pub mod test { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub trait Host {} - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker.instance("paths:path1/test")?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host + ?Sized> Host for &mut _T {} - } - } -} diff --git a/crates/component-macro/tests/expanded/path1_tracing_async.rs b/crates/component-macro/tests/expanded/path1_tracing_async.rs index ed817f6c2bc4..fa468c5f0aeb 100644 --- a/crates/component-macro/tests/expanded/path1_tracing_async.rs +++ b/crates/component-macro/tests/expanded/path1_tracing_async.rs @@ -144,16 +144,16 @@ const _: () = { let indices = Path1Indices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: paths::path1::test::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: paths::path1::test::Host + Send, + T: Send + 'static, { - paths::path1::test::add_to_linker(linker, get)?; + paths::path1::test::add_to_linker::(linker, get)?; Ok(()) } } @@ -166,29 +166,18 @@ pub mod paths { use wasmtime::component::__internal::{anyhow, Box}; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("paths:path1/test")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/path2.rs b/crates/component-macro/tests/expanded/path2.rs index a1533c96b9f7..28ead4ee6383 100644 --- a/crates/component-macro/tests/expanded/path2.rs +++ b/crates/component-macro/tests/expanded/path2.rs @@ -138,15 +138,16 @@ const _: () = { let indices = Path2Indices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: paths::path2::test::Host, T: 'static, - U: paths::path2::test::Host, { - paths::path2::test::add_to_linker(linker, get)?; + paths::path2::test::add_to_linker::(linker, get)?; Ok(()) } } @@ -158,27 +159,18 @@ pub mod paths { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; pub trait Host {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("paths:path2/test")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/path2_async.rs b/crates/component-macro/tests/expanded/path2_async.rs index 0658cc66ced2..aad6d3a5479c 100644 --- a/crates/component-macro/tests/expanded/path2_async.rs +++ b/crates/component-macro/tests/expanded/path2_async.rs @@ -144,16 +144,16 @@ const _: () = { let indices = Path2Indices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: paths::path2::test::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: paths::path2::test::Host + Send, + T: Send + 'static, { - paths::path2::test::add_to_linker(linker, get)?; + paths::path2::test::add_to_linker::(linker, get)?; Ok(()) } } @@ -166,29 +166,18 @@ pub mod paths { use wasmtime::component::__internal::{anyhow, Box}; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("paths:path2/test")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/path2_tracing_async.rs b/crates/component-macro/tests/expanded/path2_tracing_async.rs index 0658cc66ced2..aad6d3a5479c 100644 --- a/crates/component-macro/tests/expanded/path2_tracing_async.rs +++ b/crates/component-macro/tests/expanded/path2_tracing_async.rs @@ -144,16 +144,16 @@ const _: () = { let indices = Path2Indices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: paths::path2::test::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: paths::path2::test::Host + Send, + T: Send + 'static, { - paths::path2::test::add_to_linker(linker, get)?; + paths::path2::test::add_to_linker::(linker, get)?; Ok(()) } } @@ -166,29 +166,18 @@ pub mod paths { use wasmtime::component::__internal::{anyhow, Box}; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("paths:path2/test")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/records.rs b/crates/component-macro/tests/expanded/records.rs index 6e7b4bdf7531..4f27c4563d9c 100644 --- a/crates/component-macro/tests/expanded/records.rs +++ b/crates/component-macro/tests/expanded/records.rs @@ -146,15 +146,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::records::Host, T: 'static, - U: foo::foo::records::Host, { - foo::foo::records::add_to_linker(linker, get)?; + foo::foo::records::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_records(&self) -> &exports::foo::foo::records::Guest { @@ -336,13 +337,14 @@ pub mod foo { fn aggregate_result(&mut self) -> Aggregates; fn typedef_inout(&mut self, e: TupleTypedef2) -> i32; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/records")?; inst.func_wrap( @@ -453,16 +455,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn tuple_arg(&mut self, x: (char, u32)) -> () { Host::tuple_arg(*self, x) diff --git a/crates/component-macro/tests/expanded/records_async.rs b/crates/component-macro/tests/expanded/records_async.rs index 98843b9b9a98..ad2ba65f92d5 100644 --- a/crates/component-macro/tests/expanded/records_async.rs +++ b/crates/component-macro/tests/expanded/records_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::records::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::records::Host + Send, + T: Send + 'static, { - foo::foo::records::add_to_linker(linker, get)?; + foo::foo::records::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_records(&self) -> &exports::foo::foo::records::Guest { @@ -344,14 +344,14 @@ pub mod foo { async fn aggregate_result(&mut self) -> Aggregates; async fn typedef_inout(&mut self, e: TupleTypedef2) -> i32; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/records")?; inst.func_wrap_async( @@ -484,17 +484,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn tuple_arg(&mut self, x: (char, u32)) -> () { Host::tuple_arg(*self, x).await diff --git a/crates/component-macro/tests/expanded/records_concurrent.rs b/crates/component-macro/tests/expanded/records_concurrent.rs deleted file mode 100644 index a06dfb290daf..000000000000 --- a/crates/component-macro/tests/expanded/records_concurrent.rs +++ /dev/null @@ -1,1529 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `the-world`. -/// -/// This structure is created through [`TheWorldPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`TheWorld`] as well. -pub struct TheWorldPre { - instance_pre: wasmtime::component::InstancePre, - indices: TheWorldIndices, -} -impl Clone for TheWorldPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> TheWorldPre<_T> { - /// Creates a new copy of `TheWorldPre` 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 { - let indices = TheWorldIndices::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 [`TheWorld`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `the-world`. -/// -/// This is an implementation detail of [`TheWorldPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`TheWorld`] as well. -#[derive(Clone)] -pub struct TheWorldIndices { - interface0: exports::foo::foo::records::GuestIndices, -} -/// Auto-generated bindings for an instance a component which -/// implements the world `the-world`. -/// -/// 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 -/// [`TheWorld::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`TheWorldPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`TheWorldPre::instantiate_async`] to -/// create a [`TheWorld`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`TheWorld::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 TheWorld { - interface0: exports::foo::foo::records::Guest, -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl TheWorldIndices { - /// Creates a new copy of `TheWorldIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - let interface0 = exports::foo::foo::records::GuestIndices::new( - _instance_pre, - )?; - Ok(TheWorldIndices { interface0 }) - } - /// Uses the indices stored in `self` to load an instance - /// of [`TheWorld`] 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 { - let _ = &mut store; - let _instance = instance; - let interface0 = self.interface0.load(&mut store, &_instance)?; - Ok(TheWorld { interface0 }) - } - } - impl TheWorld { - /// Convenience wrapper around [`TheWorldPre::new`] and - /// [`TheWorldPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - TheWorldPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`TheWorldIndices::new`] and - /// [`TheWorldIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + foo::foo::records::Host, - U: Send + foo::foo::records::Host, - { - foo::foo::records::add_to_linker(linker, get)?; - Ok(()) - } - pub fn foo_foo_records(&self) -> &exports::foo::foo::records::Guest { - &self.interface0 - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod records { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone, Copy)] - pub struct Empty {} - impl core::fmt::Debug for Empty { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("Empty").finish() - } - } - const _: () = { - assert!(0 == < Empty as wasmtime::component::ComponentType >::SIZE32); - assert!(1 == < Empty as wasmtime::component::ComponentType >::ALIGN32); - }; - /// A record containing two scalar fields - /// that both have the same type - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone, Copy)] - pub struct Scalars { - /// The first field, named a - #[component(name = "a")] - pub a: u32, - /// The second field, named b - #[component(name = "b")] - pub b: u32, - } - impl core::fmt::Debug for Scalars { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("Scalars") - .field("a", &self.a) - .field("b", &self.b) - .finish() - } - } - const _: () = { - assert!(8 == < Scalars as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < Scalars as wasmtime::component::ComponentType >::ALIGN32); - }; - /// A record that is really just flags - /// All of the fields are bool - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone, Copy)] - pub struct ReallyFlags { - #[component(name = "a")] - pub a: bool, - #[component(name = "b")] - pub b: bool, - #[component(name = "c")] - pub c: bool, - #[component(name = "d")] - pub d: bool, - #[component(name = "e")] - pub e: bool, - #[component(name = "f")] - pub f: bool, - #[component(name = "g")] - pub g: bool, - #[component(name = "h")] - pub h: bool, - #[component(name = "i")] - pub i: bool, - } - impl core::fmt::Debug for ReallyFlags { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("ReallyFlags") - .field("a", &self.a) - .field("b", &self.b) - .field("c", &self.c) - .field("d", &self.d) - .field("e", &self.e) - .field("f", &self.f) - .field("g", &self.g) - .field("h", &self.h) - .field("i", &self.i) - .finish() - } - } - const _: () = { - assert!( - 9 == < ReallyFlags as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 1 == < ReallyFlags as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone)] - pub struct Aggregates { - #[component(name = "a")] - pub a: Scalars, - #[component(name = "b")] - pub b: u32, - #[component(name = "c")] - pub c: Empty, - #[component(name = "d")] - pub d: wasmtime::component::__internal::String, - #[component(name = "e")] - pub e: ReallyFlags, - } - impl core::fmt::Debug for Aggregates { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("Aggregates") - .field("a", &self.a) - .field("b", &self.b) - .field("c", &self.c) - .field("d", &self.d) - .field("e", &self.e) - .finish() - } - } - const _: () = { - assert!( - 32 == < Aggregates as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 4 == < Aggregates as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - pub type TupleTypedef = (i32,); - const _: () = { - assert!( - 4 == < TupleTypedef as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 4 == < TupleTypedef as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - pub type IntTypedef = i32; - const _: () = { - assert!( - 4 == < IntTypedef as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 4 == < IntTypedef as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - pub type TupleTypedef2 = (IntTypedef,); - const _: () = { - assert!( - 4 == < TupleTypedef2 as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 4 == < TupleTypedef2 as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - pub trait Host { - type Data; - fn tuple_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: (char, u32), - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn tuple_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> (char, u32) + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn empty_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Empty, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn empty_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Empty + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn scalar_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Scalars, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn scalar_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Scalars + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn flags_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: ReallyFlags, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn flags_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> ReallyFlags + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn aggregate_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Aggregates, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn aggregate_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Aggregates + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn typedef_inout( - store: wasmtime::StoreContextMut<'_, Self::Data>, - e: TupleTypedef2, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> i32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host + Send, - >, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/records")?; - inst.func_wrap_concurrent( - "tuple-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): ((char, u32),)| - { - let host = caller; - let r = ::tuple_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "tuple-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::tuple_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<((char, u32),)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<((char, u32),)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "empty-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (Empty,)| - { - let host = caller; - let r = ::empty_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "empty-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::empty_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Empty,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Empty,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "scalar-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (Scalars,)| - { - let host = caller; - let r = ::scalar_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "scalar-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::scalar_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Scalars,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Scalars,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "flags-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (ReallyFlags,)| - { - let host = caller; - let r = ::flags_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "flags-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::flags_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(ReallyFlags,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(ReallyFlags,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "aggregate-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (Aggregates,)| - { - let host = caller; - let r = ::aggregate_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "aggregate-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::aggregate_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Aggregates,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Aggregates,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "typedef-inout", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (TupleTypedef2,)| - { - let host = caller; - let r = ::typedef_inout(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(i32,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(i32,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn tuple_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: (char, u32), - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::tuple_arg(store, x) - } - fn tuple_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> (char, u32) + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::tuple_result(store) - } - fn empty_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Empty, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::empty_arg(store, x) - } - fn empty_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Empty + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::empty_result(store) - } - fn scalar_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Scalars, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::scalar_arg(store, x) - } - fn scalar_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Scalars + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::scalar_result(store) - } - fn flags_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: ReallyFlags, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::flags_arg(store, x) - } - fn flags_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> ReallyFlags + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::flags_result(store) - } - fn aggregate_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Aggregates, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::aggregate_arg(store, x) - } - fn aggregate_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Aggregates + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::aggregate_result(store) - } - fn typedef_inout( - store: wasmtime::StoreContextMut<'_, Self::Data>, - e: TupleTypedef2, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> i32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::typedef_inout(store, e) - } - } - } - } -} -pub mod exports { - pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod records { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone, Copy)] - pub struct Empty {} - impl core::fmt::Debug for Empty { - fn fmt( - &self, - f: &mut core::fmt::Formatter<'_>, - ) -> core::fmt::Result { - f.debug_struct("Empty").finish() - } - } - const _: () = { - assert!( - 0 == < Empty as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 1 == < Empty as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - /// A record containing two scalar fields - /// that both have the same type - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone, Copy)] - pub struct Scalars { - /// The first field, named a - #[component(name = "a")] - pub a: u32, - /// The second field, named b - #[component(name = "b")] - pub b: u32, - } - impl core::fmt::Debug for Scalars { - fn fmt( - &self, - f: &mut core::fmt::Formatter<'_>, - ) -> core::fmt::Result { - f.debug_struct("Scalars") - .field("a", &self.a) - .field("b", &self.b) - .finish() - } - } - const _: () = { - assert!( - 8 == < Scalars as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 4 == < Scalars as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - /// A record that is really just flags - /// All of the fields are bool - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone, Copy)] - pub struct ReallyFlags { - #[component(name = "a")] - pub a: bool, - #[component(name = "b")] - pub b: bool, - #[component(name = "c")] - pub c: bool, - #[component(name = "d")] - pub d: bool, - #[component(name = "e")] - pub e: bool, - #[component(name = "f")] - pub f: bool, - #[component(name = "g")] - pub g: bool, - #[component(name = "h")] - pub h: bool, - #[component(name = "i")] - pub i: bool, - } - impl core::fmt::Debug for ReallyFlags { - fn fmt( - &self, - f: &mut core::fmt::Formatter<'_>, - ) -> core::fmt::Result { - f.debug_struct("ReallyFlags") - .field("a", &self.a) - .field("b", &self.b) - .field("c", &self.c) - .field("d", &self.d) - .field("e", &self.e) - .field("f", &self.f) - .field("g", &self.g) - .field("h", &self.h) - .field("i", &self.i) - .finish() - } - } - const _: () = { - assert!( - 9 == < ReallyFlags as wasmtime::component::ComponentType - >::SIZE32 - ); - assert!( - 1 == < ReallyFlags as wasmtime::component::ComponentType - >::ALIGN32 - ); - }; - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone)] - pub struct Aggregates { - #[component(name = "a")] - pub a: Scalars, - #[component(name = "b")] - pub b: u32, - #[component(name = "c")] - pub c: Empty, - #[component(name = "d")] - pub d: wasmtime::component::__internal::String, - #[component(name = "e")] - pub e: ReallyFlags, - } - impl core::fmt::Debug for Aggregates { - fn fmt( - &self, - f: &mut core::fmt::Formatter<'_>, - ) -> core::fmt::Result { - f.debug_struct("Aggregates") - .field("a", &self.a) - .field("b", &self.b) - .field("c", &self.c) - .field("d", &self.d) - .field("e", &self.e) - .finish() - } - } - const _: () = { - assert!( - 32 == < Aggregates as wasmtime::component::ComponentType - >::SIZE32 - ); - assert!( - 4 == < Aggregates as wasmtime::component::ComponentType - >::ALIGN32 - ); - }; - pub type TupleTypedef = (i32,); - const _: () = { - assert!( - 4 == < TupleTypedef as wasmtime::component::ComponentType - >::SIZE32 - ); - assert!( - 4 == < TupleTypedef as wasmtime::component::ComponentType - >::ALIGN32 - ); - }; - pub type IntTypedef = i32; - const _: () = { - assert!( - 4 == < IntTypedef as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 4 == < IntTypedef as wasmtime::component::ComponentType - >::ALIGN32 - ); - }; - pub type TupleTypedef2 = (IntTypedef,); - const _: () = { - assert!( - 4 == < TupleTypedef2 as wasmtime::component::ComponentType - >::SIZE32 - ); - assert!( - 4 == < TupleTypedef2 as wasmtime::component::ComponentType - >::ALIGN32 - ); - }; - pub struct Guest { - tuple_arg: wasmtime::component::Func, - tuple_result: wasmtime::component::Func, - empty_arg: wasmtime::component::Func, - empty_result: wasmtime::component::Func, - scalar_arg: wasmtime::component::Func, - scalar_result: wasmtime::component::Func, - flags_arg: wasmtime::component::Func, - flags_result: wasmtime::component::Func, - aggregate_arg: wasmtime::component::Func, - aggregate_result: wasmtime::component::Func, - typedef_inout: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - tuple_arg: wasmtime::component::ComponentExportIndex, - tuple_result: wasmtime::component::ComponentExportIndex, - empty_arg: wasmtime::component::ComponentExportIndex, - empty_result: wasmtime::component::ComponentExportIndex, - scalar_arg: wasmtime::component::ComponentExportIndex, - scalar_result: wasmtime::component::ComponentExportIndex, - flags_arg: wasmtime::component::ComponentExportIndex, - flags_result: wasmtime::component::ComponentExportIndex, - aggregate_arg: wasmtime::component::ComponentExportIndex, - aggregate_result: wasmtime::component::ComponentExportIndex, - typedef_inout: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "foo:foo/records") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/records`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `foo:foo/records` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let tuple_arg = lookup("tuple-arg")?; - let tuple_result = lookup("tuple-result")?; - let empty_arg = lookup("empty-arg")?; - let empty_result = lookup("empty-result")?; - let scalar_arg = lookup("scalar-arg")?; - let scalar_result = lookup("scalar-result")?; - let flags_arg = lookup("flags-arg")?; - let flags_result = lookup("flags-result")?; - let aggregate_arg = lookup("aggregate-arg")?; - let aggregate_result = lookup("aggregate-result")?; - let typedef_inout = lookup("typedef-inout")?; - Ok(GuestIndices { - tuple_arg, - tuple_result, - empty_arg, - empty_result, - scalar_arg, - scalar_result, - flags_arg, - flags_result, - aggregate_arg, - aggregate_result, - typedef_inout, - }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let tuple_arg = *_instance - .get_typed_func::< - ((char, u32),), - (), - >(&mut store, &self.tuple_arg)? - .func(); - let tuple_result = *_instance - .get_typed_func::< - (), - ((char, u32),), - >(&mut store, &self.tuple_result)? - .func(); - let empty_arg = *_instance - .get_typed_func::<(Empty,), ()>(&mut store, &self.empty_arg)? - .func(); - let empty_result = *_instance - .get_typed_func::< - (), - (Empty,), - >(&mut store, &self.empty_result)? - .func(); - let scalar_arg = *_instance - .get_typed_func::< - (Scalars,), - (), - >(&mut store, &self.scalar_arg)? - .func(); - let scalar_result = *_instance - .get_typed_func::< - (), - (Scalars,), - >(&mut store, &self.scalar_result)? - .func(); - let flags_arg = *_instance - .get_typed_func::< - (ReallyFlags,), - (), - >(&mut store, &self.flags_arg)? - .func(); - let flags_result = *_instance - .get_typed_func::< - (), - (ReallyFlags,), - >(&mut store, &self.flags_result)? - .func(); - let aggregate_arg = *_instance - .get_typed_func::< - (&Aggregates,), - (), - >(&mut store, &self.aggregate_arg)? - .func(); - let aggregate_result = *_instance - .get_typed_func::< - (), - (Aggregates,), - >(&mut store, &self.aggregate_result)? - .func(); - let typedef_inout = *_instance - .get_typed_func::< - (TupleTypedef2,), - (i32,), - >(&mut store, &self.typedef_inout)? - .func(); - Ok(Guest { - tuple_arg, - tuple_result, - empty_arg, - empty_result, - scalar_arg, - scalar_result, - flags_arg, - flags_result, - aggregate_arg, - aggregate_result, - typedef_inout, - }) - } - } - impl Guest { - pub async fn call_tuple_arg( - &self, - mut store: S, - arg0: (char, u32), - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - ((char, u32),), - (), - >::new_unchecked(self.tuple_arg) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise) - } - pub async fn call_tuple_result( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - ((char, u32),), - >::new_unchecked(self.tuple_result) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_empty_arg( - &self, - mut store: S, - arg0: Empty, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (Empty,), - (), - >::new_unchecked(self.empty_arg) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise) - } - pub async fn call_empty_result( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (Empty,), - >::new_unchecked(self.empty_result) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_scalar_arg( - &self, - mut store: S, - arg0: Scalars, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (Scalars,), - (), - >::new_unchecked(self.scalar_arg) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise) - } - pub async fn call_scalar_result( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (Scalars,), - >::new_unchecked(self.scalar_result) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_flags_arg( - &self, - mut store: S, - arg0: ReallyFlags, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (ReallyFlags,), - (), - >::new_unchecked(self.flags_arg) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise) - } - pub async fn call_flags_result( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (ReallyFlags,), - >::new_unchecked(self.flags_result) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_aggregate_arg( - &self, - mut store: S, - arg0: Aggregates, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (Aggregates,), - (), - >::new_unchecked(self.aggregate_arg) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise) - } - pub async fn call_aggregate_result( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (Aggregates,), - >::new_unchecked(self.aggregate_result) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_typedef_inout( - &self, - mut store: S, - arg0: TupleTypedef2, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (TupleTypedef2,), - (i32,), - >::new_unchecked(self.typedef_inout) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise.map(|(v,)| v)) - } - } - } - } - } -} diff --git a/crates/component-macro/tests/expanded/records_tracing_async.rs b/crates/component-macro/tests/expanded/records_tracing_async.rs index 4dc5737101d1..439f7226ac42 100644 --- a/crates/component-macro/tests/expanded/records_tracing_async.rs +++ b/crates/component-macro/tests/expanded/records_tracing_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::records::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::records::Host + Send, + T: Send + 'static, { - foo::foo::records::add_to_linker(linker, get)?; + foo::foo::records::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_records(&self) -> &exports::foo::foo::records::Guest { @@ -344,14 +344,14 @@ pub mod foo { async fn aggregate_result(&mut self) -> Aggregates; async fn typedef_inout(&mut self, e: TupleTypedef2) -> i32; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/records")?; inst.func_wrap_async( @@ -645,17 +645,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn tuple_arg(&mut self, x: (char, u32)) -> () { Host::tuple_arg(*self, x).await diff --git a/crates/component-macro/tests/expanded/rename.rs b/crates/component-macro/tests/expanded/rename.rs index 3c0bd3339dc2..9849f2857aba 100644 --- a/crates/component-macro/tests/expanded/rename.rs +++ b/crates/component-macro/tests/expanded/rename.rs @@ -138,16 +138,17 @@ const _: () = { let indices = NeptuneIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::green::Host + foo::foo::red::Host, T: 'static, - U: foo::foo::green::Host + foo::foo::red::Host, { - foo::foo::green::add_to_linker(linker, get)?; - foo::foo::red::add_to_linker(linker, get)?; + foo::foo::green::add_to_linker::(linker, get)?; + foo::foo::red::add_to_linker::(linker, get)?; Ok(()) } } @@ -164,27 +165,18 @@ pub mod foo { assert!(4 == < Thing as wasmtime::component::ComponentType >::ALIGN32); }; pub trait Host {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/green")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } #[allow(clippy::all)] @@ -199,13 +191,14 @@ pub mod foo { pub trait Host { fn foo(&mut self) -> Thing; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/red")?; inst.func_wrap( @@ -218,16 +211,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn foo(&mut self) -> Thing { Host::foo(*self) diff --git a/crates/component-macro/tests/expanded/rename_async.rs b/crates/component-macro/tests/expanded/rename_async.rs index 234682ddf5ca..c82c2b1e1625 100644 --- a/crates/component-macro/tests/expanded/rename_async.rs +++ b/crates/component-macro/tests/expanded/rename_async.rs @@ -144,17 +144,17 @@ const _: () = { let indices = NeptuneIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::green::Host + foo::foo::red::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::green::Host + foo::foo::red::Host + Send, + T: Send + 'static, { - foo::foo::green::add_to_linker(linker, get)?; - foo::foo::red::add_to_linker(linker, get)?; + foo::foo::green::add_to_linker::(linker, get)?; + foo::foo::red::add_to_linker::(linker, get)?; Ok(()) } } @@ -172,29 +172,18 @@ pub mod foo { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/green")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } #[allow(clippy::all)] @@ -210,14 +199,14 @@ pub mod foo { pub trait Host: Send { async fn foo(&mut self) -> Thing; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/red")?; inst.func_wrap_async( @@ -232,17 +221,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn foo(&mut self) -> Thing { Host::foo(*self).await diff --git a/crates/component-macro/tests/expanded/rename_concurrent.rs b/crates/component-macro/tests/expanded/rename_concurrent.rs deleted file mode 100644 index 7873bb818510..000000000000 --- a/crates/component-macro/tests/expanded/rename_concurrent.rs +++ /dev/null @@ -1,293 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `neptune`. -/// -/// This structure is created through [`NeptunePre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`Neptune`] as well. -pub struct NeptunePre { - instance_pre: wasmtime::component::InstancePre, - indices: NeptuneIndices, -} -impl Clone for NeptunePre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> NeptunePre<_T> { - /// Creates a new copy of `NeptunePre` 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 { - let indices = NeptuneIndices::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 [`Neptune`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `neptune`. -/// -/// This is an implementation detail of [`NeptunePre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`Neptune`] as well. -#[derive(Clone)] -pub struct NeptuneIndices {} -/// Auto-generated bindings for an instance a component which -/// implements the world `neptune`. -/// -/// 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 -/// [`Neptune::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`NeptunePre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`NeptunePre::instantiate_async`] to -/// create a [`Neptune`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`Neptune::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 Neptune {} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl NeptuneIndices { - /// Creates a new copy of `NeptuneIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - Ok(NeptuneIndices {}) - } - /// Uses the indices stored in `self` to load an instance - /// of [`Neptune`] 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 { - let _ = &mut store; - let _instance = instance; - Ok(Neptune {}) - } - } - impl Neptune { - /// Convenience wrapper around [`NeptunePre::new`] and - /// [`NeptunePre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - NeptunePre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`NeptuneIndices::new`] and - /// [`NeptuneIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = NeptuneIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + foo::foo::green::Host + foo::foo::red::Host, - U: Send + foo::foo::green::Host + foo::foo::red::Host, - { - foo::foo::green::add_to_linker(linker, get)?; - foo::foo::red::add_to_linker(linker, get)?; - Ok(()) - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod green { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type Thing = i32; - const _: () = { - assert!(4 == < Thing as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < Thing as wasmtime::component::ComponentType >::ALIGN32); - }; - pub trait Host {} - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/green")?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host + ?Sized> Host for &mut _T {} - } - #[allow(clippy::all)] - pub mod red { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type Thing = super::super::super::foo::foo::green::Thing; - const _: () = { - assert!(4 == < Thing as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < Thing as wasmtime::component::ComponentType >::ALIGN32); - }; - pub trait Host { - type Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Thing + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host + Send, - >, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/red")?; - inst.func_wrap_concurrent( - "foo", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::foo(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Thing,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Thing,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Thing + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::foo(store) - } - } - } - } -} diff --git a/crates/component-macro/tests/expanded/rename_tracing_async.rs b/crates/component-macro/tests/expanded/rename_tracing_async.rs index 99a927718aac..a97041fba5b1 100644 --- a/crates/component-macro/tests/expanded/rename_tracing_async.rs +++ b/crates/component-macro/tests/expanded/rename_tracing_async.rs @@ -144,17 +144,17 @@ const _: () = { let indices = NeptuneIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::green::Host + foo::foo::red::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::green::Host + foo::foo::red::Host + Send, + T: Send + 'static, { - foo::foo::green::add_to_linker(linker, get)?; - foo::foo::red::add_to_linker(linker, get)?; + foo::foo::green::add_to_linker::(linker, get)?; + foo::foo::red::add_to_linker::(linker, get)?; Ok(()) } } @@ -172,29 +172,18 @@ pub mod foo { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/green")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } #[allow(clippy::all)] @@ -210,14 +199,14 @@ pub mod foo { pub trait Host: Send { async fn foo(&mut self) -> Thing; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/red")?; inst.func_wrap_async( @@ -245,17 +234,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn foo(&mut self) -> Thing { Host::foo(*self).await diff --git a/crates/component-macro/tests/expanded/resources-export.rs b/crates/component-macro/tests/expanded/resources-export.rs index 5988e800b313..d58dd8fd2a67 100644 --- a/crates/component-macro/tests/expanded/resources-export.rs +++ b/crates/component-macro/tests/expanded/resources-export.rs @@ -174,15 +174,16 @@ const _: () = { let indices = WIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::transitive_import::Host, T: 'static, - U: foo::foo::transitive_import::Host, { - foo::foo::transitive_import::add_to_linker(linker, get)?; + foo::foo::transitive_import::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_simple_export(&self) -> &exports::foo::foo::simple_export::Guest { @@ -227,13 +228,14 @@ pub mod foo { } } pub trait Host: HostY + Sized {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/transitive-import")?; inst.resource( @@ -248,16 +250,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/resources-export_async.rs b/crates/component-macro/tests/expanded/resources-export_async.rs index 3b5652bda6e1..80e3a82c2a64 100644 --- a/crates/component-macro/tests/expanded/resources-export_async.rs +++ b/crates/component-macro/tests/expanded/resources-export_async.rs @@ -180,16 +180,16 @@ const _: () = { let indices = WIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::transitive_import::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::transitive_import::Host + Send, + T: Send + 'static, { - foo::foo::transitive_import::add_to_linker(linker, get)?; + foo::foo::transitive_import::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_simple_export(&self) -> &exports::foo::foo::simple_export::Guest { @@ -236,14 +236,14 @@ pub mod foo { } #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send + HostY + Sized {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/transitive-import")?; inst.resource_async( @@ -261,17 +261,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/resources-export_concurrent.rs b/crates/component-macro/tests/expanded/resources-export_concurrent.rs deleted file mode 100644 index 1be05e0d39d0..000000000000 --- a/crates/component-macro/tests/expanded/resources-export_concurrent.rs +++ /dev/null @@ -1,804 +0,0 @@ -/// 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 { - instance_pre: wasmtime::component::InstancePre, - indices: WIndices, -} -impl Clone for WPre { - 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 { - 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 { - interface0: exports::foo::foo::simple_export::GuestIndices, - interface1: exports::foo::foo::export_using_import::GuestIndices, - interface2: exports::foo::foo::export_using_export1::GuestIndices, - interface3: exports::foo::foo::export_using_export2::GuestIndices, -} -/// 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_async`] 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_async`] 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 { - interface0: exports::foo::foo::simple_export::Guest, - interface1: exports::foo::foo::export_using_import::Guest, - interface2: exports::foo::foo::export_using_export1::Guest, - interface3: exports::foo::foo::export_using_export2::Guest, -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - let interface0 = exports::foo::foo::simple_export::GuestIndices::new( - _instance_pre, - )?; - let interface1 = exports::foo::foo::export_using_import::GuestIndices::new( - _instance_pre, - )?; - let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new( - _instance_pre, - )?; - let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new( - _instance_pre, - )?; - Ok(WIndices { - interface0, - interface1, - interface2, - interface3, - }) - } - /// 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 { - let _ = &mut store; - let _instance = instance; - let interface0 = self.interface0.load(&mut store, &_instance)?; - let interface1 = self.interface1.load(&mut store, &_instance)?; - let interface2 = self.interface2.load(&mut store, &_instance)?; - let interface3 = self.interface3.load(&mut store, &_instance)?; - Ok(W { - interface0, - interface1, - interface2, - interface3, - }) - } - } - impl W { - /// Convenience wrapper around [`WPre::new`] and - /// [`WPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - WPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`WIndices::new`] and - /// [`WIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = WIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + foo::foo::transitive_import::Host, - U: Send + foo::foo::transitive_import::Host, - { - foo::foo::transitive_import::add_to_linker(linker, get)?; - Ok(()) - } - pub fn foo_foo_simple_export(&self) -> &exports::foo::foo::simple_export::Guest { - &self.interface0 - } - pub fn foo_foo_export_using_import( - &self, - ) -> &exports::foo::foo::export_using_import::Guest { - &self.interface1 - } - pub fn foo_foo_export_using_export1( - &self, - ) -> &exports::foo::foo::export_using_export1::Guest { - &self.interface2 - } - pub fn foo_foo_export_using_export2( - &self, - ) -> &exports::foo::foo::export_using_export2::Guest { - &self.interface3 - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod transitive_import { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub enum Y {} - pub trait HostY: Sized { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()>; - } - impl<_T: HostY + ?Sized> HostY for &mut _T { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostY::drop(*self, rep) - } - } - pub trait Host: HostY + Sized {} - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/transitive-import")?; - inst.resource( - "y", - wasmtime::component::ResourceType::host::(), - move |mut store, rep| -> wasmtime::Result<()> { - HostY::drop( - &mut host_getter(store.data_mut()), - wasmtime::component::Resource::new_own(rep), - ) - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host + ?Sized> Host for &mut _T {} - } - } -} -pub mod exports { - pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod simple_export { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type A = wasmtime::component::ResourceAny; - pub struct GuestA<'a> { - funcs: &'a Guest, - } - pub struct Guest { - constructor_a_constructor: wasmtime::component::Func, - static_a_static_a: wasmtime::component::Func, - method_a_method_a: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - constructor_a_constructor: wasmtime::component::ComponentExportIndex, - static_a_static_a: wasmtime::component::ComponentExportIndex, - method_a_method_a: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "foo:foo/simple-export") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-export`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `foo:foo/simple-export` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let constructor_a_constructor = lookup("[constructor]a")?; - let static_a_static_a = lookup("[static]a.static-a")?; - let method_a_method_a = lookup("[method]a.method-a")?; - Ok(GuestIndices { - constructor_a_constructor, - static_a_static_a, - method_a_method_a, - }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let constructor_a_constructor = *_instance - .get_typed_func::< - (), - (wasmtime::component::ResourceAny,), - >(&mut store, &self.constructor_a_constructor)? - .func(); - let static_a_static_a = *_instance - .get_typed_func::< - (), - (u32,), - >(&mut store, &self.static_a_static_a)? - .func(); - let method_a_method_a = *_instance - .get_typed_func::< - (wasmtime::component::ResourceAny,), - (u32,), - >(&mut store, &self.method_a_method_a)? - .func(); - Ok(Guest { - constructor_a_constructor, - static_a_static_a, - method_a_method_a, - }) - } - } - impl Guest { - pub fn a(&self) -> GuestA<'_> { - GuestA { funcs: self } - } - } - impl GuestA<'_> { - pub async fn call_constructor( - &self, - mut store: S, - ) -> wasmtime::Result< - wasmtime::component::Promise, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (wasmtime::component::ResourceAny,), - >::new_unchecked(self.funcs.constructor_a_constructor) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_static_a( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (u32,), - >::new_unchecked(self.funcs.static_a_static_a) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_method_a( - &self, - mut store: S, - arg0: wasmtime::component::ResourceAny, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (wasmtime::component::ResourceAny,), - (u32,), - >::new_unchecked(self.funcs.method_a_method_a) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise.map(|(v,)| v)) - } - } - } - #[allow(clippy::all)] - pub mod export_using_import { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type Y = super::super::super::super::foo::foo::transitive_import::Y; - pub type A = wasmtime::component::ResourceAny; - pub struct GuestA<'a> { - funcs: &'a Guest, - } - pub struct Guest { - constructor_a_constructor: wasmtime::component::Func, - static_a_static_a: wasmtime::component::Func, - method_a_method_a: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - constructor_a_constructor: wasmtime::component::ComponentExportIndex, - static_a_static_a: wasmtime::component::ComponentExportIndex, - method_a_method_a: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "foo:foo/export-using-import") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-import`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `foo:foo/export-using-import` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let constructor_a_constructor = lookup("[constructor]a")?; - let static_a_static_a = lookup("[static]a.static-a")?; - let method_a_method_a = lookup("[method]a.method-a")?; - Ok(GuestIndices { - constructor_a_constructor, - static_a_static_a, - method_a_method_a, - }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let constructor_a_constructor = *_instance - .get_typed_func::< - (wasmtime::component::Resource,), - (wasmtime::component::ResourceAny,), - >(&mut store, &self.constructor_a_constructor)? - .func(); - let static_a_static_a = *_instance - .get_typed_func::< - (), - (wasmtime::component::Resource,), - >(&mut store, &self.static_a_static_a)? - .func(); - let method_a_method_a = *_instance - .get_typed_func::< - ( - wasmtime::component::ResourceAny, - wasmtime::component::Resource, - ), - (wasmtime::component::Resource,), - >(&mut store, &self.method_a_method_a)? - .func(); - Ok(Guest { - constructor_a_constructor, - static_a_static_a, - method_a_method_a, - }) - } - } - impl Guest { - pub fn a(&self) -> GuestA<'_> { - GuestA { funcs: self } - } - } - impl GuestA<'_> { - pub async fn call_constructor( - &self, - mut store: S, - arg0: wasmtime::component::Resource, - ) -> wasmtime::Result< - wasmtime::component::Promise, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (wasmtime::component::Resource,), - (wasmtime::component::ResourceAny,), - >::new_unchecked(self.funcs.constructor_a_constructor) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_static_a( - &self, - mut store: S, - ) -> wasmtime::Result< - wasmtime::component::Promise>, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (wasmtime::component::Resource,), - >::new_unchecked(self.funcs.static_a_static_a) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_method_a( - &self, - mut store: S, - arg0: wasmtime::component::ResourceAny, - arg1: wasmtime::component::Resource, - ) -> wasmtime::Result< - wasmtime::component::Promise>, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - ( - wasmtime::component::ResourceAny, - wasmtime::component::Resource, - ), - (wasmtime::component::Resource,), - >::new_unchecked(self.funcs.method_a_method_a) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0, arg1)) - .await?; - Ok(promise.map(|(v,)| v)) - } - } - } - #[allow(clippy::all)] - pub mod export_using_export1 { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type A = wasmtime::component::ResourceAny; - pub struct GuestA<'a> { - funcs: &'a Guest, - } - pub struct Guest { - constructor_a_constructor: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - constructor_a_constructor: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "foo:foo/export-using-export1") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export1`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `foo:foo/export-using-export1` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let constructor_a_constructor = lookup("[constructor]a")?; - Ok(GuestIndices { - constructor_a_constructor, - }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let constructor_a_constructor = *_instance - .get_typed_func::< - (), - (wasmtime::component::ResourceAny,), - >(&mut store, &self.constructor_a_constructor)? - .func(); - Ok(Guest { constructor_a_constructor }) - } - } - impl Guest { - pub fn a(&self) -> GuestA<'_> { - GuestA { funcs: self } - } - } - impl GuestA<'_> { - pub async fn call_constructor( - &self, - mut store: S, - ) -> wasmtime::Result< - wasmtime::component::Promise, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (wasmtime::component::ResourceAny,), - >::new_unchecked(self.funcs.constructor_a_constructor) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - } - } - #[allow(clippy::all)] - pub mod export_using_export2 { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type A = super::super::super::super::exports::foo::foo::export_using_export1::A; - pub type B = wasmtime::component::ResourceAny; - pub struct GuestB<'a> { - funcs: &'a Guest, - } - pub struct Guest { - constructor_b_constructor: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - constructor_b_constructor: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "foo:foo/export-using-export2") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export2`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `foo:foo/export-using-export2` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let constructor_b_constructor = lookup("[constructor]b")?; - Ok(GuestIndices { - constructor_b_constructor, - }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let constructor_b_constructor = *_instance - .get_typed_func::< - (wasmtime::component::ResourceAny,), - (wasmtime::component::ResourceAny,), - >(&mut store, &self.constructor_b_constructor)? - .func(); - Ok(Guest { constructor_b_constructor }) - } - } - impl Guest { - pub fn b(&self) -> GuestB<'_> { - GuestB { funcs: self } - } - } - impl GuestB<'_> { - pub async fn call_constructor( - &self, - mut store: S, - arg0: wasmtime::component::ResourceAny, - ) -> wasmtime::Result< - wasmtime::component::Promise, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (wasmtime::component::ResourceAny,), - (wasmtime::component::ResourceAny,), - >::new_unchecked(self.funcs.constructor_b_constructor) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise.map(|(v,)| v)) - } - } - } - } - } -} diff --git a/crates/component-macro/tests/expanded/resources-export_tracing_async.rs b/crates/component-macro/tests/expanded/resources-export_tracing_async.rs index e98578c26455..b0291f984653 100644 --- a/crates/component-macro/tests/expanded/resources-export_tracing_async.rs +++ b/crates/component-macro/tests/expanded/resources-export_tracing_async.rs @@ -180,16 +180,16 @@ const _: () = { let indices = WIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::transitive_import::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::transitive_import::Host + Send, + T: Send + 'static, { - foo::foo::transitive_import::add_to_linker(linker, get)?; + foo::foo::transitive_import::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_simple_export(&self) -> &exports::foo::foo::simple_export::Guest { @@ -236,14 +236,14 @@ pub mod foo { } #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send + HostY + Sized {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/transitive-import")?; inst.resource_async( @@ -261,17 +261,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/resources-import.rs b/crates/component-macro/tests/expanded/resources-import.rs index f467e1284293..e371bb7adf9a 100644 --- a/crates/component-macro/tests/expanded/resources-import.rs +++ b/crates/component-macro/tests/expanded/resources-import.rs @@ -222,12 +222,14 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: TheWorldImports>, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: TheWorldImports, + T: 'static, { let mut linker = linker.root(); linker @@ -282,24 +284,30 @@ const _: () = { )?; Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - U: foo::foo::resources::Host + foo::foo::long_use_chain1::Host + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: foo::foo::resources::Host + foo::foo::long_use_chain1::Host + foo::foo::long_use_chain2::Host + foo::foo::long_use_chain3::Host + foo::foo::long_use_chain4::Host + foo::foo::transitive_interface_with_resource::Host + TheWorldImports, + T: 'static, { - Self::add_to_linker_imports_get_host(linker, get)?; - foo::foo::resources::add_to_linker(linker, get)?; - foo::foo::long_use_chain1::add_to_linker(linker, get)?; - foo::foo::long_use_chain2::add_to_linker(linker, get)?; - foo::foo::long_use_chain3::add_to_linker(linker, get)?; - foo::foo::long_use_chain4::add_to_linker(linker, get)?; - foo::foo::transitive_interface_with_resource::add_to_linker(linker, get)?; + Self::add_to_linker_imports::(linker, get)?; + foo::foo::resources::add_to_linker::(linker, get)?; + foo::foo::long_use_chain1::add_to_linker::(linker, get)?; + foo::foo::long_use_chain2::add_to_linker::(linker, get)?; + foo::foo::long_use_chain3::add_to_linker::(linker, get)?; + foo::foo::long_use_chain4::add_to_linker::(linker, get)?; + foo::foo::transitive_interface_with_resource::add_to_linker::< + T, + D, + >(linker, get)?; Ok(()) } pub fn call_some_world_func2( @@ -474,13 +482,14 @@ pub mod foo { fn record_result(&mut self) -> NestedOwn; fn func_with_handle_typedef(&mut self, x: SomeHandle) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/resources")?; inst.resource( @@ -725,16 +734,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn bar_own_arg(&mut self, x: wasmtime::component::Resource) -> () { Host::bar_own_arg(*self, x) @@ -854,13 +853,14 @@ pub mod foo { } } pub trait Host: HostA + Sized {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/long-use-chain1")?; inst.resource( @@ -875,16 +875,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } #[allow(clippy::all)] @@ -893,27 +883,18 @@ pub mod foo { use wasmtime::component::__internal::{anyhow, Box}; pub type A = super::super::super::foo::foo::long_use_chain1::A; pub trait Host {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/long-use-chain2")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } #[allow(clippy::all)] @@ -922,27 +903,18 @@ pub mod foo { use wasmtime::component::__internal::{anyhow, Box}; pub type A = super::super::super::foo::foo::long_use_chain2::A; pub trait Host {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/long-use-chain3")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } #[allow(clippy::all)] @@ -953,13 +925,14 @@ pub mod foo { pub trait Host { fn foo(&mut self) -> wasmtime::component::Resource; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/long-use-chain4")?; inst.func_wrap( @@ -972,16 +945,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn foo(&mut self) -> wasmtime::component::Resource { Host::foo(*self) @@ -1008,13 +971,14 @@ pub mod foo { } } pub trait Host: HostFoo + Sized {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker .instance("foo:foo/transitive-interface-with-resource")?; @@ -1030,16 +994,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/resources-import_async.rs b/crates/component-macro/tests/expanded/resources-import_async.rs index 01032e9daa85..80836ef9203a 100644 --- a/crates/component-macro/tests/expanded/resources-import_async.rs +++ b/crates/component-macro/tests/expanded/resources-import_async.rs @@ -230,13 +230,14 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: TheWorldImports>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: TheWorldImports, + T: Send + 'static, { let mut linker = linker.root(); linker @@ -302,26 +303,31 @@ const _: () = { )?; Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::resources::Host + foo::foo::long_use_chain1::Host + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: foo::foo::resources::Host + foo::foo::long_use_chain1::Host + foo::foo::long_use_chain2::Host + foo::foo::long_use_chain3::Host + foo::foo::long_use_chain4::Host + foo::foo::transitive_interface_with_resource::Host + TheWorldImports + Send, + T: Send + 'static, { - Self::add_to_linker_imports_get_host(linker, get)?; - foo::foo::resources::add_to_linker(linker, get)?; - foo::foo::long_use_chain1::add_to_linker(linker, get)?; - foo::foo::long_use_chain2::add_to_linker(linker, get)?; - foo::foo::long_use_chain3::add_to_linker(linker, get)?; - foo::foo::long_use_chain4::add_to_linker(linker, get)?; - foo::foo::transitive_interface_with_resource::add_to_linker(linker, get)?; + Self::add_to_linker_imports::(linker, get)?; + foo::foo::resources::add_to_linker::(linker, get)?; + foo::foo::long_use_chain1::add_to_linker::(linker, get)?; + foo::foo::long_use_chain2::add_to_linker::(linker, get)?; + foo::foo::long_use_chain3::add_to_linker::(linker, get)?; + foo::foo::long_use_chain4::add_to_linker::(linker, get)?; + foo::foo::transitive_interface_with_resource::add_to_linker::< + T, + D, + >(linker, get)?; Ok(()) } pub async fn call_some_world_func2( @@ -509,14 +515,14 @@ pub mod foo { async fn record_result(&mut self) -> NestedOwn; async fn func_with_handle_typedef(&mut self, x: SomeHandle) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/resources")?; inst.resource_async( @@ -808,17 +814,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn bar_own_arg( &mut self, @@ -945,14 +940,14 @@ pub mod foo { } #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send + HostA + Sized {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/long-use-chain1")?; inst.resource_async( @@ -970,17 +965,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } #[allow(clippy::all)] @@ -990,29 +974,18 @@ pub mod foo { pub type A = super::super::super::foo::foo::long_use_chain1::A; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/long-use-chain2")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } #[allow(clippy::all)] @@ -1022,29 +995,18 @@ pub mod foo { pub type A = super::super::super::foo::foo::long_use_chain2::A; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/long-use-chain3")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } #[allow(clippy::all)] @@ -1056,14 +1018,14 @@ pub mod foo { pub trait Host: Send { async fn foo(&mut self) -> wasmtime::component::Resource; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/long-use-chain4")?; inst.func_wrap_async( @@ -1078,17 +1040,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn foo(&mut self) -> wasmtime::component::Resource { Host::foo(*self).await @@ -1117,14 +1068,14 @@ pub mod foo { } #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send + HostFoo + Sized {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker .instance("foo:foo/transitive-interface-with-resource")?; @@ -1143,17 +1094,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/resources-import_concurrent.rs b/crates/component-macro/tests/expanded/resources-import_concurrent.rs deleted file mode 100644 index bc06409ef076..000000000000 --- a/crates/component-macro/tests/expanded/resources-import_concurrent.rs +++ /dev/null @@ -1,2262 +0,0 @@ -pub enum WorldResource {} -pub trait HostWorldResource: Sized { - type WorldResourceData; - fn new( - store: wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - self_: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn static_foo( - store: wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()>; -} -impl<_T: HostWorldResource> HostWorldResource for &mut _T { - type WorldResourceData = _T::WorldResourceData; - fn new( - store: wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as HostWorldResource>::new(store) - } - fn foo( - store: wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - self_: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as HostWorldResource>::foo(store, self_) - } - fn static_foo( - store: wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::WorldResourceData>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as HostWorldResource>::static_foo(store) - } - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostWorldResource::drop(*self, rep) - } -} -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `the-world`. -/// -/// This structure is created through [`TheWorldPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`TheWorld`] as well. -pub struct TheWorldPre { - instance_pre: wasmtime::component::InstancePre, - indices: TheWorldIndices, -} -impl Clone for TheWorldPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> TheWorldPre<_T> { - /// Creates a new copy of `TheWorldPre` 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 { - let indices = TheWorldIndices::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 [`TheWorld`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `the-world`. -/// -/// This is an implementation detail of [`TheWorldPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`TheWorld`] as well. -#[derive(Clone)] -pub struct TheWorldIndices { - interface1: exports::foo::foo::uses_resource_transitively::GuestIndices, - some_world_func2: wasmtime::component::ComponentExportIndex, -} -/// Auto-generated bindings for an instance a component which -/// implements the world `the-world`. -/// -/// 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 -/// [`TheWorld::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`TheWorldPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`TheWorldPre::instantiate_async`] to -/// create a [`TheWorld`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`TheWorld::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 TheWorld { - interface1: exports::foo::foo::uses_resource_transitively::Guest, - some_world_func2: wasmtime::component::Func, -} -pub trait TheWorldImports: HostWorldResource { - type Data; - fn some_world_func( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; -} -impl<_T: TheWorldImports> TheWorldImports for &mut _T { - type Data = _T::Data; - fn some_world_func( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as TheWorldImports>::some_world_func(store) - } -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl TheWorldIndices { - /// Creates a new copy of `TheWorldIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - let interface1 = exports::foo::foo::uses_resource_transitively::GuestIndices::new( - _instance_pre, - )?; - let some_world_func2 = { - let (item, index) = _component - .get_export(None, "some-world-func2") - .ok_or_else(|| { - anyhow::anyhow!("no export `some-world-func2` found") - })?; - match item { - wasmtime::component::types::ComponentItem::ComponentFunc(func) => { - anyhow::Context::context( - func - .typecheck::< - (), - (wasmtime::component::Resource,), - >(&_instance_type), - "type-checking export func `some-world-func2`", - )?; - index - } - _ => { - Err( - anyhow::anyhow!( - "export `some-world-func2` is not a function" - ), - )? - } - } - }; - Ok(TheWorldIndices { - interface1, - some_world_func2, - }) - } - /// Uses the indices stored in `self` to load an instance - /// of [`TheWorld`] 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 { - let _ = &mut store; - let _instance = instance; - let interface1 = self.interface1.load(&mut store, &_instance)?; - let some_world_func2 = *_instance - .get_typed_func::< - (), - (wasmtime::component::Resource,), - >(&mut store, &self.some_world_func2)? - .func(); - Ok(TheWorld { - interface1, - some_world_func2, - }) - } - } - impl TheWorld { - /// Convenience wrapper around [`TheWorldPre::new`] and - /// [`TheWorldPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - TheWorldPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`TheWorldIndices::new`] and - /// [`TheWorldIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker_imports_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: TheWorldImports, - >, - T: Send, - { - let mut linker = linker.root(); - linker - .resource( - "world-resource", - wasmtime::component::ResourceType::host::(), - move |mut store, rep| -> wasmtime::Result<()> { - HostWorldResource::drop( - &mut host_getter(store.data_mut()), - wasmtime::component::Resource::new_own(rep), - ) - }, - )?; - linker - .func_wrap_concurrent( - "some-world-func", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::some_world_func(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - linker - .func_wrap_concurrent( - "[constructor]world-resource", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::new(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - linker - .func_wrap_concurrent( - "[method]world-resource.foo", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (wasmtime::component::Resource,)| - { - let host = caller; - let r = ::foo(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - linker - .func_wrap_concurrent( - "[static]world-resource.static-foo", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::static_foo(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + foo::foo::resources::Host - + foo::foo::long_use_chain1::Host + foo::foo::long_use_chain2::Host - + foo::foo::long_use_chain3::Host - + foo::foo::long_use_chain4::Host - + foo::foo::transitive_interface_with_resource::Host - + TheWorldImports, - U: Send + foo::foo::resources::Host - + foo::foo::long_use_chain1::Host + foo::foo::long_use_chain2::Host - + foo::foo::long_use_chain3::Host - + foo::foo::long_use_chain4::Host - + foo::foo::transitive_interface_with_resource::Host - + TheWorldImports, - { - Self::add_to_linker_imports_get_host(linker, get)?; - foo::foo::resources::add_to_linker(linker, get)?; - foo::foo::long_use_chain1::add_to_linker(linker, get)?; - foo::foo::long_use_chain2::add_to_linker(linker, get)?; - foo::foo::long_use_chain3::add_to_linker(linker, get)?; - foo::foo::long_use_chain4::add_to_linker(linker, get)?; - foo::foo::transitive_interface_with_resource::add_to_linker(linker, get)?; - Ok(()) - } - pub async fn call_some_world_func2( - &self, - mut store: S, - ) -> wasmtime::Result< - wasmtime::component::Promise>, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (wasmtime::component::Resource,), - >::new_unchecked(self.some_world_func2) - }; - let promise = callee.call_concurrent(store.as_context_mut(), ()).await?; - Ok(promise.map(|(v,)| v)) - } - pub fn foo_foo_uses_resource_transitively( - &self, - ) -> &exports::foo::foo::uses_resource_transitively::Guest { - &self.interface1 - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod resources { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub enum Bar {} - pub trait HostBar: Sized { - type BarData; - fn new( - store: wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn static_a( - store: wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> u32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn method_a( - store: wasmtime::StoreContextMut<'_, Self::BarData>, - self_: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> u32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()>; - } - impl<_T: HostBar> HostBar for &mut _T { - type BarData = _T::BarData; - fn new( - store: wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as HostBar>::new(store) - } - fn static_a( - store: wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> u32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as HostBar>::static_a(store) - } - fn method_a( - store: wasmtime::StoreContextMut<'_, Self::BarData>, - self_: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> u32 + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as HostBar>::method_a(store, self_) - } - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostBar::drop(*self, rep) - } - } - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - pub struct NestedOwn { - #[component(name = "nested-bar")] - pub nested_bar: wasmtime::component::Resource, - } - impl core::fmt::Debug for NestedOwn { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("NestedOwn") - .field("nested-bar", &self.nested_bar) - .finish() - } - } - const _: () = { - assert!( - 4 == < NestedOwn as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 4 == < NestedOwn as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - pub struct NestedBorrow { - #[component(name = "nested-bar")] - pub nested_bar: wasmtime::component::Resource, - } - impl core::fmt::Debug for NestedBorrow { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("NestedBorrow") - .field("nested-bar", &self.nested_bar) - .finish() - } - } - const _: () = { - assert!( - 4 == < NestedBorrow as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 4 == < NestedBorrow as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - pub type SomeHandle = wasmtime::component::Resource; - const _: () = { - assert!( - 4 == < SomeHandle as wasmtime::component::ComponentType >::SIZE32 - ); - assert!( - 4 == < SomeHandle as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - pub trait Host: HostBar + Sized { - type Data; - fn bar_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn bar_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn bar_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn tuple_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: (wasmtime::component::Resource, u32), - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn tuple_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: (wasmtime::component::Resource, u32), - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn tuple_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> ( - wasmtime::component::Resource, - u32, - ) + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn option_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Option>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn option_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Option>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn option_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Option< - wasmtime::component::Resource, - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn result_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Result, ()>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn result_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Result, ()>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn result_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Result< - wasmtime::component::Resource, - (), - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn list_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - >, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn list_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - >, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn list_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn record_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: NestedOwn, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn record_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: NestedBorrow, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn record_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> NestedOwn + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn func_with_handle_typedef( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: SomeHandle, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host + Send, - >, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/resources")?; - inst.resource( - "bar", - wasmtime::component::ResourceType::host::(), - move |mut store, rep| -> wasmtime::Result<()> { - HostBar::drop( - &mut host_getter(store.data_mut()), - wasmtime::component::Resource::new_own(rep), - ) - }, - )?; - inst.func_wrap_concurrent( - "[constructor]bar", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::new(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "[static]bar.static-a", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::static_a(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "[method]bar.method-a", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (wasmtime::component::Resource,)| - { - let host = caller; - let r = ::method_a(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(u32,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "bar-own-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (wasmtime::component::Resource,)| - { - let host = caller; - let r = ::bar_own_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "bar-borrow-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (wasmtime::component::Resource,)| - { - let host = caller; - let r = ::bar_borrow_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "bar-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::bar_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "tuple-own-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): ((wasmtime::component::Resource, u32),)| - { - let host = caller; - let r = ::tuple_own_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "tuple-borrow-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): ((wasmtime::component::Resource, u32),)| - { - let host = caller; - let r = ::tuple_borrow_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "tuple-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::tuple_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - ((wasmtime::component::Resource, u32),), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - ((wasmtime::component::Resource, u32),), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "option-own-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (Option>,)| - { - let host = caller; - let r = ::option_own_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "option-borrow-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (Option>,)| - { - let host = caller; - let r = ::option_borrow_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "option-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::option_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (Option>,), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (Option>,), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "result-own-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (Result, ()>,)| - { - let host = caller; - let r = ::result_own_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "result-borrow-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (Result, ()>,)| - { - let host = caller; - let r = ::result_borrow_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "result-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::result_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (Result, ()>,), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (Result, ()>,), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "list-own-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - ( - arg0, - ): ( - wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - >, - )| - { - let host = caller; - let r = ::list_own_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "list-borrow-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - ( - arg0, - ): ( - wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - >, - )| - { - let host = caller; - let r = ::list_borrow_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "list-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::list_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - ( - wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - >, - ), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - ( - wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - >, - ), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "record-own-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (NestedOwn,)| - { - let host = caller; - let r = ::record_own_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "record-borrow-arg", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (NestedBorrow,)| - { - let host = caller; - let r = ::record_borrow_arg(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "record-result", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::record_result(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(NestedOwn,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(NestedOwn,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "func-with-handle-typedef", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (SomeHandle,)| - { - let host = caller; - let r = ::func_with_handle_typedef(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn bar_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::bar_own_arg(store, x) - } - fn bar_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::bar_borrow_arg(store, x) - } - fn bar_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::bar_result(store) - } - fn tuple_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: (wasmtime::component::Resource, u32), - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::tuple_own_arg(store, x) - } - fn tuple_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: (wasmtime::component::Resource, u32), - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::tuple_borrow_arg(store, x) - } - fn tuple_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> ( - wasmtime::component::Resource, - u32, - ) + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::tuple_result(store) - } - fn option_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Option>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::option_own_arg(store, x) - } - fn option_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Option>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::option_borrow_arg(store, x) - } - fn option_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Option< - wasmtime::component::Resource, - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::option_result(store) - } - fn result_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Result, ()>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::result_own_arg(store, x) - } - fn result_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: Result, ()>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::result_borrow_arg(store, x) - } - fn result_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Result< - wasmtime::component::Resource, - (), - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::result_result(store) - } - fn list_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - >, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::list_own_arg(store, x) - } - fn list_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - >, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::list_borrow_arg(store, x) - } - fn list_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::__internal::Vec< - wasmtime::component::Resource, - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::list_result(store) - } - fn record_own_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: NestedOwn, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::record_own_arg(store, x) - } - fn record_borrow_arg( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: NestedBorrow, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::record_borrow_arg(store, x) - } - fn record_result( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> NestedOwn + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::record_result(store) - } - fn func_with_handle_typedef( - store: wasmtime::StoreContextMut<'_, Self::Data>, - x: SomeHandle, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::func_with_handle_typedef(store, x) - } - } - } - #[allow(clippy::all)] - pub mod long_use_chain1 { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub enum A {} - pub trait HostA: Sized { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()>; - } - impl<_T: HostA + ?Sized> HostA for &mut _T { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostA::drop(*self, rep) - } - } - pub trait Host: HostA + Sized {} - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/long-use-chain1")?; - inst.resource( - "a", - wasmtime::component::ResourceType::host::(), - move |mut store, rep| -> wasmtime::Result<()> { - HostA::drop( - &mut host_getter(store.data_mut()), - wasmtime::component::Resource::new_own(rep), - ) - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host + ?Sized> Host for &mut _T {} - } - #[allow(clippy::all)] - pub mod long_use_chain2 { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type A = super::super::super::foo::foo::long_use_chain1::A; - pub trait Host {} - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/long-use-chain2")?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host + ?Sized> Host for &mut _T {} - } - #[allow(clippy::all)] - pub mod long_use_chain3 { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type A = super::super::super::foo::foo::long_use_chain2::A; - pub trait Host {} - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/long-use-chain3")?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host + ?Sized> Host for &mut _T {} - } - #[allow(clippy::all)] - pub mod long_use_chain4 { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type A = super::super::super::foo::foo::long_use_chain3::A; - pub trait Host { - type Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host + Send, - >, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/long-use-chain4")?; - inst.func_wrap_concurrent( - "foo", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::foo(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::Resource,), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::Resource + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::foo(store) - } - } - } - #[allow(clippy::all)] - pub mod transitive_interface_with_resource { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub enum Foo {} - pub trait HostFoo: Sized { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()>; - } - impl<_T: HostFoo + ?Sized> HostFoo for &mut _T { - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostFoo::drop(*self, rep) - } - } - pub trait Host: HostFoo + Sized {} - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker - .instance("foo:foo/transitive-interface-with-resource")?; - inst.resource( - "foo", - wasmtime::component::ResourceType::host::(), - move |mut store, rep| -> wasmtime::Result<()> { - HostFoo::drop( - &mut host_getter(store.data_mut()), - wasmtime::component::Resource::new_own(rep), - ) - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host + ?Sized> Host for &mut _T {} - } - } -} -pub mod exports { - pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod uses_resource_transitively { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type Foo = super::super::super::super::foo::foo::transitive_interface_with_resource::Foo; - pub struct Guest { - handle: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - handle: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "foo:foo/uses-resource-transitively") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/uses-resource-transitively`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `foo:foo/uses-resource-transitively` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let handle = lookup("handle")?; - Ok(GuestIndices { handle }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let handle = *_instance - .get_typed_func::< - (wasmtime::component::Resource,), - (), - >(&mut store, &self.handle)? - .func(); - Ok(Guest { handle }) - } - } - impl Guest { - pub async fn call_handle( - &self, - mut store: S, - arg0: wasmtime::component::Resource, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (wasmtime::component::Resource,), - (), - >::new_unchecked(self.handle) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise) - } - } - } - } - } -} diff --git a/crates/component-macro/tests/expanded/resources-import_tracing_async.rs b/crates/component-macro/tests/expanded/resources-import_tracing_async.rs index 60259c4c597b..b9a169027280 100644 --- a/crates/component-macro/tests/expanded/resources-import_tracing_async.rs +++ b/crates/component-macro/tests/expanded/resources-import_tracing_async.rs @@ -230,13 +230,14 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: TheWorldImports>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: TheWorldImports, + T: Send + 'static, { let mut linker = linker.root(); linker @@ -357,26 +358,31 @@ const _: () = { )?; Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::resources::Host + foo::foo::long_use_chain1::Host + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: foo::foo::resources::Host + foo::foo::long_use_chain1::Host + foo::foo::long_use_chain2::Host + foo::foo::long_use_chain3::Host + foo::foo::long_use_chain4::Host + foo::foo::transitive_interface_with_resource::Host + TheWorldImports + Send, + T: Send + 'static, { - Self::add_to_linker_imports_get_host(linker, get)?; - foo::foo::resources::add_to_linker(linker, get)?; - foo::foo::long_use_chain1::add_to_linker(linker, get)?; - foo::foo::long_use_chain2::add_to_linker(linker, get)?; - foo::foo::long_use_chain3::add_to_linker(linker, get)?; - foo::foo::long_use_chain4::add_to_linker(linker, get)?; - foo::foo::transitive_interface_with_resource::add_to_linker(linker, get)?; + Self::add_to_linker_imports::(linker, get)?; + foo::foo::resources::add_to_linker::(linker, get)?; + foo::foo::long_use_chain1::add_to_linker::(linker, get)?; + foo::foo::long_use_chain2::add_to_linker::(linker, get)?; + foo::foo::long_use_chain3::add_to_linker::(linker, get)?; + foo::foo::long_use_chain4::add_to_linker::(linker, get)?; + foo::foo::transitive_interface_with_resource::add_to_linker::< + T, + D, + >(linker, get)?; Ok(()) } pub async fn call_some_world_func2( @@ -572,14 +578,14 @@ pub mod foo { async fn record_result(&mut self) -> NestedOwn; async fn func_with_handle_typedef(&mut self, x: SomeHandle) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/resources")?; inst.resource_async( @@ -1199,17 +1205,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn bar_own_arg( &mut self, @@ -1336,14 +1331,14 @@ pub mod foo { } #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send + HostA + Sized {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/long-use-chain1")?; inst.resource_async( @@ -1361,17 +1356,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } #[allow(clippy::all)] @@ -1381,29 +1365,18 @@ pub mod foo { pub type A = super::super::super::foo::foo::long_use_chain1::A; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/long-use-chain2")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } #[allow(clippy::all)] @@ -1413,29 +1386,18 @@ pub mod foo { pub type A = super::super::super::foo::foo::long_use_chain2::A; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/long-use-chain3")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } #[allow(clippy::all)] @@ -1447,14 +1409,14 @@ pub mod foo { pub trait Host: Send { async fn foo(&mut self) -> wasmtime::component::Resource; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/long-use-chain4")?; inst.func_wrap_async( @@ -1482,17 +1444,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn foo(&mut self) -> wasmtime::component::Resource { Host::foo(*self).await @@ -1521,14 +1472,14 @@ pub mod foo { } #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send + HostFoo + Sized {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker .instance("foo:foo/transitive-interface-with-resource")?; @@ -1547,17 +1498,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/share-types.rs b/crates/component-macro/tests/expanded/share-types.rs index d19cb549aac8..e28013da8ddb 100644 --- a/crates/component-macro/tests/expanded/share-types.rs +++ b/crates/component-macro/tests/expanded/share-types.rs @@ -144,16 +144,17 @@ const _: () = { let indices = HttpInterfaceIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::http_types::Host + http_fetch::Host, T: 'static, - U: foo::foo::http_types::Host + http_fetch::Host, { - foo::foo::http_types::add_to_linker(linker, get)?; - http_fetch::add_to_linker(linker, get)?; + foo::foo::http_types::add_to_linker::(linker, get)?; + http_fetch::add_to_linker::(linker, get)?; Ok(()) } pub fn http_handler(&self) -> &exports::http_handler::Guest { @@ -206,27 +207,18 @@ pub mod foo { ); }; pub trait Host {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/http-types")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } } @@ -248,13 +240,14 @@ pub mod http_fetch { pub trait Host { fn fetch_request(&mut self, request: Request) -> Response; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("http-fetch")?; inst.func_wrap( @@ -267,16 +260,6 @@ pub mod http_fetch { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn fetch_request(&mut self, request: Request) -> Response { Host::fetch_request(*self, request) diff --git a/crates/component-macro/tests/expanded/share-types_async.rs b/crates/component-macro/tests/expanded/share-types_async.rs index a81290880be0..3777169b90a7 100644 --- a/crates/component-macro/tests/expanded/share-types_async.rs +++ b/crates/component-macro/tests/expanded/share-types_async.rs @@ -150,17 +150,17 @@ const _: () = { let indices = HttpInterfaceIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::http_types::Host + http_fetch::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::http_types::Host + http_fetch::Host + Send, + T: Send + 'static, { - foo::foo::http_types::add_to_linker(linker, get)?; - http_fetch::add_to_linker(linker, get)?; + foo::foo::http_types::add_to_linker::(linker, get)?; + http_fetch::add_to_linker::(linker, get)?; Ok(()) } pub fn http_handler(&self) -> &exports::http_handler::Guest { @@ -214,29 +214,18 @@ pub mod foo { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/http-types")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } @@ -259,14 +248,14 @@ pub mod http_fetch { pub trait Host: Send { async fn fetch_request(&mut self, request: Request) -> Response; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("http-fetch")?; inst.func_wrap_async( @@ -281,17 +270,6 @@ pub mod http_fetch { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn fetch_request(&mut self, request: Request) -> Response { Host::fetch_request(*self, request).await diff --git a/crates/component-macro/tests/expanded/share-types_concurrent.rs b/crates/component-macro/tests/expanded/share-types_concurrent.rs deleted file mode 100644 index ea067bf6ec90..000000000000 --- a/crates/component-macro/tests/expanded/share-types_concurrent.rs +++ /dev/null @@ -1,434 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `http-interface`. -/// -/// This structure is created through [`HttpInterfacePre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`HttpInterface`] as well. -pub struct HttpInterfacePre { - instance_pre: wasmtime::component::InstancePre, - indices: HttpInterfaceIndices, -} -impl Clone for HttpInterfacePre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> HttpInterfacePre<_T> { - /// Creates a new copy of `HttpInterfacePre` 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 { - let indices = HttpInterfaceIndices::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 [`HttpInterface`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `http-interface`. -/// -/// This is an implementation detail of [`HttpInterfacePre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`HttpInterface`] as well. -#[derive(Clone)] -pub struct HttpInterfaceIndices { - interface0: exports::http_handler::GuestIndices, -} -/// Auto-generated bindings for an instance a component which -/// implements the world `http-interface`. -/// -/// 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 -/// [`HttpInterface::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`HttpInterfacePre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`HttpInterfacePre::instantiate_async`] to -/// create a [`HttpInterface`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`HttpInterface::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 HttpInterface { - interface0: exports::http_handler::Guest, -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl HttpInterfaceIndices { - /// Creates a new copy of `HttpInterfaceIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - let interface0 = exports::http_handler::GuestIndices::new(_instance_pre)?; - Ok(HttpInterfaceIndices { interface0 }) - } - /// Uses the indices stored in `self` to load an instance - /// of [`HttpInterface`] 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 { - let _ = &mut store; - let _instance = instance; - let interface0 = self.interface0.load(&mut store, &_instance)?; - Ok(HttpInterface { interface0 }) - } - } - impl HttpInterface { - /// Convenience wrapper around [`HttpInterfacePre::new`] and - /// [`HttpInterfacePre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - HttpInterfacePre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`HttpInterfaceIndices::new`] and - /// [`HttpInterfaceIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = HttpInterfaceIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + foo::foo::http_types::Host + http_fetch::Host, - U: Send + foo::foo::http_types::Host + http_fetch::Host, - { - foo::foo::http_types::add_to_linker(linker, get)?; - http_fetch::add_to_linker(linker, get)?; - Ok(()) - } - pub fn http_handler(&self) -> &exports::http_handler::Guest { - &self.interface0 - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod http_types { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone)] - pub struct Request { - #[component(name = "method")] - pub method: wasmtime::component::__internal::String, - } - impl core::fmt::Debug for Request { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("Request").field("method", &self.method).finish() - } - } - const _: () = { - assert!(8 == < Request as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < Request as wasmtime::component::ComponentType >::ALIGN32); - }; - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(record)] - #[derive(Clone)] - pub struct Response { - #[component(name = "body")] - pub body: wasmtime::component::__internal::String, - } - impl core::fmt::Debug for Response { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("Response").field("body", &self.body).finish() - } - } - const _: () = { - assert!(8 == < Response as wasmtime::component::ComponentType >::SIZE32); - assert!( - 4 == < Response as wasmtime::component::ComponentType >::ALIGN32 - ); - }; - pub trait Host {} - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/http-types")?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host + ?Sized> Host for &mut _T {} - } - } -} -#[allow(clippy::all)] -pub mod http_fetch { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type Request = super::foo::foo::http_types::Request; - const _: () = { - assert!(8 == < Request as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < Request as wasmtime::component::ComponentType >::ALIGN32); - }; - pub type Response = super::foo::foo::http_types::Response; - const _: () = { - assert!(8 == < Response as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < Response as wasmtime::component::ComponentType >::ALIGN32); - }; - pub trait Host { - type Data; - fn fetch_request( - store: wasmtime::StoreContextMut<'_, Self::Data>, - request: Request, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Response + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker.instance("http-fetch")?; - inst.func_wrap_concurrent( - "fetch-request", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (Request,)| { - let host = caller; - let r = ::fetch_request(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Response,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Response,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn fetch_request( - store: wasmtime::StoreContextMut<'_, Self::Data>, - request: Request, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Response + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::fetch_request(store, request) - } - } -} -pub mod exports { - #[allow(clippy::all)] - pub mod http_handler { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type Request = super::super::foo::foo::http_types::Request; - const _: () = { - assert!(8 == < Request as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < Request as wasmtime::component::ComponentType >::ALIGN32); - }; - pub type Response = super::super::foo::foo::http_types::Response; - const _: () = { - assert!(8 == < Response as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < Response as wasmtime::component::ComponentType >::ALIGN32); - }; - pub struct Guest { - handle_request: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - handle_request: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "http-handler") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `http-handler`") - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `http-handler` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let handle_request = lookup("handle-request")?; - Ok(GuestIndices { handle_request }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let handle_request = *_instance - .get_typed_func::< - (&Request,), - (Response,), - >(&mut store, &self.handle_request)? - .func(); - Ok(Guest { handle_request }) - } - } - impl Guest { - pub async fn call_handle_request( - &self, - mut store: S, - arg0: Request, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (Request,), - (Response,), - >::new_unchecked(self.handle_request) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise.map(|(v,)| v)) - } - } - } -} diff --git a/crates/component-macro/tests/expanded/share-types_tracing_async.rs b/crates/component-macro/tests/expanded/share-types_tracing_async.rs index 88034bd9de10..477e7cdd2ee3 100644 --- a/crates/component-macro/tests/expanded/share-types_tracing_async.rs +++ b/crates/component-macro/tests/expanded/share-types_tracing_async.rs @@ -150,17 +150,17 @@ const _: () = { let indices = HttpInterfaceIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::http_types::Host + http_fetch::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::http_types::Host + http_fetch::Host + Send, + T: Send + 'static, { - foo::foo::http_types::add_to_linker(linker, get)?; - http_fetch::add_to_linker(linker, get)?; + foo::foo::http_types::add_to_linker::(linker, get)?; + http_fetch::add_to_linker::(linker, get)?; Ok(()) } pub fn http_handler(&self) -> &exports::http_handler::Guest { @@ -214,29 +214,18 @@ pub mod foo { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/http-types")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } @@ -259,14 +248,14 @@ pub mod http_fetch { pub trait Host: Send { async fn fetch_request(&mut self, request: Request) -> Response; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("http-fetch")?; inst.func_wrap_async( @@ -297,17 +286,6 @@ pub mod http_fetch { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn fetch_request(&mut self, request: Request) -> Response { Host::fetch_request(*self, request).await diff --git a/crates/component-macro/tests/expanded/simple-functions.rs b/crates/component-macro/tests/expanded/simple-functions.rs index 3bd1c33a24c1..d023569c02ba 100644 --- a/crates/component-macro/tests/expanded/simple-functions.rs +++ b/crates/component-macro/tests/expanded/simple-functions.rs @@ -146,15 +146,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::simple::Host, T: 'static, - U: foo::foo::simple::Host, { - foo::foo::simple::add_to_linker(linker, get)?; + foo::foo::simple::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_simple(&self) -> &exports::foo::foo::simple::Guest { @@ -176,13 +177,14 @@ pub mod foo { fn f5(&mut self) -> (u32, u32); fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/simple")?; inst.func_wrap( @@ -241,16 +243,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn f1(&mut self) -> () { Host::f1(*self) diff --git a/crates/component-macro/tests/expanded/simple-functions_async.rs b/crates/component-macro/tests/expanded/simple-functions_async.rs index f52360398d20..ab796fbc4df7 100644 --- a/crates/component-macro/tests/expanded/simple-functions_async.rs +++ b/crates/component-macro/tests/expanded/simple-functions_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::simple::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::simple::Host + Send, + T: Send + 'static, { - foo::foo::simple::add_to_linker(linker, get)?; + foo::foo::simple::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_simple(&self) -> &exports::foo::foo::simple::Guest { @@ -184,14 +184,14 @@ pub mod foo { async fn f5(&mut self) -> (u32, u32); async fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/simple")?; inst.func_wrap_async( @@ -262,17 +262,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn f1(&mut self) -> () { Host::f1(*self).await diff --git a/crates/component-macro/tests/expanded/simple-functions_tracing_async.rs b/crates/component-macro/tests/expanded/simple-functions_tracing_async.rs index b1c716d6303f..2cdafc1d4ad3 100644 --- a/crates/component-macro/tests/expanded/simple-functions_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-functions_tracing_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::simple::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::simple::Host + Send, + T: Send + 'static, { - foo::foo::simple::add_to_linker(linker, get)?; + foo::foo::simple::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_simple(&self) -> &exports::foo::foo::simple::Guest { @@ -184,14 +184,14 @@ pub mod foo { async fn f5(&mut self) -> (u32, u32); async fn f6(&mut self, a: u32, b: u32, c: u32) -> (u32, u32, u32); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/simple")?; inst.func_wrap_async( @@ -350,17 +350,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn f1(&mut self) -> () { Host::f1(*self).await diff --git a/crates/component-macro/tests/expanded/simple-lists.rs b/crates/component-macro/tests/expanded/simple-lists.rs index ad026602850f..933f19bbdb8a 100644 --- a/crates/component-macro/tests/expanded/simple-lists.rs +++ b/crates/component-macro/tests/expanded/simple-lists.rs @@ -146,15 +146,16 @@ const _: () = { let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::simple_lists::Host, T: 'static, - U: foo::foo::simple_lists::Host, { - foo::foo::simple_lists::add_to_linker(linker, get)?; + foo::foo::simple_lists::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_simple_lists(&self) -> &exports::foo::foo::simple_lists::Guest { @@ -191,13 +192,14 @@ pub mod foo { wasmtime::component::__internal::Vec, >; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/simple-lists")?; inst.func_wrap( @@ -255,16 +257,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn simple_list1( &mut self, diff --git a/crates/component-macro/tests/expanded/simple-lists_async.rs b/crates/component-macro/tests/expanded/simple-lists_async.rs index a93e802a6692..6b10914dc2b0 100644 --- a/crates/component-macro/tests/expanded/simple-lists_async.rs +++ b/crates/component-macro/tests/expanded/simple-lists_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::simple_lists::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::simple_lists::Host + Send, + T: Send + 'static, { - foo::foo::simple_lists::add_to_linker(linker, get)?; + foo::foo::simple_lists::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_simple_lists(&self) -> &exports::foo::foo::simple_lists::Guest { @@ -201,14 +201,14 @@ pub mod foo { wasmtime::component::__internal::Vec, >; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/simple-lists")?; inst.func_wrap_async( @@ -274,17 +274,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn simple_list1( &mut self, diff --git a/crates/component-macro/tests/expanded/simple-lists_concurrent.rs b/crates/component-macro/tests/expanded/simple-lists_concurrent.rs deleted file mode 100644 index 75d318393455..000000000000 --- a/crates/component-macro/tests/expanded/simple-lists_concurrent.rs +++ /dev/null @@ -1,722 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `my-world`. -/// -/// This structure is created through [`MyWorldPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`MyWorld`] as well. -pub struct MyWorldPre { - instance_pre: wasmtime::component::InstancePre, - indices: MyWorldIndices, -} -impl Clone for MyWorldPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> MyWorldPre<_T> { - /// Creates a new copy of `MyWorldPre` 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 { - let indices = MyWorldIndices::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 [`MyWorld`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `my-world`. -/// -/// This is an implementation detail of [`MyWorldPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`MyWorld`] as well. -#[derive(Clone)] -pub struct MyWorldIndices { - interface0: exports::foo::foo::simple_lists::GuestIndices, -} -/// Auto-generated bindings for an instance a component which -/// implements the world `my-world`. -/// -/// 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 -/// [`MyWorld::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`MyWorldPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`MyWorldPre::instantiate_async`] to -/// create a [`MyWorld`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`MyWorld::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 MyWorld { - interface0: exports::foo::foo::simple_lists::Guest, -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl MyWorldIndices { - /// Creates a new copy of `MyWorldIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - let interface0 = exports::foo::foo::simple_lists::GuestIndices::new( - _instance_pre, - )?; - Ok(MyWorldIndices { interface0 }) - } - /// Uses the indices stored in `self` to load an instance - /// of [`MyWorld`] 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 { - let _ = &mut store; - let _instance = instance; - let interface0 = self.interface0.load(&mut store, &_instance)?; - Ok(MyWorld { interface0 }) - } - } - impl MyWorld { - /// Convenience wrapper around [`MyWorldPre::new`] and - /// [`MyWorldPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - MyWorldPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`MyWorldIndices::new`] and - /// [`MyWorldIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + foo::foo::simple_lists::Host, - U: Send + foo::foo::simple_lists::Host, - { - foo::foo::simple_lists::add_to_linker(linker, get)?; - Ok(()) - } - pub fn foo_foo_simple_lists(&self) -> &exports::foo::foo::simple_lists::Guest { - &self.interface0 - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod simple_lists { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub trait Host { - type Data; - fn simple_list1( - store: wasmtime::StoreContextMut<'_, Self::Data>, - l: wasmtime::component::__internal::Vec, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn simple_list2( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::__internal::Vec< - u32, - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn simple_list3( - store: wasmtime::StoreContextMut<'_, Self::Data>, - a: wasmtime::component::__internal::Vec, - b: wasmtime::component::__internal::Vec, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> ( - wasmtime::component::__internal::Vec, - wasmtime::component::__internal::Vec, - ) + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn simple_list4( - store: wasmtime::StoreContextMut<'_, Self::Data>, - l: wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host + Send, - >, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/simple-lists")?; - inst.func_wrap_concurrent( - "simple-list1", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (wasmtime::component::__internal::Vec,)| - { - let host = caller; - let r = ::simple_list1(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "simple-list2", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::simple_list2(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::__internal::Vec,), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - (wasmtime::component::__internal::Vec,), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "simple-list3", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - ( - arg0, - arg1, - ): ( - wasmtime::component::__internal::Vec, - wasmtime::component::__internal::Vec, - )| - { - let host = caller; - let r = ::simple_list3(host, arg0, arg1); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - ( - ( - wasmtime::component::__internal::Vec, - wasmtime::component::__internal::Vec, - ), - ), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - ( - ( - wasmtime::component::__internal::Vec, - wasmtime::component::__internal::Vec, - ), - ), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - inst.func_wrap_concurrent( - "simple-list4", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - ( - arg0, - ): ( - wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - )| - { - let host = caller; - let r = ::simple_list4(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - ( - wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - ), - > + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result< - ( - wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - ), - > + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn simple_list1( - store: wasmtime::StoreContextMut<'_, Self::Data>, - l: wasmtime::component::__internal::Vec, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::simple_list1(store, l) - } - fn simple_list2( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::__internal::Vec< - u32, - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::simple_list2(store) - } - fn simple_list3( - store: wasmtime::StoreContextMut<'_, Self::Data>, - a: wasmtime::component::__internal::Vec, - b: wasmtime::component::__internal::Vec, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> ( - wasmtime::component::__internal::Vec, - wasmtime::component::__internal::Vec, - ) + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::simple_list3(store, a, b) - } - fn simple_list4( - store: wasmtime::StoreContextMut<'_, Self::Data>, - l: wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - > + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::simple_list4(store, l) - } - } - } - } -} -pub mod exports { - pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod simple_lists { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub struct Guest { - simple_list1: wasmtime::component::Func, - simple_list2: wasmtime::component::Func, - simple_list3: wasmtime::component::Func, - simple_list4: wasmtime::component::Func, - } - #[derive(Clone)] - pub struct GuestIndices { - simple_list1: wasmtime::component::ComponentExportIndex, - simple_list2: wasmtime::component::ComponentExportIndex, - simple_list3: wasmtime::component::ComponentExportIndex, - simple_list4: wasmtime::component::ComponentExportIndex, - } - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "foo:foo/simple-lists") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-lists`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `foo:foo/simple-lists` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - let simple_list1 = lookup("simple-list1")?; - let simple_list2 = lookup("simple-list2")?; - let simple_list3 = lookup("simple-list3")?; - let simple_list4 = lookup("simple-list4")?; - Ok(GuestIndices { - simple_list1, - simple_list2, - simple_list3, - simple_list4, - }) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - let simple_list1 = *_instance - .get_typed_func::< - (&[u32],), - (), - >(&mut store, &self.simple_list1)? - .func(); - let simple_list2 = *_instance - .get_typed_func::< - (), - (wasmtime::component::__internal::Vec,), - >(&mut store, &self.simple_list2)? - .func(); - let simple_list3 = *_instance - .get_typed_func::< - (&[u32], &[u32]), - ( - ( - wasmtime::component::__internal::Vec, - wasmtime::component::__internal::Vec, - ), - ), - >(&mut store, &self.simple_list3)? - .func(); - let simple_list4 = *_instance - .get_typed_func::< - (&[wasmtime::component::__internal::Vec],), - ( - wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - ), - >(&mut store, &self.simple_list4)? - .func(); - Ok(Guest { - simple_list1, - simple_list2, - simple_list3, - simple_list4, - }) - } - } - impl Guest { - pub async fn call_simple_list1( - &self, - mut store: S, - arg0: wasmtime::component::__internal::Vec, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (wasmtime::component::__internal::Vec,), - (), - >::new_unchecked(self.simple_list1) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise) - } - pub async fn call_simple_list2( - &self, - mut store: S, - ) -> wasmtime::Result< - wasmtime::component::Promise< - wasmtime::component::__internal::Vec, - >, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - (), - (wasmtime::component::__internal::Vec,), - >::new_unchecked(self.simple_list2) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), ()) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_simple_list3( - &self, - mut store: S, - arg0: wasmtime::component::__internal::Vec, - arg1: wasmtime::component::__internal::Vec, - ) -> wasmtime::Result< - wasmtime::component::Promise< - ( - wasmtime::component::__internal::Vec, - wasmtime::component::__internal::Vec, - ), - >, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - ( - wasmtime::component::__internal::Vec, - wasmtime::component::__internal::Vec, - ), - ( - ( - wasmtime::component::__internal::Vec, - wasmtime::component::__internal::Vec, - ), - ), - >::new_unchecked(self.simple_list3) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0, arg1)) - .await?; - Ok(promise.map(|(v,)| v)) - } - pub async fn call_simple_list4( - &self, - mut store: S, - arg0: wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - ) -> wasmtime::Result< - wasmtime::component::Promise< - wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - >, - > - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::< - ( - wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - ), - ( - wasmtime::component::__internal::Vec< - wasmtime::component::__internal::Vec, - >, - ), - >::new_unchecked(self.simple_list4) - }; - let promise = callee - .call_concurrent(store.as_context_mut(), (arg0,)) - .await?; - Ok(promise.map(|(v,)| v)) - } - } - } - } - } -} diff --git a/crates/component-macro/tests/expanded/simple-lists_tracing_async.rs b/crates/component-macro/tests/expanded/simple-lists_tracing_async.rs index bb6a18caf2a2..2417873bbfc3 100644 --- a/crates/component-macro/tests/expanded/simple-lists_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-lists_tracing_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::simple_lists::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::simple_lists::Host + Send, + T: Send + 'static, { - foo::foo::simple_lists::add_to_linker(linker, get)?; + foo::foo::simple_lists::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_simple_lists(&self) -> &exports::foo::foo::simple_lists::Guest { @@ -201,14 +201,14 @@ pub mod foo { wasmtime::component::__internal::Vec, >; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/simple-lists")?; inst.func_wrap_async( @@ -335,17 +335,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn simple_list1( &mut self, diff --git a/crates/component-macro/tests/expanded/simple-wasi.rs b/crates/component-macro/tests/expanded/simple-wasi.rs index 831d8c1ca9d7..142f228c7462 100644 --- a/crates/component-macro/tests/expanded/simple-wasi.rs +++ b/crates/component-macro/tests/expanded/simple-wasi.rs @@ -138,16 +138,19 @@ const _: () = { let indices = WasiIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: foo::foo::wasi_filesystem::Host + foo::foo::wall_clock::Host, T: 'static, - U: foo::foo::wasi_filesystem::Host + foo::foo::wall_clock::Host, { - foo::foo::wasi_filesystem::add_to_linker(linker, get)?; - foo::foo::wall_clock::add_to_linker(linker, get)?; + foo::foo::wasi_filesystem::add_to_linker::(linker, get)?; + foo::foo::wall_clock::add_to_linker::(linker, get)?; Ok(()) } } @@ -223,13 +226,14 @@ pub mod foo { fn create_directory_at(&mut self) -> Result<(), Errno>; fn stat(&mut self) -> Result; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/wasi-filesystem")?; inst.func_wrap( @@ -250,16 +254,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn create_directory_at(&mut self) -> Result<(), Errno> { Host::create_directory_at(*self) @@ -293,27 +287,18 @@ pub mod foo { ); }; pub trait Host {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/wall-clock")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/simple-wasi_async.rs b/crates/component-macro/tests/expanded/simple-wasi_async.rs index b7af18742d8a..8361f127d5da 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_async.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_async.rs @@ -144,17 +144,19 @@ const _: () = { let indices = WasiIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::wasi_filesystem::Host + foo::foo::wall_clock::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: foo::foo::wasi_filesystem::Host + foo::foo::wall_clock::Host + Send, + T: Send + 'static, { - foo::foo::wasi_filesystem::add_to_linker(linker, get)?; - foo::foo::wall_clock::add_to_linker(linker, get)?; + foo::foo::wasi_filesystem::add_to_linker::(linker, get)?; + foo::foo::wall_clock::add_to_linker::(linker, get)?; Ok(()) } } @@ -231,14 +233,14 @@ pub mod foo { async fn create_directory_at(&mut self) -> Result<(), Errno>; async fn stat(&mut self) -> Result; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/wasi-filesystem")?; inst.func_wrap_async( @@ -263,17 +265,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn create_directory_at(&mut self) -> Result<(), Errno> { Host::create_directory_at(*self).await @@ -308,29 +299,18 @@ pub mod foo { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/wall-clock")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs b/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs index c5782ec0a1ad..b1a08c911b6f 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs @@ -144,17 +144,19 @@ const _: () = { let indices = WasiIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::wasi_filesystem::Host + foo::foo::wall_clock::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: foo::foo::wasi_filesystem::Host + foo::foo::wall_clock::Host + Send, + T: Send + 'static, { - foo::foo::wasi_filesystem::add_to_linker(linker, get)?; - foo::foo::wall_clock::add_to_linker(linker, get)?; + foo::foo::wasi_filesystem::add_to_linker::(linker, get)?; + foo::foo::wall_clock::add_to_linker::(linker, get)?; Ok(()) } } @@ -231,14 +233,14 @@ pub mod foo { async fn create_directory_at(&mut self) -> Result<(), Errno>; async fn stat(&mut self) -> Result; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/wasi-filesystem")?; inst.func_wrap_async( @@ -289,17 +291,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn create_directory_at(&mut self) -> Result<(), Errno> { Host::create_directory_at(*self).await @@ -334,29 +325,18 @@ pub mod foo { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/wall-clock")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/small-anonymous.rs b/crates/component-macro/tests/expanded/small-anonymous.rs index 4f0bbbea24f9..c5ef5cf1ca01 100644 --- a/crates/component-macro/tests/expanded/small-anonymous.rs +++ b/crates/component-macro/tests/expanded/small-anonymous.rs @@ -144,15 +144,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::anon::Host, T: 'static, - U: foo::foo::anon::Host, { - foo::foo::anon::add_to_linker(linker, get)?; + foo::foo::anon::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_anon(&self) -> &exports::foo::foo::anon::Guest { @@ -216,13 +217,14 @@ pub mod foo { &mut self, ) -> Result, Error>; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/anon")?; inst.func_wrap( @@ -235,16 +237,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn option_test( &mut self, diff --git a/crates/component-macro/tests/expanded/small-anonymous_async.rs b/crates/component-macro/tests/expanded/small-anonymous_async.rs index f36c96736d62..6930b7e099ed 100644 --- a/crates/component-macro/tests/expanded/small-anonymous_async.rs +++ b/crates/component-macro/tests/expanded/small-anonymous_async.rs @@ -150,16 +150,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::anon::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::anon::Host + Send, + T: Send + 'static, { - foo::foo::anon::add_to_linker(linker, get)?; + foo::foo::anon::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_anon(&self) -> &exports::foo::foo::anon::Guest { @@ -224,14 +224,14 @@ pub mod foo { &mut self, ) -> Result, Error>; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/anon")?; inst.func_wrap_async( @@ -246,17 +246,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn option_test( &mut self, diff --git a/crates/component-macro/tests/expanded/small-anonymous_tracing_async.rs b/crates/component-macro/tests/expanded/small-anonymous_tracing_async.rs index fbfca9076135..afa026644c26 100644 --- a/crates/component-macro/tests/expanded/small-anonymous_tracing_async.rs +++ b/crates/component-macro/tests/expanded/small-anonymous_tracing_async.rs @@ -150,16 +150,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::anon::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::anon::Host + Send, + T: Send + 'static, { - foo::foo::anon::add_to_linker(linker, get)?; + foo::foo::anon::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_anon(&self) -> &exports::foo::foo::anon::Guest { @@ -224,14 +224,14 @@ pub mod foo { &mut self, ) -> Result, Error>; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/anon")?; inst.func_wrap_async( @@ -259,17 +259,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn option_test( &mut self, diff --git a/crates/component-macro/tests/expanded/smoke-default_concurrent.rs b/crates/component-macro/tests/expanded/smoke-default_concurrent.rs deleted file mode 100644 index a923abb5c839..000000000000 --- a/crates/component-macro/tests/expanded/smoke-default_concurrent.rs +++ /dev/null @@ -1,181 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `the-world`. -/// -/// This structure is created through [`TheWorldPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`TheWorld`] as well. -pub struct TheWorldPre { - instance_pre: wasmtime::component::InstancePre, - indices: TheWorldIndices, -} -impl Clone for TheWorldPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> TheWorldPre<_T> { - /// Creates a new copy of `TheWorldPre` 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 { - let indices = TheWorldIndices::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 [`TheWorld`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `the-world`. -/// -/// This is an implementation detail of [`TheWorldPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`TheWorld`] as well. -#[derive(Clone)] -pub struct TheWorldIndices { - y: wasmtime::component::ComponentExportIndex, -} -/// Auto-generated bindings for an instance a component which -/// implements the world `the-world`. -/// -/// 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 -/// [`TheWorld::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`TheWorldPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`TheWorldPre::instantiate_async`] to -/// create a [`TheWorld`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`TheWorld::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 TheWorld { - y: wasmtime::component::Func, -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl TheWorldIndices { - /// Creates a new copy of `TheWorldIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - let y = { - let (item, index) = _component - .get_export(None, "y") - .ok_or_else(|| anyhow::anyhow!("no export `y` found"))?; - match item { - wasmtime::component::types::ComponentItem::ComponentFunc(func) => { - anyhow::Context::context( - func.typecheck::<(), ()>(&_instance_type), - "type-checking export func `y`", - )?; - index - } - _ => Err(anyhow::anyhow!("export `y` is not a function"))?, - } - }; - Ok(TheWorldIndices { y }) - } - /// Uses the indices stored in `self` to load an instance - /// of [`TheWorld`] 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 { - let _ = &mut store; - let _instance = instance; - let y = *_instance.get_typed_func::<(), ()>(&mut store, &self.y)?.func(); - Ok(TheWorld { y }) - } - } - impl TheWorld { - /// Convenience wrapper around [`TheWorldPre::new`] and - /// [`TheWorldPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - TheWorldPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`TheWorldIndices::new`] and - /// [`TheWorldIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub async fn call_y( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.y) - }; - let promise = callee.call_concurrent(store.as_context_mut(), ()).await?; - Ok(promise) - } - } -}; diff --git a/crates/component-macro/tests/expanded/smoke.rs b/crates/component-macro/tests/expanded/smoke.rs index 68aef4053bf9..c0a45566fabe 100644 --- a/crates/component-macro/tests/expanded/smoke.rs +++ b/crates/component-macro/tests/expanded/smoke.rs @@ -138,15 +138,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: imports::Host, T: 'static, - U: imports::Host, { - imports::add_to_linker(linker, get)?; + imports::add_to_linker::(linker, get)?; Ok(()) } } @@ -158,13 +159,14 @@ pub mod imports { pub trait Host { fn y(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("imports")?; inst.func_wrap( @@ -177,16 +179,6 @@ pub mod imports { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn y(&mut self) -> () { Host::y(*self) diff --git a/crates/component-macro/tests/expanded/smoke_async.rs b/crates/component-macro/tests/expanded/smoke_async.rs index ef1531dd4914..0946c21b4a97 100644 --- a/crates/component-macro/tests/expanded/smoke_async.rs +++ b/crates/component-macro/tests/expanded/smoke_async.rs @@ -144,16 +144,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: imports::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: imports::Host + Send, + T: Send + 'static, { - imports::add_to_linker(linker, get)?; + imports::add_to_linker::(linker, get)?; Ok(()) } } @@ -166,14 +166,14 @@ pub mod imports { pub trait Host: Send { async fn y(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("imports")?; inst.func_wrap_async( @@ -188,17 +188,6 @@ pub mod imports { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn y(&mut self) -> () { Host::y(*self).await diff --git a/crates/component-macro/tests/expanded/smoke_tracing_async.rs b/crates/component-macro/tests/expanded/smoke_tracing_async.rs index 5b7ca7f75ab4..42214aa15f70 100644 --- a/crates/component-macro/tests/expanded/smoke_tracing_async.rs +++ b/crates/component-macro/tests/expanded/smoke_tracing_async.rs @@ -144,16 +144,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: imports::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: imports::Host + Send, + T: Send + 'static, { - imports::add_to_linker(linker, get)?; + imports::add_to_linker::(linker, get)?; Ok(()) } } @@ -166,14 +166,14 @@ pub mod imports { pub trait Host: Send { async fn y(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("imports")?; inst.func_wrap_async( @@ -201,17 +201,6 @@ pub mod imports { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn y(&mut self) -> () { Host::y(*self).await diff --git a/crates/component-macro/tests/expanded/strings.rs b/crates/component-macro/tests/expanded/strings.rs index d6e055e59a6f..611980965913 100644 --- a/crates/component-macro/tests/expanded/strings.rs +++ b/crates/component-macro/tests/expanded/strings.rs @@ -146,15 +146,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::strings::Host, T: 'static, - U: foo::foo::strings::Host, { - foo::foo::strings::add_to_linker(linker, get)?; + foo::foo::strings::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { @@ -177,13 +178,14 @@ pub mod foo { b: wasmtime::component::__internal::String, ) -> wasmtime::component::__internal::String; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/strings")?; inst.func_wrap( @@ -224,16 +226,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn a(&mut self, x: wasmtime::component::__internal::String) -> () { Host::a(*self, x) diff --git a/crates/component-macro/tests/expanded/strings_async.rs b/crates/component-macro/tests/expanded/strings_async.rs index 9df9afc692f7..e5219ceef66c 100644 --- a/crates/component-macro/tests/expanded/strings_async.rs +++ b/crates/component-macro/tests/expanded/strings_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::strings::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::strings::Host + Send, + T: Send + 'static, { - foo::foo::strings::add_to_linker(linker, get)?; + foo::foo::strings::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { @@ -185,14 +185,14 @@ pub mod foo { b: wasmtime::component::__internal::String, ) -> wasmtime::component::__internal::String; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/strings")?; inst.func_wrap_async( @@ -239,17 +239,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a(&mut self, x: wasmtime::component::__internal::String) -> () { Host::a(*self, x).await diff --git a/crates/component-macro/tests/expanded/strings_tracing_async.rs b/crates/component-macro/tests/expanded/strings_tracing_async.rs index bb4f71cb622a..f130f24ed1af 100644 --- a/crates/component-macro/tests/expanded/strings_tracing_async.rs +++ b/crates/component-macro/tests/expanded/strings_tracing_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::strings::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::strings::Host + Send, + T: Send + 'static, { - foo::foo::strings::add_to_linker(linker, get)?; + foo::foo::strings::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_strings(&self) -> &exports::foo::foo::strings::Guest { @@ -185,14 +185,14 @@ pub mod foo { b: wasmtime::component::__internal::String, ) -> wasmtime::component::__internal::String; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/strings")?; inst.func_wrap_async( @@ -284,17 +284,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a(&mut self, x: wasmtime::component::__internal::String) -> () { Host::a(*self, x).await diff --git a/crates/component-macro/tests/expanded/unstable-features.rs b/crates/component-macro/tests/expanded/unstable-features.rs index 7389e25ce2c5..79c39557a7a0 100644 --- a/crates/component-macro/tests/expanded/unstable-features.rs +++ b/crates/component-macro/tests/expanded/unstable-features.rs @@ -239,13 +239,15 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, options: &LinkOptions, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: TheWorldImports>, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: TheWorldImports, + T: 'static, { let mut linker = linker.root(); if options.experimental_world { @@ -290,23 +292,23 @@ const _: () = { } Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, options: &LinkOptions, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::the_interface::Host + TheWorldImports, T: 'static, - U: foo::foo::the_interface::Host + TheWorldImports, { if options.experimental_world { - Self::add_to_linker_imports_get_host(linker, options, get)?; + Self::add_to_linker_imports::(linker, options, get)?; if options.experimental_world_interface_import { - foo::foo::the_interface::add_to_linker( - linker, - &options.into(), - get, - )?; + foo::foo::the_interface::add_to_linker::< + T, + D, + >(linker, &options.into(), get)?; } } Ok(()) @@ -380,14 +382,15 @@ pub mod foo { pub trait Host: HostBar + Sized { fn foo(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, options: &LinkOptions, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { if options.experimental_interface { let mut inst = linker.instance("foo:foo/the-interface")?; @@ -429,17 +432,6 @@ pub mod foo { } Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - options: &LinkOptions, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, options, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn foo(&mut self) -> () { Host::foo(*self) diff --git a/crates/component-macro/tests/expanded/unstable-features_async.rs b/crates/component-macro/tests/expanded/unstable-features_async.rs index 12a7e62461be..e4c24f185acc 100644 --- a/crates/component-macro/tests/expanded/unstable-features_async.rs +++ b/crates/component-macro/tests/expanded/unstable-features_async.rs @@ -253,14 +253,15 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, options: &LinkOptions, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: TheWorldImports>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: TheWorldImports, + T: Send + 'static, { let mut linker = linker.root(); if options.experimental_world { @@ -312,24 +313,23 @@ const _: () = { } Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, options: &LinkOptions, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::the_interface::Host + TheWorldImports + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::the_interface::Host + TheWorldImports + Send, + T: Send + 'static, { if options.experimental_world { - Self::add_to_linker_imports_get_host(linker, options, get)?; + Self::add_to_linker_imports::(linker, options, get)?; if options.experimental_world_interface_import { - foo::foo::the_interface::add_to_linker( - linker, - &options.into(), - get, - )?; + foo::foo::the_interface::add_to_linker::< + T, + D, + >(linker, &options.into(), get)?; } } Ok(()) @@ -408,15 +408,15 @@ pub mod foo { pub trait Host: Send + HostBar + Sized { async fn foo(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, options: &LinkOptions, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { if options.experimental_interface { let mut inst = linker.instance("foo:foo/the-interface")?; @@ -465,18 +465,6 @@ pub mod foo { } Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - options: &LinkOptions, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, options, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn foo(&mut self) -> () { Host::foo(*self).await diff --git a/crates/component-macro/tests/expanded/unstable-features_concurrent.rs b/crates/component-macro/tests/expanded/unstable-features_concurrent.rs deleted file mode 100644 index 51a1f8f54be0..000000000000 --- a/crates/component-macro/tests/expanded/unstable-features_concurrent.rs +++ /dev/null @@ -1,644 +0,0 @@ -/// Link-time configurations. -#[derive(Clone, Debug, Default)] -pub struct LinkOptions { - experimental_interface: bool, - experimental_interface_function: bool, - experimental_interface_resource: bool, - experimental_interface_resource_method: bool, - experimental_world: bool, - experimental_world_function_import: bool, - experimental_world_interface_import: bool, - experimental_world_resource: bool, - experimental_world_resource_method: bool, -} -impl LinkOptions { - /// Enable members marked as `@unstable(feature = experimental-interface)` - pub fn experimental_interface(&mut self, enabled: bool) -> &mut Self { - self.experimental_interface = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-interface-function)` - pub fn experimental_interface_function(&mut self, enabled: bool) -> &mut Self { - self.experimental_interface_function = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-interface-resource)` - pub fn experimental_interface_resource(&mut self, enabled: bool) -> &mut Self { - self.experimental_interface_resource = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-interface-resource-method)` - pub fn experimental_interface_resource_method( - &mut self, - enabled: bool, - ) -> &mut Self { - self.experimental_interface_resource_method = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-world)` - pub fn experimental_world(&mut self, enabled: bool) -> &mut Self { - self.experimental_world = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-world-function-import)` - pub fn experimental_world_function_import(&mut self, enabled: bool) -> &mut Self { - self.experimental_world_function_import = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-world-interface-import)` - pub fn experimental_world_interface_import(&mut self, enabled: bool) -> &mut Self { - self.experimental_world_interface_import = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-world-resource)` - pub fn experimental_world_resource(&mut self, enabled: bool) -> &mut Self { - self.experimental_world_resource = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-world-resource-method)` - pub fn experimental_world_resource_method(&mut self, enabled: bool) -> &mut Self { - self.experimental_world_resource_method = enabled; - self - } -} -impl core::convert::From for foo::foo::the_interface::LinkOptions { - fn from(src: LinkOptions) -> Self { - (&src).into() - } -} -impl core::convert::From<&LinkOptions> for foo::foo::the_interface::LinkOptions { - fn from(src: &LinkOptions) -> Self { - let mut dest = Self::default(); - dest.experimental_interface(src.experimental_interface); - dest.experimental_interface_function(src.experimental_interface_function); - dest.experimental_interface_resource(src.experimental_interface_resource); - dest.experimental_interface_resource_method( - src.experimental_interface_resource_method, - ); - dest - } -} -pub enum Baz {} -pub trait HostBaz: Sized { - type BazData; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::BazData>, - self_: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BazData>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn drop(&mut self, rep: wasmtime::component::Resource) -> wasmtime::Result<()>; -} -impl<_T: HostBaz> HostBaz for &mut _T { - type BazData = _T::BazData; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::BazData>, - self_: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BazData>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as HostBaz>::foo(store, self_) - } - fn drop(&mut self, rep: wasmtime::component::Resource) -> wasmtime::Result<()> { - HostBaz::drop(*self, rep) - } -} -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `the-world`. -/// -/// This structure is created through [`TheWorldPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`TheWorld`] as well. -pub struct TheWorldPre { - instance_pre: wasmtime::component::InstancePre, - indices: TheWorldIndices, -} -impl Clone for TheWorldPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> TheWorldPre<_T> { - /// Creates a new copy of `TheWorldPre` 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 { - let indices = TheWorldIndices::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 [`TheWorld`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `the-world`. -/// -/// This is an implementation detail of [`TheWorldPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`TheWorld`] as well. -#[derive(Clone)] -pub struct TheWorldIndices {} -/// Auto-generated bindings for an instance a component which -/// implements the world `the-world`. -/// -/// 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 -/// [`TheWorld::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`TheWorldPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`TheWorldPre::instantiate_async`] to -/// create a [`TheWorld`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`TheWorld::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 TheWorld {} -pub trait TheWorldImports: HostBaz { - type Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; -} -impl<_T: TheWorldImports> TheWorldImports for &mut _T { - type Data = _T::Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as TheWorldImports>::foo(store) - } -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl TheWorldIndices { - /// Creates a new copy of `TheWorldIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - Ok(TheWorldIndices {}) - } - /// Uses the indices stored in `self` to load an instance - /// of [`TheWorld`] 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 { - let _ = &mut store; - let _instance = instance; - Ok(TheWorld {}) - } - } - impl TheWorld { - /// Convenience wrapper around [`TheWorldPre::new`] and - /// [`TheWorldPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - TheWorldPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`TheWorldIndices::new`] and - /// [`TheWorldIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker_imports_get_host( - linker: &mut wasmtime::component::Linker, - options: &LinkOptions, - host_getter: G, - ) -> wasmtime::Result<()> - where - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: TheWorldImports, - >, - T: Send, - { - let mut linker = linker.root(); - if options.experimental_world { - if options.experimental_world_resource { - linker - .resource( - "baz", - wasmtime::component::ResourceType::host::(), - move |mut store, rep| -> wasmtime::Result<()> { - HostBaz::drop( - &mut host_getter(store.data_mut()), - wasmtime::component::Resource::new_own(rep), - ) - }, - )?; - } - if options.experimental_world_function_import { - linker - .func_wrap_concurrent( - "foo", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::foo(host); - Box::pin(async move { - let fun = r.await; - Box::new(move | - mut caller: wasmtime::StoreContextMut<'_, T>| - { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - } - if options.experimental_world_resource_method { - linker - .func_wrap_concurrent( - "[method]baz.foo", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (wasmtime::component::Resource,)| - { - let host = caller; - let r = ::foo(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move | - mut caller: wasmtime::StoreContextMut<'_, T>| - { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - } - } - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - options: &LinkOptions, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + foo::foo::the_interface::Host - + TheWorldImports, - U: Send + foo::foo::the_interface::Host - + TheWorldImports, - { - if options.experimental_world { - Self::add_to_linker_imports_get_host(linker, options, get)?; - if options.experimental_world_interface_import { - foo::foo::the_interface::add_to_linker( - linker, - &options.into(), - get, - )?; - } - } - Ok(()) - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod the_interface { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - /// Link-time configurations. - #[derive(Clone, Debug, Default)] - pub struct LinkOptions { - experimental_interface: bool, - experimental_interface_function: bool, - experimental_interface_resource: bool, - experimental_interface_resource_method: bool, - } - impl LinkOptions { - /// Enable members marked as `@unstable(feature = experimental-interface)` - pub fn experimental_interface(&mut self, enabled: bool) -> &mut Self { - self.experimental_interface = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-interface-function)` - pub fn experimental_interface_function( - &mut self, - enabled: bool, - ) -> &mut Self { - self.experimental_interface_function = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-interface-resource)` - pub fn experimental_interface_resource( - &mut self, - enabled: bool, - ) -> &mut Self { - self.experimental_interface_resource = enabled; - self - } - /// Enable members marked as `@unstable(feature = experimental-interface-resource-method)` - pub fn experimental_interface_resource_method( - &mut self, - enabled: bool, - ) -> &mut Self { - self.experimental_interface_resource_method = enabled; - self - } - } - pub enum Bar {} - pub trait HostBar: Sized { - type BarData; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::BarData>, - self_: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()>; - } - impl<_T: HostBar> HostBar for &mut _T { - type BarData = _T::BarData; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::BarData>, - self_: wasmtime::component::Resource, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::BarData>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as HostBar>::foo(store, self_) - } - fn drop( - &mut self, - rep: wasmtime::component::Resource, - ) -> wasmtime::Result<()> { - HostBar::drop(*self, rep) - } - } - pub trait Host: HostBar + Sized { - type Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - options: &LinkOptions, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host + Send, - >, - T: Send + 'static, - { - if options.experimental_interface { - let mut inst = linker.instance("foo:foo/the-interface")?; - if options.experimental_interface_resource { - inst.resource( - "bar", - wasmtime::component::ResourceType::host::(), - move |mut store, rep| -> wasmtime::Result<()> { - HostBar::drop( - &mut host_getter(store.data_mut()), - wasmtime::component::Resource::new_own(rep), - ) - }, - )?; - } - if options.experimental_interface_function { - inst.func_wrap_concurrent( - "foo", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::foo(host); - Box::pin(async move { - let fun = r.await; - Box::new(move | - mut caller: wasmtime::StoreContextMut<'_, T>| - { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - } - if options.experimental_interface_resource_method { - inst.func_wrap_concurrent( - "[method]bar.foo", - move | - mut caller: wasmtime::StoreContextMut<'_, T>, - (arg0,): (wasmtime::component::Resource,)| - { - let host = caller; - let r = ::foo(host, arg0); - Box::pin(async move { - let fun = r.await; - Box::new(move | - mut caller: wasmtime::StoreContextMut<'_, T>| - { - let r = fun(caller); - Ok(r) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<()> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - } - } - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - options: &LinkOptions, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, options, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn foo( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> () + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::foo(store) - } - } - } - } -} diff --git a/crates/component-macro/tests/expanded/unstable-features_tracing_async.rs b/crates/component-macro/tests/expanded/unstable-features_tracing_async.rs index 8b2031aa7322..46ecd082f998 100644 --- a/crates/component-macro/tests/expanded/unstable-features_tracing_async.rs +++ b/crates/component-macro/tests/expanded/unstable-features_tracing_async.rs @@ -253,14 +253,15 @@ const _: () = { let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker_imports_get_host( + pub fn add_to_linker_imports( linker: &mut wasmtime::component::Linker, options: &LinkOptions, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: TheWorldImports>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: TheWorldImports, + T: Send + 'static, { let mut linker = linker.root(); if options.experimental_world { @@ -341,24 +342,23 @@ const _: () = { } Ok(()) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, options: &LinkOptions, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::the_interface::Host + TheWorldImports + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::the_interface::Host + TheWorldImports + Send, + T: Send + 'static, { if options.experimental_world { - Self::add_to_linker_imports_get_host(linker, options, get)?; + Self::add_to_linker_imports::(linker, options, get)?; if options.experimental_world_interface_import { - foo::foo::the_interface::add_to_linker( - linker, - &options.into(), - get, - )?; + foo::foo::the_interface::add_to_linker::< + T, + D, + >(linker, &options.into(), get)?; } } Ok(()) @@ -437,15 +437,15 @@ pub mod foo { pub trait Host: Send + HostBar + Sized { async fn foo(&mut self) -> (); } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, options: &LinkOptions, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { if options.experimental_interface { let mut inst = linker.instance("foo:foo/the-interface")?; @@ -523,18 +523,6 @@ pub mod foo { } Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - options: &LinkOptions, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, options, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn foo(&mut self) -> () { Host::foo(*self).await diff --git a/crates/component-macro/tests/expanded/unversioned-foo.rs b/crates/component-macro/tests/expanded/unversioned-foo.rs index c216de5863ed..bb88a4e24c4e 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo.rs @@ -138,15 +138,16 @@ const _: () = { let indices = NopeIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::a::Host, T: 'static, - U: foo::foo::a::Host, { - foo::foo::a::add_to_linker(linker, get)?; + foo::foo::a::add_to_linker::(linker, get)?; Ok(()) } } @@ -188,13 +189,14 @@ pub mod foo { pub trait Host { fn g(&mut self) -> Result<(), Error>; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap( @@ -207,16 +209,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn g(&mut self) -> Result<(), Error> { Host::g(*self) diff --git a/crates/component-macro/tests/expanded/unversioned-foo_async.rs b/crates/component-macro/tests/expanded/unversioned-foo_async.rs index 7662a5f88891..206bc3bcffcb 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo_async.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo_async.rs @@ -144,16 +144,16 @@ const _: () = { let indices = NopeIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::a::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::a::Host + Send, + T: Send + 'static, { - foo::foo::a::add_to_linker(linker, get)?; + foo::foo::a::add_to_linker::(linker, get)?; Ok(()) } } @@ -196,14 +196,14 @@ pub mod foo { pub trait Host: Send { async fn g(&mut self) -> Result<(), Error>; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_async( @@ -218,17 +218,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn g(&mut self) -> Result<(), Error> { Host::g(*self).await diff --git a/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs b/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs deleted file mode 100644 index d08de2f8ebcf..000000000000 --- a/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs +++ /dev/null @@ -1,280 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `nope`. -/// -/// This structure is created through [`NopePre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`Nope`] as well. -pub struct NopePre { - instance_pre: wasmtime::component::InstancePre, - indices: NopeIndices, -} -impl Clone for NopePre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> NopePre<_T> { - /// Creates a new copy of `NopePre` 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 { - let indices = NopeIndices::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 [`Nope`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `nope`. -/// -/// This is an implementation detail of [`NopePre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`Nope`] as well. -#[derive(Clone)] -pub struct NopeIndices {} -/// Auto-generated bindings for an instance a component which -/// implements the world `nope`. -/// -/// 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 -/// [`Nope::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`NopePre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`NopePre::instantiate_async`] to -/// create a [`Nope`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`Nope::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 Nope {} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl NopeIndices { - /// Creates a new copy of `NopeIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - Ok(NopeIndices {}) - } - /// Uses the indices stored in `self` to load an instance - /// of [`Nope`] 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 { - let _ = &mut store; - let _instance = instance; - Ok(Nope {}) - } - } - impl Nope { - /// Convenience wrapper around [`NopePre::new`] and - /// [`NopePre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - NopePre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`NopeIndices::new`] and - /// [`NopeIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = NopeIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + foo::foo::a::Host, - U: Send + foo::foo::a::Host, - { - foo::foo::a::add_to_linker(linker, get)?; - Ok(()) - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod a { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - #[derive(wasmtime::component::ComponentType)] - #[derive(wasmtime::component::Lift)] - #[derive(wasmtime::component::Lower)] - #[component(variant)] - #[derive(Clone)] - pub enum Error { - #[component(name = "other")] - Other(wasmtime::component::__internal::String), - } - impl core::fmt::Debug for Error { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match self { - Error::Other(e) => { - f.debug_tuple("Error::Other").field(e).finish() - } - } - } - } - impl core::fmt::Display for Error { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "{:?}", self) - } - } - impl core::error::Error for Error {} - const _: () = { - assert!(12 == < Error as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < Error as wasmtime::component::ComponentType >::ALIGN32); - }; - pub trait Host { - type Data; - fn g( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Result<(), Error> + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized; - } - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost< - &'a mut T, - Host: Host + Send, - >, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/a")?; - inst.func_wrap_concurrent( - "g", - move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { - let host = caller; - let r = ::g(host); - Box::pin(async move { - let fun = r.await; - Box::new(move |mut caller: wasmtime::StoreContextMut<'_, T>| { - let r = fun(caller); - Ok((r,)) - }) - as Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Result<(), Error>,)> + Send + Sync, - > - }) - as ::core::pin::Pin< - Box< - dyn ::core::future::Future< - Output = Box< - dyn FnOnce( - wasmtime::StoreContextMut<'_, T>, - ) -> wasmtime::Result<(Result<(), Error>,)> + Send + Sync, - >, - > + Send + Sync + 'static, - >, - > - }, - )?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host> Host for &mut _T { - type Data = _T::Data; - fn g( - store: wasmtime::StoreContextMut<'_, Self::Data>, - ) -> impl ::core::future::Future< - Output = impl FnOnce( - wasmtime::StoreContextMut<'_, Self::Data>, - ) -> Result<(), Error> + Send + Sync + 'static, - > + Send + Sync + 'static - where - Self: Sized, - { - <_T as Host>::g(store) - } - } - } - } -} diff --git a/crates/component-macro/tests/expanded/unversioned-foo_tracing_async.rs b/crates/component-macro/tests/expanded/unversioned-foo_tracing_async.rs index e0b368af71a9..071397b7832e 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo_tracing_async.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo_tracing_async.rs @@ -144,16 +144,16 @@ const _: () = { let indices = NopeIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::a::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::a::Host + Send, + T: Send + 'static, { - foo::foo::a::add_to_linker(linker, get)?; + foo::foo::a::add_to_linker::(linker, get)?; Ok(()) } } @@ -196,14 +196,14 @@ pub mod foo { pub trait Host: Send { async fn g(&mut self) -> Result<(), Error>; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_async( @@ -231,17 +231,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn g(&mut self) -> Result<(), Error> { Host::g(*self).await diff --git a/crates/component-macro/tests/expanded/use-paths.rs b/crates/component-macro/tests/expanded/use-paths.rs index 9769f3e3006e..e6c3c9cf7300 100644 --- a/crates/component-macro/tests/expanded/use-paths.rs +++ b/crates/component-macro/tests/expanded/use-paths.rs @@ -138,18 +138,21 @@ const _: () = { let indices = DIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: foo::foo::a::Host + foo::foo::b::Host + foo::foo::c::Host + d::Host, T: 'static, - U: foo::foo::a::Host + foo::foo::b::Host + foo::foo::c::Host + d::Host, { - foo::foo::a::add_to_linker(linker, get)?; - foo::foo::b::add_to_linker(linker, get)?; - foo::foo::c::add_to_linker(linker, get)?; - d::add_to_linker(linker, get)?; + foo::foo::a::add_to_linker::(linker, get)?; + foo::foo::b::add_to_linker::(linker, get)?; + foo::foo::c::add_to_linker::(linker, get)?; + d::add_to_linker::(linker, get)?; Ok(()) } } @@ -178,13 +181,14 @@ pub mod foo { pub trait Host { fn a(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap( @@ -197,16 +201,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn a(&mut self) -> Foo { Host::a(*self) @@ -225,13 +219,14 @@ pub mod foo { pub trait Host { fn a(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/b")?; inst.func_wrap( @@ -244,16 +239,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn a(&mut self) -> Foo { Host::a(*self) @@ -272,13 +257,14 @@ pub mod foo { pub trait Host { fn a(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/c")?; inst.func_wrap( @@ -291,16 +277,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn a(&mut self) -> Foo { Host::a(*self) @@ -321,13 +297,14 @@ pub mod d { pub trait Host { fn b(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("d")?; inst.func_wrap( @@ -340,16 +317,6 @@ pub mod d { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn b(&mut self) -> Foo { Host::b(*self) diff --git a/crates/component-macro/tests/expanded/use-paths_async.rs b/crates/component-macro/tests/expanded/use-paths_async.rs index ab0c4827ab77..291d8adcfe57 100644 --- a/crates/component-macro/tests/expanded/use-paths_async.rs +++ b/crates/component-macro/tests/expanded/use-paths_async.rs @@ -144,20 +144,22 @@ const _: () = { let indices = DIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::a::Host + foo::foo::b::Host + foo::foo::c::Host + d::Host + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: foo::foo::a::Host + foo::foo::b::Host + foo::foo::c::Host + d::Host + Send, + T: Send + 'static, { - foo::foo::a::add_to_linker(linker, get)?; - foo::foo::b::add_to_linker(linker, get)?; - foo::foo::c::add_to_linker(linker, get)?; - d::add_to_linker(linker, get)?; + foo::foo::a::add_to_linker::(linker, get)?; + foo::foo::b::add_to_linker::(linker, get)?; + foo::foo::c::add_to_linker::(linker, get)?; + d::add_to_linker::(linker, get)?; Ok(()) } } @@ -187,14 +189,14 @@ pub mod foo { pub trait Host: Send { async fn a(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_async( @@ -209,17 +211,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a(&mut self) -> Foo { Host::a(*self).await @@ -239,14 +230,14 @@ pub mod foo { pub trait Host: Send { async fn a(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/b")?; inst.func_wrap_async( @@ -261,17 +252,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a(&mut self) -> Foo { Host::a(*self).await @@ -291,14 +271,14 @@ pub mod foo { pub trait Host: Send { async fn a(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/c")?; inst.func_wrap_async( @@ -313,17 +293,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a(&mut self) -> Foo { Host::a(*self).await @@ -345,14 +314,14 @@ pub mod d { pub trait Host: Send { async fn b(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("d")?; inst.func_wrap_async( @@ -367,17 +336,6 @@ pub mod d { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn b(&mut self) -> Foo { Host::b(*self).await diff --git a/crates/component-macro/tests/expanded/use-paths_tracing_async.rs b/crates/component-macro/tests/expanded/use-paths_tracing_async.rs index acc90319e1e7..e23108d70195 100644 --- a/crates/component-macro/tests/expanded/use-paths_tracing_async.rs +++ b/crates/component-macro/tests/expanded/use-paths_tracing_async.rs @@ -144,20 +144,22 @@ const _: () = { let indices = DIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::a::Host + foo::foo::b::Host + foo::foo::c::Host + d::Host + D: wasmtime::component::HasData, + for<'a> D::Data< + 'a, + >: foo::foo::a::Host + foo::foo::b::Host + foo::foo::c::Host + d::Host + Send, + T: Send + 'static, { - foo::foo::a::add_to_linker(linker, get)?; - foo::foo::b::add_to_linker(linker, get)?; - foo::foo::c::add_to_linker(linker, get)?; - d::add_to_linker(linker, get)?; + foo::foo::a::add_to_linker::(linker, get)?; + foo::foo::b::add_to_linker::(linker, get)?; + foo::foo::c::add_to_linker::(linker, get)?; + d::add_to_linker::(linker, get)?; Ok(()) } } @@ -187,14 +189,14 @@ pub mod foo { pub trait Host: Send { async fn a(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_async( @@ -222,17 +224,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a(&mut self) -> Foo { Host::a(*self).await @@ -252,14 +243,14 @@ pub mod foo { pub trait Host: Send { async fn a(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/b")?; inst.func_wrap_async( @@ -287,17 +278,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a(&mut self) -> Foo { Host::a(*self).await @@ -317,14 +297,14 @@ pub mod foo { pub trait Host: Send { async fn a(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/c")?; inst.func_wrap_async( @@ -352,17 +332,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn a(&mut self) -> Foo { Host::a(*self).await @@ -384,14 +353,14 @@ pub mod d { pub trait Host: Send { async fn b(&mut self) -> Foo; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("d")?; inst.func_wrap_async( @@ -419,17 +388,6 @@ pub mod d { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn b(&mut self) -> Foo { Host::b(*self).await diff --git a/crates/component-macro/tests/expanded/variants.rs b/crates/component-macro/tests/expanded/variants.rs index e467e89776d4..ab411eaa161b 100644 --- a/crates/component-macro/tests/expanded/variants.rs +++ b/crates/component-macro/tests/expanded/variants.rs @@ -146,15 +146,16 @@ const _: () = { let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::variants::Host, T: 'static, - U: foo::foo::variants::Host, { - foo::foo::variants::add_to_linker(linker, get)?; + foo::foo::variants::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_variants(&self) -> &exports::foo::foo::variants::Guest { @@ -510,13 +511,14 @@ pub mod foo { fn is_clone_arg(&mut self, a: IsClone) -> (); fn is_clone_return(&mut self) -> IsClone; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/variants")?; inst.func_wrap( @@ -750,16 +752,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T { fn e1_arg(&mut self, x: E1) -> () { Host::e1_arg(*self, x) diff --git a/crates/component-macro/tests/expanded/variants_async.rs b/crates/component-macro/tests/expanded/variants_async.rs index ad1aee6d5048..4998aa871f32 100644 --- a/crates/component-macro/tests/expanded/variants_async.rs +++ b/crates/component-macro/tests/expanded/variants_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::variants::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::variants::Host + Send, + T: Send + 'static, { - foo::foo::variants::add_to_linker(linker, get)?; + foo::foo::variants::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_variants(&self) -> &exports::foo::foo::variants::Guest { @@ -518,14 +518,14 @@ pub mod foo { async fn is_clone_arg(&mut self, a: IsClone) -> (); async fn is_clone_return(&mut self) -> IsClone; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/variants")?; inst.func_wrap_async( @@ -802,17 +802,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn e1_arg(&mut self, x: E1) -> () { Host::e1_arg(*self, x).await diff --git a/crates/component-macro/tests/expanded/variants_tracing_async.rs b/crates/component-macro/tests/expanded/variants_tracing_async.rs index 72418e696530..40c91e9a7d5e 100644 --- a/crates/component-macro/tests/expanded/variants_tracing_async.rs +++ b/crates/component-macro/tests/expanded/variants_tracing_async.rs @@ -152,16 +152,16 @@ const _: () = { let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::variants::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::variants::Host + Send, + T: Send + 'static, { - foo::foo::variants::add_to_linker(linker, get)?; + foo::foo::variants::add_to_linker::(linker, get)?; Ok(()) } pub fn foo_foo_variants(&self) -> &exports::foo::foo::variants::Guest { @@ -518,14 +518,14 @@ pub mod foo { async fn is_clone_arg(&mut self, a: IsClone) -> (); async fn is_clone_return(&mut self) -> IsClone; } - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/variants")?; inst.func_wrap_async( @@ -1100,17 +1100,6 @@ pub mod foo { )?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T { async fn e1_arg(&mut self, x: E1) -> () { Host::e1_arg(*self, x).await diff --git a/crates/component-macro/tests/expanded/wat_concurrent.rs b/crates/component-macro/tests/expanded/wat_concurrent.rs deleted file mode 100644 index 8c08894c8a3e..000000000000 --- a/crates/component-macro/tests/expanded/wat_concurrent.rs +++ /dev/null @@ -1,225 +0,0 @@ -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `example`. -/// -/// This structure is created through [`ExamplePre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`Example`] as well. -pub struct ExamplePre { - instance_pre: wasmtime::component::InstancePre, - indices: ExampleIndices, -} -impl Clone for ExamplePre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> ExamplePre<_T> { - /// Creates a new copy of `ExamplePre` 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 { - let indices = ExampleIndices::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 [`Example`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `example`. -/// -/// This is an implementation detail of [`ExamplePre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`Example`] as well. -#[derive(Clone)] -pub struct ExampleIndices { - interface0: exports::same::name::this_name_is_duplicated::GuestIndices, -} -/// Auto-generated bindings for an instance a component which -/// implements the world `example`. -/// -/// 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 -/// [`Example::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`ExamplePre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`ExamplePre::instantiate_async`] to -/// create a [`Example`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`Example::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 Example { - interface0: exports::same::name::this_name_is_duplicated::Guest, -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl ExampleIndices { - /// Creates a new copy of `ExampleIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - let interface0 = exports::same::name::this_name_is_duplicated::GuestIndices::new( - _instance_pre, - )?; - Ok(ExampleIndices { interface0 }) - } - /// Uses the indices stored in `self` to load an instance - /// of [`Example`] 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 { - let _ = &mut store; - let _instance = instance; - let interface0 = self.interface0.load(&mut store, &_instance)?; - Ok(Example { interface0 }) - } - } - impl Example { - /// Convenience wrapper around [`ExamplePre::new`] and - /// [`ExamplePre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - ExamplePre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`ExampleIndices::new`] and - /// [`ExampleIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = ExampleIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn same_name_this_name_is_duplicated( - &self, - ) -> &exports::same::name::this_name_is_duplicated::Guest { - &self.interface0 - } - } -}; -pub mod exports { - pub mod same { - pub mod name { - #[allow(clippy::all)] - pub mod this_name_is_duplicated { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type ThisNameIsDuplicated = wasmtime::component::ResourceAny; - pub struct GuestThisNameIsDuplicated<'a> { - funcs: &'a Guest, - } - pub struct Guest {} - #[derive(Clone)] - pub struct GuestIndices {} - impl GuestIndices { - /// Constructor for [`GuestIndices`] which takes a - /// [`Component`](wasmtime::component::Component) as input and can be executed - /// before instantiation. - /// - /// This constructor can be used to front-load string lookups to find exports - /// within a component. - pub fn new<_T>( - _instance_pre: &wasmtime::component::InstancePre<_T>, - ) -> wasmtime::Result { - let instance = _instance_pre - .component() - .get_export_index(None, "same:name/this-name-is-duplicated") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `same:name/this-name-is-duplicated`" - ) - })?; - let mut lookup = move |name| { - _instance_pre - .component() - .get_export_index(Some(&instance), name) - .ok_or_else(|| { - anyhow::anyhow!( - "instance export `same:name/this-name-is-duplicated` does \ - not have export `{name}`" - ) - }) - }; - let _ = &mut lookup; - Ok(GuestIndices {}) - } - pub fn load( - &self, - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let _instance_pre = _instance.instance_pre(&store); - let _instance_type = _instance_pre.instance_type(); - let mut store = store.as_context_mut(); - let _ = &mut store; - Ok(Guest {}) - } - } - impl Guest {} - } - } - } -} diff --git a/crates/component-macro/tests/expanded/worlds-with-types.rs b/crates/component-macro/tests/expanded/worlds-with-types.rs index 3201a22f95b8..98f78190167c 100644 --- a/crates/component-macro/tests/expanded/worlds-with-types.rs +++ b/crates/component-macro/tests/expanded/worlds-with-types.rs @@ -185,15 +185,16 @@ const _: () = { let indices = FooIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::i::Host, T: 'static, - U: foo::foo::i::Host, { - foo::foo::i::add_to_linker(linker, get)?; + foo::foo::i::add_to_linker::(linker, get)?; Ok(()) } pub fn call_f( @@ -221,27 +222,18 @@ pub mod foo { assert!(2 == < T as wasmtime::component::ComponentType >::ALIGN32); }; pub trait Host {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host, T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>, { let mut inst = linker.instance("foo:foo/i")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/worlds-with-types_async.rs b/crates/component-macro/tests/expanded/worlds-with-types_async.rs index 59c7d8a2db05..eb66ee42014b 100644 --- a/crates/component-macro/tests/expanded/worlds-with-types_async.rs +++ b/crates/component-macro/tests/expanded/worlds-with-types_async.rs @@ -191,16 +191,16 @@ const _: () = { let indices = FooIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::i::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::i::Host + Send, + T: Send + 'static, { - foo::foo::i::add_to_linker(linker, get)?; + foo::foo::i::add_to_linker::(linker, get)?; Ok(()) } pub async fn call_f( @@ -232,29 +232,18 @@ pub mod foo { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/i")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } } diff --git a/crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs b/crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs deleted file mode 100644 index b841bac3a1ee..000000000000 --- a/crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs +++ /dev/null @@ -1,259 +0,0 @@ -pub type U = foo::foo::i::T; -const _: () = { - assert!(2 == < U as wasmtime::component::ComponentType >::SIZE32); - assert!(2 == < U as wasmtime::component::ComponentType >::ALIGN32); -}; -pub type T = u32; -const _: () = { - assert!(4 == < T as wasmtime::component::ComponentType >::SIZE32); - assert!(4 == < T as wasmtime::component::ComponentType >::ALIGN32); -}; -#[derive(wasmtime::component::ComponentType)] -#[derive(wasmtime::component::Lift)] -#[derive(wasmtime::component::Lower)] -#[component(record)] -#[derive(Clone, Copy)] -pub struct R {} -impl core::fmt::Debug for R { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("R").finish() - } -} -const _: () = { - assert!(0 == < R as wasmtime::component::ComponentType >::SIZE32); - assert!(1 == < R as wasmtime::component::ComponentType >::ALIGN32); -}; -/// Auto-generated bindings for a pre-instantiated version of a -/// component which implements the world `foo`. -/// -/// This structure is created through [`FooPre::new`] which -/// takes a [`InstancePre`](wasmtime::component::InstancePre) that -/// has been created through a [`Linker`](wasmtime::component::Linker). -/// -/// For more information see [`Foo`] as well. -pub struct FooPre { - instance_pre: wasmtime::component::InstancePre, - indices: FooIndices, -} -impl Clone for FooPre { - fn clone(&self) -> Self { - Self { - instance_pre: self.instance_pre.clone(), - indices: self.indices.clone(), - } - } -} -impl<_T: 'static> FooPre<_T> { - /// Creates a new copy of `FooPre` 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 { - let indices = FooIndices::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 [`Foo`] 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 async fn instantiate_async( - &self, - mut store: impl wasmtime::AsContextMut, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - 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 -/// `foo`. -/// -/// This is an implementation detail of [`FooPre`] and can -/// be constructed if needed as well. -/// -/// For more information see [`Foo`] as well. -#[derive(Clone)] -pub struct FooIndices { - f: wasmtime::component::ComponentExportIndex, -} -/// Auto-generated bindings for an instance a component which -/// implements the world `foo`. -/// -/// 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 -/// [`Foo::instantiate_async`] which only needs a -/// [`Store`], [`Component`], and [`Linker`]. -/// -/// * Alternatively you can create a [`FooPre`] ahead of -/// time with a [`Component`] to front-load string lookups -/// of exports once instead of per-instantiation. This -/// method then uses [`FooPre::instantiate_async`] to -/// create a [`Foo`]. -/// -/// * If you've instantiated the instance yourself already -/// then you can use [`Foo::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 Foo { - f: wasmtime::component::Func, -} -const _: () = { - #[allow(unused_imports)] - use wasmtime::component::__internal::anyhow; - impl FooIndices { - /// Creates a new copy of `FooIndices` 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 { - let _component = _instance_pre.component(); - let _instance_type = _instance_pre.instance_type(); - let f = { - let (item, index) = _component - .get_export(None, "f") - .ok_or_else(|| anyhow::anyhow!("no export `f` found"))?; - match item { - wasmtime::component::types::ComponentItem::ComponentFunc(func) => { - anyhow::Context::context( - func.typecheck::<(), ((T, U, R),)>(&_instance_type), - "type-checking export func `f`", - )?; - index - } - _ => Err(anyhow::anyhow!("export `f` is not a function"))?, - } - }; - Ok(FooIndices { f }) - } - /// Uses the indices stored in `self` to load an instance - /// of [`Foo`] 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 { - let _ = &mut store; - let _instance = instance; - let f = *_instance - .get_typed_func::<(), ((T, U, R),)>(&mut store, &self.f)? - .func(); - Ok(Foo { f }) - } - } - impl Foo { - /// Convenience wrapper around [`FooPre::new`] and - /// [`FooPre::instantiate_async`]. - pub async fn instantiate_async<_T>( - store: impl wasmtime::AsContextMut, - component: &wasmtime::component::Component, - linker: &wasmtime::component::Linker<_T>, - ) -> wasmtime::Result - where - _T: Send + 'static, - { - let pre = linker.instantiate_pre(component)?; - FooPre::new(pre)?.instantiate_async(store).await - } - /// Convenience wrapper around [`FooIndices::new`] and - /// [`FooIndices::load`]. - pub fn new( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let indices = FooIndices::new(&instance.instance_pre(&store))?; - indices.load(&mut store, instance) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - T: Send + foo::foo::i::Host, - U: Send + foo::foo::i::Host, - { - foo::foo::i::add_to_linker(linker, get)?; - Ok(()) - } - pub async fn call_f( - &self, - mut store: S, - ) -> wasmtime::Result> - where - ::Data: Send + 'static, - { - let callee = unsafe { - wasmtime::component::TypedFunc::<(), ((T, U, R),)>::new_unchecked(self.f) - }; - let promise = callee.call_concurrent(store.as_context_mut(), ()).await?; - Ok(promise.map(|(v,)| v)) - } - } -}; -pub mod foo { - pub mod foo { - #[allow(clippy::all)] - pub mod i { - #[allow(unused_imports)] - use wasmtime::component::__internal::{anyhow, Box}; - pub type T = u16; - const _: () = { - assert!(2 == < T as wasmtime::component::ComponentType >::SIZE32); - assert!(2 == < T as wasmtime::component::ComponentType >::ALIGN32); - }; - pub trait Host {} - pub fn add_to_linker_get_host( - linker: &mut wasmtime::component::Linker, - host_getter: G, - ) -> wasmtime::Result<()> - where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send + 'static, - { - let mut inst = linker.instance("foo:foo/i")?; - Ok(()) - } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send + 'static, - { - add_to_linker_get_host(linker, get) - } - impl<_T: Host + ?Sized> Host for &mut _T {} - } - } -} diff --git a/crates/component-macro/tests/expanded/worlds-with-types_tracing_async.rs b/crates/component-macro/tests/expanded/worlds-with-types_tracing_async.rs index 6814bdbbdc6f..8fdc366dbfed 100644 --- a/crates/component-macro/tests/expanded/worlds-with-types_tracing_async.rs +++ b/crates/component-macro/tests/expanded/worlds-with-types_tracing_async.rs @@ -191,16 +191,16 @@ const _: () = { let indices = FooIndices::new(&instance.instance_pre(&store))?; indices.load(&mut store, instance) } - pub fn add_to_linker( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, + get: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - T: Send, - U: foo::foo::i::Host + Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: foo::foo::i::Host + Send, + T: Send + 'static, { - foo::foo::i::add_to_linker(linker, get)?; + foo::foo::i::add_to_linker::(linker, get)?; Ok(()) } pub async fn call_f( @@ -240,29 +240,18 @@ pub mod foo { }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} - pub fn add_to_linker_get_host( + pub fn add_to_linker( linker: &mut wasmtime::component::Linker, - host_getter: G, + host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where - T: 'static, - G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>, - T: Send, + D: wasmtime::component::HasData, + for<'a> D::Data<'a>: Host + Send, + T: Send + 'static, { let mut inst = linker.instance("foo:foo/i")?; Ok(()) } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, - get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static, - ) -> wasmtime::Result<()> - where - T: 'static, - U: Host + Send, - T: Send, - { - add_to_linker_get_host(linker, get) - } impl<_T: Host + ?Sized + Send> Host for &mut _T {} } }