diff --git a/crates/component-macro/src/bindgen.rs b/crates/component-macro/src/bindgen.rs index 8d3d49f2db02..3f742c6e9267 100644 --- a/crates/component-macro/src/bindgen.rs +++ b/crates/component-macro/src/bindgen.rs @@ -20,8 +20,8 @@ pub struct Config { include_generated_code_from_file: bool, } -pub fn expand(input: &Config) -> Result { - let mut src = match input.opts.generate(&input.resolve, input.world) { +pub fn expand(input: &mut Config) -> Result { + let mut src = match input.opts.generate(&mut input.resolve, input.world) { Ok(s) => s, Err(e) => return Err(Error::new(Span::call_site(), e.to_string())), }; diff --git a/crates/component-macro/src/lib.rs b/crates/component-macro/src/lib.rs index 9fe6cd7835e5..e87d31c00723 100644 --- a/crates/component-macro/src/lib.rs +++ b/crates/component-macro/src/lib.rs @@ -49,7 +49,7 @@ pub fn flags(input: proc_macro::TokenStream) -> proc_macro::TokenStream { #[proc_macro] pub fn bindgen(input: proc_macro::TokenStream) -> proc_macro::TokenStream { - bindgen::expand(&parse_macro_input!(input as bindgen::Config)) + bindgen::expand(&mut parse_macro_input!(input as bindgen::Config)) .unwrap_or_else(Error::into_compile_error) .into() } diff --git a/crates/component-macro/tests/codegen/implements.wit b/crates/component-macro/tests/codegen/implements.wit new file mode 100644 index 000000000000..f4781f61b431 --- /dev/null +++ b/crates/component-macro/tests/codegen/implements.wit @@ -0,0 +1,11 @@ +package foo:foo; + +interface store { + get: func(key: string) -> option; + set: func(key: string, value: string); +} + +world cache { + import hot-cache: store; + import durable: store; +} diff --git a/crates/component-macro/tests/expanded.rs b/crates/component-macro/tests/expanded.rs index 4b27b2ae68b4..b342a429232e 100644 --- a/crates/component-macro/tests/expanded.rs +++ b/crates/component-macro/tests/expanded.rs @@ -43,6 +43,11 @@ fn process_expanded(path: &str, suffix: &str, src: &str) -> Result<()> { Path::new("tests/expanded").join(stem).with_extension("rs") }; if std::env::var("BINDGEN_TEST_BLESS").is_ok_and(|val| !val.is_empty()) { + if let Ok(prev) = std::fs::read(&expanded_path) + && prev == formatted_src.as_bytes() + { + return Ok(()); + } std::fs::write(expanded_path, formatted_src)?; } else { match std::fs::read_to_string(&expanded_path) { diff --git a/crates/component-macro/tests/expanded/char.rs b/crates/component-macro/tests/expanded/char.rs index a5b7d1bedba6..2d3bb179bff8 100644 --- a/crates/component-macro/tests/expanded/char.rs +++ b/crates/component-macro/tests/expanded/char.rs @@ -210,8 +210,8 @@ pub mod foo { Host::return_char(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -219,7 +219,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/chars")?; inst.func_wrap( "take-char", move | @@ -241,6 +240,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/chars")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/char_async.rs b/crates/component-macro/tests/expanded/char_async.rs index 8d454304c5fb..02e1d66be7e9 100644 --- a/crates/component-macro/tests/expanded/char_async.rs +++ b/crates/component-macro/tests/expanded/char_async.rs @@ -220,8 +220,8 @@ pub mod foo { async move { Host::return_char(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -229,7 +229,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/chars")?; inst.func_wrap_async( "take-char", move | @@ -255,6 +254,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/chars")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/char_concurrent.rs b/crates/component-macro/tests/expanded/char_concurrent.rs index d2063efb6947..fbfe3f050ecf 100644 --- a/crates/component-macro/tests/expanded/char_concurrent.rs +++ b/crates/component-macro/tests/expanded/char_concurrent.rs @@ -202,8 +202,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -211,7 +211,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/chars")?; inst.func_wrap_concurrent( "take-char", move |caller: &wasmtime::component::Accessor, (arg0,): (char,)| { @@ -234,6 +233,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/chars")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/char_tracing_async.rs b/crates/component-macro/tests/expanded/char_tracing_async.rs index 6d69397a3b97..71a3224bcaa6 100644 --- a/crates/component-macro/tests/expanded/char_tracing_async.rs +++ b/crates/component-macro/tests/expanded/char_tracing_async.rs @@ -220,8 +220,8 @@ pub mod foo { async move { Host::return_char(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -229,7 +229,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/chars")?; inst.func_wrap_async( "take-char", move | @@ -284,6 +283,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/chars")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/conventions.rs b/crates/component-macro/tests/expanded/conventions.rs index 4bc40a0e36ca..ea9e065fdb24 100644 --- a/crates/component-macro/tests/expanded/conventions.rs +++ b/crates/component-macro/tests/expanded/conventions.rs @@ -292,8 +292,8 @@ pub mod foo { Host::bool(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -301,7 +301,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/conventions")?; inst.func_wrap( "kebab-case", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -403,6 +402,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/conventions")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/conventions_async.rs b/crates/component-macro/tests/expanded/conventions_async.rs index 1167d3575409..0a6a3ae11dad 100644 --- a/crates/component-macro/tests/expanded/conventions_async.rs +++ b/crates/component-macro/tests/expanded/conventions_async.rs @@ -326,8 +326,8 @@ pub mod foo { async move { Host::bool(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -335,7 +335,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/conventions")?; inst.func_wrap_async( "kebab-case", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -461,6 +460,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/conventions")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/conventions_concurrent.rs b/crates/component-macro/tests/expanded/conventions_concurrent.rs index 69d5c2bfaac8..9f92b9cf8c3a 100644 --- a/crates/component-macro/tests/expanded/conventions_concurrent.rs +++ b/crates/component-macro/tests/expanded/conventions_concurrent.rs @@ -270,8 +270,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -279,7 +279,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/conventions")?; inst.func_wrap_concurrent( "kebab-case", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -409,6 +408,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/conventions")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/conventions_tracing_async.rs b/crates/component-macro/tests/expanded/conventions_tracing_async.rs index ad8d5d0083c9..c8d7f64a431f 100644 --- a/crates/component-macro/tests/expanded/conventions_tracing_async.rs +++ b/crates/component-macro/tests/expanded/conventions_tracing_async.rs @@ -326,8 +326,8 @@ pub mod foo { async move { Host::bool(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -335,7 +335,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/conventions")?; inst.func_wrap_async( "kebab-case", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -621,6 +620,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/conventions")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/dead-code.rs b/crates/component-macro/tests/expanded/dead-code.rs index a0b2da6c502b..e29a1492836f 100644 --- a/crates/component-macro/tests/expanded/dead-code.rs +++ b/crates/component-macro/tests/expanded/dead-code.rs @@ -218,8 +218,8 @@ pub mod a { Host::f(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -227,7 +227,6 @@ pub mod a { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("a:b/interface-with-live-type")?; inst.func_wrap( "f", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -238,6 +237,18 @@ pub mod a { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("a:b/interface-with-live-type")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod interface_with_dead_type { @@ -300,6 +311,17 @@ pub mod a { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -310,7 +332,7 @@ pub mod a { T: 'static, { let mut inst = linker.instance("a:b/interface-with-dead-type")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/dead-code_async.rs b/crates/component-macro/tests/expanded/dead-code_async.rs index 9a56d636810b..38a2d5b9b9b4 100644 --- a/crates/component-macro/tests/expanded/dead-code_async.rs +++ b/crates/component-macro/tests/expanded/dead-code_async.rs @@ -220,8 +220,8 @@ pub mod a { async move { Host::f(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -229,7 +229,6 @@ pub mod a { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("a:b/interface-with-live-type")?; inst.func_wrap_async( "f", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -242,6 +241,18 @@ pub mod a { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("a:b/interface-with-live-type")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod interface_with_dead_type { @@ -304,6 +315,17 @@ pub mod a { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -314,7 +336,7 @@ pub mod a { T: 'static, { let mut inst = linker.instance("a:b/interface-with-dead-type")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/dead-code_concurrent.rs b/crates/component-macro/tests/expanded/dead-code_concurrent.rs index b1277ba78c33..748260707065 100644 --- a/crates/component-macro/tests/expanded/dead-code_concurrent.rs +++ b/crates/component-macro/tests/expanded/dead-code_concurrent.rs @@ -212,8 +212,8 @@ pub mod a { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -221,7 +221,6 @@ pub mod a { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("a:b/interface-with-live-type")?; inst.func_wrap_concurrent( "f", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -234,6 +233,18 @@ pub mod a { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("a:b/interface-with-live-type")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod interface_with_dead_type { @@ -296,6 +307,17 @@ pub mod a { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -306,7 +328,7 @@ pub mod a { T: 'static, { let mut inst = linker.instance("a:b/interface-with-dead-type")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } 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 bbe2b7fb491c..74221c5e85a9 100644 --- a/crates/component-macro/tests/expanded/dead-code_tracing_async.rs +++ b/crates/component-macro/tests/expanded/dead-code_tracing_async.rs @@ -220,8 +220,8 @@ pub mod a { async move { Host::f(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -229,7 +229,6 @@ pub mod a { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("a:b/interface-with-live-type")?; inst.func_wrap_async( "f", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -255,6 +254,18 @@ pub mod a { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("a:b/interface-with-live-type")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod interface_with_dead_type { @@ -317,6 +328,17 @@ pub mod a { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -327,7 +349,7 @@ pub mod a { T: 'static, { let mut inst = linker.instance("a:b/interface-with-dead-type")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/flags.rs b/crates/component-macro/tests/expanded/flags.rs index 83618cb4eaa8..249ad2616a37 100644 --- a/crates/component-macro/tests/expanded/flags.rs +++ b/crates/component-macro/tests/expanded/flags.rs @@ -340,8 +340,8 @@ pub mod foo { Host::roundtrip_flag64(*self, x) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -349,7 +349,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/flegs")?; inst.func_wrap( "roundtrip-flag1", move | @@ -429,6 +428,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/flegs")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/flags_async.rs b/crates/component-macro/tests/expanded/flags_async.rs index 9d0e0fcb92fa..275c1768b84a 100644 --- a/crates/component-macro/tests/expanded/flags_async.rs +++ b/crates/component-macro/tests/expanded/flags_async.rs @@ -382,8 +382,8 @@ pub mod foo { async move { Host::roundtrip_flag64(*self, x).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -391,7 +391,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/flegs")?; inst.func_wrap_async( "roundtrip-flag1", move | @@ -485,6 +484,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/flegs")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/flags_concurrent.rs b/crates/component-macro/tests/expanded/flags_concurrent.rs index ff3546bdefe9..9b4f3f0608ae 100644 --- a/crates/component-macro/tests/expanded/flags_concurrent.rs +++ b/crates/component-macro/tests/expanded/flags_concurrent.rs @@ -335,8 +335,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -344,7 +344,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/flegs")?; inst.func_wrap_concurrent( "roundtrip-flag1", move |caller: &wasmtime::component::Accessor, (arg0,): (Flag1,)| { @@ -424,6 +423,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/flegs")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/flags_tracing_async.rs b/crates/component-macro/tests/expanded/flags_tracing_async.rs index adbd44dc046b..7c2554ce62fc 100644 --- a/crates/component-macro/tests/expanded/flags_tracing_async.rs +++ b/crates/component-macro/tests/expanded/flags_tracing_async.rs @@ -382,8 +382,8 @@ pub mod foo { async move { Host::roundtrip_flag64(*self, x).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -391,7 +391,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/flegs")?; inst.func_wrap_async( "roundtrip-flag1", move | @@ -597,6 +596,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/flegs")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/floats.rs b/crates/component-macro/tests/expanded/floats.rs index f1a8fa975d21..e77427e60ad9 100644 --- a/crates/component-macro/tests/expanded/floats.rs +++ b/crates/component-macro/tests/expanded/floats.rs @@ -216,8 +216,8 @@ pub mod foo { Host::f64_result(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -225,7 +225,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/floats")?; inst.func_wrap( "f32-param", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f32,)| { @@ -260,6 +259,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/floats")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/floats_async.rs b/crates/component-macro/tests/expanded/floats_async.rs index 7eb526573480..cd4088936aaa 100644 --- a/crates/component-macro/tests/expanded/floats_async.rs +++ b/crates/component-macro/tests/expanded/floats_async.rs @@ -236,8 +236,8 @@ pub mod foo { async move { Host::f64_result(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -245,7 +245,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/floats")?; inst.func_wrap_async( "f32-param", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f32,)| { @@ -288,6 +287,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/floats")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/floats_concurrent.rs b/crates/component-macro/tests/expanded/floats_concurrent.rs index b67fa9b2ec09..12e70930e6a0 100644 --- a/crates/component-macro/tests/expanded/floats_concurrent.rs +++ b/crates/component-macro/tests/expanded/floats_concurrent.rs @@ -209,8 +209,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -218,7 +218,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/floats")?; inst.func_wrap_concurrent( "f32-param", move |caller: &wasmtime::component::Accessor, (arg0,): (f32,)| { @@ -261,6 +260,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/floats")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/floats_tracing_async.rs b/crates/component-macro/tests/expanded/floats_tracing_async.rs index 685d2074d5f8..763f3d8b522c 100644 --- a/crates/component-macro/tests/expanded/floats_tracing_async.rs +++ b/crates/component-macro/tests/expanded/floats_tracing_async.rs @@ -236,8 +236,8 @@ pub mod foo { async move { Host::f64_result(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -245,7 +245,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/floats")?; inst.func_wrap_async( "f32-param", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (f32,)| { @@ -346,6 +345,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/floats")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/implements.rs b/crates/component-macro/tests/expanded/implements.rs new file mode 100644 index 000000000000..2099c91e53ac --- /dev/null +++ b/crates/component-macro/tests/expanded/implements.rs @@ -0,0 +1,274 @@ +/// Auto-generated bindings for a pre-instantiated version of a +/// component which implements the world `cache`. +/// +/// This structure is created through [`CachePre::new`] which +/// takes a [`InstancePre`](wasmtime::component::InstancePre) that +/// has been created through a [`Linker`](wasmtime::component::Linker). +/// +/// For more information see [`Cache`] as well. +pub struct CachePre { + instance_pre: wasmtime::component::InstancePre, + indices: CacheIndices, +} +impl Clone for CachePre { + fn clone(&self) -> Self { + Self { + instance_pre: self.instance_pre.clone(), + indices: self.indices.clone(), + } + } +} +impl<_T: 'static> CachePre<_T> { + /// Creates a new copy of `CachePre` 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 = CacheIndices::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 [`Cache`] within the + /// `store` provided. + /// + /// This function will use `self` as the pre-instantiated + /// instance to perform instantiation. Afterwards the preloaded + /// indices in `self` are used to lookup all exports on the + /// resulting instance. + pub fn instantiate( + &self, + mut store: impl wasmtime::AsContextMut, + ) -> wasmtime::Result { + let mut store = store.as_context_mut(); + let instance = self.instance_pre.instantiate(&mut store)?; + self.indices.load(&mut store, &instance) + } +} +impl<_T: Send + 'static> CachePre<_T> { + /// Same as [`Self::instantiate`], except with `async`. + pub async fn instantiate_async( + &self, + mut store: impl wasmtime::AsContextMut, + ) -> wasmtime::Result { + 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 +/// `cache`. +/// +/// This is an implementation detail of [`CachePre`] and can +/// be constructed if needed as well. +/// +/// For more information see [`Cache`] as well. +#[derive(Clone)] +pub struct CacheIndices {} +/// Auto-generated bindings for an instance a component which +/// implements the world `cache`. +/// +/// 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 +/// [`Cache::instantiate`] which only needs a +/// [`Store`], [`Component`], and [`Linker`]. +/// +/// * Alternatively you can create a [`CachePre`] ahead of +/// time with a [`Component`] to front-load string lookups +/// of exports once instead of per-instantiation. This +/// method then uses [`CachePre::instantiate`] to +/// create a [`Cache`]. +/// +/// * If you've instantiated the instance yourself already +/// then you can use [`Cache::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 Cache {} +const _: () = { + impl CacheIndices { + /// Creates a new copy of `CacheIndices` 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(CacheIndices {}) + } + /// Uses the indices stored in `self` to load an instance + /// of [`Cache`] 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(Cache {}) + } + } + impl Cache { + /// Convenience wrapper around [`CachePre::new`] and + /// [`CachePre::instantiate`]. + pub fn instantiate<_T>( + store: impl wasmtime::AsContextMut, + component: &wasmtime::component::Component, + linker: &wasmtime::component::Linker<_T>, + ) -> wasmtime::Result { + let pre = linker.instantiate_pre(component)?; + CachePre::new(pre)?.instantiate(store) + } + /// Convenience wrapper around [`CacheIndices::new`] and + /// [`CacheIndices::load`]. + pub fn new( + mut store: impl wasmtime::AsContextMut, + instance: &wasmtime::component::Instance, + ) -> wasmtime::Result { + let indices = CacheIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) + } + /// Convenience wrapper around [`CachePre::new`] and + /// [`CachePre::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, + { + let pre = linker.instantiate_pre(component)?; + CachePre::new(pre)?.instantiate_async(store).await + } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: foo::foo::store::HostWithStore + foo::foo::store::HostWithStore, + for<'a> D::Data<'a>: foo::foo::store::Host + foo::foo::store::Host, + T: 'static, + { + foo::foo::store::add_to_linker_instance::< + T, + D, + >(&mut linker.instance("hot-cache")?, host_getter)?; + foo::foo::store::add_to_linker_instance::< + T, + D, + >(&mut linker.instance("durable")?, host_getter)?; + Ok(()) + } + } +}; +pub mod foo { + pub mod foo { + #[allow(clippy::all)] + pub mod store { + #[allow(unused_imports)] + use wasmtime::component::__internal::Box; + pub trait HostWithStore: wasmtime::component::HasData {} + impl<_T: ?Sized> HostWithStore for _T + where + _T: wasmtime::component::HasData, + {} + pub trait Host { + fn get( + &mut self, + key: wasmtime::component::__internal::String, + ) -> Option; + fn set( + &mut self, + key: wasmtime::component::__internal::String, + value: wasmtime::component::__internal::String, + ) -> (); + } + impl<_T: Host + ?Sized> Host for &mut _T { + fn get( + &mut self, + key: wasmtime::component::__internal::String, + ) -> Option { + Host::get(*self, key) + } + fn set( + &mut self, + key: wasmtime::component::__internal::String, + value: wasmtime::component::__internal::String, + ) -> () { + Host::set(*self, key, value) + } + } + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + inst.func_wrap( + "get", + move | + mut caller: wasmtime::StoreContextMut<'_, T>, + (arg0,): (wasmtime::component::__internal::String,)| + { + let host = &mut host_getter(caller.data_mut()); + let r = Host::get(host, arg0); + Ok((r,)) + }, + )?; + inst.func_wrap( + "set", + move | + mut caller: wasmtime::StoreContextMut<'_, T>, + ( + arg0, + arg1, + ): ( + wasmtime::component::__internal::String, + wasmtime::component::__internal::String, + )| + { + let host = &mut host_getter(caller.data_mut()); + let r = Host::set(host, arg0, arg1); + Ok(r) + }, + )?; + Ok(()) + } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/store")?; + add_to_linker_instance(&mut inst, host_getter) + } + } + } +} diff --git a/crates/component-macro/tests/expanded/implements_async.rs b/crates/component-macro/tests/expanded/implements_async.rs new file mode 100644 index 000000000000..517aca975971 --- /dev/null +++ b/crates/component-macro/tests/expanded/implements_async.rs @@ -0,0 +1,282 @@ +/// Auto-generated bindings for a pre-instantiated version of a +/// component which implements the world `cache`. +/// +/// This structure is created through [`CachePre::new`] which +/// takes a [`InstancePre`](wasmtime::component::InstancePre) that +/// has been created through a [`Linker`](wasmtime::component::Linker). +/// +/// For more information see [`Cache`] as well. +pub struct CachePre { + instance_pre: wasmtime::component::InstancePre, + indices: CacheIndices, +} +impl Clone for CachePre { + fn clone(&self) -> Self { + Self { + instance_pre: self.instance_pre.clone(), + indices: self.indices.clone(), + } + } +} +impl<_T: 'static> CachePre<_T> { + /// Creates a new copy of `CachePre` 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 = CacheIndices::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 [`Cache`] within the + /// `store` provided. + /// + /// This function will use `self` as the pre-instantiated + /// instance to perform instantiation. Afterwards the preloaded + /// indices in `self` are used to lookup all exports on the + /// resulting instance. + pub fn instantiate( + &self, + mut store: impl wasmtime::AsContextMut, + ) -> wasmtime::Result { + let mut store = store.as_context_mut(); + let instance = self.instance_pre.instantiate(&mut store)?; + self.indices.load(&mut store, &instance) + } +} +impl<_T: Send + 'static> CachePre<_T> { + /// Same as [`Self::instantiate`], except with `async`. + pub async fn instantiate_async( + &self, + mut store: impl wasmtime::AsContextMut, + ) -> wasmtime::Result { + 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 +/// `cache`. +/// +/// This is an implementation detail of [`CachePre`] and can +/// be constructed if needed as well. +/// +/// For more information see [`Cache`] as well. +#[derive(Clone)] +pub struct CacheIndices {} +/// Auto-generated bindings for an instance a component which +/// implements the world `cache`. +/// +/// 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 +/// [`Cache::instantiate`] which only needs a +/// [`Store`], [`Component`], and [`Linker`]. +/// +/// * Alternatively you can create a [`CachePre`] ahead of +/// time with a [`Component`] to front-load string lookups +/// of exports once instead of per-instantiation. This +/// method then uses [`CachePre::instantiate`] to +/// create a [`Cache`]. +/// +/// * If you've instantiated the instance yourself already +/// then you can use [`Cache::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 Cache {} +const _: () = { + impl CacheIndices { + /// Creates a new copy of `CacheIndices` 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(CacheIndices {}) + } + /// Uses the indices stored in `self` to load an instance + /// of [`Cache`] 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(Cache {}) + } + } + impl Cache { + /// Convenience wrapper around [`CachePre::new`] and + /// [`CachePre::instantiate`]. + pub fn instantiate<_T>( + store: impl wasmtime::AsContextMut, + component: &wasmtime::component::Component, + linker: &wasmtime::component::Linker<_T>, + ) -> wasmtime::Result { + let pre = linker.instantiate_pre(component)?; + CachePre::new(pre)?.instantiate(store) + } + /// Convenience wrapper around [`CacheIndices::new`] and + /// [`CacheIndices::load`]. + pub fn new( + mut store: impl wasmtime::AsContextMut, + instance: &wasmtime::component::Instance, + ) -> wasmtime::Result { + let indices = CacheIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) + } + /// Convenience wrapper around [`CachePre::new`] and + /// [`CachePre::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, + { + let pre = linker.instantiate_pre(component)?; + CachePre::new(pre)?.instantiate_async(store).await + } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: foo::foo::store::HostWithStore + foo::foo::store::HostWithStore + Send, + for<'a> D::Data<'a>: foo::foo::store::Host + foo::foo::store::Host + Send, + T: 'static + Send, + { + foo::foo::store::add_to_linker_instance::< + T, + D, + >(&mut linker.instance("hot-cache")?, host_getter)?; + foo::foo::store::add_to_linker_instance::< + T, + D, + >(&mut linker.instance("durable")?, host_getter)?; + Ok(()) + } + } +}; +pub mod foo { + pub mod foo { + #[allow(clippy::all)] + pub mod store { + #[allow(unused_imports)] + use wasmtime::component::__internal::Box; + pub trait HostWithStore: wasmtime::component::HasData + Send {} + impl<_T: ?Sized> HostWithStore for _T + where + _T: wasmtime::component::HasData + Send, + {} + pub trait Host: Send { + fn get( + &mut self, + key: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future< + Output = Option, + > + Send; + fn set( + &mut self, + key: wasmtime::component::__internal::String, + value: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future + Send; + } + impl<_T: Host + ?Sized + Send> Host for &mut _T { + fn get( + &mut self, + key: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future< + Output = Option, + > + Send { + async move { Host::get(*self, key).await } + } + fn set( + &mut self, + key: wasmtime::component::__internal::String, + value: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future + Send { + async move { Host::set(*self, key, value).await } + } + } + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + inst.func_wrap_async( + "get", + move | + mut caller: wasmtime::StoreContextMut<'_, T>, + (arg0,): (wasmtime::component::__internal::String,)| + { + wasmtime::component::__internal::Box::new(async move { + let host = &mut host_getter(caller.data_mut()); + let r = Host::get(host, arg0).await; + Ok((r,)) + }) + }, + )?; + inst.func_wrap_async( + "set", + move | + mut caller: wasmtime::StoreContextMut<'_, T>, + ( + arg0, + arg1, + ): ( + wasmtime::component::__internal::String, + wasmtime::component::__internal::String, + )| + { + wasmtime::component::__internal::Box::new(async move { + let host = &mut host_getter(caller.data_mut()); + let r = Host::set(host, arg0, arg1).await; + Ok(r) + }) + }, + )?; + Ok(()) + } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/store")?; + add_to_linker_instance(&mut inst, host_getter) + } + } + } +} diff --git a/crates/component-macro/tests/expanded/implements_concurrent.rs b/crates/component-macro/tests/expanded/implements_concurrent.rs new file mode 100644 index 000000000000..09805793a125 --- /dev/null +++ b/crates/component-macro/tests/expanded/implements_concurrent.rs @@ -0,0 +1,262 @@ +/// Auto-generated bindings for a pre-instantiated version of a +/// component which implements the world `cache`. +/// +/// This structure is created through [`CachePre::new`] which +/// takes a [`InstancePre`](wasmtime::component::InstancePre) that +/// has been created through a [`Linker`](wasmtime::component::Linker). +/// +/// For more information see [`Cache`] as well. +pub struct CachePre { + instance_pre: wasmtime::component::InstancePre, + indices: CacheIndices, +} +impl Clone for CachePre { + fn clone(&self) -> Self { + Self { + instance_pre: self.instance_pre.clone(), + indices: self.indices.clone(), + } + } +} +impl<_T: 'static> CachePre<_T> { + /// Creates a new copy of `CachePre` 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 = CacheIndices::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 [`Cache`] within the + /// `store` provided. + /// + /// This function will use `self` as the pre-instantiated + /// instance to perform instantiation. Afterwards the preloaded + /// indices in `self` are used to lookup all exports on the + /// resulting instance. + pub fn instantiate( + &self, + mut store: impl wasmtime::AsContextMut, + ) -> wasmtime::Result { + let mut store = store.as_context_mut(); + let instance = self.instance_pre.instantiate(&mut store)?; + self.indices.load(&mut store, &instance) + } +} +impl<_T: Send + 'static> CachePre<_T> { + /// Same as [`Self::instantiate`], except with `async`. + pub async fn instantiate_async( + &self, + mut store: impl wasmtime::AsContextMut, + ) -> wasmtime::Result { + 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 +/// `cache`. +/// +/// This is an implementation detail of [`CachePre`] and can +/// be constructed if needed as well. +/// +/// For more information see [`Cache`] as well. +#[derive(Clone)] +pub struct CacheIndices {} +/// Auto-generated bindings for an instance a component which +/// implements the world `cache`. +/// +/// 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 +/// [`Cache::instantiate`] which only needs a +/// [`Store`], [`Component`], and [`Linker`]. +/// +/// * Alternatively you can create a [`CachePre`] ahead of +/// time with a [`Component`] to front-load string lookups +/// of exports once instead of per-instantiation. This +/// method then uses [`CachePre::instantiate`] to +/// create a [`Cache`]. +/// +/// * If you've instantiated the instance yourself already +/// then you can use [`Cache::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 Cache {} +const _: () = { + impl CacheIndices { + /// Creates a new copy of `CacheIndices` 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(CacheIndices {}) + } + /// Uses the indices stored in `self` to load an instance + /// of [`Cache`] 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(Cache {}) + } + } + impl Cache { + /// Convenience wrapper around [`CachePre::new`] and + /// [`CachePre::instantiate`]. + pub fn instantiate<_T>( + store: impl wasmtime::AsContextMut, + component: &wasmtime::component::Component, + linker: &wasmtime::component::Linker<_T>, + ) -> wasmtime::Result { + let pre = linker.instantiate_pre(component)?; + CachePre::new(pre)?.instantiate(store) + } + /// Convenience wrapper around [`CacheIndices::new`] and + /// [`CacheIndices::load`]. + pub fn new( + mut store: impl wasmtime::AsContextMut, + instance: &wasmtime::component::Instance, + ) -> wasmtime::Result { + let indices = CacheIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) + } + /// Convenience wrapper around [`CachePre::new`] and + /// [`CachePre::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, + { + let pre = linker.instantiate_pre(component)?; + CachePre::new(pre)?.instantiate_async(store).await + } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: foo::foo::store::HostWithStore + foo::foo::store::HostWithStore + Send, + for<'a> D::Data<'a>: foo::foo::store::Host + foo::foo::store::Host + Send, + T: 'static + Send, + { + foo::foo::store::add_to_linker_instance::< + T, + D, + >(&mut linker.instance("hot-cache")?, host_getter)?; + foo::foo::store::add_to_linker_instance::< + T, + D, + >(&mut linker.instance("durable")?, host_getter)?; + Ok(()) + } + } +}; +pub mod foo { + pub mod foo { + #[allow(clippy::all)] + pub mod store { + #[allow(unused_imports)] + use wasmtime::component::__internal::Box; + pub trait HostWithStore: wasmtime::component::HasData + Send { + fn get( + accessor: &wasmtime::component::Accessor, + key: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future< + Output = Option, + > + Send; + fn set( + accessor: &wasmtime::component::Accessor, + key: wasmtime::component::__internal::String, + value: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future + Send; + } + pub trait Host: Send {} + impl<_T: Host + ?Sized + Send> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + inst.func_wrap_concurrent( + "get", + move | + caller: &wasmtime::component::Accessor, + (arg0,): (wasmtime::component::__internal::String,)| + { + wasmtime::component::__internal::Box::pin(async move { + let host = &caller.with_getter(host_getter); + let r = ::get(host, arg0).await; + Ok((r,)) + }) + }, + )?; + inst.func_wrap_concurrent( + "set", + move | + caller: &wasmtime::component::Accessor, + ( + arg0, + arg1, + ): ( + wasmtime::component::__internal::String, + wasmtime::component::__internal::String, + )| + { + wasmtime::component::__internal::Box::pin(async move { + let host = &caller.with_getter(host_getter); + let r = ::set(host, arg0, arg1).await; + Ok(r) + }) + }, + )?; + Ok(()) + } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/store")?; + add_to_linker_instance(&mut inst, host_getter) + } + } + } +} diff --git a/crates/component-macro/tests/expanded/implements_tracing_async.rs b/crates/component-macro/tests/expanded/implements_tracing_async.rs new file mode 100644 index 000000000000..90d41843d071 --- /dev/null +++ b/crates/component-macro/tests/expanded/implements_tracing_async.rs @@ -0,0 +1,314 @@ +/// Auto-generated bindings for a pre-instantiated version of a +/// component which implements the world `cache`. +/// +/// This structure is created through [`CachePre::new`] which +/// takes a [`InstancePre`](wasmtime::component::InstancePre) that +/// has been created through a [`Linker`](wasmtime::component::Linker). +/// +/// For more information see [`Cache`] as well. +pub struct CachePre { + instance_pre: wasmtime::component::InstancePre, + indices: CacheIndices, +} +impl Clone for CachePre { + fn clone(&self) -> Self { + Self { + instance_pre: self.instance_pre.clone(), + indices: self.indices.clone(), + } + } +} +impl<_T: 'static> CachePre<_T> { + /// Creates a new copy of `CachePre` 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 = CacheIndices::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 [`Cache`] within the + /// `store` provided. + /// + /// This function will use `self` as the pre-instantiated + /// instance to perform instantiation. Afterwards the preloaded + /// indices in `self` are used to lookup all exports on the + /// resulting instance. + pub fn instantiate( + &self, + mut store: impl wasmtime::AsContextMut, + ) -> wasmtime::Result { + let mut store = store.as_context_mut(); + let instance = self.instance_pre.instantiate(&mut store)?; + self.indices.load(&mut store, &instance) + } +} +impl<_T: Send + 'static> CachePre<_T> { + /// Same as [`Self::instantiate`], except with `async`. + pub async fn instantiate_async( + &self, + mut store: impl wasmtime::AsContextMut, + ) -> wasmtime::Result { + 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 +/// `cache`. +/// +/// This is an implementation detail of [`CachePre`] and can +/// be constructed if needed as well. +/// +/// For more information see [`Cache`] as well. +#[derive(Clone)] +pub struct CacheIndices {} +/// Auto-generated bindings for an instance a component which +/// implements the world `cache`. +/// +/// 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 +/// [`Cache::instantiate`] which only needs a +/// [`Store`], [`Component`], and [`Linker`]. +/// +/// * Alternatively you can create a [`CachePre`] ahead of +/// time with a [`Component`] to front-load string lookups +/// of exports once instead of per-instantiation. This +/// method then uses [`CachePre::instantiate`] to +/// create a [`Cache`]. +/// +/// * If you've instantiated the instance yourself already +/// then you can use [`Cache::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 Cache {} +const _: () = { + impl CacheIndices { + /// Creates a new copy of `CacheIndices` 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(CacheIndices {}) + } + /// Uses the indices stored in `self` to load an instance + /// of [`Cache`] 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(Cache {}) + } + } + impl Cache { + /// Convenience wrapper around [`CachePre::new`] and + /// [`CachePre::instantiate`]. + pub fn instantiate<_T>( + store: impl wasmtime::AsContextMut, + component: &wasmtime::component::Component, + linker: &wasmtime::component::Linker<_T>, + ) -> wasmtime::Result { + let pre = linker.instantiate_pre(component)?; + CachePre::new(pre)?.instantiate(store) + } + /// Convenience wrapper around [`CacheIndices::new`] and + /// [`CacheIndices::load`]. + pub fn new( + mut store: impl wasmtime::AsContextMut, + instance: &wasmtime::component::Instance, + ) -> wasmtime::Result { + let indices = CacheIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) + } + /// Convenience wrapper around [`CachePre::new`] and + /// [`CachePre::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, + { + let pre = linker.instantiate_pre(component)?; + CachePre::new(pre)?.instantiate_async(store).await + } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: foo::foo::store::HostWithStore + foo::foo::store::HostWithStore + Send, + for<'a> D::Data<'a>: foo::foo::store::Host + foo::foo::store::Host + Send, + T: 'static + Send, + { + foo::foo::store::add_to_linker_instance::< + T, + D, + >(&mut linker.instance("hot-cache")?, host_getter)?; + foo::foo::store::add_to_linker_instance::< + T, + D, + >(&mut linker.instance("durable")?, host_getter)?; + Ok(()) + } + } +}; +pub mod foo { + pub mod foo { + #[allow(clippy::all)] + pub mod store { + #[allow(unused_imports)] + use wasmtime::component::__internal::Box; + pub trait HostWithStore: wasmtime::component::HasData + Send {} + impl<_T: ?Sized> HostWithStore for _T + where + _T: wasmtime::component::HasData + Send, + {} + pub trait Host: Send { + fn get( + &mut self, + key: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future< + Output = Option, + > + Send; + fn set( + &mut self, + key: wasmtime::component::__internal::String, + value: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future + Send; + } + impl<_T: Host + ?Sized + Send> Host for &mut _T { + fn get( + &mut self, + key: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future< + Output = Option, + > + Send { + async move { Host::get(*self, key).await } + } + fn set( + &mut self, + key: wasmtime::component::__internal::String, + value: wasmtime::component::__internal::String, + ) -> impl ::core::future::Future + Send { + async move { Host::set(*self, key, value).await } + } + } + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + inst.func_wrap_async( + "get", + move | + mut caller: wasmtime::StoreContextMut<'_, T>, + (arg0,): (wasmtime::component::__internal::String,)| + { + use tracing::Instrument; + let span = tracing::span!( + tracing::Level::TRACE, "wit-bindgen import", module = + "store", function = "get", + ); + wasmtime::component::__internal::Box::new( + async move { + tracing::event!( + tracing::Level::TRACE, key = tracing::field::debug(& arg0), + "call" + ); + let host = &mut host_getter(caller.data_mut()); + let r = Host::get(host, arg0).await; + tracing::event!( + tracing::Level::TRACE, result = tracing::field::debug(& r), + "return" + ); + Ok((r,)) + } + .instrument(span), + ) + }, + )?; + inst.func_wrap_async( + "set", + move | + mut caller: wasmtime::StoreContextMut<'_, T>, + ( + arg0, + arg1, + ): ( + wasmtime::component::__internal::String, + wasmtime::component::__internal::String, + )| + { + use tracing::Instrument; + let span = tracing::span!( + tracing::Level::TRACE, "wit-bindgen import", module = + "store", function = "set", + ); + wasmtime::component::__internal::Box::new( + async move { + tracing::event!( + tracing::Level::TRACE, key = tracing::field::debug(& arg0), + value = tracing::field::debug(& arg1), "call" + ); + let host = &mut host_getter(caller.data_mut()); + let r = Host::set(host, arg0, arg1).await; + tracing::event!( + tracing::Level::TRACE, result = tracing::field::debug(& r), + "return" + ); + Ok(r) + } + .instrument(span), + ) + }, + )?; + Ok(()) + } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/store")?; + add_to_linker_instance(&mut inst, host_getter) + } + } + } +} diff --git a/crates/component-macro/tests/expanded/integers.rs b/crates/component-macro/tests/expanded/integers.rs index 295860b8ef6d..a54569789907 100644 --- a/crates/component-macro/tests/expanded/integers.rs +++ b/crates/component-macro/tests/expanded/integers.rs @@ -292,8 +292,8 @@ pub mod foo { Host::pair_ret(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -301,7 +301,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/integers")?; inst.func_wrap( "a1", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (u8,)| { @@ -470,6 +469,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/integers")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/integers_async.rs b/crates/component-macro/tests/expanded/integers_async.rs index db1617f4c4c1..3520a6b7738a 100644 --- a/crates/component-macro/tests/expanded/integers_async.rs +++ b/crates/component-macro/tests/expanded/integers_async.rs @@ -344,8 +344,8 @@ pub mod foo { async move { Host::pair_ret(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -353,7 +353,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/integers")?; inst.func_wrap_async( "a1", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (u8,)| { @@ -559,6 +558,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/integers")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/integers_concurrent.rs b/crates/component-macro/tests/expanded/integers_concurrent.rs index d46cdc28e931..e691709d6b58 100644 --- a/crates/component-macro/tests/expanded/integers_concurrent.rs +++ b/crates/component-macro/tests/expanded/integers_concurrent.rs @@ -265,8 +265,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -274,7 +274,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/integers")?; inst.func_wrap_concurrent( "a1", move |caller: &wasmtime::component::Accessor, (arg0,): (u8,)| { @@ -480,6 +479,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/integers")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/integers_tracing_async.rs b/crates/component-macro/tests/expanded/integers_tracing_async.rs index bf7da5352009..1489276ab75d 100644 --- a/crates/component-macro/tests/expanded/integers_tracing_async.rs +++ b/crates/component-macro/tests/expanded/integers_tracing_async.rs @@ -344,8 +344,8 @@ pub mod foo { async move { Host::pair_ret(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -353,7 +353,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/integers")?; inst.func_wrap_async( "a1", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (u8,)| { @@ -824,6 +823,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/integers")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/lists.rs b/crates/component-macro/tests/expanded/lists.rs index 613735361d9d..0c6486b0614d 100644 --- a/crates/component-macro/tests/expanded/lists.rs +++ b/crates/component-macro/tests/expanded/lists.rs @@ -630,8 +630,8 @@ pub mod foo { Host::load_store_everything(*self, a) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -639,7 +639,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/lists")?; inst.func_wrap( "list-u8-param", move | @@ -946,6 +945,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/lists")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/lists_async.rs b/crates/component-macro/tests/expanded/lists_async.rs index 3f7866b3b7a3..0bf2ccb4a22b 100644 --- a/crates/component-macro/tests/expanded/lists_async.rs +++ b/crates/component-macro/tests/expanded/lists_async.rs @@ -738,8 +738,8 @@ pub mod foo { async move { Host::load_store_everything(*self, a).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -747,7 +747,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/lists")?; inst.func_wrap_async( "list-u8-param", move | @@ -1112,6 +1111,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/lists")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/lists_concurrent.rs b/crates/component-macro/tests/expanded/lists_concurrent.rs index 578048c49ae9..59c3e495ca42 100644 --- a/crates/component-macro/tests/expanded/lists_concurrent.rs +++ b/crates/component-macro/tests/expanded/lists_concurrent.rs @@ -524,8 +524,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -533,7 +533,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/lists")?; inst.func_wrap_concurrent( "list-u8-param", move | @@ -915,6 +914,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/lists")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/lists_tracing_async.rs b/crates/component-macro/tests/expanded/lists_tracing_async.rs index c2b5f0c04bf4..754cff877dbe 100644 --- a/crates/component-macro/tests/expanded/lists_tracing_async.rs +++ b/crates/component-macro/tests/expanded/lists_tracing_async.rs @@ -738,8 +738,8 @@ pub mod foo { async move { Host::load_store_everything(*self, a).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -747,7 +747,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/lists")?; inst.func_wrap_async( "list-u8-param", move | @@ -1543,6 +1542,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/lists")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/many-arguments.rs b/crates/component-macro/tests/expanded/many-arguments.rs index 69ee064a7973..cf3b63a56598 100644 --- a/crates/component-macro/tests/expanded/many-arguments.rs +++ b/crates/component-macro/tests/expanded/many-arguments.rs @@ -343,8 +343,8 @@ pub mod foo { Host::big_argument(*self, x) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -352,7 +352,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/manyarg")?; inst.func_wrap( "many-args", move | @@ -429,6 +428,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/manyarg")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/many-arguments_async.rs b/crates/component-macro/tests/expanded/many-arguments_async.rs index 5657799da780..ab25e3d32c73 100644 --- a/crates/component-macro/tests/expanded/many-arguments_async.rs +++ b/crates/component-macro/tests/expanded/many-arguments_async.rs @@ -352,8 +352,8 @@ pub mod foo { async move { Host::big_argument(*self, x).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -361,7 +361,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/manyarg")?; inst.func_wrap_async( "many-args", move | @@ -443,6 +442,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/manyarg")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/many-arguments_concurrent.rs b/crates/component-macro/tests/expanded/many-arguments_concurrent.rs index 96d7f850897f..82aeabe6df86 100644 --- a/crates/component-macro/tests/expanded/many-arguments_concurrent.rs +++ b/crates/component-macro/tests/expanded/many-arguments_concurrent.rs @@ -299,8 +299,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -308,7 +308,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/manyarg")?; inst.func_wrap_concurrent( "many-args", move | @@ -390,6 +389,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/manyarg")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } 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 bae7812bc364..04b188b86b1e 100644 --- a/crates/component-macro/tests/expanded/many-arguments_tracing_async.rs +++ b/crates/component-macro/tests/expanded/many-arguments_tracing_async.rs @@ -352,8 +352,8 @@ pub mod foo { async move { Host::big_argument(*self, x).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -361,7 +361,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/manyarg")?; inst.func_wrap_async( "many-args", move | @@ -486,6 +485,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/manyarg")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/multiversion.rs b/crates/component-macro/tests/expanded/multiversion.rs index 3e60b5ebb487..63662c243ce6 100644 --- a/crates/component-macro/tests/expanded/multiversion.rs +++ b/crates/component-macro/tests/expanded/multiversion.rs @@ -213,8 +213,8 @@ pub mod my { Host::x(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -222,7 +222,6 @@ pub mod my { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("my:dep/a@0.1.0")?; inst.func_wrap( "x", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -233,6 +232,18 @@ pub mod my { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("my:dep/a@0.1.0")?; + add_to_linker_instance(&mut inst, host_getter) + } } } pub mod dep0_2_0 { @@ -253,8 +264,8 @@ pub mod my { Host::x(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -262,7 +273,6 @@ pub mod my { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("my:dep/a@0.2.0")?; inst.func_wrap( "x", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -273,6 +283,18 @@ pub mod my { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("my:dep/a@0.2.0")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/multiversion_async.rs b/crates/component-macro/tests/expanded/multiversion_async.rs index ead3b04e9615..138227ce725a 100644 --- a/crates/component-macro/tests/expanded/multiversion_async.rs +++ b/crates/component-macro/tests/expanded/multiversion_async.rs @@ -213,8 +213,8 @@ pub mod my { async move { Host::x(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -222,7 +222,6 @@ pub mod my { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("my:dep/a@0.1.0")?; inst.func_wrap_async( "x", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -235,6 +234,18 @@ pub mod my { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("my:dep/a@0.1.0")?; + add_to_linker_instance(&mut inst, host_getter) + } } } pub mod dep0_2_0 { @@ -255,8 +266,8 @@ pub mod my { async move { Host::x(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -264,7 +275,6 @@ pub mod my { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("my:dep/a@0.2.0")?; inst.func_wrap_async( "x", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -277,6 +287,18 @@ pub mod my { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("my:dep/a@0.2.0")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/multiversion_concurrent.rs b/crates/component-macro/tests/expanded/multiversion_concurrent.rs index 1d47f0bf7f05..3839bc415d9e 100644 --- a/crates/component-macro/tests/expanded/multiversion_concurrent.rs +++ b/crates/component-macro/tests/expanded/multiversion_concurrent.rs @@ -207,8 +207,8 @@ pub mod my { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -216,7 +216,6 @@ pub mod my { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("my:dep/a@0.1.0")?; inst.func_wrap_concurrent( "x", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -229,6 +228,18 @@ pub mod my { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("my:dep/a@0.1.0")?; + add_to_linker_instance(&mut inst, host_getter) + } } } pub mod dep0_2_0 { @@ -243,8 +254,8 @@ pub mod my { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -252,7 +263,6 @@ pub mod my { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("my:dep/a@0.2.0")?; inst.func_wrap_concurrent( "x", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -265,6 +275,18 @@ pub mod my { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("my:dep/a@0.2.0")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/multiversion_tracing_async.rs b/crates/component-macro/tests/expanded/multiversion_tracing_async.rs index f8c6d2557186..9e45c4ddd4aa 100644 --- a/crates/component-macro/tests/expanded/multiversion_tracing_async.rs +++ b/crates/component-macro/tests/expanded/multiversion_tracing_async.rs @@ -213,8 +213,8 @@ pub mod my { async move { Host::x(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -222,7 +222,6 @@ pub mod my { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("my:dep/a@0.1.0")?; inst.func_wrap_async( "x", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -248,6 +247,18 @@ pub mod my { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("my:dep/a@0.1.0")?; + add_to_linker_instance(&mut inst, host_getter) + } } } pub mod dep0_2_0 { @@ -268,8 +279,8 @@ pub mod my { async move { Host::x(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -277,7 +288,6 @@ pub mod my { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("my:dep/a@0.2.0")?; inst.func_wrap_async( "x", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -303,6 +313,18 @@ pub mod my { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("my:dep/a@0.2.0")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/path1.rs b/crates/component-macro/tests/expanded/path1.rs index f0ac5431574b..ae09dd55c5e0 100644 --- a/crates/component-macro/tests/expanded/path1.rs +++ b/crates/component-macro/tests/expanded/path1.rs @@ -187,6 +187,17 @@ pub mod paths { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -197,7 +208,7 @@ pub mod paths { T: 'static, { let mut inst = linker.instance("paths:path1/test")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/path1_async.rs b/crates/component-macro/tests/expanded/path1_async.rs index a6de5e5469df..70bc13bd1bcc 100644 --- a/crates/component-macro/tests/expanded/path1_async.rs +++ b/crates/component-macro/tests/expanded/path1_async.rs @@ -187,6 +187,17 @@ pub mod paths { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -197,7 +208,7 @@ pub mod paths { T: 'static, { let mut inst = linker.instance("paths:path1/test")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/path1_concurrent.rs b/crates/component-macro/tests/expanded/path1_concurrent.rs index a6de5e5469df..70bc13bd1bcc 100644 --- a/crates/component-macro/tests/expanded/path1_concurrent.rs +++ b/crates/component-macro/tests/expanded/path1_concurrent.rs @@ -187,6 +187,17 @@ pub mod paths { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -197,7 +208,7 @@ pub mod paths { T: 'static, { let mut inst = linker.instance("paths:path1/test")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/path1_tracing_async.rs b/crates/component-macro/tests/expanded/path1_tracing_async.rs index a6de5e5469df..70bc13bd1bcc 100644 --- a/crates/component-macro/tests/expanded/path1_tracing_async.rs +++ b/crates/component-macro/tests/expanded/path1_tracing_async.rs @@ -187,6 +187,17 @@ pub mod paths { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -197,7 +208,7 @@ pub mod paths { T: 'static, { let mut inst = linker.instance("paths:path1/test")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/path2.rs b/crates/component-macro/tests/expanded/path2.rs index d399ee46de49..3a6583d74055 100644 --- a/crates/component-macro/tests/expanded/path2.rs +++ b/crates/component-macro/tests/expanded/path2.rs @@ -187,6 +187,17 @@ pub mod paths { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -197,7 +208,7 @@ pub mod paths { T: 'static, { let mut inst = linker.instance("paths:path2/test")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/path2_async.rs b/crates/component-macro/tests/expanded/path2_async.rs index a6d98f0564ff..a356bf6fc530 100644 --- a/crates/component-macro/tests/expanded/path2_async.rs +++ b/crates/component-macro/tests/expanded/path2_async.rs @@ -187,6 +187,17 @@ pub mod paths { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -197,7 +208,7 @@ pub mod paths { T: 'static, { let mut inst = linker.instance("paths:path2/test")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/path2_concurrent.rs b/crates/component-macro/tests/expanded/path2_concurrent.rs index a6d98f0564ff..a356bf6fc530 100644 --- a/crates/component-macro/tests/expanded/path2_concurrent.rs +++ b/crates/component-macro/tests/expanded/path2_concurrent.rs @@ -187,6 +187,17 @@ pub mod paths { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -197,7 +208,7 @@ pub mod paths { T: 'static, { let mut inst = linker.instance("paths:path2/test")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/path2_tracing_async.rs b/crates/component-macro/tests/expanded/path2_tracing_async.rs index a6d98f0564ff..a356bf6fc530 100644 --- a/crates/component-macro/tests/expanded/path2_tracing_async.rs +++ b/crates/component-macro/tests/expanded/path2_tracing_async.rs @@ -187,6 +187,17 @@ pub mod paths { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -197,7 +208,7 @@ pub mod paths { T: 'static, { let mut inst = linker.instance("paths:path2/test")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/records.rs b/crates/component-macro/tests/expanded/records.rs index 0096aad14f67..3e9c48afcf0b 100644 --- a/crates/component-macro/tests/expanded/records.rs +++ b/crates/component-macro/tests/expanded/records.rs @@ -441,8 +441,8 @@ pub mod foo { Host::typedef_inout(*self, e) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -450,7 +450,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/records")?; inst.func_wrap( "tuple-arg", move | @@ -559,6 +558,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/records")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/records_async.rs b/crates/component-macro/tests/expanded/records_async.rs index cdbc698678ac..006377295bb8 100644 --- a/crates/component-macro/tests/expanded/records_async.rs +++ b/crates/component-macro/tests/expanded/records_async.rs @@ -497,8 +497,8 @@ pub mod foo { async move { Host::typedef_inout(*self, e).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -506,7 +506,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/records")?; inst.func_wrap_async( "tuple-arg", move | @@ -637,6 +636,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/records")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/records_concurrent.rs b/crates/component-macro/tests/expanded/records_concurrent.rs index 432af1ed8e5b..3062b9e0267a 100644 --- a/crates/component-macro/tests/expanded/records_concurrent.rs +++ b/crates/component-macro/tests/expanded/records_concurrent.rs @@ -431,8 +431,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -440,7 +440,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/records")?; inst.func_wrap_concurrent( "tuple-arg", move | @@ -570,6 +569,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/records")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/records_tracing_async.rs b/crates/component-macro/tests/expanded/records_tracing_async.rs index f86158bbc621..a78d5875b0dd 100644 --- a/crates/component-macro/tests/expanded/records_tracing_async.rs +++ b/crates/component-macro/tests/expanded/records_tracing_async.rs @@ -497,8 +497,8 @@ pub mod foo { async move { Host::typedef_inout(*self, e).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -506,7 +506,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/records")?; inst.func_wrap_async( "tuple-arg", move | @@ -798,6 +797,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/records")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/rename.rs b/crates/component-macro/tests/expanded/rename.rs index c0182645fd97..e996204e4166 100644 --- a/crates/component-macro/tests/expanded/rename.rs +++ b/crates/component-macro/tests/expanded/rename.rs @@ -193,6 +193,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -203,7 +214,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/green")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -228,8 +239,8 @@ pub mod foo { Host::foo(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -237,7 +248,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/red")?; inst.func_wrap( "foo", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -248,6 +258,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/red")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/rename_async.rs b/crates/component-macro/tests/expanded/rename_async.rs index 87047d066111..90040d2b3bfd 100644 --- a/crates/component-macro/tests/expanded/rename_async.rs +++ b/crates/component-macro/tests/expanded/rename_async.rs @@ -193,6 +193,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -203,7 +214,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/green")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -228,8 +239,8 @@ pub mod foo { async move { Host::foo(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -237,7 +248,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/red")?; inst.func_wrap_async( "foo", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -250,6 +260,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/red")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/rename_concurrent.rs b/crates/component-macro/tests/expanded/rename_concurrent.rs index 45bea9853835..87ee9f9846e1 100644 --- a/crates/component-macro/tests/expanded/rename_concurrent.rs +++ b/crates/component-macro/tests/expanded/rename_concurrent.rs @@ -193,6 +193,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -203,7 +214,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/green")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -222,8 +233,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -231,7 +242,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/red")?; inst.func_wrap_concurrent( "foo", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -244,6 +254,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/red")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/rename_tracing_async.rs b/crates/component-macro/tests/expanded/rename_tracing_async.rs index 384b4cbaef23..76e48291225d 100644 --- a/crates/component-macro/tests/expanded/rename_tracing_async.rs +++ b/crates/component-macro/tests/expanded/rename_tracing_async.rs @@ -193,6 +193,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -203,7 +214,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/green")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -228,8 +239,8 @@ pub mod foo { async move { Host::foo(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -237,7 +248,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/red")?; inst.func_wrap_async( "foo", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -263,6 +273,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/red")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/resources-export.rs b/crates/component-macro/tests/expanded/resources-export.rs index a846a09b5cb7..81eafe6d02b1 100644 --- a/crates/component-macro/tests/expanded/resources-export.rs +++ b/crates/component-macro/tests/expanded/resources-export.rs @@ -261,8 +261,8 @@ pub mod foo { {} pub trait Host: HostY {} impl<_T: Host + ?Sized> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -270,7 +270,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/transitive-import")?; inst.resource( "y", wasmtime::component::ResourceType::host::(), @@ -283,6 +282,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/transitive-import")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/resources-export_async.rs b/crates/component-macro/tests/expanded/resources-export_async.rs index fdf53e75f3f5..4841dd9e46e2 100644 --- a/crates/component-macro/tests/expanded/resources-export_async.rs +++ b/crates/component-macro/tests/expanded/resources-export_async.rs @@ -261,8 +261,8 @@ pub mod foo { {} pub trait Host: HostY + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -270,7 +270,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/transitive-import")?; inst.resource_async( "y", wasmtime::component::ResourceType::host::(), @@ -288,6 +287,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/transitive-import")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/resources-export_concurrent.rs b/crates/component-macro/tests/expanded/resources-export_concurrent.rs index 6502e19a7f5b..39719a430ff8 100644 --- a/crates/component-macro/tests/expanded/resources-export_concurrent.rs +++ b/crates/component-macro/tests/expanded/resources-export_concurrent.rs @@ -252,8 +252,8 @@ pub mod foo { {} pub trait Host: HostY + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -261,7 +261,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/transitive-import")?; inst.resource_concurrent( "y", wasmtime::component::ResourceType::host::(), @@ -280,6 +279,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/transitive-import")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } 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 1a58489516bc..21fd692b69ce 100644 --- a/crates/component-macro/tests/expanded/resources-export_tracing_async.rs +++ b/crates/component-macro/tests/expanded/resources-export_tracing_async.rs @@ -261,8 +261,8 @@ pub mod foo { {} pub trait Host: HostY + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -270,7 +270,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/transitive-import")?; inst.resource_async( "y", wasmtime::component::ResourceType::host::(), @@ -288,6 +287,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/transitive-import")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/resources-import.rs b/crates/component-macro/tests/expanded/resources-import.rs index 2fdab2bdbbb3..189e388a6b37 100644 --- a/crates/component-macro/tests/expanded/resources-import.rs +++ b/crates/component-macro/tests/expanded/resources-import.rs @@ -665,8 +665,8 @@ pub mod foo { Host::func_with_handle_typedef(*self, x) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -674,7 +674,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/resources")?; inst.resource( "bar", wasmtime::component::ResourceType::host::(), @@ -938,6 +937,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/resources")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod long_use_chain1 { @@ -970,8 +981,8 @@ pub mod foo { {} pub trait Host: HostA {} impl<_T: Host + ?Sized> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -979,7 +990,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/long-use-chain1")?; inst.resource( "a", wasmtime::component::ResourceType::host::(), @@ -992,6 +1002,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/long-use-chain1")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod long_use_chain2 { @@ -1005,6 +1027,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -1015,7 +1048,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/long-use-chain2")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -1030,6 +1063,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -1040,7 +1084,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/long-use-chain3")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -1061,8 +1105,8 @@ pub mod foo { Host::foo(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1070,7 +1114,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/long-use-chain4")?; inst.func_wrap( "foo", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -1081,6 +1124,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/long-use-chain4")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod transitive_interface_with_resource { @@ -1113,8 +1168,8 @@ pub mod foo { {} pub trait Host: HostFoo {} impl<_T: Host + ?Sized> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1122,8 +1177,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker - .instance("foo:foo/transitive-interface-with-resource")?; inst.resource( "foo", wasmtime::component::ResourceType::host::(), @@ -1136,6 +1189,19 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker + .instance("foo:foo/transitive-interface-with-resource")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/resources-import_async.rs b/crates/component-macro/tests/expanded/resources-import_async.rs index 92ca4ebc0c9b..a5f5ae140cb0 100644 --- a/crates/component-macro/tests/expanded/resources-import_async.rs +++ b/crates/component-macro/tests/expanded/resources-import_async.rs @@ -776,8 +776,8 @@ pub mod foo { async move { Host::func_with_handle_typedef(*self, x).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -785,7 +785,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/resources")?; inst.resource_async( "bar", wasmtime::component::ResourceType::host::(), @@ -1102,6 +1101,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/resources")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod long_use_chain1 { @@ -1134,8 +1145,8 @@ pub mod foo { {} pub trait Host: HostA + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1143,7 +1154,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/long-use-chain1")?; inst.resource_async( "a", wasmtime::component::ResourceType::host::(), @@ -1161,6 +1171,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/long-use-chain1")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod long_use_chain2 { @@ -1174,6 +1196,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -1184,7 +1217,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/long-use-chain2")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -1199,6 +1232,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -1209,7 +1253,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/long-use-chain3")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -1238,8 +1282,8 @@ pub mod foo { async move { Host::foo(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1247,7 +1291,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/long-use-chain4")?; inst.func_wrap_async( "foo", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -1260,6 +1303,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/long-use-chain4")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod transitive_interface_with_resource { @@ -1292,8 +1347,8 @@ pub mod foo { {} pub trait Host: HostFoo + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1301,8 +1356,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker - .instance("foo:foo/transitive-interface-with-resource")?; inst.resource_async( "foo", wasmtime::component::ResourceType::host::(), @@ -1320,6 +1373,19 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker + .instance("foo:foo/transitive-interface-with-resource")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/resources-import_concurrent.rs b/crates/component-macro/tests/expanded/resources-import_concurrent.rs index d2ce0f188259..196fdb1b1bfe 100644 --- a/crates/component-macro/tests/expanded/resources-import_concurrent.rs +++ b/crates/component-macro/tests/expanded/resources-import_concurrent.rs @@ -572,8 +572,8 @@ pub mod foo { } pub trait Host: HostBar + HostFallible + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -581,7 +581,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/resources")?; inst.resource_concurrent( "bar", wasmtime::component::ResourceType::host::(), @@ -914,6 +913,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/resources")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod long_use_chain1 { @@ -937,8 +948,8 @@ pub mod foo { {} pub trait Host: HostA + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -946,7 +957,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/long-use-chain1")?; inst.resource_concurrent( "a", wasmtime::component::ResourceType::host::(), @@ -965,6 +975,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/long-use-chain1")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod long_use_chain2 { @@ -978,6 +1000,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -988,7 +1021,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/long-use-chain2")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -1003,6 +1036,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -1013,7 +1057,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/long-use-chain3")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -1030,8 +1074,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1039,7 +1083,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/long-use-chain4")?; inst.func_wrap_concurrent( "foo", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -1052,6 +1095,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/long-use-chain4")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod transitive_interface_with_resource { @@ -1075,8 +1130,8 @@ pub mod foo { {} pub trait Host: HostFoo + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1084,8 +1139,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker - .instance("foo:foo/transitive-interface-with-resource")?; inst.resource_concurrent( "foo", wasmtime::component::ResourceType::host::(), @@ -1104,6 +1157,19 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker + .instance("foo:foo/transitive-interface-with-resource")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } 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 da20739ca4a7..e1ccb9e32366 100644 --- a/crates/component-macro/tests/expanded/resources-import_tracing_async.rs +++ b/crates/component-macro/tests/expanded/resources-import_tracing_async.rs @@ -839,8 +839,8 @@ pub mod foo { async move { Host::func_with_handle_typedef(*self, x).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -848,7 +848,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/resources")?; inst.resource_async( "bar", wasmtime::component::ResourceType::host::(), @@ -1506,6 +1505,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/resources")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod long_use_chain1 { @@ -1538,8 +1549,8 @@ pub mod foo { {} pub trait Host: HostA + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1547,7 +1558,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/long-use-chain1")?; inst.resource_async( "a", wasmtime::component::ResourceType::host::(), @@ -1565,6 +1575,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/long-use-chain1")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod long_use_chain2 { @@ -1578,6 +1600,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -1588,7 +1621,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/long-use-chain2")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -1603,6 +1636,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -1613,7 +1657,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/long-use-chain3")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } #[allow(clippy::all)] @@ -1642,8 +1686,8 @@ pub mod foo { async move { Host::foo(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1651,7 +1695,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/long-use-chain4")?; inst.func_wrap_async( "foo", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -1677,6 +1720,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/long-use-chain4")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod transitive_interface_with_resource { @@ -1709,8 +1764,8 @@ pub mod foo { {} pub trait Host: HostFoo + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -1718,8 +1773,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker - .instance("foo:foo/transitive-interface-with-resource")?; inst.resource_async( "foo", wasmtime::component::ResourceType::host::(), @@ -1737,6 +1790,19 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker + .instance("foo:foo/transitive-interface-with-resource")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/share-types.rs b/crates/component-macro/tests/expanded/share-types.rs index c09a5e9fad58..3ca15f019d59 100644 --- a/crates/component-macro/tests/expanded/share-types.rs +++ b/crates/component-macro/tests/expanded/share-types.rs @@ -235,6 +235,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -245,7 +256,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/http-types")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } @@ -277,8 +288,8 @@ pub mod http_fetch { Host::fetch_request(*self, request) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -286,7 +297,6 @@ pub mod http_fetch { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("http-fetch")?; inst.func_wrap( "fetch-request", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (Request,)| { @@ -297,6 +307,18 @@ pub mod http_fetch { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("http-fetch")?; + add_to_linker_instance(&mut inst, host_getter) + } } pub mod exports { #[allow(clippy::all)] diff --git a/crates/component-macro/tests/expanded/share-types_async.rs b/crates/component-macro/tests/expanded/share-types_async.rs index 38f3fbfaabd2..f6d394e0c3ba 100644 --- a/crates/component-macro/tests/expanded/share-types_async.rs +++ b/crates/component-macro/tests/expanded/share-types_async.rs @@ -235,6 +235,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -245,7 +256,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/http-types")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } @@ -283,8 +294,8 @@ pub mod http_fetch { async move { Host::fetch_request(*self, request).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -292,7 +303,6 @@ pub mod http_fetch { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("http-fetch")?; inst.func_wrap_async( "fetch-request", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (Request,)| { @@ -305,6 +315,18 @@ pub mod http_fetch { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("http-fetch")?; + add_to_linker_instance(&mut inst, host_getter) + } } pub mod exports { #[allow(clippy::all)] diff --git a/crates/component-macro/tests/expanded/share-types_concurrent.rs b/crates/component-macro/tests/expanded/share-types_concurrent.rs index f9888178b69b..fe2f5aeadba7 100644 --- a/crates/component-macro/tests/expanded/share-types_concurrent.rs +++ b/crates/component-macro/tests/expanded/share-types_concurrent.rs @@ -235,6 +235,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -245,7 +256,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/http-types")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } @@ -272,8 +283,8 @@ pub mod http_fetch { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -281,7 +292,6 @@ pub mod http_fetch { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("http-fetch")?; inst.func_wrap_concurrent( "fetch-request", move |caller: &wasmtime::component::Accessor, (arg0,): (Request,)| { @@ -294,6 +304,18 @@ pub mod http_fetch { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("http-fetch")?; + add_to_linker_instance(&mut inst, host_getter) + } } pub mod exports { #[allow(clippy::all)] 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 8b2308e0f5de..c272f4f72fe4 100644 --- a/crates/component-macro/tests/expanded/share-types_tracing_async.rs +++ b/crates/component-macro/tests/expanded/share-types_tracing_async.rs @@ -235,6 +235,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -245,7 +256,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/http-types")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } @@ -283,8 +294,8 @@ pub mod http_fetch { async move { Host::fetch_request(*self, request).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -292,7 +303,6 @@ pub mod http_fetch { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("http-fetch")?; inst.func_wrap_async( "fetch-request", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (Request,)| { @@ -321,6 +331,18 @@ pub mod http_fetch { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("http-fetch")?; + add_to_linker_instance(&mut inst, host_getter) + } } pub mod exports { #[allow(clippy::all)] diff --git a/crates/component-macro/tests/expanded/simple-functions.rs b/crates/component-macro/tests/expanded/simple-functions.rs index bb3ab3f3ec93..683b082662ec 100644 --- a/crates/component-macro/tests/expanded/simple-functions.rs +++ b/crates/component-macro/tests/expanded/simple-functions.rs @@ -224,8 +224,8 @@ pub mod foo { Host::f6(*self, a, b, c) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -233,7 +233,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/simple")?; inst.func_wrap( "f1", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -290,6 +289,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/simple")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/simple-functions_async.rs b/crates/component-macro/tests/expanded/simple-functions_async.rs index dd74e75b2604..c24de7c820a3 100644 --- a/crates/component-macro/tests/expanded/simple-functions_async.rs +++ b/crates/component-macro/tests/expanded/simple-functions_async.rs @@ -252,8 +252,8 @@ pub mod foo { async move { Host::f6(*self, a, b, c).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -261,7 +261,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/simple")?; inst.func_wrap_async( "f1", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -330,6 +329,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/simple")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/simple-functions_concurrent.rs b/crates/component-macro/tests/expanded/simple-functions_concurrent.rs index f637188fe110..801da1d4d6c5 100644 --- a/crates/component-macro/tests/expanded/simple-functions_concurrent.rs +++ b/crates/component-macro/tests/expanded/simple-functions_concurrent.rs @@ -219,8 +219,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -228,7 +228,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/simple")?; inst.func_wrap_concurrent( "f1", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -298,6 +297,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/simple")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } 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 c52a322f375f..707a195a2b40 100644 --- a/crates/component-macro/tests/expanded/simple-functions_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-functions_tracing_async.rs @@ -252,8 +252,8 @@ pub mod foo { async move { Host::f6(*self, a, b, c).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -261,7 +261,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/simple")?; inst.func_wrap_async( "f1", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -418,6 +417,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/simple")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/simple-lists.rs b/crates/component-macro/tests/expanded/simple-lists.rs index a6cc8f30eb1e..f67ef23c2798 100644 --- a/crates/component-macro/tests/expanded/simple-lists.rs +++ b/crates/component-macro/tests/expanded/simple-lists.rs @@ -250,8 +250,8 @@ pub mod foo { Host::simple_list4(*self, l) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -259,7 +259,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/simple-lists")?; inst.func_wrap( "simple-list1", move | @@ -315,6 +314,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/simple-lists")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/simple-lists_async.rs b/crates/component-macro/tests/expanded/simple-lists_async.rs index d2270ce4667b..ac63fe24ba70 100644 --- a/crates/component-macro/tests/expanded/simple-lists_async.rs +++ b/crates/component-macro/tests/expanded/simple-lists_async.rs @@ -266,8 +266,8 @@ pub mod foo { async move { Host::simple_list4(*self, l).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -275,7 +275,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/simple-lists")?; inst.func_wrap_async( "simple-list1", move | @@ -339,6 +338,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/simple-lists")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/simple-lists_concurrent.rs b/crates/component-macro/tests/expanded/simple-lists_concurrent.rs index 7a299abbb748..6837bf9dd26f 100644 --- a/crates/component-macro/tests/expanded/simple-lists_concurrent.rs +++ b/crates/component-macro/tests/expanded/simple-lists_concurrent.rs @@ -224,8 +224,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -233,7 +233,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/simple-lists")?; inst.func_wrap_concurrent( "simple-list1", move | @@ -298,6 +297,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/simple-lists")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } 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 3e3784d15cc5..dd0636749623 100644 --- a/crates/component-macro/tests/expanded/simple-lists_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-lists_tracing_async.rs @@ -266,8 +266,8 @@ pub mod foo { async move { Host::simple_list4(*self, l).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -275,7 +275,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/simple-lists")?; inst.func_wrap_async( "simple-list1", move | @@ -400,6 +399,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/simple-lists")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/simple-wasi.rs b/crates/component-macro/tests/expanded/simple-wasi.rs index 0a0ce4ea8946..df40c5724964 100644 --- a/crates/component-macro/tests/expanded/simple-wasi.rs +++ b/crates/component-macro/tests/expanded/simple-wasi.rs @@ -262,8 +262,8 @@ pub mod foo { Host::stat(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -271,7 +271,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/wasi-filesystem")?; inst.func_wrap( "create-directory-at", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -290,6 +289,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/wasi-filesystem")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod wall_clock { @@ -321,6 +332,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -331,7 +353,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/wall-clock")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/simple-wasi_async.rs b/crates/component-macro/tests/expanded/simple-wasi_async.rs index be9f3ff57b63..4a0402d59f12 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_async.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_async.rs @@ -274,8 +274,8 @@ pub mod foo { async move { Host::stat(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -283,7 +283,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/wasi-filesystem")?; inst.func_wrap_async( "create-directory-at", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -306,6 +305,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/wasi-filesystem")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod wall_clock { @@ -337,6 +348,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -347,7 +369,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/wall-clock")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs b/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs index 629d6b81eb2d..42f23867b2c5 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs @@ -257,8 +257,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -266,7 +266,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/wasi-filesystem")?; inst.func_wrap_concurrent( "create-directory-at", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -290,6 +289,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/wasi-filesystem")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod wall_clock { @@ -321,6 +332,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -331,7 +353,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/wall-clock")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } 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 eb159e7c510d..04d9b5d9adcc 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs @@ -274,8 +274,8 @@ pub mod foo { async move { Host::stat(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -283,7 +283,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/wasi-filesystem")?; inst.func_wrap_async( "create-directory-at", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -332,6 +331,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/wasi-filesystem")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod wall_clock { @@ -363,6 +374,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -373,7 +395,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/wall-clock")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/small-anonymous.rs b/crates/component-macro/tests/expanded/small-anonymous.rs index 3d78db6a4080..a2c06d099994 100644 --- a/crates/component-macro/tests/expanded/small-anonymous.rs +++ b/crates/component-macro/tests/expanded/small-anonymous.rs @@ -251,8 +251,8 @@ pub mod foo { Host::option_test(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -260,7 +260,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/anon")?; inst.func_wrap( "option-test", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -271,6 +270,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/anon")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/small-anonymous_async.rs b/crates/component-macro/tests/expanded/small-anonymous_async.rs index db741f17d441..cba4b3308f64 100644 --- a/crates/component-macro/tests/expanded/small-anonymous_async.rs +++ b/crates/component-macro/tests/expanded/small-anonymous_async.rs @@ -261,8 +261,8 @@ pub mod foo { async move { Host::option_test(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -270,7 +270,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/anon")?; inst.func_wrap_async( "option-test", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -283,6 +282,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/anon")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/small-anonymous_concurrent.rs b/crates/component-macro/tests/expanded/small-anonymous_concurrent.rs index 9d6cbe2b8394..90bead019447 100644 --- a/crates/component-macro/tests/expanded/small-anonymous_concurrent.rs +++ b/crates/component-macro/tests/expanded/small-anonymous_concurrent.rs @@ -246,8 +246,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -255,7 +255,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/anon")?; inst.func_wrap_concurrent( "option-test", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -268,6 +267,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/anon")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } 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 da5042425421..edb50ba2fc6a 100644 --- a/crates/component-macro/tests/expanded/small-anonymous_tracing_async.rs +++ b/crates/component-macro/tests/expanded/small-anonymous_tracing_async.rs @@ -261,8 +261,8 @@ pub mod foo { async move { Host::option_test(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -270,7 +270,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/anon")?; inst.func_wrap_async( "option-test", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -296,6 +295,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/anon")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/smoke.rs b/crates/component-macro/tests/expanded/smoke.rs index 16be78927ed2..e904cca3d63e 100644 --- a/crates/component-macro/tests/expanded/smoke.rs +++ b/crates/component-macro/tests/expanded/smoke.rs @@ -191,8 +191,8 @@ pub mod imports { Host::y(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -200,7 +200,6 @@ pub mod imports { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("imports")?; inst.func_wrap( "y", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -211,4 +210,16 @@ pub mod imports { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("imports")?; + add_to_linker_instance(&mut inst, host_getter) + } } diff --git a/crates/component-macro/tests/expanded/smoke_async.rs b/crates/component-macro/tests/expanded/smoke_async.rs index 9bbe696320c3..c82706f4d6b7 100644 --- a/crates/component-macro/tests/expanded/smoke_async.rs +++ b/crates/component-macro/tests/expanded/smoke_async.rs @@ -191,8 +191,8 @@ pub mod imports { async move { Host::y(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -200,7 +200,6 @@ pub mod imports { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("imports")?; inst.func_wrap_async( "y", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -213,4 +212,16 @@ pub mod imports { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("imports")?; + add_to_linker_instance(&mut inst, host_getter) + } } diff --git a/crates/component-macro/tests/expanded/smoke_concurrent.rs b/crates/component-macro/tests/expanded/smoke_concurrent.rs index 2754c31736a4..e73fb7a33ba9 100644 --- a/crates/component-macro/tests/expanded/smoke_concurrent.rs +++ b/crates/component-macro/tests/expanded/smoke_concurrent.rs @@ -185,8 +185,8 @@ pub mod imports { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -194,7 +194,6 @@ pub mod imports { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("imports")?; inst.func_wrap_concurrent( "y", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -207,4 +206,16 @@ pub mod imports { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("imports")?; + add_to_linker_instance(&mut inst, host_getter) + } } diff --git a/crates/component-macro/tests/expanded/smoke_tracing_async.rs b/crates/component-macro/tests/expanded/smoke_tracing_async.rs index a2158b63438e..4a01174a2118 100644 --- a/crates/component-macro/tests/expanded/smoke_tracing_async.rs +++ b/crates/component-macro/tests/expanded/smoke_tracing_async.rs @@ -191,8 +191,8 @@ pub mod imports { async move { Host::y(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -200,7 +200,6 @@ pub mod imports { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("imports")?; inst.func_wrap_async( "y", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -226,4 +225,16 @@ pub mod imports { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("imports")?; + add_to_linker_instance(&mut inst, host_getter) + } } diff --git a/crates/component-macro/tests/expanded/strings.rs b/crates/component-macro/tests/expanded/strings.rs index d3f3395eaa71..892ef471b4b5 100644 --- a/crates/component-macro/tests/expanded/strings.rs +++ b/crates/component-macro/tests/expanded/strings.rs @@ -220,8 +220,8 @@ pub mod foo { Host::c(*self, a, b) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -229,7 +229,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/strings")?; inst.func_wrap( "a", move | @@ -268,6 +267,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/strings")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/strings_async.rs b/crates/component-macro/tests/expanded/strings_async.rs index 3fa28e332491..7fa81e9309db 100644 --- a/crates/component-macro/tests/expanded/strings_async.rs +++ b/crates/component-macro/tests/expanded/strings_async.rs @@ -238,8 +238,8 @@ pub mod foo { async move { Host::c(*self, a, b).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -247,7 +247,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/strings")?; inst.func_wrap_async( "a", move | @@ -292,6 +291,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/strings")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/strings_concurrent.rs b/crates/component-macro/tests/expanded/strings_concurrent.rs index 2f5ad4aa0ef8..5a1b8c26d5ee 100644 --- a/crates/component-macro/tests/expanded/strings_concurrent.rs +++ b/crates/component-macro/tests/expanded/strings_concurrent.rs @@ -211,8 +211,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -220,7 +220,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/strings")?; inst.func_wrap_concurrent( "a", move | @@ -265,6 +264,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/strings")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/strings_tracing_async.rs b/crates/component-macro/tests/expanded/strings_tracing_async.rs index 115d9abf328f..d98979af2dd6 100644 --- a/crates/component-macro/tests/expanded/strings_tracing_async.rs +++ b/crates/component-macro/tests/expanded/strings_tracing_async.rs @@ -238,8 +238,8 @@ pub mod foo { async move { Host::c(*self, a, b).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -247,7 +247,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/strings")?; inst.func_wrap_async( "a", move | @@ -337,6 +336,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/strings")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/unstable-features.rs b/crates/component-macro/tests/expanded/unstable-features.rs index 4667ddfea848..d914b29f1f77 100644 --- a/crates/component-macro/tests/expanded/unstable-features.rs +++ b/crates/component-macro/tests/expanded/unstable-features.rs @@ -429,8 +429,8 @@ pub mod foo { Host::foo(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, options: &LinkOptions, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> @@ -440,7 +440,6 @@ pub mod foo { T: 'static, { if options.experimental_interface { - let mut inst = linker.instance("foo:foo/the-interface")?; if options.experimental_interface_resource { inst.resource( "bar", @@ -479,6 +478,19 @@ pub mod foo { } Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + options: &LinkOptions, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/the-interface")?; + add_to_linker_instance(&mut inst, options, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/unstable-features_async.rs b/crates/component-macro/tests/expanded/unstable-features_async.rs index 829a7e7eb44e..a9e2bfd028c0 100644 --- a/crates/component-macro/tests/expanded/unstable-features_async.rs +++ b/crates/component-macro/tests/expanded/unstable-features_async.rs @@ -456,8 +456,8 @@ pub mod foo { async move { Host::foo(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, options: &LinkOptions, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> @@ -467,7 +467,6 @@ pub mod foo { T: 'static + Send, { if options.experimental_interface { - let mut inst = linker.instance("foo:foo/the-interface")?; if options.experimental_interface_resource { inst.resource_async( "bar", @@ -515,6 +514,19 @@ pub mod foo { } Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + options: &LinkOptions, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/the-interface")?; + add_to_linker_instance(&mut inst, options, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/unstable-features_concurrent.rs b/crates/component-macro/tests/expanded/unstable-features_concurrent.rs index 7acdfb841cfd..86632064b04e 100644 --- a/crates/component-macro/tests/expanded/unstable-features_concurrent.rs +++ b/crates/component-macro/tests/expanded/unstable-features_concurrent.rs @@ -415,8 +415,8 @@ pub mod foo { } pub trait Host: HostBar + Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, options: &LinkOptions, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> @@ -426,7 +426,6 @@ pub mod foo { T: 'static + Send, { if options.experimental_interface { - let mut inst = linker.instance("foo:foo/the-interface")?; if options.experimental_interface_resource { inst.resource_concurrent( "bar", @@ -475,6 +474,19 @@ pub mod foo { } Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + options: &LinkOptions, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/the-interface")?; + add_to_linker_instance(&mut inst, options, host_getter) + } } } } 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 46313d963cf8..02db339e8fef 100644 --- a/crates/component-macro/tests/expanded/unstable-features_tracing_async.rs +++ b/crates/component-macro/tests/expanded/unstable-features_tracing_async.rs @@ -485,8 +485,8 @@ pub mod foo { async move { Host::foo(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, options: &LinkOptions, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> @@ -496,7 +496,6 @@ pub mod foo { T: 'static + Send, { if options.experimental_interface { - let mut inst = linker.instance("foo:foo/the-interface")?; if options.experimental_interface_resource { inst.resource_async( "bar", @@ -573,6 +572,19 @@ pub mod foo { } Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + options: &LinkOptions, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/the-interface")?; + add_to_linker_instance(&mut inst, options, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/unversioned-foo.rs b/crates/component-macro/tests/expanded/unversioned-foo.rs index f0cd89271b8f..f6f14dad9b95 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo.rs @@ -221,8 +221,8 @@ pub mod foo { Host::g(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -230,7 +230,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap( "g", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -241,6 +240,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/a")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/unversioned-foo_async.rs b/crates/component-macro/tests/expanded/unversioned-foo_async.rs index b3cde77ae0f3..2b08e24359d0 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo_async.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo_async.rs @@ -225,8 +225,8 @@ pub mod foo { async move { Host::g(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -234,7 +234,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_async( "g", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -247,6 +246,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/a")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs b/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs index c658c7b0dd81..f8db3a8074f9 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs @@ -215,8 +215,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -224,7 +224,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_concurrent( "g", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -237,6 +236,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/a")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } 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 3e8ebdac04ae..13e5eca80e4d 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo_tracing_async.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo_tracing_async.rs @@ -225,8 +225,8 @@ pub mod foo { async move { Host::g(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -234,7 +234,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_async( "g", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -260,6 +259,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/a")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/use-paths.rs b/crates/component-macro/tests/expanded/use-paths.rs index d82f4e696dd1..efee63c8f568 100644 --- a/crates/component-macro/tests/expanded/use-paths.rs +++ b/crates/component-macro/tests/expanded/use-paths.rs @@ -214,8 +214,8 @@ pub mod foo { Host::a(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -223,7 +223,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap( "a", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -234,6 +233,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/a")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod b { @@ -257,8 +268,8 @@ pub mod foo { Host::a(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -266,7 +277,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/b")?; inst.func_wrap( "a", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -277,6 +287,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/b")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod c { @@ -300,8 +322,8 @@ pub mod foo { Host::a(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -309,7 +331,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/c")?; inst.func_wrap( "a", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -320,6 +341,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/c")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } @@ -345,8 +378,8 @@ pub mod d { Host::b(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -354,7 +387,6 @@ pub mod d { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("d")?; inst.func_wrap( "b", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -365,4 +397,16 @@ pub mod d { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("d")?; + add_to_linker_instance(&mut inst, host_getter) + } } diff --git a/crates/component-macro/tests/expanded/use-paths_async.rs b/crates/component-macro/tests/expanded/use-paths_async.rs index aaeed11d0035..299aee3a347b 100644 --- a/crates/component-macro/tests/expanded/use-paths_async.rs +++ b/crates/component-macro/tests/expanded/use-paths_async.rs @@ -215,8 +215,8 @@ pub mod foo { async move { Host::a(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -224,7 +224,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_async( "a", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -237,6 +236,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/a")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod b { @@ -260,8 +271,8 @@ pub mod foo { async move { Host::a(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -269,7 +280,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/b")?; inst.func_wrap_async( "a", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -282,6 +292,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/b")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod c { @@ -305,8 +327,8 @@ pub mod foo { async move { Host::a(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -314,7 +336,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/c")?; inst.func_wrap_async( "a", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -327,6 +348,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/c")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } @@ -352,8 +385,8 @@ pub mod d { async move { Host::b(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -361,7 +394,6 @@ pub mod d { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("d")?; inst.func_wrap_async( "b", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -374,4 +406,16 @@ pub mod d { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("d")?; + add_to_linker_instance(&mut inst, host_getter) + } } diff --git a/crates/component-macro/tests/expanded/use-paths_concurrent.rs b/crates/component-macro/tests/expanded/use-paths_concurrent.rs index 0cc1d8b81346..221540efd95f 100644 --- a/crates/component-macro/tests/expanded/use-paths_concurrent.rs +++ b/crates/component-macro/tests/expanded/use-paths_concurrent.rs @@ -209,8 +209,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -218,7 +218,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_concurrent( "a", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -231,6 +230,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/a")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod b { @@ -248,8 +259,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -257,7 +268,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/b")?; inst.func_wrap_concurrent( "a", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -270,6 +280,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/b")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod c { @@ -287,8 +309,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -296,7 +318,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/c")?; inst.func_wrap_concurrent( "a", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -309,6 +330,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/c")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } @@ -328,8 +361,8 @@ pub mod d { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -337,7 +370,6 @@ pub mod d { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("d")?; inst.func_wrap_concurrent( "b", move |caller: &wasmtime::component::Accessor, (): ()| { @@ -350,4 +382,16 @@ pub mod d { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("d")?; + add_to_linker_instance(&mut inst, host_getter) + } } 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 0a1b3de7e333..796b6c6c926e 100644 --- a/crates/component-macro/tests/expanded/use-paths_tracing_async.rs +++ b/crates/component-macro/tests/expanded/use-paths_tracing_async.rs @@ -215,8 +215,8 @@ pub mod foo { async move { Host::a(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -224,7 +224,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/a")?; inst.func_wrap_async( "a", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -250,6 +249,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/a")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod b { @@ -273,8 +284,8 @@ pub mod foo { async move { Host::a(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -282,7 +293,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/b")?; inst.func_wrap_async( "a", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -308,6 +318,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/b")?; + add_to_linker_instance(&mut inst, host_getter) + } } #[allow(clippy::all)] pub mod c { @@ -331,8 +353,8 @@ pub mod foo { async move { Host::a(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -340,7 +362,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/c")?; inst.func_wrap_async( "a", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -366,6 +387,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/c")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } @@ -391,8 +424,8 @@ pub mod d { async move { Host::b(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -400,7 +433,6 @@ pub mod d { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("d")?; inst.func_wrap_async( "b", move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| { @@ -426,4 +458,16 @@ pub mod d { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("d")?; + add_to_linker_instance(&mut inst, host_getter) + } } diff --git a/crates/component-macro/tests/expanded/variants.rs b/crates/component-macro/tests/expanded/variants.rs index 307e68f46f41..926e9f987e28 100644 --- a/crates/component-macro/tests/expanded/variants.rs +++ b/crates/component-macro/tests/expanded/variants.rs @@ -648,8 +648,8 @@ pub mod foo { Host::is_clone_return(*self) } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -657,7 +657,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static, { - let mut inst = linker.instance("foo:foo/variants")?; inst.func_wrap( "e1-arg", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (E1,)| { @@ -889,6 +888,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + let mut inst = linker.instance("foo:foo/variants")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/variants_async.rs b/crates/component-macro/tests/expanded/variants_async.rs index 942c27cee235..110a48a1cd17 100644 --- a/crates/component-macro/tests/expanded/variants_async.rs +++ b/crates/component-macro/tests/expanded/variants_async.rs @@ -736,8 +736,8 @@ pub mod foo { async move { Host::is_clone_return(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -745,7 +745,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/variants")?; inst.func_wrap_async( "e1-arg", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (E1,)| { @@ -1020,6 +1019,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/variants")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/variants_concurrent.rs b/crates/component-macro/tests/expanded/variants_concurrent.rs index b7959a200f30..7d46b403525a 100644 --- a/crates/component-macro/tests/expanded/variants_concurrent.rs +++ b/crates/component-macro/tests/expanded/variants_concurrent.rs @@ -579,8 +579,8 @@ pub mod foo { } pub trait Host: Send {} impl<_T: Host + ?Sized + Send> Host for &mut _T {} - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -588,7 +588,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/variants")?; inst.func_wrap_concurrent( "e1-arg", move |caller: &wasmtime::component::Accessor, (arg0,): (E1,)| { @@ -874,6 +873,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/variants")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/variants_tracing_async.rs b/crates/component-macro/tests/expanded/variants_tracing_async.rs index b51de12b4207..08c730eb5ee1 100644 --- a/crates/component-macro/tests/expanded/variants_tracing_async.rs +++ b/crates/component-macro/tests/expanded/variants_tracing_async.rs @@ -736,8 +736,8 @@ pub mod foo { async move { Host::is_clone_return(*self).await } } } - pub fn add_to_linker( - linker: &mut wasmtime::component::Linker, + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, host_getter: fn(&mut T) -> D::Data<'_>, ) -> wasmtime::Result<()> where @@ -745,7 +745,6 @@ pub mod foo { for<'a> D::Data<'a>: Host, T: 'static + Send, { - let mut inst = linker.instance("foo:foo/variants")?; inst.func_wrap_async( "e1-arg", move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,): (E1,)| { @@ -1318,6 +1317,18 @@ pub mod foo { )?; Ok(()) } + pub fn add_to_linker( + linker: &mut wasmtime::component::Linker, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static + Send, + { + let mut inst = linker.instance("foo:foo/variants")?; + add_to_linker_instance(&mut inst, host_getter) + } } } } diff --git a/crates/component-macro/tests/expanded/worlds-with-types.rs b/crates/component-macro/tests/expanded/worlds-with-types.rs index 4e212025a442..19041eaaab16 100644 --- a/crates/component-macro/tests/expanded/worlds-with-types.rs +++ b/crates/component-macro/tests/expanded/worlds-with-types.rs @@ -249,6 +249,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -259,7 +270,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/i")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } 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 0215a6561930..796067c36cca 100644 --- a/crates/component-macro/tests/expanded/worlds-with-types_async.rs +++ b/crates/component-macro/tests/expanded/worlds-with-types_async.rs @@ -252,6 +252,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -262,7 +273,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/i")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs b/crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs index ca8776030b4a..01b7a15cd800 100644 --- a/crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs +++ b/crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs @@ -253,6 +253,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -263,7 +274,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/i")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } 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 ae5286ec1b3b..25d4f4dca61b 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 @@ -260,6 +260,17 @@ pub mod foo { {} pub trait Host {} impl<_T: Host + ?Sized> Host for &mut _T {} + pub fn add_to_linker_instance( + inst: &mut wasmtime::component::LinkerInstance<'_, T>, + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> wasmtime::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: Host, + T: 'static, + { + Ok(()) + } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, host_getter: fn(&mut T) -> D::Data<'_>, @@ -270,7 +281,7 @@ pub mod foo { T: 'static, { let mut inst = linker.instance("foo:foo/i")?; - Ok(()) + add_to_linker_instance(&mut inst, host_getter) } } } diff --git a/crates/wit-bindgen/src/lib.rs b/crates/wit-bindgen/src/lib.rs index 609eb7d7eb2c..72c50c8586ed 100644 --- a/crates/wit-bindgen/src/lib.rs +++ b/crates/wit-bindgen/src/lib.rs @@ -68,13 +68,14 @@ struct Wasmtime { src: Source, opts: Opts, /// A list of all interfaces which were imported by this world. - import_interfaces: Vec, - import_functions: Vec, + import_interfaces: IndexMap, + world_import_functions: Vec, + world_implements_interfaces: Vec<(String, InterfaceId)>, + interfaces_for_implements: HashMap, exports: Exports, types: Types, sizes: SizeAlign, interface_names: HashMap, - interface_last_seen_as_import: HashMap, trappable_errors: IndexMap, // Track the with options that were used. Remapped interfaces provided via `with` // are required to be used. @@ -84,10 +85,10 @@ struct Wasmtime { } struct ImportInterface { - id: InterfaceId, contents: String, name: InterfaceName, all_func_flags: FunctionFlags, + in_world: bool, } #[derive(Default)] @@ -201,7 +202,8 @@ pub struct TrappableError { } impl Opts { - pub fn generate(&self, resolve: &Resolve, world: WorldId) -> anyhow::Result { + pub fn generate(&self, resolve: &mut Resolve, world: WorldId) -> anyhow::Result { + resolve.generate_nominal_type_ids(world); // TODO: Should we refine this test to inspect only types reachable from // the specified world? if !cfg!(feature = "component-model-async") @@ -272,7 +274,8 @@ impl Wasmtime { }; let remapped = matches!(entry, InterfaceName::Remapped { .. }); - self.interface_names.insert(id, entry); + let prev = self.interface_names.insert(id, entry); + assert!(prev.is_none()); remapped } @@ -400,89 +403,35 @@ impl Wasmtime { } fn import(&mut self, resolve: &Resolve, name: &WorldKey, item: &WorldItem) { - let mut generator = InterfaceGenerator::new(self, resolve); match item { WorldItem::Function(func) => { - self.import_functions.push(func.clone()); + self.world_import_functions.push(func.clone()); } WorldItem::Interface { id, .. } => { - generator - .generator - .interface_last_seen_as_import - .insert(*id, true); - generator.current_interface = Some((*id, name, false)); - let snake = to_rust_ident(&match name { - WorldKey::Name(s) => s.to_snake_case(), - WorldKey::Interface(id) => resolve.interfaces[*id] - .name - .as_ref() - .unwrap() - .to_snake_case(), - }); - let module = if generator - .generator - .name_interface(resolve, *id, name, false) + if let WorldKey::Name(kebab) = name + && resolve.interfaces[*id].name.is_some() { - // If this interface is remapped then that means that it was - // provided via the `with` key in the bindgen configuration. - // That means that bindings generation is skipped here. To - // accommodate future bindgens depending on this bindgen - // though we still generate a module which reexports the - // original module. This helps maintain the same output - // structure regardless of whether `with` is used. - let name_at_root = match &generator.generator.interface_names[id] { - InterfaceName::Remapped { name_at_root, .. } => name_at_root, - InterfaceName::Path(_) => unreachable!(), + let og_interface = resolve.interfaces[*id].clone_of.unwrap_or(*id); + let implements = match self.interfaces_for_implements.get(&og_interface) { + Some(id) => *id, + None => { + self.interfaces_for_implements.insert(og_interface, *id); + self.import_interface(resolve, &WorldKey::Interface(*id), *id, false); + *id + } }; - let path_to_root = generator.path_to_root(); - format!( - " - pub mod {snake} {{ - #[allow(unused_imports)] - pub use {path_to_root}{name_at_root}::*; - }} - " - ) + self.world_implements_interfaces + .push((kebab.to_string(), implements)); } else { - // If this interface is not remapped then it's time to - // actually generate bindings here. - generator.generator.interface_link_options[id].write_struct(&mut generator.src); - generator.types(*id); - let key_name = resolve.name_world_key(name); - generator.generate_add_to_linker(*id, &key_name); - - let module = &generator.src[..]; - let wt = generator.generator.wasmtime_path(); - - format!( - " - #[allow(clippy::all)] - pub mod {snake} {{ - #[allow(unused_imports)] - use {wt}::component::__internal::Box; - - {module} - }} - " - ) - }; - let all_func_flags = generator.all_func_flags; - self.import_interfaces.push(ImportInterface { - id: *id, - contents: module, - name: self.interface_names[id].clone(), - all_func_flags, - }); - - let interface_path = self.import_interface_path(id); - self.interface_link_options[id] - .write_impl_from_world(&mut self.src, &interface_path); + self.import_interface(resolve, name, *id, true); + } } WorldItem::Type { id, .. } => { let name = match name { WorldKey::Name(name) => name, WorldKey::Interface(_) => unreachable!(), }; + let mut generator = InterfaceGenerator::new(self, resolve); generator.define_type(name, *id); let body = mem::take(&mut generator.src); self.src.push_str(&body); @@ -490,6 +439,84 @@ impl Wasmtime { }; } + fn import_interface( + &mut self, + resolve: &Resolve, + name: &WorldKey, + id: InterfaceId, + in_world: bool, + ) { + let mut generator = InterfaceGenerator::new(self, resolve); + + generator.current_interface = Some((id, name, false)); + let snake = to_rust_ident(&match name { + WorldKey::Name(s) => s.to_snake_case(), + WorldKey::Interface(id) => resolve.interfaces[*id] + .name + .as_ref() + .unwrap() + .to_snake_case(), + }); + let module = if generator.generator.name_interface(resolve, id, name, false) { + // If this interface is remapped then that means that it was + // provided via the `with` key in the bindgen configuration. + // That means that bindings generation is skipped here. To + // accommodate future bindgens depending on this bindgen + // though we still generate a module which reexports the + // original module. This helps maintain the same output + // structure regardless of whether `with` is used. + let name_at_root = match &generator.generator.interface_names[&id] { + InterfaceName::Remapped { name_at_root, .. } => name_at_root, + InterfaceName::Path(_) => unreachable!(), + }; + let path_to_root = generator.path_to_root(); + format!( + " + pub mod {snake} {{ + #[allow(unused_imports)] + pub use {path_to_root}{name_at_root}::*; + }} + " + ) + } else { + // If this interface is not remapped then it's time to + // actually generate bindings here. + generator.generator.interface_link_options[&id].write_struct(&mut generator.src); + generator.types(id); + let key_name = resolve.name_world_key(name); + generator.generate_add_to_linker(id, &key_name); + + let module = &generator.src[..]; + let wt = generator.generator.wasmtime_path(); + + format!( + " + #[allow(clippy::all)] + pub mod {snake} {{ + #[allow(unused_imports)] + use {wt}::component::__internal::Box; + + {module} + }} + " + ) + }; + let all_func_flags = generator.all_func_flags; + let prev = self.import_interfaces.insert( + id, + ImportInterface { + contents: module, + name: self.interface_names[&id].clone(), + all_func_flags, + in_world, + }, + ); + assert!(prev.is_none()); + + let interface_path = self.import_interface_path(&id); + self.interface_link_options[&id].write_impl_from_world(&mut self.src, &interface_path); + } + fn export(&mut self, resolve: &Resolve, name: &WorldKey, item: &WorldItem) { let wt = self.wasmtime_path(); let mut generator = InterfaceGenerator::new(self, resolve); @@ -532,10 +559,6 @@ impl Wasmtime { } WorldItem::Type { .. } => unreachable!(), WorldItem::Interface { id, .. } => { - generator - .generator - .interface_last_seen_as_import - .insert(*id, false); generator.generator.name_interface(resolve, *id, name, true); generator.current_interface = Some((*id, name, true)); generator.types(*id); @@ -1016,7 +1039,7 @@ impl<_T: Send + 'static> {camel}Pre<_T> {{ self.emit_modules( imports .into_iter() - .map(|i| (i.id, i.contents, i.name)) + .map(|(id, i)| (id, i.contents, i.name)) .collect(), ); @@ -1275,7 +1298,7 @@ fn lookup_keys( impl Wasmtime { fn has_world_imports_trait(&self, resolve: &Resolve, world: WorldId) -> bool { - !self.import_functions.is_empty() || get_world_resources(resolve, world).count() > 0 + !self.world_import_functions.is_empty() || get_world_resources(resolve, world).count() > 0 } fn world_imports_trait(&mut self, resolve: &Resolve, world: WorldId) -> Option { @@ -1285,7 +1308,7 @@ impl Wasmtime { let world_camel = to_rust_upper_camel_case(&resolve.worlds[world].name); - let functions = self.import_functions.clone(); + let functions = self.world_import_functions.clone(); let mut generator = InterfaceGenerator::new(self, resolve); let generated_trait = generator.generate_trait( &format!("{world_camel}Imports"), @@ -1301,16 +1324,17 @@ impl Wasmtime { Some(generated_trait) } - fn import_interface_paths(&self) -> Vec<(InterfaceId, String)> { + fn import_interface_paths(&self) -> Vec<(InterfaceId, String, Option)> { self.import_interfaces .iter() - .map(|i| { - let path = match &i.name { - InterfaceName::Path(path) => path.join("::"), - InterfaceName::Remapped { name_at_root, .. } => name_at_root.clone(), - }; - (i.id, path) - }) + .filter(|(_, i)| i.in_world) + .map(|(id, _)| (*id, None)) + .chain( + self.world_implements_interfaces + .iter() + .map(|(name, id)| (*id, Some(name.clone()))), + ) + .map(|(id, name_override)| (id, self.import_interface_path(&id), name_override)) .collect() } @@ -1322,14 +1346,7 @@ impl Wasmtime { } fn import_interface_all_func_flags(&self, id: InterfaceId) -> FunctionFlags { - for i in self.import_interfaces.iter() { - if id != i.id { - continue; - } - - return i.all_func_flags; - } - unreachable!() + self.import_interfaces[&id].all_func_flags } fn world_host_traits( @@ -1340,7 +1357,7 @@ impl Wasmtime { let mut without_store_async = false; let mut with_store = Vec::new(); let mut with_store_async = false; - for (id, path) in self.import_interface_paths() { + for (id, path, _) in self.import_interface_paths() { without_store.push(format!("{path}::Host")); let flags = self.import_interface_all_func_flags(id); without_store_async = without_store_async || flags.contains(FunctionFlags::ASYNC); @@ -1394,7 +1411,7 @@ impl Wasmtime { if let Some(world_trait) = world_trait { all_func_flags |= world_trait.all_func_flags; } - for i in self.import_interfaces.iter() { + for i in self.import_interfaces.values() { all_func_flags |= i.all_func_flags; } @@ -1435,7 +1452,7 @@ impl Wasmtime { for (ty, _name) in get_world_resources(resolve, world) { self.generate_add_resource_to_linker(None, None, "linker", resolve, ty); } - for f in self.import_functions.clone() { + for f in self.world_import_functions.clone() { let mut generator = InterfaceGenerator::new(self, resolve); generator.generate_add_function_to_linker(TypeOwner::World(world), &f, "linker"); let src = String::from(generator.src); @@ -1477,7 +1494,7 @@ impl Wasmtime { "Self::add_to_linker_imports::(linker {options_arg}, host_getter)?;" ); } - for (interface_id, path) in self.import_interface_paths() { + for (interface_id, path, name_override) in self.import_interface_paths() { let options_arg = if self.interface_link_options[&interface_id].has_any() { ", &options.into()" } else { @@ -1497,10 +1514,26 @@ impl Wasmtime { .unwrap_or(Stability::Unknown); let gate = FeatureGate::open(&mut self.src, &import_stability); - uwriteln!( - self.src, - "{path}::add_to_linker::(linker {options_arg}, host_getter)?;" - ); + match &name_override { + Some(name) => { + uwriteln!( + self.src, + "{path}::add_to_linker_instance::( + &mut linker.instance({name:?})? + {options_arg}, + host_getter, + )?;" + ); + } + None => { + uwriteln!( + self.src, + "{path}::add_to_linker::( + linker {options_arg}, host_getter, + )?;" + ); + } + } gate.close(&mut self.src); } gate.close(&mut self.src); @@ -2362,12 +2395,17 @@ impl<'a> InterfaceGenerator<'a> { } else { "" }; + let options_param_forward = if self.generator.interface_link_options[&id].has_any() { + "options," + } else { + "" + }; uwriteln!( self.src, " - pub fn add_to_linker( - linker: &mut {wt}::component::Linker, + pub fn add_to_linker_instance( + inst: &mut {wt}::component::LinkerInstance<'_, T>, {options_param} host_getter: fn(&mut T) -> D::Data<'_>, ) -> {wt}::Result<()> @@ -2380,8 +2418,6 @@ impl<'a> InterfaceGenerator<'a> { ); let gate = FeatureGate::open(&mut self.src, &iface.stability); - uwriteln!(self.src, "let mut inst = linker.instance(\"{name}\")?;"); - for (ty, _name) in get_resources(self.resolve, id) { self.generator.generate_add_resource_to_linker( self.current_interface.map(|p| p.1), @@ -2398,6 +2434,25 @@ impl<'a> InterfaceGenerator<'a> { gate.close(&mut self.src); uwriteln!(self.src, "Ok(())"); uwriteln!(self.src, "}}"); + + uwriteln!( + self.src, + " + pub fn add_to_linker( + linker: &mut {wt}::component::Linker, + {options_param} + host_getter: fn(&mut T) -> D::Data<'_>, + ) -> {wt}::Result<()> + where + D: HostWithStore, + for<'a> D::Data<'a>: {sync_bounds}, + T: 'static {opt_t_send_bound}, + {{ + let mut inst = linker.instance(\"{name}\")?; + add_to_linker_instance(&mut inst, {options_param_forward} host_getter) + }} + " + ); } fn import_resource_drop_flags(&mut self, name: &str) -> FunctionFlags { @@ -3207,7 +3262,12 @@ impl<'a> RustGenerator<'a> for InterfaceGenerator<'a> { } fn is_imported_interface(&self, interface: InterfaceId) -> bool { - self.generator.interface_last_seen_as_import[&interface] + if let Some((cur, _, is_export)) = self.current_interface { + if cur == interface { + return !is_export; + } + } + self.generator.import_interfaces.contains_key(&interface) } fn wasmtime_path(&self) -> String { diff --git a/tests/all/component_model/bindgen.rs b/tests/all/component_model/bindgen.rs index 5df31dc32dd9..70a9ba238d97 100644 --- a/tests/all/component_model/bindgen.rs +++ b/tests/all/component_model/bindgen.rs @@ -1001,3 +1001,99 @@ mod anyhow_errors { Ok(()) } } + +mod implements { + use super::*; + use std::collections::HashMap; + use wasmtime::component::HasSelf; + + wasmtime::component::bindgen!({ + inline: " + package demo:pkg; + + interface store { + get: func(key: string) -> option; + set: func(key: string, value: string); + } + + world cache { + import hot-cache: store; + import durable: store; + } + ", + }); + + const DUMMY: &str = r#" + (component + (import "hot-cache" (instance $hot + (export "get" (func (param "key" string) (result (option string)))) + (export "set" (func (param "key" string) (param "value" string))) + )) + (import "durable" (instance $dur + (export "get" (func (param "key" string) (result (option string)))) + (export "set" (func (param "key" string) (param "value" string))) + )) + ) + "#; + + #[test] + fn add_to_linker_can_instantiate() -> Result<()> { + #[derive(Default)] + struct MyHost { + kv: HashMap, + } + + impl demo::pkg::store::Host for MyHost { + fn get(&mut self, key: String) -> Option { + self.kv.get(&key).cloned() + } + + fn set(&mut self, key: String, value: String) { + self.kv.insert(key, value); + } + } + + let engine = engine(); + let mut linker = Linker::new(&engine); + Cache::add_to_linker::<_, HasSelf>(&mut linker, |s| s)?; + let component = Component::new(&engine, DUMMY)?; + let mut store = Store::new(&engine, MyHost::default()); + let _instance = Cache::instantiate(&mut store, &component, &linker)?; + Ok(()) + } + + impl demo::pkg::store::Host for HashMap { + fn get(&mut self, key: String) -> Option { + HashMap::get(self, &key).cloned() + } + + fn set(&mut self, key: String, value: String) { + self.insert(key, value); + } + } + + #[test] + fn add_to_linker_instance_can_instantiate() -> Result<()> { + #[derive(Default)] + struct MyHost { + hot_cache: HashMap, + durable: HashMap, + } + + let engine = engine(); + let mut linker = Linker::new(&engine); + demo::pkg::store::add_to_linker_instance::>>( + &mut linker.instance("hot-cache")?, + |s| &mut s.hot_cache, + )?; + demo::pkg::store::add_to_linker_instance::>>( + &mut linker.instance("durable")?, + |s| &mut s.durable, + )?; + let component = Component::new(&engine, DUMMY)?; + let mut store = Store::new(&engine, MyHost::default()); + let _pre = linker.instantiate_pre(&component)?; + let _ = _pre.instantiate(&mut store)?; + Ok(()) + } +}