diff --git a/crates/component-macro/tests/expanded/char.rs b/crates/component-macro/tests/expanded/char.rs index 62f8f480f471..777df2d88fd6 100644 --- a/crates/component-macro/tests/expanded/char.rs +++ b/crates/component-macro/tests/expanded/char.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,29 +100,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::chars::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::chars::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::chars::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -140,6 +118,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -149,21 +128,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -279,45 +258,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/chars") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/chars`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/chars") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/chars`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/chars` does \ @@ -338,9 +293,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let take_char = *_instance .get_typed_func::<(char,), ()>(&mut store, &self.take_char)? .func(); diff --git a/crates/component-macro/tests/expanded/char_async.rs b/crates/component-macro/tests/expanded/char_async.rs index 7146c887e979..e92debe479b1 100644 --- a/crates/component-macro/tests/expanded/char_async.rs +++ b/crates/component-macro/tests/expanded/char_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::chars::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::chars::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::chars::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -295,45 +274,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/chars") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/chars`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/chars") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/chars`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/chars` does \ @@ -354,9 +309,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let take_char = *_instance .get_typed_func::<(char,), ()>(&mut store, &self.take_char)? .func(); diff --git a/crates/component-macro/tests/expanded/char_concurrent.rs b/crates/component-macro/tests/expanded/char_concurrent.rs index b3dd147ab2cf..814bb2048e2d 100644 --- a/crates/component-macro/tests/expanded/char_concurrent.rs +++ b/crates/component-macro/tests/expanded/char_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::chars::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::chars::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::chars::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -372,45 +351,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/chars") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/chars`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/chars") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/chars`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/chars` does \ @@ -431,9 +386,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let take_char = *_instance .get_typed_func::<(char,), ()>(&mut store, &self.take_char)? .func(); diff --git a/crates/component-macro/tests/expanded/char_tracing_async.rs b/crates/component-macro/tests/expanded/char_tracing_async.rs index 795de69c9e74..fb4e45d95922 100644 --- a/crates/component-macro/tests/expanded/char_tracing_async.rs +++ b/crates/component-macro/tests/expanded/char_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::chars::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::chars::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::chars::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -324,45 +303,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/chars") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/chars`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/chars") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/chars`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/chars` does \ @@ -383,9 +338,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let take_char = *_instance .get_typed_func::<(char,), ()>(&mut store, &self.take_char)? .func(); diff --git a/crates/component-macro/tests/expanded/conventions.rs b/crates/component-macro/tests/expanded/conventions.rs index 1c220e7fbcf3..07aa1563d7b7 100644 --- a/crates/component-macro/tests/expanded/conventions.rs +++ b/crates/component-macro/tests/expanded/conventions.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,30 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::conventions::GuestIndices::new( - _component, - )?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::conventions::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -142,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -151,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -499,45 +478,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/conventions") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/conventions`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/conventions") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/conventions`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/conventions` does \ @@ -580,9 +535,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let kebab_case = *_instance .get_typed_func::<(), ()>(&mut store, &self.kebab_case)? .func(); diff --git a/crates/component-macro/tests/expanded/conventions_async.rs b/crates/component-macro/tests/expanded/conventions_async.rs index 586dbd5f2556..4f1a374bc7d3 100644 --- a/crates/component-macro/tests/expanded/conventions_async.rs +++ b/crates/component-macro/tests/expanded/conventions_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::conventions::GuestIndices::new( - _component, - )?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::conventions::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -145,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -154,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -164,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -535,45 +514,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/conventions") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/conventions`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/conventions") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/conventions`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/conventions` does \ @@ -616,9 +571,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let kebab_case = *_instance .get_typed_func::<(), ()>(&mut store, &self.kebab_case)? .func(); diff --git a/crates/component-macro/tests/expanded/conventions_concurrent.rs b/crates/component-macro/tests/expanded/conventions_concurrent.rs index 528d15eddb96..74768a38df2c 100644 --- a/crates/component-macro/tests/expanded/conventions_concurrent.rs +++ b/crates/component-macro/tests/expanded/conventions_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::conventions::GuestIndices::new( - _component, - )?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::conventions::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -145,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -154,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -164,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -984,45 +963,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/conventions") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/conventions`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/conventions") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/conventions`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/conventions` does \ @@ -1065,9 +1020,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let kebab_case = *_instance .get_typed_func::<(), ()>(&mut store, &self.kebab_case)? .func(); diff --git a/crates/component-macro/tests/expanded/conventions_tracing_async.rs b/crates/component-macro/tests/expanded/conventions_tracing_async.rs index 368fa2b1c01d..2a8687cdac43 100644 --- a/crates/component-macro/tests/expanded/conventions_tracing_async.rs +++ b/crates/component-macro/tests/expanded/conventions_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::conventions::GuestIndices::new( - _component, - )?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::conventions::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -145,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -154,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -164,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -695,45 +674,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/conventions") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/conventions`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/conventions") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/conventions`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/conventions` does \ @@ -776,9 +731,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let kebab_case = *_instance .get_typed_func::<(), ()>(&mut store, &self.kebab_case)? .func(); diff --git a/crates/component-macro/tests/expanded/dead-code.rs b/crates/component-macro/tests/expanded/dead-code.rs index 02180de797f9..4f1b0875cc14 100644 --- a/crates/component-macro/tests/expanded/dead-code.rs +++ b/crates/component-macro/tests/expanded/dead-code.rs @@ -27,7 +27,7 @@ impl<_T> ImportsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = ImportsIndices::new(instance_pre.component())?; + let indices = ImportsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct ImportsIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Imports::new`]. /// -/// * You can also access the guts of instantiation through -/// [`ImportsIndices::new_instance`] followed -/// by [`ImportsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -101,24 +96,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(ImportsIndices {}) - } - /// Creates a new instance of [`ImportsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Imports`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Imports`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(ImportsIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -131,6 +113,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Imports {}) } @@ -139,21 +122,21 @@ const _: () = { /// Convenience wrapper around [`ImportsPre::new`] and /// [`ImportsPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; ImportsPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`ImportsIndices::new_instance`] and + /// Convenience wrapper around [`ImportsIndices::new`] and /// [`ImportsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = ImportsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = ImportsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/dead-code_async.rs b/crates/component-macro/tests/expanded/dead-code_async.rs index 83045e481901..ae86d4147030 100644 --- a/crates/component-macro/tests/expanded/dead-code_async.rs +++ b/crates/component-macro/tests/expanded/dead-code_async.rs @@ -27,7 +27,7 @@ impl<_T> ImportsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = ImportsIndices::new(instance_pre.component())?; + let indices = ImportsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct ImportsIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Imports::new`]. /// -/// * You can also access the guts of instantiation through -/// [`ImportsIndices::new_instance`] followed -/// by [`ImportsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(ImportsIndices {}) - } - /// Creates a new instance of [`ImportsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Imports`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Imports`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(ImportsIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Imports {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`ImportsPre::new`] and /// [`ImportsPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; ImportsPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`ImportsIndices::new_instance`] and + /// Convenience wrapper around [`ImportsIndices::new`] and /// [`ImportsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = ImportsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = ImportsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/dead-code_concurrent.rs b/crates/component-macro/tests/expanded/dead-code_concurrent.rs index a5c1352ab196..c04c48a5a114 100644 --- a/crates/component-macro/tests/expanded/dead-code_concurrent.rs +++ b/crates/component-macro/tests/expanded/dead-code_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> ImportsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = ImportsIndices::new(instance_pre.component())?; + let indices = ImportsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct ImportsIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Imports::new`]. /// -/// * You can also access the guts of instantiation through -/// [`ImportsIndices::new_instance`] followed -/// by [`ImportsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(ImportsIndices {}) - } - /// Creates a new instance of [`ImportsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Imports`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Imports`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(ImportsIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Imports {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`ImportsPre::new`] and /// [`ImportsPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; ImportsPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`ImportsIndices::new_instance`] and + /// Convenience wrapper around [`ImportsIndices::new`] and /// [`ImportsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = ImportsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = ImportsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, 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 023fe0456d64..cbe719f5f439 100644 --- a/crates/component-macro/tests/expanded/dead-code_tracing_async.rs +++ b/crates/component-macro/tests/expanded/dead-code_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> ImportsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = ImportsIndices::new(instance_pre.component())?; + let indices = ImportsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct ImportsIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Imports::new`]. /// -/// * You can also access the guts of instantiation through -/// [`ImportsIndices::new_instance`] followed -/// by [`ImportsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(ImportsIndices {}) - } - /// Creates a new instance of [`ImportsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Imports`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Imports`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(ImportsIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Imports {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`ImportsPre::new`] and /// [`ImportsPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; ImportsPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`ImportsIndices::new_instance`] and + /// Convenience wrapper around [`ImportsIndices::new`] and /// [`ImportsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = ImportsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = ImportsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/direct-import.rs b/crates/component-macro/tests/expanded/direct-import.rs index aa46750e55d5..aee4af6880f5 100644 --- a/crates/component-macro/tests/expanded/direct-import.rs +++ b/crates/component-macro/tests/expanded/direct-import.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct FooIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -122,24 +117,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(FooIndices {}) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(FooIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -152,6 +134,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Foo {}) } @@ -160,21 +143,21 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/direct-import_async.rs b/crates/component-macro/tests/expanded/direct-import_async.rs index cb94824920e3..9d535f0f1a02 100644 --- a/crates/component-macro/tests/expanded/direct-import_async.rs +++ b/crates/component-macro/tests/expanded/direct-import_async.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct FooIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -126,24 +121,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(FooIndices {}) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(FooIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -156,6 +138,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Foo {}) } @@ -164,7 +147,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -174,14 +157,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/direct-import_concurrent.rs b/crates/component-macro/tests/expanded/direct-import_concurrent.rs index 56caaac656ed..8c76029f7ab6 100644 --- a/crates/component-macro/tests/expanded/direct-import_concurrent.rs +++ b/crates/component-macro/tests/expanded/direct-import_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct FooIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -144,24 +139,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(FooIndices {}) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(FooIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -174,6 +156,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Foo {}) } @@ -182,7 +165,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -192,14 +175,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/direct-import_tracing_async.rs b/crates/component-macro/tests/expanded/direct-import_tracing_async.rs index fac78c56f736..2a588c222a5e 100644 --- a/crates/component-macro/tests/expanded/direct-import_tracing_async.rs +++ b/crates/component-macro/tests/expanded/direct-import_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct FooIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -126,24 +121,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(FooIndices {}) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(FooIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -156,6 +138,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Foo {}) } @@ -164,7 +147,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -174,14 +157,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/empty.rs b/crates/component-macro/tests/expanded/empty.rs index 573b02c84e74..51705a4d8819 100644 --- a/crates/component-macro/tests/expanded/empty.rs +++ b/crates/component-macro/tests/expanded/empty.rs @@ -27,7 +27,7 @@ impl<_T> EmptyPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = EmptyIndices::new(instance_pre.component())?; + let indices = EmptyIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct EmptyIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Empty::new`]. /// -/// * You can also access the guts of instantiation through -/// [`EmptyIndices::new_instance`] followed -/// by [`EmptyIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -101,24 +96,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(EmptyIndices {}) - } - /// Creates a new instance of [`EmptyIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Empty`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Empty`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(EmptyIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -131,6 +113,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Empty {}) } @@ -139,21 +122,21 @@ const _: () = { /// Convenience wrapper around [`EmptyPre::new`] and /// [`EmptyPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; EmptyPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`EmptyIndices::new_instance`] and + /// Convenience wrapper around [`EmptyIndices::new`] and /// [`EmptyIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = EmptyIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = EmptyIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } } }; diff --git a/crates/component-macro/tests/expanded/empty_async.rs b/crates/component-macro/tests/expanded/empty_async.rs index d2eea87b72a0..392e9f0c130a 100644 --- a/crates/component-macro/tests/expanded/empty_async.rs +++ b/crates/component-macro/tests/expanded/empty_async.rs @@ -27,7 +27,7 @@ impl<_T> EmptyPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = EmptyIndices::new(instance_pre.component())?; + let indices = EmptyIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct EmptyIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Empty::new`]. /// -/// * You can also access the guts of instantiation through -/// [`EmptyIndices::new_instance`] followed -/// by [`EmptyIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(EmptyIndices {}) - } - /// Creates a new instance of [`EmptyIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Empty`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Empty`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(EmptyIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Empty {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`EmptyPre::new`] and /// [`EmptyPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; EmptyPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`EmptyIndices::new_instance`] and + /// Convenience wrapper around [`EmptyIndices::new`] and /// [`EmptyIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = EmptyIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = EmptyIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } } }; diff --git a/crates/component-macro/tests/expanded/empty_concurrent.rs b/crates/component-macro/tests/expanded/empty_concurrent.rs index fd5817a596b0..32a758e6e393 100644 --- a/crates/component-macro/tests/expanded/empty_concurrent.rs +++ b/crates/component-macro/tests/expanded/empty_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> EmptyPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = EmptyIndices::new(instance_pre.component())?; + let indices = EmptyIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct EmptyIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Empty::new`]. /// -/// * You can also access the guts of instantiation through -/// [`EmptyIndices::new_instance`] followed -/// by [`EmptyIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(EmptyIndices {}) - } - /// Creates a new instance of [`EmptyIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Empty`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Empty`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(EmptyIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Empty {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`EmptyPre::new`] and /// [`EmptyPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; EmptyPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`EmptyIndices::new_instance`] and + /// Convenience wrapper around [`EmptyIndices::new`] and /// [`EmptyIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = EmptyIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = EmptyIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } } }; diff --git a/crates/component-macro/tests/expanded/empty_tracing_async.rs b/crates/component-macro/tests/expanded/empty_tracing_async.rs index d2eea87b72a0..392e9f0c130a 100644 --- a/crates/component-macro/tests/expanded/empty_tracing_async.rs +++ b/crates/component-macro/tests/expanded/empty_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> EmptyPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = EmptyIndices::new(instance_pre.component())?; + let indices = EmptyIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct EmptyIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Empty::new`]. /// -/// * You can also access the guts of instantiation through -/// [`EmptyIndices::new_instance`] followed -/// by [`EmptyIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(EmptyIndices {}) - } - /// Creates a new instance of [`EmptyIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Empty`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Empty`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(EmptyIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Empty {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`EmptyPre::new`] and /// [`EmptyPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; EmptyPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`EmptyIndices::new_instance`] and + /// Convenience wrapper around [`EmptyIndices::new`] and /// [`EmptyIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = EmptyIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = EmptyIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } } }; diff --git a/crates/component-macro/tests/expanded/flags.rs b/crates/component-macro/tests/expanded/flags.rs index c1be2c70fed7..1f9bffc7aecf 100644 --- a/crates/component-macro/tests/expanded/flags.rs +++ b/crates/component-macro/tests/expanded/flags.rs @@ -27,7 +27,7 @@ impl<_T> TheFlagsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheFlagsIndices::new(instance_pre.component())?; + let indices = TheFlagsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheFlagsIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheFlags::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheFlagsIndices::new_instance`] followed -/// by [`TheFlagsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,29 +100,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::flegs::GuestIndices::new(_component)?; - Ok(TheFlagsIndices { interface0 }) - } - /// Creates a new instance of [`TheFlagsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheFlags`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheFlags`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::flegs::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::flegs::GuestIndices::new(_instance_pre)?; Ok(TheFlagsIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -140,6 +118,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheFlags { interface0 }) @@ -149,21 +128,21 @@ const _: () = { /// Convenience wrapper around [`TheFlagsPre::new`] and /// [`TheFlagsPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheFlagsPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheFlagsIndices::new_instance`] and + /// Convenience wrapper around [`TheFlagsIndices::new`] and /// [`TheFlagsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheFlagsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheFlagsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -626,45 +605,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/flegs") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/flegs`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/flegs") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/flegs`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/flegs` does \ @@ -695,9 +650,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let roundtrip_flag1 = *_instance .get_typed_func::< (Flag1,), diff --git a/crates/component-macro/tests/expanded/flags_async.rs b/crates/component-macro/tests/expanded/flags_async.rs index b355b6d0a2da..fe18958f426e 100644 --- a/crates/component-macro/tests/expanded/flags_async.rs +++ b/crates/component-macro/tests/expanded/flags_async.rs @@ -27,7 +27,7 @@ impl<_T> TheFlagsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheFlagsIndices::new(instance_pre.component())?; + let indices = TheFlagsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheFlagsIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheFlags::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheFlagsIndices::new_instance`] followed -/// by [`TheFlagsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::flegs::GuestIndices::new(_component)?; - Ok(TheFlagsIndices { interface0 }) - } - /// Creates a new instance of [`TheFlagsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheFlags`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheFlags`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::flegs::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::flegs::GuestIndices::new(_instance_pre)?; Ok(TheFlagsIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheFlags { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheFlagsPre::new`] and /// [`TheFlagsPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheFlagsPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheFlagsIndices::new_instance`] and + /// Convenience wrapper around [`TheFlagsIndices::new`] and /// [`TheFlagsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheFlagsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheFlagsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -652,45 +631,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/flegs") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/flegs`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/flegs") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/flegs`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/flegs` does \ @@ -721,9 +676,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let roundtrip_flag1 = *_instance .get_typed_func::< (Flag1,), diff --git a/crates/component-macro/tests/expanded/flags_concurrent.rs b/crates/component-macro/tests/expanded/flags_concurrent.rs index 12b59b24af57..8d40754998b8 100644 --- a/crates/component-macro/tests/expanded/flags_concurrent.rs +++ b/crates/component-macro/tests/expanded/flags_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheFlagsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheFlagsIndices::new(instance_pre.component())?; + let indices = TheFlagsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheFlagsIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheFlags::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheFlagsIndices::new_instance`] followed -/// by [`TheFlagsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::flegs::GuestIndices::new(_component)?; - Ok(TheFlagsIndices { interface0 }) - } - /// Creates a new instance of [`TheFlagsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheFlags`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheFlags`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::flegs::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::flegs::GuestIndices::new(_instance_pre)?; Ok(TheFlagsIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheFlags { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheFlagsPre::new`] and /// [`TheFlagsPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheFlagsPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheFlagsIndices::new_instance`] and + /// Convenience wrapper around [`TheFlagsIndices::new`] and /// [`TheFlagsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheFlagsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheFlagsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -926,45 +905,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/flegs") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/flegs`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/flegs") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/flegs`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/flegs` does \ @@ -995,9 +950,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let roundtrip_flag1 = *_instance .get_typed_func::< (Flag1,), diff --git a/crates/component-macro/tests/expanded/flags_tracing_async.rs b/crates/component-macro/tests/expanded/flags_tracing_async.rs index bb6e5064db40..b0874848781c 100644 --- a/crates/component-macro/tests/expanded/flags_tracing_async.rs +++ b/crates/component-macro/tests/expanded/flags_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheFlagsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheFlagsIndices::new(instance_pre.component())?; + let indices = TheFlagsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheFlagsIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheFlags::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheFlagsIndices::new_instance`] followed -/// by [`TheFlagsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::flegs::GuestIndices::new(_component)?; - Ok(TheFlagsIndices { interface0 }) - } - /// Creates a new instance of [`TheFlagsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheFlags`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheFlags`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::flegs::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::flegs::GuestIndices::new(_instance_pre)?; Ok(TheFlagsIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheFlags { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheFlagsPre::new`] and /// [`TheFlagsPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheFlagsPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheFlagsIndices::new_instance`] and + /// Convenience wrapper around [`TheFlagsIndices::new`] and /// [`TheFlagsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheFlagsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheFlagsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -764,45 +743,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/flegs") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/flegs`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/flegs") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/flegs`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/flegs` does \ @@ -833,9 +788,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let roundtrip_flag1 = *_instance .get_typed_func::< (Flag1,), diff --git a/crates/component-macro/tests/expanded/floats.rs b/crates/component-macro/tests/expanded/floats.rs index 865dfc2ce826..dcfd5a7d1d2e 100644 --- a/crates/component-macro/tests/expanded/floats.rs +++ b/crates/component-macro/tests/expanded/floats.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,28 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::floats::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::floats::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::floats::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -140,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -149,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -300,45 +281,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/floats") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/floats`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/floats") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/floats`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/floats` does \ @@ -363,9 +320,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let f32_param = *_instance .get_typed_func::<(f32,), ()>(&mut store, &self.f32_param)? .func(); diff --git a/crates/component-macro/tests/expanded/floats_async.rs b/crates/component-macro/tests/expanded/floats_async.rs index c729f3cc933a..a8f60c258424 100644 --- a/crates/component-macro/tests/expanded/floats_async.rs +++ b/crates/component-macro/tests/expanded/floats_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::floats::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::floats::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::floats::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -320,45 +301,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/floats") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/floats`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/floats") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/floats`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/floats` does \ @@ -383,9 +340,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let f32_param = *_instance .get_typed_func::<(f32,), ()>(&mut store, &self.f32_param)? .func(); diff --git a/crates/component-macro/tests/expanded/floats_concurrent.rs b/crates/component-macro/tests/expanded/floats_concurrent.rs index 7b0f10c4034b..36cda92eab99 100644 --- a/crates/component-macro/tests/expanded/floats_concurrent.rs +++ b/crates/component-macro/tests/expanded/floats_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::floats::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::floats::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::floats::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -473,45 +454,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/floats") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/floats`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/floats") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/floats`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/floats` does \ @@ -536,9 +493,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let f32_param = *_instance .get_typed_func::<(f32,), ()>(&mut store, &self.f32_param)? .func(); diff --git a/crates/component-macro/tests/expanded/floats_tracing_async.rs b/crates/component-macro/tests/expanded/floats_tracing_async.rs index 735764e45c78..093ddfd6c170 100644 --- a/crates/component-macro/tests/expanded/floats_tracing_async.rs +++ b/crates/component-macro/tests/expanded/floats_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::floats::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::floats::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::floats::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -378,45 +359,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/floats") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/floats`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/floats") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/floats`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/floats` does \ @@ -441,9 +398,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let f32_param = *_instance .get_typed_func::<(f32,), ()>(&mut store, &self.f32_param)? .func(); diff --git a/crates/component-macro/tests/expanded/function-new.rs b/crates/component-macro/tests/expanded/function-new.rs index 70dce6ea6385..235eab4b3ed5 100644 --- a/crates/component-macro/tests/expanded/function-new.rs +++ b/crates/component-macro/tests/expanded/function-new.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,30 +100,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let new = _component - .get_export_index(None, "new") - .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?; - Ok(FooIndices { new }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let new = _instance - .get_export_index(&mut store, None, "new") - .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let new = { + let (item, index) = _component + .get_export(None, "new") + .ok_or_else(|| anyhow::anyhow!("no export `new` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ()>(&_instance_type), + "type-checking export func `new`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `new` is not a function"))?, + } + }; Ok(FooIndices { new }) } /// Uses the indices stored in `self` to load an instance @@ -141,6 +132,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func(); Ok(Foo { new }) @@ -150,21 +142,21 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn call_new( &self, diff --git a/crates/component-macro/tests/expanded/function-new_async.rs b/crates/component-macro/tests/expanded/function-new_async.rs index 70240b5dce07..a954ac63a861 100644 --- a/crates/component-macro/tests/expanded/function-new_async.rs +++ b/crates/component-macro/tests/expanded/function-new_async.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let new = _component - .get_export_index(None, "new") - .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?; - Ok(FooIndices { new }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let new = _instance - .get_export_index(&mut store, None, "new") - .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let new = { + let (item, index) = _component + .get_export(None, "new") + .ok_or_else(|| anyhow::anyhow!("no export `new` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ()>(&_instance_type), + "type-checking export func `new`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `new` is not a function"))?, + } + }; Ok(FooIndices { new }) } /// Uses the indices stored in `self` to load an instance @@ -144,6 +135,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func(); Ok(Foo { new }) @@ -153,7 +145,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -163,14 +155,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub async fn call_new( &self, diff --git a/crates/component-macro/tests/expanded/function-new_concurrent.rs b/crates/component-macro/tests/expanded/function-new_concurrent.rs index 2c4954d9ee50..6eb4364b0ab6 100644 --- a/crates/component-macro/tests/expanded/function-new_concurrent.rs +++ b/crates/component-macro/tests/expanded/function-new_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let new = _component - .get_export_index(None, "new") - .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?; - Ok(FooIndices { new }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let new = _instance - .get_export_index(&mut store, None, "new") - .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let new = { + let (item, index) = _component + .get_export(None, "new") + .ok_or_else(|| anyhow::anyhow!("no export `new` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ()>(&_instance_type), + "type-checking export func `new`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `new` is not a function"))?, + } + }; Ok(FooIndices { new }) } /// Uses the indices stored in `self` to load an instance @@ -144,6 +135,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func(); Ok(Foo { new }) @@ -153,7 +145,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -163,14 +155,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub async fn call_new( &self, diff --git a/crates/component-macro/tests/expanded/function-new_tracing_async.rs b/crates/component-macro/tests/expanded/function-new_tracing_async.rs index 608f8f66c5e9..08ec1ad5341f 100644 --- a/crates/component-macro/tests/expanded/function-new_tracing_async.rs +++ b/crates/component-macro/tests/expanded/function-new_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let new = _component - .get_export_index(None, "new") - .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?; - Ok(FooIndices { new }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let new = _instance - .get_export_index(&mut store, None, "new") - .ok_or_else(|| anyhow::anyhow!("no function export `new` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let new = { + let (item, index) = _component + .get_export(None, "new") + .ok_or_else(|| anyhow::anyhow!("no export `new` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ()>(&_instance_type), + "type-checking export func `new`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `new` is not a function"))?, + } + }; Ok(FooIndices { new }) } /// Uses the indices stored in `self` to load an instance @@ -144,6 +135,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func(); Ok(Foo { new }) @@ -153,7 +145,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -163,14 +155,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub async fn call_new( &self, diff --git a/crates/component-macro/tests/expanded/host-world.rs b/crates/component-macro/tests/expanded/host-world.rs index 7b976fd68428..2e7f389dd44e 100644 --- a/crates/component-macro/tests/expanded/host-world.rs +++ b/crates/component-macro/tests/expanded/host-world.rs @@ -27,7 +27,7 @@ impl<_T> Host_Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Host_Indices::new(instance_pre.component())?; + let indices = Host_Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct Host_Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Host_::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Host_Indices::new_instance`] followed -/// by [`Host_Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -122,24 +117,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Host_Indices {}) - } - /// Creates a new instance of [`Host_Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Host_`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Host_`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Host_Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -152,6 +134,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Host_ {}) } @@ -160,21 +143,21 @@ const _: () = { /// Convenience wrapper around [`Host_Pre::new`] and /// [`Host_Pre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; Host_Pre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`Host_Indices::new_instance`] and + /// Convenience wrapper around [`Host_Indices::new`] and /// [`Host_Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Host_Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Host_Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/host-world_async.rs b/crates/component-macro/tests/expanded/host-world_async.rs index c5485f75a522..b810dc95c51b 100644 --- a/crates/component-macro/tests/expanded/host-world_async.rs +++ b/crates/component-macro/tests/expanded/host-world_async.rs @@ -27,7 +27,7 @@ impl<_T> Host_Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Host_Indices::new(instance_pre.component())?; + let indices = Host_Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct Host_Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Host_::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Host_Indices::new_instance`] followed -/// by [`Host_Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -126,24 +121,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Host_Indices {}) - } - /// Creates a new instance of [`Host_Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Host_`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Host_`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Host_Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -156,6 +138,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Host_ {}) } @@ -164,7 +147,7 @@ const _: () = { /// Convenience wrapper around [`Host_Pre::new`] and /// [`Host_Pre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -174,14 +157,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; Host_Pre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`Host_Indices::new_instance`] and + /// Convenience wrapper around [`Host_Indices::new`] and /// [`Host_Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Host_Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Host_Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/host-world_concurrent.rs b/crates/component-macro/tests/expanded/host-world_concurrent.rs index bd8b604cf51a..336ed857d347 100644 --- a/crates/component-macro/tests/expanded/host-world_concurrent.rs +++ b/crates/component-macro/tests/expanded/host-world_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> Host_Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Host_Indices::new(instance_pre.component())?; + let indices = Host_Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct Host_Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Host_::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Host_Indices::new_instance`] followed -/// by [`Host_Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -144,24 +139,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Host_Indices {}) - } - /// Creates a new instance of [`Host_Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Host_`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Host_`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Host_Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -174,6 +156,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Host_ {}) } @@ -182,7 +165,7 @@ const _: () = { /// Convenience wrapper around [`Host_Pre::new`] and /// [`Host_Pre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -192,14 +175,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; Host_Pre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`Host_Indices::new_instance`] and + /// Convenience wrapper around [`Host_Indices::new`] and /// [`Host_Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Host_Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Host_Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/host-world_tracing_async.rs b/crates/component-macro/tests/expanded/host-world_tracing_async.rs index 82b0a7bbaaa4..6f3a11919738 100644 --- a/crates/component-macro/tests/expanded/host-world_tracing_async.rs +++ b/crates/component-macro/tests/expanded/host-world_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> Host_Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Host_Indices::new(instance_pre.component())?; + let indices = Host_Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct Host_Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Host_::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Host_Indices::new_instance`] followed -/// by [`Host_Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -126,24 +121,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Host_Indices {}) - } - /// Creates a new instance of [`Host_Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Host_`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Host_`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Host_Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -156,6 +138,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Host_ {}) } @@ -164,7 +147,7 @@ const _: () = { /// Convenience wrapper around [`Host_Pre::new`] and /// [`Host_Pre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -174,14 +157,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; Host_Pre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`Host_Indices::new_instance`] and + /// Convenience wrapper around [`Host_Indices::new`] and /// [`Host_Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Host_Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Host_Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/integers.rs b/crates/component-macro/tests/expanded/integers.rs index 9298cafa2d56..1506a85003fa 100644 --- a/crates/component-macro/tests/expanded/integers.rs +++ b/crates/component-macro/tests/expanded/integers.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,28 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::integers::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::integers::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::integers::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -140,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -149,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -538,45 +519,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/integers") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/integers`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/integers") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/integers`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/integers` does \ @@ -629,9 +586,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let a1 = *_instance .get_typed_func::<(u8,), ()>(&mut store, &self.a1)? .func(); diff --git a/crates/component-macro/tests/expanded/integers_async.rs b/crates/component-macro/tests/expanded/integers_async.rs index e79a2a18d6c9..f940a3139da3 100644 --- a/crates/component-macro/tests/expanded/integers_async.rs +++ b/crates/component-macro/tests/expanded/integers_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::integers::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::integers::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::integers::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -587,45 +568,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/integers") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/integers`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/integers") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/integers`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/integers` does \ @@ -678,9 +635,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let a1 = *_instance .get_typed_func::<(u8,), ()>(&mut store, &self.a1)? .func(); diff --git a/crates/component-macro/tests/expanded/integers_concurrent.rs b/crates/component-macro/tests/expanded/integers_concurrent.rs index 12559390dd7f..8b77f9068422 100644 --- a/crates/component-macro/tests/expanded/integers_concurrent.rs +++ b/crates/component-macro/tests/expanded/integers_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::integers::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::integers::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::integers::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -1265,45 +1246,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/integers") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/integers`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/integers") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/integers`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/integers` does \ @@ -1356,9 +1313,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let a1 = *_instance .get_typed_func::<(u8,), ()>(&mut store, &self.a1)? .func(); diff --git a/crates/component-macro/tests/expanded/integers_tracing_async.rs b/crates/component-macro/tests/expanded/integers_tracing_async.rs index 5014597a9fe8..266d4f5d723e 100644 --- a/crates/component-macro/tests/expanded/integers_tracing_async.rs +++ b/crates/component-macro/tests/expanded/integers_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::integers::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::integers::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::integers::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -852,45 +833,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/integers") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/integers`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/integers") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/integers`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/integers` does \ @@ -943,9 +900,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let a1 = *_instance .get_typed_func::<(u8,), ()>(&mut store, &self.a1)? .func(); diff --git a/crates/component-macro/tests/expanded/lists.rs b/crates/component-macro/tests/expanded/lists.rs index 88a98ea618e5..f78b4bea4a2f 100644 --- a/crates/component-macro/tests/expanded/lists.rs +++ b/crates/component-macro/tests/expanded/lists.rs @@ -27,7 +27,7 @@ impl<_T> TheListsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheListsIndices::new(instance_pre.component())?; + let indices = TheListsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheListsIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheLists::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheListsIndices::new_instance`] followed -/// by [`TheListsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,29 +100,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::lists::GuestIndices::new(_component)?; - Ok(TheListsIndices { interface0 }) - } - /// Creates a new instance of [`TheListsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheLists`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheLists`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::lists::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::lists::GuestIndices::new(_instance_pre)?; Ok(TheListsIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -140,6 +118,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheLists { interface0 }) @@ -149,21 +128,21 @@ const _: () = { /// Convenience wrapper around [`TheListsPre::new`] and /// [`TheListsPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheListsPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheListsIndices::new_instance`] and + /// Convenience wrapper around [`TheListsIndices::new`] and /// [`TheListsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheListsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheListsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -1238,45 +1217,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/lists") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/lists`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/lists") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/lists`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/lists` does \ @@ -1351,9 +1306,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let list_u8_param = *_instance .get_typed_func::< (&[u8],), diff --git a/crates/component-macro/tests/expanded/lists_async.rs b/crates/component-macro/tests/expanded/lists_async.rs index 6291b0f73d16..d5e0decd2092 100644 --- a/crates/component-macro/tests/expanded/lists_async.rs +++ b/crates/component-macro/tests/expanded/lists_async.rs @@ -27,7 +27,7 @@ impl<_T> TheListsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheListsIndices::new(instance_pre.component())?; + let indices = TheListsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheListsIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheLists::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheListsIndices::new_instance`] followed -/// by [`TheListsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::lists::GuestIndices::new(_component)?; - Ok(TheListsIndices { interface0 }) - } - /// Creates a new instance of [`TheListsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheLists`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheLists`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::lists::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::lists::GuestIndices::new(_instance_pre)?; Ok(TheListsIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheLists { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheListsPre::new`] and /// [`TheListsPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheListsPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheListsIndices::new_instance`] and + /// Convenience wrapper around [`TheListsIndices::new`] and /// [`TheListsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheListsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheListsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -1348,45 +1327,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/lists") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/lists`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/lists") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/lists`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/lists` does \ @@ -1461,9 +1416,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let list_u8_param = *_instance .get_typed_func::< (&[u8],), diff --git a/crates/component-macro/tests/expanded/lists_concurrent.rs b/crates/component-macro/tests/expanded/lists_concurrent.rs index 26319b4f7ede..96deed0b32b7 100644 --- a/crates/component-macro/tests/expanded/lists_concurrent.rs +++ b/crates/component-macro/tests/expanded/lists_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheListsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheListsIndices::new(instance_pre.component())?; + let indices = TheListsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheListsIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheLists::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheListsIndices::new_instance`] followed -/// by [`TheListsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::lists::GuestIndices::new(_component)?; - Ok(TheListsIndices { interface0 }) - } - /// Creates a new instance of [`TheListsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheLists`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheLists`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::lists::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::lists::GuestIndices::new(_instance_pre)?; Ok(TheListsIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheLists { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheListsPre::new`] and /// [`TheListsPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheListsPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheListsIndices::new_instance`] and + /// Convenience wrapper around [`TheListsIndices::new`] and /// [`TheListsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheListsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheListsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -2446,45 +2425,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/lists") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/lists`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/lists") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/lists`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/lists` does \ @@ -2559,9 +2514,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let list_u8_param = *_instance .get_typed_func::< (&[u8],), diff --git a/crates/component-macro/tests/expanded/lists_tracing_async.rs b/crates/component-macro/tests/expanded/lists_tracing_async.rs index 2575e380826d..6763a5a8c19e 100644 --- a/crates/component-macro/tests/expanded/lists_tracing_async.rs +++ b/crates/component-macro/tests/expanded/lists_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheListsPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheListsIndices::new(instance_pre.component())?; + let indices = TheListsIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheListsIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheLists::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheListsIndices::new_instance`] followed -/// by [`TheListsIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::lists::GuestIndices::new(_component)?; - Ok(TheListsIndices { interface0 }) - } - /// Creates a new instance of [`TheListsIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheLists`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheLists`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::lists::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::lists::GuestIndices::new(_instance_pre)?; Ok(TheListsIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheLists { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheListsPre::new`] and /// [`TheListsPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheListsPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheListsIndices::new_instance`] and + /// Convenience wrapper around [`TheListsIndices::new`] and /// [`TheListsIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheListsIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheListsIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -1779,45 +1758,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/lists") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/lists`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/lists") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/lists`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/lists` does \ @@ -1892,9 +1847,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let list_u8_param = *_instance .get_typed_func::< (&[u8],), diff --git a/crates/component-macro/tests/expanded/many-arguments.rs b/crates/component-macro/tests/expanded/many-arguments.rs index cf269eb49728..2f07d9504773 100644 --- a/crates/component-macro/tests/expanded/many-arguments.rs +++ b/crates/component-macro/tests/expanded/many-arguments.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,28 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::manyarg::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::manyarg::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::manyarg::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -140,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -149,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -550,45 +531,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/manyarg") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/manyarg`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/manyarg") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/manyarg`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/manyarg` does \ @@ -609,9 +566,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let many_args = *_instance .get_typed_func::< ( diff --git a/crates/component-macro/tests/expanded/many-arguments_async.rs b/crates/component-macro/tests/expanded/many-arguments_async.rs index bd07573c95e3..c5b81f8e22a4 100644 --- a/crates/component-macro/tests/expanded/many-arguments_async.rs +++ b/crates/component-macro/tests/expanded/many-arguments_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::manyarg::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::manyarg::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::manyarg::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -568,45 +549,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/manyarg") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/manyarg`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/manyarg") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/manyarg`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/manyarg` does \ @@ -627,9 +584,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let many_args = *_instance .get_typed_func::< ( diff --git a/crates/component-macro/tests/expanded/many-arguments_concurrent.rs b/crates/component-macro/tests/expanded/many-arguments_concurrent.rs index d7a987b0eefe..e7aedf65637a 100644 --- a/crates/component-macro/tests/expanded/many-arguments_concurrent.rs +++ b/crates/component-macro/tests/expanded/many-arguments_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::manyarg::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::manyarg::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::manyarg::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -639,45 +620,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/manyarg") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/manyarg`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/manyarg") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/manyarg`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/manyarg` does \ @@ -698,9 +655,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let many_args = *_instance .get_typed_func::< ( 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 3e101a3942d7..37e2215fef3d 100644 --- a/crates/component-macro/tests/expanded/many-arguments_tracing_async.rs +++ b/crates/component-macro/tests/expanded/many-arguments_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::manyarg::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::manyarg::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::manyarg::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -611,45 +592,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/manyarg") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/manyarg`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/manyarg") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/manyarg`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/manyarg` does \ @@ -670,9 +627,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let many_args = *_instance .get_typed_func::< ( diff --git a/crates/component-macro/tests/expanded/multiversion.rs b/crates/component-macro/tests/expanded/multiversion.rs index f5210658ed5b..42ab45ac3405 100644 --- a/crates/component-macro/tests/expanded/multiversion.rs +++ b/crates/component-macro/tests/expanded/multiversion.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -107,37 +102,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_component)?; - let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_component)?; - Ok(FooIndices { - interface0, - interface1, - }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::my::dep0_1_0::a::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface1 = exports::my::dep0_2_0::a::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_instance_pre)?; + let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_instance_pre)?; Ok(FooIndices { interface0, interface1, @@ -153,6 +124,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; let interface1 = self.interface1.load(&mut store, &_instance)?; @@ -163,21 +135,21 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -331,45 +303,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "my:dep/a@0.1.0") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `my:dep/a@0.1.0`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "my:dep/a@0.1.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.1.0`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `my:dep/a@0.1.0` does \ @@ -386,9 +334,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let x = *_instance .get_typed_func::<(), ()>(&mut store, &self.x)? .func(); @@ -435,45 +385,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "my:dep/a@0.2.0") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `my:dep/a@0.2.0`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "my:dep/a@0.2.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.2.0`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `my:dep/a@0.2.0` does \ @@ -490,9 +416,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let x = *_instance .get_typed_func::<(), ()>(&mut store, &self.x)? .func(); diff --git a/crates/component-macro/tests/expanded/multiversion_async.rs b/crates/component-macro/tests/expanded/multiversion_async.rs index 2abaf14be256..e5816097d56b 100644 --- a/crates/component-macro/tests/expanded/multiversion_async.rs +++ b/crates/component-macro/tests/expanded/multiversion_async.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -86,11 +86,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -110,37 +105,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_component)?; - let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_component)?; - Ok(FooIndices { - interface0, - interface1, - }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::my::dep0_1_0::a::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface1 = exports::my::dep0_2_0::a::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_instance_pre)?; + let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_instance_pre)?; Ok(FooIndices { interface0, interface1, @@ -156,6 +127,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; let interface1 = self.interface1.load(&mut store, &_instance)?; @@ -166,7 +138,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -176,14 +148,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -352,45 +324,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "my:dep/a@0.1.0") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `my:dep/a@0.1.0`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "my:dep/a@0.1.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.1.0`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `my:dep/a@0.1.0` does \ @@ -407,9 +355,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let x = *_instance .get_typed_func::<(), ()>(&mut store, &self.x)? .func(); @@ -456,45 +406,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "my:dep/a@0.2.0") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `my:dep/a@0.2.0`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "my:dep/a@0.2.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.2.0`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `my:dep/a@0.2.0` does \ @@ -511,9 +437,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let x = *_instance .get_typed_func::<(), ()>(&mut store, &self.x)? .func(); diff --git a/crates/component-macro/tests/expanded/multiversion_concurrent.rs b/crates/component-macro/tests/expanded/multiversion_concurrent.rs index 1872af05addf..1df3d3bb95b1 100644 --- a/crates/component-macro/tests/expanded/multiversion_concurrent.rs +++ b/crates/component-macro/tests/expanded/multiversion_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -86,11 +86,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -110,37 +105,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_component)?; - let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_component)?; - Ok(FooIndices { - interface0, - interface1, - }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::my::dep0_1_0::a::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface1 = exports::my::dep0_2_0::a::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_instance_pre)?; + let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_instance_pre)?; Ok(FooIndices { interface0, interface1, @@ -156,6 +127,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; let interface1 = self.interface1.load(&mut store, &_instance)?; @@ -166,7 +138,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -176,14 +148,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -429,45 +401,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "my:dep/a@0.1.0") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `my:dep/a@0.1.0`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "my:dep/a@0.1.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.1.0`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `my:dep/a@0.1.0` does \ @@ -484,9 +432,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let x = *_instance .get_typed_func::<(), ()>(&mut store, &self.x)? .func(); @@ -534,45 +484,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "my:dep/a@0.2.0") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `my:dep/a@0.2.0`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "my:dep/a@0.2.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.2.0`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `my:dep/a@0.2.0` does \ @@ -589,9 +515,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let x = *_instance .get_typed_func::<(), ()>(&mut store, &self.x)? .func(); diff --git a/crates/component-macro/tests/expanded/multiversion_tracing_async.rs b/crates/component-macro/tests/expanded/multiversion_tracing_async.rs index 2a59cd6e4e64..2489a2d39e96 100644 --- a/crates/component-macro/tests/expanded/multiversion_tracing_async.rs +++ b/crates/component-macro/tests/expanded/multiversion_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -86,11 +86,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -110,37 +105,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_component)?; - let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_component)?; - Ok(FooIndices { - interface0, - interface1, - }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::my::dep0_1_0::a::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface1 = exports::my::dep0_2_0::a::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::my::dep0_1_0::a::GuestIndices::new(_instance_pre)?; + let interface1 = exports::my::dep0_2_0::a::GuestIndices::new(_instance_pre)?; Ok(FooIndices { interface0, interface1, @@ -156,6 +127,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; let interface1 = self.interface1.load(&mut store, &_instance)?; @@ -166,7 +138,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -176,14 +148,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -378,45 +350,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "my:dep/a@0.1.0") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `my:dep/a@0.1.0`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "my:dep/a@0.1.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.1.0`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `my:dep/a@0.1.0` does \ @@ -433,9 +381,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let x = *_instance .get_typed_func::<(), ()>(&mut store, &self.x)? .func(); @@ -493,45 +443,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "my:dep/a@0.2.0") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `my:dep/a@0.2.0`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "my:dep/a@0.2.0") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `my:dep/a@0.2.0`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `my:dep/a@0.2.0` does \ @@ -548,9 +474,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let x = *_instance .get_typed_func::<(), ()>(&mut store, &self.x)? .func(); diff --git a/crates/component-macro/tests/expanded/path1.rs b/crates/component-macro/tests/expanded/path1.rs index a991592c753e..68c41e11b56e 100644 --- a/crates/component-macro/tests/expanded/path1.rs +++ b/crates/component-macro/tests/expanded/path1.rs @@ -27,7 +27,7 @@ impl<_T> Path1Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Path1Indices::new(instance_pre.component())?; + let indices = Path1Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct Path1Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Path1::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Path1Indices::new_instance`] followed -/// by [`Path1Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -101,24 +96,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Path1Indices {}) - } - /// Creates a new instance of [`Path1Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Path1`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Path1`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Path1Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -131,6 +113,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Path1 {}) } @@ -139,21 +122,21 @@ const _: () = { /// Convenience wrapper around [`Path1Pre::new`] and /// [`Path1Pre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; Path1Pre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`Path1Indices::new_instance`] and + /// Convenience wrapper around [`Path1Indices::new`] and /// [`Path1Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Path1Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Path1Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/path1_async.rs b/crates/component-macro/tests/expanded/path1_async.rs index 6d2c4fdb3b46..ebe625a8fb74 100644 --- a/crates/component-macro/tests/expanded/path1_async.rs +++ b/crates/component-macro/tests/expanded/path1_async.rs @@ -27,7 +27,7 @@ impl<_T> Path1Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Path1Indices::new(instance_pre.component())?; + let indices = Path1Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct Path1Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Path1::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Path1Indices::new_instance`] followed -/// by [`Path1Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Path1Indices {}) - } - /// Creates a new instance of [`Path1Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Path1`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Path1`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Path1Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Path1 {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`Path1Pre::new`] and /// [`Path1Pre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; Path1Pre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`Path1Indices::new_instance`] and + /// Convenience wrapper around [`Path1Indices::new`] and /// [`Path1Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Path1Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Path1Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/path1_concurrent.rs b/crates/component-macro/tests/expanded/path1_concurrent.rs index 3161aac96263..c88d31b7e849 100644 --- a/crates/component-macro/tests/expanded/path1_concurrent.rs +++ b/crates/component-macro/tests/expanded/path1_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> Path1Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Path1Indices::new(instance_pre.component())?; + let indices = Path1Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct Path1Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Path1::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Path1Indices::new_instance`] followed -/// by [`Path1Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Path1Indices {}) - } - /// Creates a new instance of [`Path1Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Path1`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Path1`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Path1Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Path1 {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`Path1Pre::new`] and /// [`Path1Pre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; Path1Pre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`Path1Indices::new_instance`] and + /// Convenience wrapper around [`Path1Indices::new`] and /// [`Path1Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Path1Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Path1Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/path1_tracing_async.rs b/crates/component-macro/tests/expanded/path1_tracing_async.rs index 6d2c4fdb3b46..ebe625a8fb74 100644 --- a/crates/component-macro/tests/expanded/path1_tracing_async.rs +++ b/crates/component-macro/tests/expanded/path1_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> Path1Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Path1Indices::new(instance_pre.component())?; + let indices = Path1Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct Path1Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Path1::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Path1Indices::new_instance`] followed -/// by [`Path1Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Path1Indices {}) - } - /// Creates a new instance of [`Path1Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Path1`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Path1`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Path1Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Path1 {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`Path1Pre::new`] and /// [`Path1Pre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; Path1Pre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`Path1Indices::new_instance`] and + /// Convenience wrapper around [`Path1Indices::new`] and /// [`Path1Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Path1Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Path1Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/path2.rs b/crates/component-macro/tests/expanded/path2.rs index cbafd8fe984f..85a7893c472c 100644 --- a/crates/component-macro/tests/expanded/path2.rs +++ b/crates/component-macro/tests/expanded/path2.rs @@ -27,7 +27,7 @@ impl<_T> Path2Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Path2Indices::new(instance_pre.component())?; + let indices = Path2Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct Path2Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Path2::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Path2Indices::new_instance`] followed -/// by [`Path2Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -101,24 +96,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Path2Indices {}) - } - /// Creates a new instance of [`Path2Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Path2`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Path2`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Path2Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -131,6 +113,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Path2 {}) } @@ -139,21 +122,21 @@ const _: () = { /// Convenience wrapper around [`Path2Pre::new`] and /// [`Path2Pre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; Path2Pre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`Path2Indices::new_instance`] and + /// Convenience wrapper around [`Path2Indices::new`] and /// [`Path2Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Path2Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Path2Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/path2_async.rs b/crates/component-macro/tests/expanded/path2_async.rs index e726c8bb0356..ece0ed112a8e 100644 --- a/crates/component-macro/tests/expanded/path2_async.rs +++ b/crates/component-macro/tests/expanded/path2_async.rs @@ -27,7 +27,7 @@ impl<_T> Path2Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Path2Indices::new(instance_pre.component())?; + let indices = Path2Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct Path2Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Path2::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Path2Indices::new_instance`] followed -/// by [`Path2Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Path2Indices {}) - } - /// Creates a new instance of [`Path2Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Path2`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Path2`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Path2Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Path2 {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`Path2Pre::new`] and /// [`Path2Pre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; Path2Pre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`Path2Indices::new_instance`] and + /// Convenience wrapper around [`Path2Indices::new`] and /// [`Path2Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Path2Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Path2Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/path2_concurrent.rs b/crates/component-macro/tests/expanded/path2_concurrent.rs index d5d1dd82597f..86fe0716646f 100644 --- a/crates/component-macro/tests/expanded/path2_concurrent.rs +++ b/crates/component-macro/tests/expanded/path2_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> Path2Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Path2Indices::new(instance_pre.component())?; + let indices = Path2Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct Path2Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Path2::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Path2Indices::new_instance`] followed -/// by [`Path2Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Path2Indices {}) - } - /// Creates a new instance of [`Path2Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Path2`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Path2`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Path2Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Path2 {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`Path2Pre::new`] and /// [`Path2Pre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; Path2Pre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`Path2Indices::new_instance`] and + /// Convenience wrapper around [`Path2Indices::new`] and /// [`Path2Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Path2Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Path2Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/path2_tracing_async.rs b/crates/component-macro/tests/expanded/path2_tracing_async.rs index e726c8bb0356..ece0ed112a8e 100644 --- a/crates/component-macro/tests/expanded/path2_tracing_async.rs +++ b/crates/component-macro/tests/expanded/path2_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> Path2Pre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = Path2Indices::new(instance_pre.component())?; + let indices = Path2Indices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct Path2Indices {} /// * If you've instantiated the instance yourself already /// then you can use [`Path2::new`]. /// -/// * You can also access the guts of instantiation through -/// [`Path2Indices::new_instance`] followed -/// by [`Path2Indices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(Path2Indices {}) - } - /// Creates a new instance of [`Path2Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`Path2`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Path2`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(Path2Indices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Path2 {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`Path2Pre::new`] and /// [`Path2Pre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; Path2Pre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`Path2Indices::new_instance`] and + /// Convenience wrapper around [`Path2Indices::new`] and /// [`Path2Indices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = Path2Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = Path2Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/records.rs b/crates/component-macro/tests/expanded/records.rs index 16d350cfd1b5..7719009c6397 100644 --- a/crates/component-macro/tests/expanded/records.rs +++ b/crates/component-macro/tests/expanded/records.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,28 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::records::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::records::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::records::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -140,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -149,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -755,45 +736,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/records") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/records`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/records") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/records`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/records` does \ @@ -832,9 +789,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let tuple_arg = *_instance .get_typed_func::< ((char, u32),), diff --git a/crates/component-macro/tests/expanded/records_async.rs b/crates/component-macro/tests/expanded/records_async.rs index 1aa382e27544..d1cadd3b4051 100644 --- a/crates/component-macro/tests/expanded/records_async.rs +++ b/crates/component-macro/tests/expanded/records_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::records::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::records::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::records::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -789,45 +770,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/records") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/records`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/records") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/records`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/records` does \ @@ -866,9 +823,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let tuple_arg = *_instance .get_typed_func::< ((char, u32),), diff --git a/crates/component-macro/tests/expanded/records_concurrent.rs b/crates/component-macro/tests/expanded/records_concurrent.rs index f21271b4402d..fa787a187c89 100644 --- a/crates/component-macro/tests/expanded/records_concurrent.rs +++ b/crates/component-macro/tests/expanded/records_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::records::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::records::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::records::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -1209,45 +1190,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/records") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/records`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/records") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/records`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/records` does \ @@ -1286,9 +1243,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let tuple_arg = *_instance .get_typed_func::< ((char, u32),), diff --git a/crates/component-macro/tests/expanded/records_tracing_async.rs b/crates/component-macro/tests/expanded/records_tracing_async.rs index 3ad1b88be5c5..1d09543d62fa 100644 --- a/crates/component-macro/tests/expanded/records_tracing_async.rs +++ b/crates/component-macro/tests/expanded/records_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::records::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::records::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::records::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -950,45 +931,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/records") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/records`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/records") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/records`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/records` does \ @@ -1027,9 +984,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let tuple_arg = *_instance .get_typed_func::< ((char, u32),), diff --git a/crates/component-macro/tests/expanded/rename.rs b/crates/component-macro/tests/expanded/rename.rs index 65b7b4344bf7..f0ad13844789 100644 --- a/crates/component-macro/tests/expanded/rename.rs +++ b/crates/component-macro/tests/expanded/rename.rs @@ -27,7 +27,7 @@ impl<_T> NeptunePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = NeptuneIndices::new(instance_pre.component())?; + let indices = NeptuneIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct NeptuneIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Neptune::new`]. /// -/// * You can also access the guts of instantiation through -/// [`NeptuneIndices::new_instance`] followed -/// by [`NeptuneIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -101,24 +96,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(NeptuneIndices {}) - } - /// Creates a new instance of [`NeptuneIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Neptune`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Neptune`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(NeptuneIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -131,6 +113,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Neptune {}) } @@ -139,21 +122,21 @@ const _: () = { /// Convenience wrapper around [`NeptunePre::new`] and /// [`NeptunePre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; NeptunePre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`NeptuneIndices::new_instance`] and + /// Convenience wrapper around [`NeptuneIndices::new`] and /// [`NeptuneIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = NeptuneIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = NeptuneIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/rename_async.rs b/crates/component-macro/tests/expanded/rename_async.rs index 96522907ae3e..8e9fb95454f4 100644 --- a/crates/component-macro/tests/expanded/rename_async.rs +++ b/crates/component-macro/tests/expanded/rename_async.rs @@ -27,7 +27,7 @@ impl<_T> NeptunePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = NeptuneIndices::new(instance_pre.component())?; + let indices = NeptuneIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct NeptuneIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Neptune::new`]. /// -/// * You can also access the guts of instantiation through -/// [`NeptuneIndices::new_instance`] followed -/// by [`NeptuneIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(NeptuneIndices {}) - } - /// Creates a new instance of [`NeptuneIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Neptune`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Neptune`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(NeptuneIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Neptune {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`NeptunePre::new`] and /// [`NeptunePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; NeptunePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`NeptuneIndices::new_instance`] and + /// Convenience wrapper around [`NeptuneIndices::new`] and /// [`NeptuneIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = NeptuneIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = NeptuneIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/rename_concurrent.rs b/crates/component-macro/tests/expanded/rename_concurrent.rs index 551be2e36e5a..eb75fc1221b7 100644 --- a/crates/component-macro/tests/expanded/rename_concurrent.rs +++ b/crates/component-macro/tests/expanded/rename_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> NeptunePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = NeptuneIndices::new(instance_pre.component())?; + let indices = NeptuneIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct NeptuneIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Neptune::new`]. /// -/// * You can also access the guts of instantiation through -/// [`NeptuneIndices::new_instance`] followed -/// by [`NeptuneIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(NeptuneIndices {}) - } - /// Creates a new instance of [`NeptuneIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Neptune`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Neptune`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(NeptuneIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Neptune {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`NeptunePre::new`] and /// [`NeptunePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; NeptunePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`NeptuneIndices::new_instance`] and + /// Convenience wrapper around [`NeptuneIndices::new`] and /// [`NeptuneIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = NeptuneIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = NeptuneIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/rename_tracing_async.rs b/crates/component-macro/tests/expanded/rename_tracing_async.rs index a7641fca9f4a..6bf45a246770 100644 --- a/crates/component-macro/tests/expanded/rename_tracing_async.rs +++ b/crates/component-macro/tests/expanded/rename_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> NeptunePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = NeptuneIndices::new(instance_pre.component())?; + let indices = NeptuneIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct NeptuneIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Neptune::new`]. /// -/// * You can also access the guts of instantiation through -/// [`NeptuneIndices::new_instance`] followed -/// by [`NeptuneIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(NeptuneIndices {}) - } - /// Creates a new instance of [`NeptuneIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Neptune`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Neptune`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(NeptuneIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Neptune {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`NeptunePre::new`] and /// [`NeptunePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; NeptunePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`NeptuneIndices::new_instance`] and + /// Convenience wrapper around [`NeptuneIndices::new`] and /// [`NeptuneIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = NeptuneIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = NeptuneIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/resources-export.rs b/crates/component-macro/tests/expanded/resources-export.rs index d8f48d60250c..31ecd8d2e0a6 100644 --- a/crates/component-macro/tests/expanded/resources-export.rs +++ b/crates/component-macro/tests/expanded/resources-export.rs @@ -27,7 +27,7 @@ impl<_T> WPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = WIndices::new(instance_pre.component())?; + let indices = WIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct WIndices { /// * If you've instantiated the instance yourself already /// then you can use [`W::new`]. /// -/// * You can also access the guts of instantiation through -/// [`WIndices::new_instance`] followed -/// by [`WIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -111,56 +106,22 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::simple_export::GuestIndices::new( - _component, + _instance_pre, )?; let interface1 = exports::foo::foo::export_using_import::GuestIndices::new( - _component, + _instance_pre, )?; let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new( - _component, + _instance_pre, )?; let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new( - _component, - )?; - Ok(WIndices { - interface0, - interface1, - interface2, - interface3, - }) - } - /// Creates a new instance of [`WIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`W`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`W`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple_export::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface1 = exports::foo::foo::export_using_import::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(WIndices { interface0, @@ -179,6 +140,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; let interface1 = self.interface1.load(&mut store, &_instance)?; @@ -196,21 +158,21 @@ const _: () = { /// Convenience wrapper around [`WPre::new`] and /// [`WPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; WPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`WIndices::new_instance`] and + /// Convenience wrapper around [`WIndices::new`] and /// [`WIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = WIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = WIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -339,45 +301,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple-export") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple-export`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple-export") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-export`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple-export` does \ @@ -400,9 +338,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (), @@ -515,49 +455,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-import") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-import`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-import", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-import`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-import` does \ @@ -580,9 +492,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (wasmtime::component::Resource,), @@ -698,49 +612,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-export1") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-export1`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-export1", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export1`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-export1` does \ @@ -759,9 +645,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (), @@ -819,49 +707,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-export2") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-export2`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-export2", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export2`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-export2` does \ @@ -880,9 +740,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_b_constructor = *_instance .get_typed_func::< (wasmtime::component::ResourceAny,), diff --git a/crates/component-macro/tests/expanded/resources-export_async.rs b/crates/component-macro/tests/expanded/resources-export_async.rs index 96d70de7b3b1..5a1b8d39493c 100644 --- a/crates/component-macro/tests/expanded/resources-export_async.rs +++ b/crates/component-macro/tests/expanded/resources-export_async.rs @@ -27,7 +27,7 @@ impl<_T> WPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = WIndices::new(instance_pre.component())?; + let indices = WIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -88,11 +88,6 @@ pub struct WIndices { /// * If you've instantiated the instance yourself already /// then you can use [`W::new`]. /// -/// * You can also access the guts of instantiation through -/// [`WIndices::new_instance`] followed -/// by [`WIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -114,56 +109,22 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::simple_export::GuestIndices::new( - _component, + _instance_pre, )?; let interface1 = exports::foo::foo::export_using_import::GuestIndices::new( - _component, + _instance_pre, )?; let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new( - _component, + _instance_pre, )?; let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new( - _component, - )?; - Ok(WIndices { - interface0, - interface1, - interface2, - interface3, - }) - } - /// Creates a new instance of [`WIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`W`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`W`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple_export::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface1 = exports::foo::foo::export_using_import::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(WIndices { interface0, @@ -182,6 +143,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; let interface1 = self.interface1.load(&mut store, &_instance)?; @@ -199,7 +161,7 @@ const _: () = { /// Convenience wrapper around [`WPre::new`] and /// [`WPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -209,14 +171,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; WPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`WIndices::new_instance`] and + /// Convenience wrapper around [`WIndices::new`] and /// [`WIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = WIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = WIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -355,45 +317,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple-export") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple-export`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple-export") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-export`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple-export` does \ @@ -416,9 +354,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (), @@ -537,49 +477,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-import") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-import`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-import", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-import`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-import` does \ @@ -602,9 +514,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (wasmtime::component::Resource,), @@ -726,49 +640,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-export1") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-export1`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-export1", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export1`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-export1` does \ @@ -787,9 +673,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (), @@ -849,49 +737,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-export2") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-export2`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-export2", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export2`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-export2` does \ @@ -910,9 +770,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_b_constructor = *_instance .get_typed_func::< (wasmtime::component::ResourceAny,), diff --git a/crates/component-macro/tests/expanded/resources-export_concurrent.rs b/crates/component-macro/tests/expanded/resources-export_concurrent.rs index 764c13f3c018..056b78d2043a 100644 --- a/crates/component-macro/tests/expanded/resources-export_concurrent.rs +++ b/crates/component-macro/tests/expanded/resources-export_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> WPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = WIndices::new(instance_pre.component())?; + let indices = WIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -88,11 +88,6 @@ pub struct WIndices { /// * If you've instantiated the instance yourself already /// then you can use [`W::new`]. /// -/// * You can also access the guts of instantiation through -/// [`WIndices::new_instance`] followed -/// by [`WIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -114,56 +109,22 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::simple_export::GuestIndices::new( - _component, + _instance_pre, )?; let interface1 = exports::foo::foo::export_using_import::GuestIndices::new( - _component, + _instance_pre, )?; let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new( - _component, + _instance_pre, )?; let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new( - _component, - )?; - Ok(WIndices { - interface0, - interface1, - interface2, - interface3, - }) - } - /// Creates a new instance of [`WIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`W`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`W`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple_export::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface1 = exports::foo::foo::export_using_import::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(WIndices { interface0, @@ -182,6 +143,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; let interface1 = self.interface1.load(&mut store, &_instance)?; @@ -199,7 +161,7 @@ const _: () = { /// Convenience wrapper around [`WPre::new`] and /// [`WPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -209,14 +171,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; WPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`WIndices::new_instance`] and + /// Convenience wrapper around [`WIndices::new`] and /// [`WIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = WIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = WIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -350,45 +312,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple-export") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple-export`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple-export") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-export`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple-export` does \ @@ -411,9 +349,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (), @@ -531,49 +471,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-import") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-import`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-import", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-import`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-import` does \ @@ -596,9 +508,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (wasmtime::component::Resource,), @@ -723,49 +637,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-export1") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-export1`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-export1", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export1`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-export1` does \ @@ -784,9 +670,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (), @@ -847,49 +735,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-export2") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-export2`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-export2", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export2`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-export2` does \ @@ -908,9 +768,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_b_constructor = *_instance .get_typed_func::< (wasmtime::component::ResourceAny,), 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 45dde160e64b..3a678980b66e 100644 --- a/crates/component-macro/tests/expanded/resources-export_tracing_async.rs +++ b/crates/component-macro/tests/expanded/resources-export_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> WPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = WIndices::new(instance_pre.component())?; + let indices = WIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -88,11 +88,6 @@ pub struct WIndices { /// * If you've instantiated the instance yourself already /// then you can use [`W::new`]. /// -/// * You can also access the guts of instantiation through -/// [`WIndices::new_instance`] followed -/// by [`WIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -114,56 +109,22 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::simple_export::GuestIndices::new( - _component, + _instance_pre, )?; let interface1 = exports::foo::foo::export_using_import::GuestIndices::new( - _component, + _instance_pre, )?; let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new( - _component, + _instance_pre, )?; let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new( - _component, - )?; - Ok(WIndices { - interface0, - interface1, - interface2, - interface3, - }) - } - /// Creates a new instance of [`WIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`W`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`W`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple_export::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface1 = exports::foo::foo::export_using_import::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface2 = exports::foo::foo::export_using_export1::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let interface3 = exports::foo::foo::export_using_export2::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(WIndices { interface0, @@ -182,6 +143,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; let interface1 = self.interface1.load(&mut store, &_instance)?; @@ -199,7 +161,7 @@ const _: () = { /// Convenience wrapper around [`WPre::new`] and /// [`WPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -209,14 +171,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; WPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`WIndices::new_instance`] and + /// Convenience wrapper around [`WIndices::new`] and /// [`WIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = WIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = WIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -355,45 +317,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple-export") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple-export`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple-export") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-export`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple-export` does \ @@ -416,9 +354,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (), @@ -564,49 +504,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-import") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-import`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-import", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-import`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-import` does \ @@ -629,9 +541,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (wasmtime::component::Resource,), @@ -782,49 +696,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-export1") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-export1`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-export1", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export1`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-export1` does \ @@ -843,9 +729,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_a_constructor = *_instance .get_typed_func::< (), @@ -914,49 +802,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/export-using-export2") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/export-using-export2`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/export-using-export2", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/export-using-export2`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/export-using-export2` does \ @@ -975,9 +835,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let constructor_b_constructor = *_instance .get_typed_func::< (wasmtime::component::ResourceAny,), diff --git a/crates/component-macro/tests/expanded/resources-import.rs b/crates/component-macro/tests/expanded/resources-import.rs index da1eafb6f3bb..5617456f4d90 100644 --- a/crates/component-macro/tests/expanded/resources-import.rs +++ b/crates/component-macro/tests/expanded/resources-import.rs @@ -54,7 +54,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -110,11 +110,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -155,44 +150,41 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface1 = exports::foo::foo::uses_resource_transitively::GuestIndices::new( - _component, - )?; - let some_world_func2 = _component - .get_export_index(None, "some-world-func2") - .ok_or_else(|| { - anyhow::anyhow!("no function export `some-world-func2` found") - })?; - Ok(TheWorldIndices { - interface1, - some_world_func2, - }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface1 = exports::foo::foo::uses_resource_transitively::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; - let some_world_func2 = _instance - .get_export_index(&mut store, None, "some-world-func2") - .ok_or_else(|| { - anyhow::anyhow!("no function export `some-world-func2` found") - })?; + let some_world_func2 = { + let (item, index) = _component + .get_export(None, "some-world-func2") + .ok_or_else(|| { + anyhow::anyhow!("no export `some-world-func2` found") + })?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func + .typecheck::< + (), + (wasmtime::component::Resource,), + >(&_instance_type), + "type-checking export func `some-world-func2`", + )?; + index + } + _ => { + Err( + anyhow::anyhow!( + "export `some-world-func2` is not a function" + ), + )? + } + } + }; Ok(TheWorldIndices { interface1, some_world_func2, @@ -208,6 +200,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface1 = self.interface1.load(&mut store, &_instance)?; let some_world_func2 = *_instance @@ -226,21 +219,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, @@ -1154,53 +1147,25 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/uses-resource-transitively") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/uses-resource-transitively`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/uses-resource-transitively", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/uses-resource-transitively`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/uses-resource-transitively` does \ - not have export `{name}`" + not have export `{name}`" ) }) }; @@ -1213,9 +1178,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let handle = *_instance .get_typed_func::< (wasmtime::component::Resource,), diff --git a/crates/component-macro/tests/expanded/resources-import_async.rs b/crates/component-macro/tests/expanded/resources-import_async.rs index fb9962ad8d2b..7ba133979f70 100644 --- a/crates/component-macro/tests/expanded/resources-import_async.rs +++ b/crates/component-macro/tests/expanded/resources-import_async.rs @@ -55,7 +55,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -114,11 +114,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -160,44 +155,41 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface1 = exports::foo::foo::uses_resource_transitively::GuestIndices::new( - _component, + _instance_pre, )?; - let some_world_func2 = _component - .get_export_index(None, "some-world-func2") - .ok_or_else(|| { - anyhow::anyhow!("no function export `some-world-func2` found") - })?; - Ok(TheWorldIndices { - interface1, - some_world_func2, - }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface1 = exports::foo::foo::uses_resource_transitively::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let some_world_func2 = _instance - .get_export_index(&mut store, None, "some-world-func2") - .ok_or_else(|| { - anyhow::anyhow!("no function export `some-world-func2` found") - })?; + let some_world_func2 = { + let (item, index) = _component + .get_export(None, "some-world-func2") + .ok_or_else(|| { + anyhow::anyhow!("no export `some-world-func2` found") + })?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func + .typecheck::< + (), + (wasmtime::component::Resource,), + >(&_instance_type), + "type-checking export func `some-world-func2`", + )?; + index + } + _ => { + Err( + anyhow::anyhow!( + "export `some-world-func2` is not a function" + ), + )? + } + } + }; Ok(TheWorldIndices { interface1, some_world_func2, @@ -213,6 +205,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface1 = self.interface1.load(&mut store, &_instance)?; let some_world_func2 = *_instance @@ -231,7 +224,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -241,14 +234,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, @@ -1279,53 +1272,25 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/uses-resource-transitively") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/uses-resource-transitively`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/uses-resource-transitively", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/uses-resource-transitively`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/uses-resource-transitively` does \ - not have export `{name}`" + not have export `{name}`" ) }) }; @@ -1338,9 +1303,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let handle = *_instance .get_typed_func::< (wasmtime::component::Resource,), diff --git a/crates/component-macro/tests/expanded/resources-import_concurrent.rs b/crates/component-macro/tests/expanded/resources-import_concurrent.rs index 8e0a1f53bf0d..60c626c25d40 100644 --- a/crates/component-macro/tests/expanded/resources-import_concurrent.rs +++ b/crates/component-macro/tests/expanded/resources-import_concurrent.rs @@ -109,7 +109,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -168,11 +168,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -232,44 +227,41 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface1 = exports::foo::foo::uses_resource_transitively::GuestIndices::new( - _component, + _instance_pre, )?; - let some_world_func2 = _component - .get_export_index(None, "some-world-func2") - .ok_or_else(|| { - anyhow::anyhow!("no function export `some-world-func2` found") - })?; - Ok(TheWorldIndices { - interface1, - some_world_func2, - }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface1 = exports::foo::foo::uses_resource_transitively::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let some_world_func2 = _instance - .get_export_index(&mut store, None, "some-world-func2") - .ok_or_else(|| { - anyhow::anyhow!("no function export `some-world-func2` found") - })?; + let some_world_func2 = { + let (item, index) = _component + .get_export(None, "some-world-func2") + .ok_or_else(|| { + anyhow::anyhow!("no export `some-world-func2` found") + })?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func + .typecheck::< + (), + (wasmtime::component::Resource,), + >(&_instance_type), + "type-checking export func `some-world-func2`", + )?; + index + } + _ => { + Err( + anyhow::anyhow!( + "export `some-world-func2` is not a function" + ), + )? + } + } + }; Ok(TheWorldIndices { interface1, some_world_func2, @@ -285,6 +277,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface1 = self.interface1.load(&mut store, &_instance)?; let some_world_func2 = *_instance @@ -303,7 +296,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -313,14 +306,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, @@ -2288,49 +2281,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/uses-resource-transitively") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/uses-resource-transitively`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/uses-resource-transitively", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/uses-resource-transitively`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/uses-resource-transitively` does \ @@ -2347,9 +2312,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let handle = *_instance .get_typed_func::< (wasmtime::component::Resource,), 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 49b919e6624d..b018a6ff3059 100644 --- a/crates/component-macro/tests/expanded/resources-import_tracing_async.rs +++ b/crates/component-macro/tests/expanded/resources-import_tracing_async.rs @@ -55,7 +55,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -114,11 +114,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -160,44 +155,41 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface1 = exports::foo::foo::uses_resource_transitively::GuestIndices::new( - _component, + _instance_pre, )?; - let some_world_func2 = _component - .get_export_index(None, "some-world-func2") - .ok_or_else(|| { - anyhow::anyhow!("no function export `some-world-func2` found") - })?; - Ok(TheWorldIndices { - interface1, - some_world_func2, - }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface1 = exports::foo::foo::uses_resource_transitively::GuestIndices::new_instance( - &mut store, - _instance, - )?; - let some_world_func2 = _instance - .get_export_index(&mut store, None, "some-world-func2") - .ok_or_else(|| { - anyhow::anyhow!("no function export `some-world-func2` found") - })?; + let some_world_func2 = { + let (item, index) = _component + .get_export(None, "some-world-func2") + .ok_or_else(|| { + anyhow::anyhow!("no export `some-world-func2` found") + })?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func + .typecheck::< + (), + (wasmtime::component::Resource,), + >(&_instance_type), + "type-checking export func `some-world-func2`", + )?; + index + } + _ => { + Err( + anyhow::anyhow!( + "export `some-world-func2` is not a function" + ), + )? + } + } + }; Ok(TheWorldIndices { interface1, some_world_func2, @@ -213,6 +205,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface1 = self.interface1.load(&mut store, &_instance)?; let some_world_func2 = *_instance @@ -231,7 +224,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -241,14 +234,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, @@ -1683,53 +1676,25 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/uses-resource-transitively") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/uses-resource-transitively`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "foo:foo/uses-resource-transitively", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/uses-resource-transitively`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/uses-resource-transitively` does \ - not have export `{name}`" + not have export `{name}`" ) }) }; @@ -1742,9 +1707,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let handle = *_instance .get_typed_func::< (wasmtime::component::Resource,), diff --git a/crates/component-macro/tests/expanded/share-types.rs b/crates/component-macro/tests/expanded/share-types.rs index f683f0408e55..48494002e72a 100644 --- a/crates/component-macro/tests/expanded/share-types.rs +++ b/crates/component-macro/tests/expanded/share-types.rs @@ -27,7 +27,7 @@ impl<_T> HttpInterfacePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = HttpInterfaceIndices::new(instance_pre.component())?; + let indices = HttpInterfaceIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct HttpInterfaceIndices { /// * If you've instantiated the instance yourself already /// then you can use [`HttpInterface::new`]. /// -/// * You can also access the guts of instantiation through -/// [`HttpInterfaceIndices::new_instance`] followed -/// by [`HttpInterfaceIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,29 +100,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::http_handler::GuestIndices::new(_component)?; - Ok(HttpInterfaceIndices { interface0 }) - } - /// Creates a new instance of [`HttpInterfaceIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`HttpInterface`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`HttpInterface`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::http_handler::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::http_handler::GuestIndices::new(_instance_pre)?; Ok(HttpInterfaceIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -140,6 +118,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(HttpInterface { interface0 }) @@ -149,21 +128,21 @@ const _: () = { /// Convenience wrapper around [`HttpInterfacePre::new`] and /// [`HttpInterfacePre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; HttpInterfacePre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`HttpInterfaceIndices::new_instance`] and + /// Convenience wrapper around [`HttpInterfaceIndices::new`] and /// [`HttpInterfaceIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = HttpInterfaceIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = HttpInterfaceIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -351,38 +330,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "http-handler") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `http-handler`") })?; - Self::_new(|name| component.get_export_index(Some(&instance), name)) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "http-handler") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `http-handler`") - })?; - Self::_new(|name| { - instance.get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `http-handler` does \ @@ -399,9 +359,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let handle_request = *_instance .get_typed_func::< (&Request,), diff --git a/crates/component-macro/tests/expanded/share-types_async.rs b/crates/component-macro/tests/expanded/share-types_async.rs index c967aef2ed89..e94785cf487a 100644 --- a/crates/component-macro/tests/expanded/share-types_async.rs +++ b/crates/component-macro/tests/expanded/share-types_async.rs @@ -27,7 +27,7 @@ impl<_T> HttpInterfacePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = HttpInterfaceIndices::new(instance_pre.component())?; + let indices = HttpInterfaceIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct HttpInterfaceIndices { /// * If you've instantiated the instance yourself already /// then you can use [`HttpInterface::new`]. /// -/// * You can also access the guts of instantiation through -/// [`HttpInterfaceIndices::new_instance`] followed -/// by [`HttpInterfaceIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::http_handler::GuestIndices::new(_component)?; - Ok(HttpInterfaceIndices { interface0 }) - } - /// Creates a new instance of [`HttpInterfaceIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`HttpInterface`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`HttpInterface`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::http_handler::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::http_handler::GuestIndices::new(_instance_pre)?; Ok(HttpInterfaceIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(HttpInterface { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`HttpInterfacePre::new`] and /// [`HttpInterfacePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; HttpInterfacePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`HttpInterfaceIndices::new_instance`] and + /// Convenience wrapper around [`HttpInterfaceIndices::new`] and /// [`HttpInterfaceIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = HttpInterfaceIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = HttpInterfaceIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -373,38 +352,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "http-handler") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `http-handler`") })?; - Self::_new(|name| component.get_export_index(Some(&instance), name)) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "http-handler") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `http-handler`") - })?; - Self::_new(|name| { - instance.get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `http-handler` does \ @@ -421,9 +381,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let handle_request = *_instance .get_typed_func::< (&Request,), diff --git a/crates/component-macro/tests/expanded/share-types_concurrent.rs b/crates/component-macro/tests/expanded/share-types_concurrent.rs index b0c692083dd2..f0d83a3a99a7 100644 --- a/crates/component-macro/tests/expanded/share-types_concurrent.rs +++ b/crates/component-macro/tests/expanded/share-types_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> HttpInterfacePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = HttpInterfaceIndices::new(instance_pre.component())?; + let indices = HttpInterfaceIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct HttpInterfaceIndices { /// * If you've instantiated the instance yourself already /// then you can use [`HttpInterface::new`]. /// -/// * You can also access the guts of instantiation through -/// [`HttpInterfaceIndices::new_instance`] followed -/// by [`HttpInterfaceIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::http_handler::GuestIndices::new(_component)?; - Ok(HttpInterfaceIndices { interface0 }) - } - /// Creates a new instance of [`HttpInterfaceIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`HttpInterface`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`HttpInterface`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::http_handler::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::http_handler::GuestIndices::new(_instance_pre)?; Ok(HttpInterfaceIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(HttpInterface { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`HttpInterfacePre::new`] and /// [`HttpInterfacePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; HttpInterfacePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`HttpInterfaceIndices::new_instance`] and + /// Convenience wrapper around [`HttpInterfaceIndices::new`] and /// [`HttpInterfaceIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = HttpInterfaceIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = HttpInterfaceIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -412,38 +391,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "http-handler") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `http-handler`") })?; - Self::_new(|name| component.get_export_index(Some(&instance), name)) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "http-handler") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `http-handler`") - })?; - Self::_new(|name| { - instance.get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `http-handler` does \ @@ -460,9 +420,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let handle_request = *_instance .get_typed_func::< (&Request,), 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 1c5317bf0ed2..86033c198447 100644 --- a/crates/component-macro/tests/expanded/share-types_tracing_async.rs +++ b/crates/component-macro/tests/expanded/share-types_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> HttpInterfacePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = HttpInterfaceIndices::new(instance_pre.component())?; + let indices = HttpInterfaceIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct HttpInterfaceIndices { /// * If you've instantiated the instance yourself already /// then you can use [`HttpInterface::new`]. /// -/// * You can also access the guts of instantiation through -/// [`HttpInterfaceIndices::new_instance`] followed -/// by [`HttpInterfaceIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::http_handler::GuestIndices::new(_component)?; - Ok(HttpInterfaceIndices { interface0 }) - } - /// Creates a new instance of [`HttpInterfaceIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`HttpInterface`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`HttpInterface`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::http_handler::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::http_handler::GuestIndices::new(_instance_pre)?; Ok(HttpInterfaceIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(HttpInterface { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`HttpInterfacePre::new`] and /// [`HttpInterfacePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; HttpInterfacePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`HttpInterfaceIndices::new_instance`] and + /// Convenience wrapper around [`HttpInterfaceIndices::new`] and /// [`HttpInterfaceIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = HttpInterfaceIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = HttpInterfaceIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -389,38 +368,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "http-handler") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `http-handler`") })?; - Self::_new(|name| component.get_export_index(Some(&instance), name)) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "http-handler") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `http-handler`") - })?; - Self::_new(|name| { - instance.get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `http-handler` does \ @@ -437,9 +397,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let handle_request = *_instance .get_typed_func::< (&Request,), diff --git a/crates/component-macro/tests/expanded/simple-functions.rs b/crates/component-macro/tests/expanded/simple-functions.rs index 336274d19146..e5643c0d9ef8 100644 --- a/crates/component-macro/tests/expanded/simple-functions.rs +++ b/crates/component-macro/tests/expanded/simple-functions.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,28 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::simple::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::simple::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -140,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -149,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -334,45 +315,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple` does \ @@ -401,9 +358,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let f1 = *_instance .get_typed_func::<(), ()>(&mut store, &self.f1)? .func(); diff --git a/crates/component-macro/tests/expanded/simple-functions_async.rs b/crates/component-macro/tests/expanded/simple-functions_async.rs index fa38e0edfa96..8edeb128e234 100644 --- a/crates/component-macro/tests/expanded/simple-functions_async.rs +++ b/crates/component-macro/tests/expanded/simple-functions_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::simple::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::simple::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -358,45 +339,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple` does \ @@ -425,9 +382,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let f1 = *_instance .get_typed_func::<(), ()>(&mut store, &self.f1)? .func(); diff --git a/crates/component-macro/tests/expanded/simple-functions_concurrent.rs b/crates/component-macro/tests/expanded/simple-functions_concurrent.rs index 9277a0f3ed90..d941210c1e39 100644 --- a/crates/component-macro/tests/expanded/simple-functions_concurrent.rs +++ b/crates/component-macro/tests/expanded/simple-functions_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::simple::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::simple::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -593,45 +574,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple` does \ @@ -660,9 +617,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let f1 = *_instance .get_typed_func::<(), ()>(&mut store, &self.f1)? .func(); 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 a7259a7dd434..febbad0ed723 100644 --- a/crates/component-macro/tests/expanded/simple-functions_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-functions_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::simple::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::simple::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -446,45 +427,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple` does \ @@ -513,9 +470,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let f1 = *_instance .get_typed_func::<(), ()>(&mut store, &self.f1)? .func(); diff --git a/crates/component-macro/tests/expanded/simple-lists.rs b/crates/component-macro/tests/expanded/simple-lists.rs index 1261b3b30ab0..e5c84d2c5179 100644 --- a/crates/component-macro/tests/expanded/simple-lists.rs +++ b/crates/component-macro/tests/expanded/simple-lists.rs @@ -27,7 +27,7 @@ impl<_T> MyWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = MyWorldIndices::new(instance_pre.component())?; + let indices = MyWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct MyWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`MyWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`MyWorldIndices::new_instance`] followed -/// by [`MyWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,30 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::simple_lists::GuestIndices::new( - _component, - )?; - Ok(MyWorldIndices { interface0 }) - } - /// Creates a new instance of [`MyWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`MyWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`MyWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple_lists::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(MyWorldIndices { interface0 }) } @@ -142,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(MyWorld { interface0 }) @@ -151,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`MyWorldPre::new`] and /// [`MyWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; MyWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`MyWorldIndices::new_instance`] and + /// Convenience wrapper around [`MyWorldIndices::new`] and /// [`MyWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = MyWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -357,45 +336,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple-lists") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple-lists`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple-lists") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-lists`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple-lists` does \ @@ -420,9 +375,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let simple_list1 = *_instance .get_typed_func::< (&[u32],), diff --git a/crates/component-macro/tests/expanded/simple-lists_async.rs b/crates/component-macro/tests/expanded/simple-lists_async.rs index 27f090dcc036..0d88dd8389bf 100644 --- a/crates/component-macro/tests/expanded/simple-lists_async.rs +++ b/crates/component-macro/tests/expanded/simple-lists_async.rs @@ -27,7 +27,7 @@ impl<_T> MyWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = MyWorldIndices::new(instance_pre.component())?; + let indices = MyWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct MyWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`MyWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`MyWorldIndices::new_instance`] followed -/// by [`MyWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::simple_lists::GuestIndices::new( - _component, - )?; - Ok(MyWorldIndices { interface0 }) - } - /// Creates a new instance of [`MyWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`MyWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`MyWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple_lists::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(MyWorldIndices { interface0 }) } @@ -145,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(MyWorld { interface0 }) @@ -154,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`MyWorldPre::new`] and /// [`MyWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -164,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; MyWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`MyWorldIndices::new_instance`] and + /// Convenience wrapper around [`MyWorldIndices::new`] and /// [`MyWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = MyWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -381,45 +360,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple-lists") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple-lists`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple-lists") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-lists`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple-lists` does \ @@ -444,9 +399,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let simple_list1 = *_instance .get_typed_func::< (&[u32],), diff --git a/crates/component-macro/tests/expanded/simple-lists_concurrent.rs b/crates/component-macro/tests/expanded/simple-lists_concurrent.rs index 697eeab70fa1..df1c66cb4d13 100644 --- a/crates/component-macro/tests/expanded/simple-lists_concurrent.rs +++ b/crates/component-macro/tests/expanded/simple-lists_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> MyWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = MyWorldIndices::new(instance_pre.component())?; + let indices = MyWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct MyWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`MyWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`MyWorldIndices::new_instance`] followed -/// by [`MyWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::simple_lists::GuestIndices::new( - _component, - )?; - Ok(MyWorldIndices { interface0 }) - } - /// Creates a new instance of [`MyWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`MyWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`MyWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple_lists::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(MyWorldIndices { interface0 }) } @@ -145,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(MyWorld { interface0 }) @@ -154,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`MyWorldPre::new`] and /// [`MyWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -164,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; MyWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`MyWorldIndices::new_instance`] and + /// Convenience wrapper around [`MyWorldIndices::new`] and /// [`MyWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = MyWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -548,45 +527,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple-lists") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple-lists`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple-lists") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-lists`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple-lists` does \ @@ -611,9 +566,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let simple_list1 = *_instance .get_typed_func::< (&[u32],), 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 6f64664f077c..07dd9138a0df 100644 --- a/crates/component-macro/tests/expanded/simple-lists_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-lists_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> MyWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = MyWorldIndices::new(instance_pre.component())?; + let indices = MyWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct MyWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`MyWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`MyWorldIndices::new_instance`] followed -/// by [`MyWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::foo::foo::simple_lists::GuestIndices::new( - _component, - )?; - Ok(MyWorldIndices { interface0 }) - } - /// Creates a new instance of [`MyWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`MyWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`MyWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::simple_lists::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(MyWorldIndices { interface0 }) } @@ -145,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(MyWorld { interface0 }) @@ -154,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`MyWorldPre::new`] and /// [`MyWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -164,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; MyWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`MyWorldIndices::new_instance`] and + /// Convenience wrapper around [`MyWorldIndices::new`] and /// [`MyWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = MyWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -442,45 +421,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/simple-lists") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/simple-lists`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/simple-lists") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/simple-lists`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/simple-lists` does \ @@ -505,9 +460,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let simple_list1 = *_instance .get_typed_func::< (&[u32],), diff --git a/crates/component-macro/tests/expanded/simple-wasi.rs b/crates/component-macro/tests/expanded/simple-wasi.rs index 78d7e3dd7c83..891eb30ca4a6 100644 --- a/crates/component-macro/tests/expanded/simple-wasi.rs +++ b/crates/component-macro/tests/expanded/simple-wasi.rs @@ -27,7 +27,7 @@ impl<_T> WasiPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = WasiIndices::new(instance_pre.component())?; + let indices = WasiIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct WasiIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Wasi::new`]. /// -/// * You can also access the guts of instantiation through -/// [`WasiIndices::new_instance`] followed -/// by [`WasiIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -101,24 +96,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(WasiIndices {}) - } - /// Creates a new instance of [`WasiIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Wasi`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Wasi`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(WasiIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -131,6 +113,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Wasi {}) } @@ -139,21 +122,21 @@ const _: () = { /// Convenience wrapper around [`WasiPre::new`] and /// [`WasiPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; WasiPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`WasiIndices::new_instance`] and + /// Convenience wrapper around [`WasiIndices::new`] and /// [`WasiIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = WasiIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = WasiIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/simple-wasi_async.rs b/crates/component-macro/tests/expanded/simple-wasi_async.rs index 353b89135549..395f8788bd77 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_async.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_async.rs @@ -27,7 +27,7 @@ impl<_T> WasiPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = WasiIndices::new(instance_pre.component())?; + let indices = WasiIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct WasiIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Wasi::new`]. /// -/// * You can also access the guts of instantiation through -/// [`WasiIndices::new_instance`] followed -/// by [`WasiIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(WasiIndices {}) - } - /// Creates a new instance of [`WasiIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Wasi`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Wasi`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(WasiIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Wasi {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`WasiPre::new`] and /// [`WasiPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; WasiPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`WasiIndices::new_instance`] and + /// Convenience wrapper around [`WasiIndices::new`] and /// [`WasiIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = WasiIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = WasiIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs b/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs index 795efdc7e066..47553d7137fc 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> WasiPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = WasiIndices::new(instance_pre.component())?; + let indices = WasiIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct WasiIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Wasi::new`]. /// -/// * You can also access the guts of instantiation through -/// [`WasiIndices::new_instance`] followed -/// by [`WasiIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(WasiIndices {}) - } - /// Creates a new instance of [`WasiIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Wasi`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Wasi`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(WasiIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Wasi {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`WasiPre::new`] and /// [`WasiPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; WasiPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`WasiIndices::new_instance`] and + /// Convenience wrapper around [`WasiIndices::new`] and /// [`WasiIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = WasiIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = WasiIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, 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 06901e1fc71e..195f5068b98e 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> WasiPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = WasiIndices::new(instance_pre.component())?; + let indices = WasiIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct WasiIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Wasi::new`]. /// -/// * You can also access the guts of instantiation through -/// [`WasiIndices::new_instance`] followed -/// by [`WasiIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(WasiIndices {}) - } - /// Creates a new instance of [`WasiIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Wasi`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Wasi`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(WasiIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Wasi {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`WasiPre::new`] and /// [`WasiPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; WasiPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`WasiIndices::new_instance`] and + /// Convenience wrapper around [`WasiIndices::new`] and /// [`WasiIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = WasiIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = WasiIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/small-anonymous.rs b/crates/component-macro/tests/expanded/small-anonymous.rs index 00f75bbde2ba..39fe52307cf6 100644 --- a/crates/component-macro/tests/expanded/small-anonymous.rs +++ b/crates/component-macro/tests/expanded/small-anonymous.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,29 +100,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::anon::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::anon::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::anon::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -140,6 +118,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -149,21 +128,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -362,41 +341,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/anon") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `foo:foo/anon`") })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/anon") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `foo:foo/anon`") - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/anon` does \ @@ -413,9 +370,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let option_test = *_instance .get_typed_func::< (), diff --git a/crates/component-macro/tests/expanded/small-anonymous_async.rs b/crates/component-macro/tests/expanded/small-anonymous_async.rs index a76dc8ba35bc..48f23bbe83c9 100644 --- a/crates/component-macro/tests/expanded/small-anonymous_async.rs +++ b/crates/component-macro/tests/expanded/small-anonymous_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::anon::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::anon::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::anon::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -376,41 +355,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/anon") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `foo:foo/anon`") })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/anon") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `foo:foo/anon`") - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/anon` does \ @@ -427,9 +384,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let option_test = *_instance .get_typed_func::< (), diff --git a/crates/component-macro/tests/expanded/small-anonymous_concurrent.rs b/crates/component-macro/tests/expanded/small-anonymous_concurrent.rs index 5e3cc9ce08b2..c7c4aa02fd87 100644 --- a/crates/component-macro/tests/expanded/small-anonymous_concurrent.rs +++ b/crates/component-macro/tests/expanded/small-anonymous_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::anon::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::anon::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::anon::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -430,41 +409,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/anon") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `foo:foo/anon`") })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/anon") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `foo:foo/anon`") - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/anon` does \ @@ -481,9 +438,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let option_test = *_instance .get_typed_func::< (), 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 cfbfa8f52210..2675ea6f8b01 100644 --- a/crates/component-macro/tests/expanded/small-anonymous_tracing_async.rs +++ b/crates/component-macro/tests/expanded/small-anonymous_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::anon::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::anon::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::anon::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -389,41 +368,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/anon") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `foo:foo/anon`") })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/anon") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `foo:foo/anon`") - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/anon` does \ @@ -440,9 +397,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let option_test = *_instance .get_typed_func::< (), diff --git a/crates/component-macro/tests/expanded/smoke-default.rs b/crates/component-macro/tests/expanded/smoke-default.rs index 97a649697049..7dbcfa49bd40 100644 --- a/crates/component-macro/tests/expanded/smoke-default.rs +++ b/crates/component-macro/tests/expanded/smoke-default.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,30 +100,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let y = _component - .get_export_index(None, "y") - .ok_or_else(|| anyhow::anyhow!("no function export `y` found"))?; - Ok(TheWorldIndices { y }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let y = _instance - .get_export_index(&mut store, None, "y") - .ok_or_else(|| anyhow::anyhow!("no function export `y` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let y = { + let (item, index) = _component + .get_export(None, "y") + .ok_or_else(|| anyhow::anyhow!("no export `y` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ()>(&_instance_type), + "type-checking export func `y`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `y` is not a function"))?, + } + }; Ok(TheWorldIndices { y }) } /// Uses the indices stored in `self` to load an instance @@ -141,6 +132,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let y = *_instance.get_typed_func::<(), ()>(&mut store, &self.y)?.func(); Ok(TheWorld { y }) @@ -150,21 +142,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn call_y( &self, diff --git a/crates/component-macro/tests/expanded/smoke-default_async.rs b/crates/component-macro/tests/expanded/smoke-default_async.rs index 19fbdf4b34b0..eeef13c512f0 100644 --- a/crates/component-macro/tests/expanded/smoke-default_async.rs +++ b/crates/component-macro/tests/expanded/smoke-default_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let y = _component - .get_export_index(None, "y") - .ok_or_else(|| anyhow::anyhow!("no function export `y` found"))?; - Ok(TheWorldIndices { y }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let y = _instance - .get_export_index(&mut store, None, "y") - .ok_or_else(|| anyhow::anyhow!("no function export `y` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let y = { + let (item, index) = _component + .get_export(None, "y") + .ok_or_else(|| anyhow::anyhow!("no export `y` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ()>(&_instance_type), + "type-checking export func `y`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `y` is not a function"))?, + } + }; Ok(TheWorldIndices { y }) } /// Uses the indices stored in `self` to load an instance @@ -144,6 +135,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let y = *_instance.get_typed_func::<(), ()>(&mut store, &self.y)?.func(); Ok(TheWorld { y }) @@ -153,7 +145,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -163,14 +155,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub async fn call_y( &self, diff --git a/crates/component-macro/tests/expanded/smoke-default_concurrent.rs b/crates/component-macro/tests/expanded/smoke-default_concurrent.rs index fd88097b869f..ed9fdb7e9752 100644 --- a/crates/component-macro/tests/expanded/smoke-default_concurrent.rs +++ b/crates/component-macro/tests/expanded/smoke-default_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let y = _component - .get_export_index(None, "y") - .ok_or_else(|| anyhow::anyhow!("no function export `y` found"))?; - Ok(TheWorldIndices { y }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let y = _instance - .get_export_index(&mut store, None, "y") - .ok_or_else(|| anyhow::anyhow!("no function export `y` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let y = { + let (item, index) = _component + .get_export(None, "y") + .ok_or_else(|| anyhow::anyhow!("no export `y` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ()>(&_instance_type), + "type-checking export func `y`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `y` is not a function"))?, + } + }; Ok(TheWorldIndices { y }) } /// Uses the indices stored in `self` to load an instance @@ -144,6 +135,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let y = *_instance.get_typed_func::<(), ()>(&mut store, &self.y)?.func(); Ok(TheWorld { y }) @@ -153,7 +145,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -163,14 +155,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub async fn call_y( &self, diff --git a/crates/component-macro/tests/expanded/smoke-default_tracing_async.rs b/crates/component-macro/tests/expanded/smoke-default_tracing_async.rs index 66c96eb89140..59adb46dcfd9 100644 --- a/crates/component-macro/tests/expanded/smoke-default_tracing_async.rs +++ b/crates/component-macro/tests/expanded/smoke-default_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let y = _component - .get_export_index(None, "y") - .ok_or_else(|| anyhow::anyhow!("no function export `y` found"))?; - Ok(TheWorldIndices { y }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let y = _instance - .get_export_index(&mut store, None, "y") - .ok_or_else(|| anyhow::anyhow!("no function export `y` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let y = { + let (item, index) = _component + .get_export(None, "y") + .ok_or_else(|| anyhow::anyhow!("no export `y` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ()>(&_instance_type), + "type-checking export func `y`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `y` is not a function"))?, + } + }; Ok(TheWorldIndices { y }) } /// Uses the indices stored in `self` to load an instance @@ -144,6 +135,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let y = *_instance.get_typed_func::<(), ()>(&mut store, &self.y)?.func(); Ok(TheWorld { y }) @@ -153,7 +145,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -163,14 +155,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub async fn call_y( &self, diff --git a/crates/component-macro/tests/expanded/smoke-export.rs b/crates/component-macro/tests/expanded/smoke-export.rs index 3a4ea7ae8a1b..3919bba6c91e 100644 --- a/crates/component-macro/tests/expanded/smoke-export.rs +++ b/crates/component-macro/tests/expanded/smoke-export.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,29 +100,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::the_name::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::the_name::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::the_name::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -140,6 +118,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -149,21 +128,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn the_name(&self) -> &exports::the_name::Guest { &self.interface0 @@ -189,38 +168,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "the-name") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `the-name`") })?; - Self::_new(|name| component.get_export_index(Some(&instance), name)) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "the-name") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `the-name`") - })?; - Self::_new(|name| { - instance.get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `the-name` does \ @@ -237,9 +197,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let y = *_instance.get_typed_func::<(), ()>(&mut store, &self.y)?.func(); Ok(Guest { y }) } diff --git a/crates/component-macro/tests/expanded/smoke-export_async.rs b/crates/component-macro/tests/expanded/smoke-export_async.rs index 7bfa2092e12d..2abe17cafa45 100644 --- a/crates/component-macro/tests/expanded/smoke-export_async.rs +++ b/crates/component-macro/tests/expanded/smoke-export_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::the_name::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::the_name::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::the_name::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn the_name(&self) -> &exports::the_name::Guest { &self.interface0 @@ -195,38 +174,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "the-name") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `the-name`") })?; - Self::_new(|name| component.get_export_index(Some(&instance), name)) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "the-name") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `the-name`") - })?; - Self::_new(|name| { - instance.get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `the-name` does \ @@ -243,9 +203,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let y = *_instance.get_typed_func::<(), ()>(&mut store, &self.y)?.func(); Ok(Guest { y }) } diff --git a/crates/component-macro/tests/expanded/smoke-export_concurrent.rs b/crates/component-macro/tests/expanded/smoke-export_concurrent.rs index 429e621c12ec..c4928b427308 100644 --- a/crates/component-macro/tests/expanded/smoke-export_concurrent.rs +++ b/crates/component-macro/tests/expanded/smoke-export_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::the_name::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::the_name::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::the_name::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn the_name(&self) -> &exports::the_name::Guest { &self.interface0 @@ -195,38 +174,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "the-name") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `the-name`") })?; - Self::_new(|name| component.get_export_index(Some(&instance), name)) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "the-name") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `the-name`") - })?; - Self::_new(|name| { - instance.get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `the-name` does \ @@ -243,9 +203,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let y = *_instance.get_typed_func::<(), ()>(&mut store, &self.y)?.func(); Ok(Guest { y }) } diff --git a/crates/component-macro/tests/expanded/smoke-export_tracing_async.rs b/crates/component-macro/tests/expanded/smoke-export_tracing_async.rs index dfc97d120e87..8d2475dd97b0 100644 --- a/crates/component-macro/tests/expanded/smoke-export_tracing_async.rs +++ b/crates/component-macro/tests/expanded/smoke-export_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,29 +103,12 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::the_name::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::the_name::GuestIndices::new_instance( - &mut store, - _instance, - )?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::the_name::GuestIndices::new(_instance_pre)?; Ok(TheWorldIndices { interface0 }) } /// Uses the indices stored in `self` to load an instance @@ -143,6 +121,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +131,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +141,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn the_name(&self) -> &exports::the_name::Guest { &self.interface0 @@ -195,38 +174,19 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "the-name") .ok_or_else(|| { anyhow::anyhow!("no exported instance named `the-name`") })?; - Self::_new(|name| component.get_export_index(Some(&instance), name)) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "the-name") - .ok_or_else(|| { - anyhow::anyhow!("no exported instance named `the-name`") - })?; - Self::_new(|name| { - instance.get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `the-name` does \ @@ -243,9 +203,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let y = *_instance.get_typed_func::<(), ()>(&mut store, &self.y)?.func(); Ok(Guest { y }) } diff --git a/crates/component-macro/tests/expanded/smoke.rs b/crates/component-macro/tests/expanded/smoke.rs index b22a0aa67a3e..e8c7fce517a4 100644 --- a/crates/component-macro/tests/expanded/smoke.rs +++ b/crates/component-macro/tests/expanded/smoke.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct TheWorldIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -101,24 +96,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(TheWorldIndices {}) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(TheWorldIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -131,6 +113,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(TheWorld {}) } @@ -139,21 +122,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/smoke_async.rs b/crates/component-macro/tests/expanded/smoke_async.rs index 8881ec5defc1..24af9997357a 100644 --- a/crates/component-macro/tests/expanded/smoke_async.rs +++ b/crates/component-macro/tests/expanded/smoke_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct TheWorldIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(TheWorldIndices {}) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(TheWorldIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(TheWorld {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/smoke_concurrent.rs b/crates/component-macro/tests/expanded/smoke_concurrent.rs index 2cd14753fdfc..b8d2682cfd97 100644 --- a/crates/component-macro/tests/expanded/smoke_concurrent.rs +++ b/crates/component-macro/tests/expanded/smoke_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct TheWorldIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(TheWorldIndices {}) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(TheWorldIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(TheWorld {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/smoke_tracing_async.rs b/crates/component-macro/tests/expanded/smoke_tracing_async.rs index 319acdcf7339..af389d6adb60 100644 --- a/crates/component-macro/tests/expanded/smoke_tracing_async.rs +++ b/crates/component-macro/tests/expanded/smoke_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct TheWorldIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(TheWorldIndices {}) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(TheWorldIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(TheWorld {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/strings.rs b/crates/component-macro/tests/expanded/strings.rs index 77241f81b4db..5de55dedba80 100644 --- a/crates/component-macro/tests/expanded/strings.rs +++ b/crates/component-macro/tests/expanded/strings.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,28 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::strings::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::strings::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::strings::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -140,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -149,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -306,45 +287,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/strings") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/strings`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/strings") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/strings`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/strings` does \ @@ -363,9 +320,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let a = *_instance .get_typed_func::<(&str,), ()>(&mut store, &self.a)? .func(); diff --git a/crates/component-macro/tests/expanded/strings_async.rs b/crates/component-macro/tests/expanded/strings_async.rs index a4bdd19e3cc8..e2aab32b14cf 100644 --- a/crates/component-macro/tests/expanded/strings_async.rs +++ b/crates/component-macro/tests/expanded/strings_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::strings::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::strings::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::strings::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -324,45 +305,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/strings") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/strings`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/strings") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/strings`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/strings` does \ @@ -381,9 +338,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let a = *_instance .get_typed_func::<(&str,), ()>(&mut store, &self.a)? .func(); diff --git a/crates/component-macro/tests/expanded/strings_concurrent.rs b/crates/component-macro/tests/expanded/strings_concurrent.rs index e2085ac51125..4b128c5bc197 100644 --- a/crates/component-macro/tests/expanded/strings_concurrent.rs +++ b/crates/component-macro/tests/expanded/strings_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::strings::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::strings::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::strings::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -442,45 +423,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/strings") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/strings`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/strings") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/strings`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/strings` does \ @@ -499,9 +456,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let a = *_instance .get_typed_func::<(&str,), ()>(&mut store, &self.a)? .func(); diff --git a/crates/component-macro/tests/expanded/strings_tracing_async.rs b/crates/component-macro/tests/expanded/strings_tracing_async.rs index 30f1a3727b94..d06072db6638 100644 --- a/crates/component-macro/tests/expanded/strings_tracing_async.rs +++ b/crates/component-macro/tests/expanded/strings_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct TheWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::strings::GuestIndices::new(_component)?; - Ok(TheWorldIndices { interface0 }) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::strings::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::strings::GuestIndices::new( + _instance_pre, )?; Ok(TheWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(TheWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -369,45 +350,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/strings") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/strings`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/strings") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/strings`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/strings` does \ @@ -426,9 +383,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let a = *_instance .get_typed_func::<(&str,), ()>(&mut store, &self.a)? .func(); diff --git a/crates/component-macro/tests/expanded/unstable-features.rs b/crates/component-macro/tests/expanded/unstable-features.rs index 82dbb67771cd..e178902d0d96 100644 --- a/crates/component-macro/tests/expanded/unstable-features.rs +++ b/crates/component-macro/tests/expanded/unstable-features.rs @@ -120,7 +120,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -173,11 +173,6 @@ pub struct TheWorldIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -215,24 +210,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(TheWorldIndices {}) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(TheWorldIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -245,6 +227,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(TheWorld {}) } @@ -253,21 +236,21 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/unstable-features_async.rs b/crates/component-macro/tests/expanded/unstable-features_async.rs index 53c45414e337..486e0bbe315e 100644 --- a/crates/component-macro/tests/expanded/unstable-features_async.rs +++ b/crates/component-macro/tests/expanded/unstable-features_async.rs @@ -127,7 +127,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -183,11 +183,6 @@ pub struct TheWorldIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -226,24 +221,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(TheWorldIndices {}) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(TheWorldIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -256,6 +238,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(TheWorld {}) } @@ -264,7 +247,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -274,14 +257,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/unstable-features_concurrent.rs b/crates/component-macro/tests/expanded/unstable-features_concurrent.rs index e4862966880c..bd6ab2053347 100644 --- a/crates/component-macro/tests/expanded/unstable-features_concurrent.rs +++ b/crates/component-macro/tests/expanded/unstable-features_concurrent.rs @@ -141,7 +141,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -197,11 +197,6 @@ pub struct TheWorldIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -258,24 +253,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(TheWorldIndices {}) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(TheWorldIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -288,6 +270,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(TheWorld {}) } @@ -296,7 +279,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -306,14 +289,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, 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 3cff35365b59..b4fff2a3cb80 100644 --- a/crates/component-macro/tests/expanded/unstable-features_tracing_async.rs +++ b/crates/component-macro/tests/expanded/unstable-features_tracing_async.rs @@ -127,7 +127,7 @@ impl<_T> TheWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = TheWorldIndices::new(instance_pre.component())?; + let indices = TheWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -183,11 +183,6 @@ pub struct TheWorldIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`TheWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`TheWorldIndices::new_instance`] followed -/// by [`TheWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -226,24 +221,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(TheWorldIndices {}) - } - /// Creates a new instance of [`TheWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`TheWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`TheWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(TheWorldIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -256,6 +238,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(TheWorld {}) } @@ -264,7 +247,7 @@ const _: () = { /// Convenience wrapper around [`TheWorldPre::new`] and /// [`TheWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -274,14 +257,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; TheWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`TheWorldIndices::new_instance`] and + /// Convenience wrapper around [`TheWorldIndices::new`] and /// [`TheWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = TheWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = TheWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker_imports_get_host< T, diff --git a/crates/component-macro/tests/expanded/unversioned-foo.rs b/crates/component-macro/tests/expanded/unversioned-foo.rs index 5ca7b58ae2f0..e94db455a6b4 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo.rs @@ -27,7 +27,7 @@ impl<_T> NopePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = NopeIndices::new(instance_pre.component())?; + let indices = NopeIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct NopeIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Nope::new`]. /// -/// * You can also access the guts of instantiation through -/// [`NopeIndices::new_instance`] followed -/// by [`NopeIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -101,24 +96,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(NopeIndices {}) - } - /// Creates a new instance of [`NopeIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Nope`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Nope`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(NopeIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -131,6 +113,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Nope {}) } @@ -139,21 +122,21 @@ const _: () = { /// Convenience wrapper around [`NopePre::new`] and /// [`NopePre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; NopePre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`NopeIndices::new_instance`] and + /// Convenience wrapper around [`NopeIndices::new`] and /// [`NopeIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = NopeIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = NopeIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/unversioned-foo_async.rs b/crates/component-macro/tests/expanded/unversioned-foo_async.rs index 46eb9b5dd729..c3a0079aaebe 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo_async.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo_async.rs @@ -27,7 +27,7 @@ impl<_T> NopePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = NopeIndices::new(instance_pre.component())?; + let indices = NopeIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct NopeIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Nope::new`]. /// -/// * You can also access the guts of instantiation through -/// [`NopeIndices::new_instance`] followed -/// by [`NopeIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(NopeIndices {}) - } - /// Creates a new instance of [`NopeIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Nope`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Nope`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(NopeIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Nope {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`NopePre::new`] and /// [`NopePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; NopePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`NopeIndices::new_instance`] and + /// Convenience wrapper around [`NopeIndices::new`] and /// [`NopeIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = NopeIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = NopeIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs b/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs index cf3a146c2dbf..569e43f5ab4b 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> NopePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = NopeIndices::new(instance_pre.component())?; + let indices = NopeIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct NopeIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Nope::new`]. /// -/// * You can also access the guts of instantiation through -/// [`NopeIndices::new_instance`] followed -/// by [`NopeIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(NopeIndices {}) - } - /// Creates a new instance of [`NopeIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Nope`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Nope`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(NopeIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Nope {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`NopePre::new`] and /// [`NopePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; NopePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`NopeIndices::new_instance`] and + /// Convenience wrapper around [`NopeIndices::new`] and /// [`NopeIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = NopeIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = NopeIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, 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 104bba6fa1c8..3ddc83b90c35 100644 --- a/crates/component-macro/tests/expanded/unversioned-foo_tracing_async.rs +++ b/crates/component-macro/tests/expanded/unversioned-foo_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> NopePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = NopeIndices::new(instance_pre.component())?; + let indices = NopeIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct NopeIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`Nope::new`]. /// -/// * You can also access the guts of instantiation through -/// [`NopeIndices::new_instance`] followed -/// by [`NopeIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(NopeIndices {}) - } - /// Creates a new instance of [`NopeIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Nope`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Nope`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(NopeIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(Nope {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`NopePre::new`] and /// [`NopePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; NopePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`NopeIndices::new_instance`] and + /// Convenience wrapper around [`NopeIndices::new`] and /// [`NopeIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = NopeIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = NopeIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/use-paths.rs b/crates/component-macro/tests/expanded/use-paths.rs index 647b0a880919..9e533d46add7 100644 --- a/crates/component-macro/tests/expanded/use-paths.rs +++ b/crates/component-macro/tests/expanded/use-paths.rs @@ -27,7 +27,7 @@ impl<_T> DPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = DIndices::new(instance_pre.component())?; + let indices = DIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -80,11 +80,6 @@ pub struct DIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`D::new`]. /// -/// * You can also access the guts of instantiation through -/// [`DIndices::new_instance`] followed -/// by [`DIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -101,24 +96,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(DIndices {}) - } - /// Creates a new instance of [`DIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`D`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`D`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(DIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -131,6 +113,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(D {}) } @@ -139,21 +122,21 @@ const _: () = { /// Convenience wrapper around [`DPre::new`] and /// [`DPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; DPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`DIndices::new_instance`] and + /// Convenience wrapper around [`DIndices::new`] and /// [`DIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = DIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = DIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/use-paths_async.rs b/crates/component-macro/tests/expanded/use-paths_async.rs index 3123a45dcc94..3d1278446c33 100644 --- a/crates/component-macro/tests/expanded/use-paths_async.rs +++ b/crates/component-macro/tests/expanded/use-paths_async.rs @@ -27,7 +27,7 @@ impl<_T> DPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = DIndices::new(instance_pre.component())?; + let indices = DIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct DIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`D::new`]. /// -/// * You can also access the guts of instantiation through -/// [`DIndices::new_instance`] followed -/// by [`DIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(DIndices {}) - } - /// Creates a new instance of [`DIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`D`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`D`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(DIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(D {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`DPre::new`] and /// [`DPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; DPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`DIndices::new_instance`] and + /// Convenience wrapper around [`DIndices::new`] and /// [`DIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = DIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = DIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/use-paths_concurrent.rs b/crates/component-macro/tests/expanded/use-paths_concurrent.rs index fe0ccf94cdaa..c07747f22522 100644 --- a/crates/component-macro/tests/expanded/use-paths_concurrent.rs +++ b/crates/component-macro/tests/expanded/use-paths_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> DPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = DIndices::new(instance_pre.component())?; + let indices = DIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct DIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`D::new`]. /// -/// * You can also access the guts of instantiation through -/// [`DIndices::new_instance`] followed -/// by [`DIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(DIndices {}) - } - /// Creates a new instance of [`DIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`D`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`D`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(DIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(D {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`DPre::new`] and /// [`DPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; DPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`DIndices::new_instance`] and + /// Convenience wrapper around [`DIndices::new`] and /// [`DIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = DIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = DIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, 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 e0ffb652e629..d337f314d4f7 100644 --- a/crates/component-macro/tests/expanded/use-paths_tracing_async.rs +++ b/crates/component-macro/tests/expanded/use-paths_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> DPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = DIndices::new(instance_pre.component())?; + let indices = DIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -83,11 +83,6 @@ pub struct DIndices {} /// * If you've instantiated the instance yourself already /// then you can use [`D::new`]. /// -/// * You can also access the guts of instantiation through -/// [`DIndices::new_instance`] followed -/// by [`DIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -104,24 +99,11 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, - ) -> wasmtime::Result { - let _component = component; - Ok(DIndices {}) - } - /// Creates a new instance of [`DIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`D`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`D`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _instance = instance; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); Ok(DIndices {}) } /// Uses the indices stored in `self` to load an instance @@ -134,6 +116,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; Ok(D {}) } @@ -142,7 +125,7 @@ const _: () = { /// Convenience wrapper around [`DPre::new`] and /// [`DPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -152,14 +135,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; DPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`DIndices::new_instance`] and + /// Convenience wrapper around [`DIndices::new`] and /// [`DIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = DIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = DIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/component-macro/tests/expanded/variants.rs b/crates/component-macro/tests/expanded/variants.rs index f3416664e94a..d7ea90fac136 100644 --- a/crates/component-macro/tests/expanded/variants.rs +++ b/crates/component-macro/tests/expanded/variants.rs @@ -27,7 +27,7 @@ impl<_T> MyWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = MyWorldIndices::new(instance_pre.component())?; + let indices = MyWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct MyWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`MyWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`MyWorldIndices::new_instance`] followed -/// by [`MyWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,28 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::variants::GuestIndices::new(_component)?; - Ok(MyWorldIndices { interface0 }) - } - /// Creates a new instance of [`MyWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`MyWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`MyWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::variants::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::variants::GuestIndices::new( + _instance_pre, )?; Ok(MyWorldIndices { interface0 }) } @@ -140,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(MyWorld { interface0 }) @@ -149,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`MyWorldPre::new`] and /// [`MyWorldPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; MyWorldPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`MyWorldIndices::new_instance`] and + /// Convenience wrapper around [`MyWorldIndices::new`] and /// [`MyWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = MyWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -1305,45 +1286,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/variants") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/variants`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/variants") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/variants`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/variants` does \ @@ -1400,9 +1357,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let e1_arg = *_instance .get_typed_func::<(E1,), ()>(&mut store, &self.e1_arg)? .func(); diff --git a/crates/component-macro/tests/expanded/variants_async.rs b/crates/component-macro/tests/expanded/variants_async.rs index caa28a40bc27..b0d3bebb582e 100644 --- a/crates/component-macro/tests/expanded/variants_async.rs +++ b/crates/component-macro/tests/expanded/variants_async.rs @@ -27,7 +27,7 @@ impl<_T> MyWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = MyWorldIndices::new(instance_pre.component())?; + let indices = MyWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct MyWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`MyWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`MyWorldIndices::new_instance`] followed -/// by [`MyWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::variants::GuestIndices::new(_component)?; - Ok(MyWorldIndices { interface0 }) - } - /// Creates a new instance of [`MyWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`MyWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`MyWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::variants::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::variants::GuestIndices::new( + _instance_pre, )?; Ok(MyWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(MyWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`MyWorldPre::new`] and /// [`MyWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; MyWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`MyWorldIndices::new_instance`] and + /// Convenience wrapper around [`MyWorldIndices::new`] and /// [`MyWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = MyWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -1360,45 +1341,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/variants") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/variants`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/variants") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/variants`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/variants` does \ @@ -1455,9 +1412,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let e1_arg = *_instance .get_typed_func::<(E1,), ()>(&mut store, &self.e1_arg)? .func(); diff --git a/crates/component-macro/tests/expanded/variants_concurrent.rs b/crates/component-macro/tests/expanded/variants_concurrent.rs index c4eb60cee145..1eb94b7819de 100644 --- a/crates/component-macro/tests/expanded/variants_concurrent.rs +++ b/crates/component-macro/tests/expanded/variants_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> MyWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = MyWorldIndices::new(instance_pre.component())?; + let indices = MyWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct MyWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`MyWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`MyWorldIndices::new_instance`] followed -/// by [`MyWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::variants::GuestIndices::new(_component)?; - Ok(MyWorldIndices { interface0 }) - } - /// Creates a new instance of [`MyWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`MyWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`MyWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::variants::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::variants::GuestIndices::new( + _instance_pre, )?; Ok(MyWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(MyWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`MyWorldPre::new`] and /// [`MyWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; MyWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`MyWorldIndices::new_instance`] and + /// Convenience wrapper around [`MyWorldIndices::new`] and /// [`MyWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = MyWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -2170,45 +2151,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/variants") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/variants`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/variants") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/variants`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/variants` does \ @@ -2265,9 +2222,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let e1_arg = *_instance .get_typed_func::<(E1,), ()>(&mut store, &self.e1_arg)? .func(); diff --git a/crates/component-macro/tests/expanded/variants_tracing_async.rs b/crates/component-macro/tests/expanded/variants_tracing_async.rs index 2416df123527..b69f48608eb6 100644 --- a/crates/component-macro/tests/expanded/variants_tracing_async.rs +++ b/crates/component-macro/tests/expanded/variants_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> MyWorldPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = MyWorldIndices::new(instance_pre.component())?; + let indices = MyWorldIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct MyWorldIndices { /// * If you've instantiated the instance yourself already /// then you can use [`MyWorld::new`]. /// -/// * You can also access the guts of instantiation through -/// [`MyWorldIndices::new_instance`] followed -/// by [`MyWorldIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,28 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let interface0 = exports::foo::foo::variants::GuestIndices::new(_component)?; - Ok(MyWorldIndices { interface0 }) - } - /// Creates a new instance of [`MyWorldIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`MyWorld`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`MyWorld`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::foo::foo::variants::GuestIndices::new_instance( - &mut store, - _instance, + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let interface0 = exports::foo::foo::variants::GuestIndices::new( + _instance_pre, )?; Ok(MyWorldIndices { interface0 }) } @@ -143,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(MyWorld { interface0 }) @@ -152,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`MyWorldPre::new`] and /// [`MyWorldPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -162,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; MyWorldPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`MyWorldIndices::new_instance`] and + /// Convenience wrapper around [`MyWorldIndices::new`] and /// [`MyWorldIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = MyWorldIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = MyWorldIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, @@ -1658,45 +1639,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "foo:foo/variants") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `foo:foo/variants`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index(&mut store, None, "foo:foo/variants") - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `foo:foo/variants`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `foo:foo/variants` does \ @@ -1753,9 +1710,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; let e1_arg = *_instance .get_typed_func::<(E1,), ()>(&mut store, &self.e1_arg)? .func(); diff --git a/crates/component-macro/tests/expanded/wat.rs b/crates/component-macro/tests/expanded/wat.rs index 2b7b613ba26d..5fb8cafb1b4c 100644 --- a/crates/component-macro/tests/expanded/wat.rs +++ b/crates/component-macro/tests/expanded/wat.rs @@ -27,7 +27,7 @@ impl<_T> ExamplePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = ExampleIndices::new(instance_pre.component())?; + let indices = ExampleIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -82,11 +82,6 @@ pub struct ExampleIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Example::new`]. /// -/// * You can also access the guts of instantiation through -/// [`ExampleIndices::new_instance`] followed -/// by [`ExampleIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -105,30 +100,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::same::name::this_name_is_duplicated::GuestIndices::new( - _component, - )?; - Ok(ExampleIndices { interface0 }) - } - /// Creates a new instance of [`ExampleIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Example`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Example`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::same::name::this_name_is_duplicated::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(ExampleIndices { interface0 }) } @@ -142,6 +120,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(Example { interface0 }) @@ -151,21 +130,21 @@ const _: () = { /// Convenience wrapper around [`ExamplePre::new`] and /// [`ExamplePre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; ExamplePre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`ExampleIndices::new_instance`] and + /// Convenience wrapper around [`ExampleIndices::new`] and /// [`ExampleIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = ExampleIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = ExampleIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn same_name_this_name_is_duplicated( &self, @@ -195,49 +174,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "same:name/this-name-is-duplicated") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `same:name/this-name-is-duplicated`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "same:name/this-name-is-duplicated", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `same:name/this-name-is-duplicated`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `same:name/this-name-is-duplicated` does \ @@ -253,9 +204,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; Ok(Guest {}) } } diff --git a/crates/component-macro/tests/expanded/wat_async.rs b/crates/component-macro/tests/expanded/wat_async.rs index b9459c692f06..c9d80e532631 100644 --- a/crates/component-macro/tests/expanded/wat_async.rs +++ b/crates/component-macro/tests/expanded/wat_async.rs @@ -27,7 +27,7 @@ impl<_T> ExamplePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = ExampleIndices::new(instance_pre.component())?; + let indices = ExampleIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct ExampleIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Example::new`]. /// -/// * You can also access the guts of instantiation through -/// [`ExampleIndices::new_instance`] followed -/// by [`ExampleIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::same::name::this_name_is_duplicated::GuestIndices::new( - _component, - )?; - Ok(ExampleIndices { interface0 }) - } - /// Creates a new instance of [`ExampleIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Example`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Example`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::same::name::this_name_is_duplicated::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(ExampleIndices { interface0 }) } @@ -145,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(Example { interface0 }) @@ -154,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`ExamplePre::new`] and /// [`ExamplePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -164,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; ExamplePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`ExampleIndices::new_instance`] and + /// Convenience wrapper around [`ExampleIndices::new`] and /// [`ExampleIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = ExampleIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = ExampleIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn same_name_this_name_is_duplicated( &self, @@ -201,49 +180,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "same:name/this-name-is-duplicated") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `same:name/this-name-is-duplicated`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "same:name/this-name-is-duplicated", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `same:name/this-name-is-duplicated`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `same:name/this-name-is-duplicated` does \ @@ -259,9 +210,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; Ok(Guest {}) } } diff --git a/crates/component-macro/tests/expanded/wat_concurrent.rs b/crates/component-macro/tests/expanded/wat_concurrent.rs index 45045ea60ded..09ec410bd921 100644 --- a/crates/component-macro/tests/expanded/wat_concurrent.rs +++ b/crates/component-macro/tests/expanded/wat_concurrent.rs @@ -27,7 +27,7 @@ impl<_T> ExamplePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = ExampleIndices::new(instance_pre.component())?; + let indices = ExampleIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct ExampleIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Example::new`]. /// -/// * You can also access the guts of instantiation through -/// [`ExampleIndices::new_instance`] followed -/// by [`ExampleIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::same::name::this_name_is_duplicated::GuestIndices::new( - _component, - )?; - Ok(ExampleIndices { interface0 }) - } - /// Creates a new instance of [`ExampleIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Example`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Example`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::same::name::this_name_is_duplicated::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(ExampleIndices { interface0 }) } @@ -145,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(Example { interface0 }) @@ -154,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`ExamplePre::new`] and /// [`ExamplePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -164,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; ExamplePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`ExampleIndices::new_instance`] and + /// Convenience wrapper around [`ExampleIndices::new`] and /// [`ExampleIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = ExampleIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = ExampleIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn same_name_this_name_is_duplicated( &self, @@ -201,49 +180,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "same:name/this-name-is-duplicated") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `same:name/this-name-is-duplicated`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "same:name/this-name-is-duplicated", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `same:name/this-name-is-duplicated`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `same:name/this-name-is-duplicated` does \ @@ -259,9 +210,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; Ok(Guest {}) } } diff --git a/crates/component-macro/tests/expanded/wat_tracing_async.rs b/crates/component-macro/tests/expanded/wat_tracing_async.rs index b9459c692f06..c9d80e532631 100644 --- a/crates/component-macro/tests/expanded/wat_tracing_async.rs +++ b/crates/component-macro/tests/expanded/wat_tracing_async.rs @@ -27,7 +27,7 @@ impl<_T> ExamplePre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = ExampleIndices::new(instance_pre.component())?; + let indices = ExampleIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -85,11 +85,6 @@ pub struct ExampleIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Example::new`]. /// -/// * You can also access the guts of instantiation through -/// [`ExampleIndices::new_instance`] followed -/// by [`ExampleIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -108,30 +103,13 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); let interface0 = exports::same::name::this_name_is_duplicated::GuestIndices::new( - _component, - )?; - Ok(ExampleIndices { interface0 }) - } - /// Creates a new instance of [`ExampleIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Example`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Example`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let interface0 = exports::same::name::this_name_is_duplicated::GuestIndices::new_instance( - &mut store, - _instance, + _instance_pre, )?; Ok(ExampleIndices { interface0 }) } @@ -145,6 +123,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let interface0 = self.interface0.load(&mut store, &_instance)?; Ok(Example { interface0 }) @@ -154,7 +133,7 @@ const _: () = { /// Convenience wrapper around [`ExamplePre::new`] and /// [`ExamplePre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -164,14 +143,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; ExamplePre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`ExampleIndices::new_instance`] and + /// Convenience wrapper around [`ExampleIndices::new`] and /// [`ExampleIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = ExampleIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = ExampleIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn same_name_this_name_is_duplicated( &self, @@ -201,49 +180,21 @@ pub mod exports { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let instance = component + let instance = _instance_pre + .component() .get_export_index(None, "same:name/this-name-is-duplicated") .ok_or_else(|| { anyhow::anyhow!( "no exported instance named `same:name/this-name-is-duplicated`" ) })?; - Self::_new(|name| { - component.get_export_index(Some(&instance), name) - }) - } - /// This constructor is similar to [`GuestIndices::new`] except that it - /// performs string lookups after instantiation time. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let instance_export = instance - .get_export_index( - &mut store, - None, - "same:name/this-name-is-duplicated", - ) - .ok_or_else(|| { - anyhow::anyhow!( - "no exported instance named `same:name/this-name-is-duplicated`" - ) - })?; - Self::_new(|name| { - instance - .get_export_index(&mut store, Some(&instance_export), name) - }) - } - fn _new( - mut lookup: impl FnMut( - &str, - ) -> Option, - ) -> wasmtime::Result { let mut lookup = move |name| { - lookup(name) + _instance_pre + .component() + .get_export_index(Some(&instance), name) .ok_or_else(|| { anyhow::anyhow!( "instance export `same:name/this-name-is-duplicated` does \ @@ -259,9 +210,11 @@ pub mod exports { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; Ok(Guest {}) } } diff --git a/crates/component-macro/tests/expanded/worlds-with-types.rs b/crates/component-macro/tests/expanded/worlds-with-types.rs index 190573df44db..aa473cae5997 100644 --- a/crates/component-macro/tests/expanded/worlds-with-types.rs +++ b/crates/component-macro/tests/expanded/worlds-with-types.rs @@ -52,7 +52,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -107,11 +107,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -130,30 +125,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let f = _component - .get_export_index(None, "f") - .ok_or_else(|| anyhow::anyhow!("no function export `f` found"))?; - Ok(FooIndices { f }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let f = _instance - .get_export_index(&mut store, None, "f") - .ok_or_else(|| anyhow::anyhow!("no function export `f` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let f = { + let (item, index) = _component + .get_export(None, "f") + .ok_or_else(|| anyhow::anyhow!("no export `f` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ((T, U, R),)>(&_instance_type), + "type-checking export func `f`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `f` is not a function"))?, + } + }; Ok(FooIndices { f }) } /// Uses the indices stored in `self` to load an instance @@ -166,6 +157,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let f = *_instance .get_typed_func::<(), ((T, U, R),)>(&mut store, &self.f)? @@ -177,21 +169,21 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate`]. pub fn instantiate<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate(store) } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, 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 5578bcbe06ac..0e4feb3241c4 100644 --- a/crates/component-macro/tests/expanded/worlds-with-types_async.rs +++ b/crates/component-macro/tests/expanded/worlds-with-types_async.rs @@ -52,7 +52,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -110,11 +110,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -133,30 +128,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let f = _component - .get_export_index(None, "f") - .ok_or_else(|| anyhow::anyhow!("no function export `f` found"))?; - Ok(FooIndices { f }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let f = _instance - .get_export_index(&mut store, None, "f") - .ok_or_else(|| anyhow::anyhow!("no function export `f` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let f = { + let (item, index) = _component + .get_export(None, "f") + .ok_or_else(|| anyhow::anyhow!("no export `f` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ((T, U, R),)>(&_instance_type), + "type-checking export func `f`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `f` is not a function"))?, + } + }; Ok(FooIndices { f }) } /// Uses the indices stored in `self` to load an instance @@ -169,6 +160,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let f = *_instance .get_typed_func::<(), ((T, U, R),)>(&mut store, &self.f)? @@ -180,7 +172,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -190,14 +182,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, 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 93f30dfcb249..31fd3f09ede1 100644 --- a/crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs +++ b/crates/component-macro/tests/expanded/worlds-with-types_concurrent.rs @@ -52,7 +52,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -110,11 +110,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -133,30 +128,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let f = _component - .get_export_index(None, "f") - .ok_or_else(|| anyhow::anyhow!("no function export `f` found"))?; - Ok(FooIndices { f }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let f = _instance - .get_export_index(&mut store, None, "f") - .ok_or_else(|| anyhow::anyhow!("no function export `f` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let f = { + let (item, index) = _component + .get_export(None, "f") + .ok_or_else(|| anyhow::anyhow!("no export `f` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ((T, U, R),)>(&_instance_type), + "type-checking export func `f`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `f` is not a function"))?, + } + }; Ok(FooIndices { f }) } /// Uses the indices stored in `self` to load an instance @@ -169,6 +160,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let f = *_instance .get_typed_func::<(), ((T, U, R),)>(&mut store, &self.f)? @@ -180,7 +172,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -190,14 +182,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, 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 d2466846b101..35eb84a9602f 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 @@ -52,7 +52,7 @@ impl<_T> FooPre<_T> { pub fn new( instance_pre: wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let indices = FooIndices::new(instance_pre.component())?; + let indices = FooIndices::new(&instance_pre)?; Ok(Self { instance_pre, indices }) } pub fn engine(&self) -> &wasmtime::Engine { @@ -110,11 +110,6 @@ pub struct FooIndices { /// * If you've instantiated the instance yourself already /// then you can use [`Foo::new`]. /// -/// * You can also access the guts of instantiation through -/// [`FooIndices::new_instance`] followed -/// by [`FooIndices::load`] to crate an instance of this -/// type. -/// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -133,30 +128,26 @@ const _: () = { /// /// This method may fail if the component does not have the /// required exports. - pub fn new( - component: &wasmtime::component::Component, + pub fn new<_T>( + _instance_pre: &wasmtime::component::InstancePre<_T>, ) -> wasmtime::Result { - let _component = component; - let f = _component - .get_export_index(None, "f") - .ok_or_else(|| anyhow::anyhow!("no function export `f` found"))?; - Ok(FooIndices { f }) - } - /// Creates a new instance of [`FooIndices`] from an - /// instantiated component. - /// - /// This method of creating a [`Foo`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`Foo`]. - pub fn new_instance( - mut store: impl wasmtime::AsContextMut, - instance: &wasmtime::component::Instance, - ) -> wasmtime::Result { - let _instance = instance; - let f = _instance - .get_export_index(&mut store, None, "f") - .ok_or_else(|| anyhow::anyhow!("no function export `f` found"))?; + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); + let f = { + let (item, index) = _component + .get_export(None, "f") + .ok_or_else(|| anyhow::anyhow!("no export `f` found"))?; + match item { + wasmtime::component::types::ComponentItem::ComponentFunc(func) => { + anyhow::Context::context( + func.typecheck::<(), ((T, U, R),)>(&_instance_type), + "type-checking export func `f`", + )?; + index + } + _ => Err(anyhow::anyhow!("export `f` is not a function"))?, + } + }; Ok(FooIndices { f }) } /// Uses the indices stored in `self` to load an instance @@ -169,6 +160,7 @@ const _: () = { mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { + let _ = &mut store; let _instance = instance; let f = *_instance .get_typed_func::<(), ((T, U, R),)>(&mut store, &self.f)? @@ -180,7 +172,7 @@ const _: () = { /// Convenience wrapper around [`FooPre::new`] and /// [`FooPre::instantiate_async`]. pub async fn instantiate_async<_T>( - mut store: impl wasmtime::AsContextMut, + store: impl wasmtime::AsContextMut, component: &wasmtime::component::Component, linker: &wasmtime::component::Linker<_T>, ) -> wasmtime::Result @@ -190,14 +182,14 @@ const _: () = { let pre = linker.instantiate_pre(component)?; FooPre::new(pre)?.instantiate_async(store).await } - /// Convenience wrapper around [`FooIndices::new_instance`] and + /// Convenience wrapper around [`FooIndices::new`] and /// [`FooIndices::load`]. pub fn new( mut store: impl wasmtime::AsContextMut, instance: &wasmtime::component::Instance, ) -> wasmtime::Result { - let indices = FooIndices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = FooIndices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) } pub fn add_to_linker( linker: &mut wasmtime::component::Linker, diff --git a/crates/wasmtime/src/runtime/component/types.rs b/crates/wasmtime/src/runtime/component/types.rs index 3879442b776a..9ef8d5f8d597 100644 --- a/crates/wasmtime/src/runtime/component/types.rs +++ b/crates/wasmtime/src/runtime/component/types.rs @@ -725,6 +725,18 @@ impl ComponentFunc { .iter() .map(|ty| Type::from(ty, &self.0.instance())) } + + #[doc(hidden)] + pub fn typecheck(&self, cx: &InstanceType) -> anyhow::Result<()> + where + Params: crate::component::ComponentNamedList + crate::component::Lower, + Return: crate::component::ComponentNamedList + crate::component::Lift, + { + let ty = &self.0.types[self.0.index]; + Params::typecheck(&InterfaceType::Tuple(ty.params), cx)?; + Return::typecheck(&InterfaceType::Tuple(ty.results), cx)?; + Ok(()) + } } /// Core module type diff --git a/crates/wit-bindgen/src/lib.rs b/crates/wit-bindgen/src/lib.rs index 5ff283f9871d..5d3f1ac69e4e 100644 --- a/crates/wit-bindgen/src/lib.rs +++ b/crates/wit-bindgen/src/lib.rs @@ -96,8 +96,7 @@ struct ExportField { ty: String, ty_index: String, load: String, - get_index_from_component: String, - get_index_from_instance: String, + get_index: String, } #[derive(Default, Debug, Clone, Copy)] @@ -613,26 +612,36 @@ impl Wasmtime { let ty; let ty_index; let load; - let get_index_from_component; - let get_index_from_instance; + let get_index; match item { WorldItem::Function(func) => { generator.define_rust_guest_export(resolve, None, func); let body = mem::take(&mut generator.src).into(); load = generator.extract_typed_function(func).1; assert!(generator.src.is_empty()); - self.exports.funcs.push(body); + generator.generator.exports.funcs.push(body); ty_index = format!("{wt}::component::ComponentExportIndex"); field = func_field_name(resolve, func); ty = format!("{wt}::component::Func"); - get_index_from_component = format!( - "_component.get_export_index(None, \"{}\") - .ok_or_else(|| anyhow::anyhow!(\"no function export `{0}` found\"))?", + let sig = generator.typedfunc_sig(func, TypeMode::AllBorrowed("'_")); + let typecheck = format!( + "match item {{ + {wt}::component::types::ComponentItem::ComponentFunc(func) => {{ + anyhow::Context::context( + func.typecheck::<{sig}>(&_instance_type), + \"type-checking export func `{0}`\" + )?; + index + }} + _ => Err(anyhow::anyhow!(\"export `{0}` is not a function\"))?, + }}", func.name ); - get_index_from_instance = format!( - "_instance.get_export_index(&mut store, None, \"{}\") - .ok_or_else(|| anyhow::anyhow!(\"no function export `{0}` found\"))?", + get_index = format!( + "{{ let (item, index) = _component.get_export(None, \"{}\") + .ok_or_else(|| anyhow::anyhow!(\"no export `{0}` found\"))?; + {typecheck} + }}", func.name ); } @@ -683,30 +692,13 @@ impl Wasmtime { /// /// This constructor can be used to front-load string lookups to find exports /// within a component. -pub fn new( - component: &{wt}::component::Component, -) -> {wt}::Result<{struct_name}Indices> {{ - let instance = component.get_export_index(None, \"{instance_name}\") - .ok_or_else(|| anyhow::anyhow!(\"no exported instance named `{instance_name}`\"))?; - Self::_new(|name| component.get_export_index(Some(&instance), name)) -}} - -/// This constructor is similar to [`{struct_name}Indices::new`] except that it -/// performs string lookups after instantiation time. -pub fn new_instance( - mut store: impl {wt}::AsContextMut, - instance: &{wt}::component::Instance, +pub fn new<_T>( + _instance_pre: &{wt}::component::InstancePre<_T>, ) -> {wt}::Result<{struct_name}Indices> {{ - let instance_export = instance.get_export_index(&mut store, None, \"{instance_name}\") + let instance = _instance_pre.component().get_export_index(None, \"{instance_name}\") .ok_or_else(|| anyhow::anyhow!(\"no exported instance named `{instance_name}`\"))?; - Self::_new(|name| instance.get_export_index(&mut store, Some(&instance_export), name)) -}} - -fn _new( - mut lookup: impl FnMut (&str) -> Option<{wt}::component::ComponentExportIndex>, -) -> {wt}::Result<{struct_name}Indices> {{ let mut lookup = move |name| {{ - lookup(name).ok_or_else(|| {{ + _instance_pre.component().get_export_index(Some(&instance), name).ok_or_else(|| {{ anyhow::anyhow!( \"instance export `{instance_name}` does \\ not have export `{{name}}`\" @@ -737,9 +729,11 @@ fn _new( mut store: impl {wt}::AsContextMut, instance: &{wt}::component::Instance, ) -> {wt}::Result<{struct_name}> {{ + let _instance = instance; + let _instance_pre = _instance.instance_pre(&store); + let _instance_type = _instance_pre.instance_type(); let mut store = store.as_context_mut(); let _ = &mut store; - let _instance = instance; " ); let mut fields = Vec::new(); @@ -844,9 +838,7 @@ fn _new( )); ty_index = format!("{path}Indices"); ty = path; - get_index_from_component = format!("{ty_index}::new(_component)?"); - get_index_from_instance = - format!("{ty_index}::new_instance(&mut store, _instance)?"); + get_index = format!("{ty_index}::new(_instance_pre)?"); } } let prev = self.exports.fields.insert( @@ -855,8 +847,7 @@ fn _new( ty, ty_index, load, - get_index_from_component, - get_index_from_instance, + get_index, }, ); assert!(prev.is_none()); @@ -903,7 +894,7 @@ impl<_T> {camel}Pre<_T> {{ /// This method may fail if the component behind `instance_pre` /// does not have the required exports. pub fn new(instance_pre: {wt}::component::InstancePre<_T>) -> {wt}::Result {{ - let indices = {camel}Indices::new(instance_pre.component())?; + let indices = {camel}Indices::new(&instance_pre)?; Ok(Self {{ instance_pre, indices }}) }} @@ -976,11 +967,6 @@ impl<_T> {camel}Pre<_T> {{ /// * If you've instantiated the instance yourself already /// then you can use [`{camel}::new`]. /// - /// * You can also access the guts of instantiation through - /// [`{camel}Indices::new_instance`] followed - /// by [`{camel}Indices::load`] to crate an instance of this - /// type. - /// /// These methods are all equivalent to one another and move /// around the tradeoff of what work is performed when. /// @@ -1013,12 +999,13 @@ impl<_T> {camel}Pre<_T> {{ /// /// This method may fail if the component does not have the /// required exports. - pub fn new(component: &{wt}::component::Component) -> {wt}::Result {{ - let _component = component; + pub fn new<_T>(_instance_pre: &{wt}::component::InstancePre<_T>) -> {wt}::Result {{ + let _component = _instance_pre.component(); + let _instance_type = _instance_pre.instance_type(); ", ); for (name, field) in self.exports.fields.iter() { - uwriteln!(self.src, "let {name} = {};", field.get_index_from_component); + uwriteln!(self.src, "let {name} = {};", field.get_index); } uwriteln!(self.src, "Ok({camel}Indices {{"); for (name, _) in self.exports.fields.iter() { @@ -1027,33 +1014,6 @@ impl<_T> {camel}Pre<_T> {{ uwriteln!(self.src, "}})"); uwriteln!(self.src, "}}"); // close `fn new` - uwriteln!( - self.src, - " - /// Creates a new instance of [`{camel}Indices`] from an - /// instantiated component. - /// - /// This method of creating a [`{camel}`] will perform string - /// lookups for all exports when this method is called. This - /// will only succeed if the provided instance matches the - /// requirements of [`{camel}`]. - pub fn new_instance( - mut store: impl {wt}::AsContextMut, - instance: &{wt}::component::Instance, - ) -> {wt}::Result {{ - let _instance = instance; - ", - ); - for (name, field) in self.exports.fields.iter() { - uwriteln!(self.src, "let {name} = {};", field.get_index_from_instance); - } - uwriteln!(self.src, "Ok({camel}Indices {{"); - for (name, _) in self.exports.fields.iter() { - uwriteln!(self.src, "{name},"); - } - uwriteln!(self.src, "}})"); - uwriteln!(self.src, "}}"); // close `fn new_instance` - uwriteln!( self.src, " @@ -1067,6 +1027,7 @@ impl<_T> {camel}Pre<_T> {{ mut store: impl {wt}::AsContextMut, instance: &{wt}::component::Instance, ) -> {wt}::Result<{camel}> {{ + let _ = &mut store; let _instance = instance; ", ); @@ -1087,7 +1048,7 @@ impl<_T> {camel}Pre<_T> {{ /// Convenience wrapper around [`{camel}Pre::new`] and /// [`{camel}Pre::instantiate{async__}`]. pub {async_} fn instantiate{async__}<_T>( - mut store: impl {wt}::AsContextMut, + store: impl {wt}::AsContextMut, component: &{wt}::component::Component, linker: &{wt}::component::Linker<_T>, ) -> {wt}::Result<{camel}> @@ -1097,14 +1058,14 @@ impl<_T> {camel}Pre<_T> {{ {camel}Pre::new(pre)?.instantiate{async__}(store){await_} }} - /// Convenience wrapper around [`{camel}Indices::new_instance`] and + /// Convenience wrapper around [`{camel}Indices::new`] and /// [`{camel}Indices::load`]. pub fn new( mut store: impl {wt}::AsContextMut, instance: &{wt}::component::Instance, ) -> {wt}::Result<{camel}> {{ - let indices = {camel}Indices::new_instance(&mut store, instance)?; - indices.load(store, instance) + let indices = {camel}Indices::new(&instance.instance_pre(&store))?; + indices.load(&mut store, instance) }} ", );