Path: blob/main/crates/component-macro/tests/expanded/function-new_concurrent.rs
1692 views
/// Auto-generated bindings for a pre-instantiated version of a1/// component which implements the world `foo`.2///3/// This structure is created through [`FooPre::new`] which4/// takes a [`InstancePre`](wasmtime::component::InstancePre) that5/// has been created through a [`Linker`](wasmtime::component::Linker).6///7/// For more information see [`Foo`] as well.8pub struct FooPre<T: 'static> {9instance_pre: wasmtime::component::InstancePre<T>,10indices: FooIndices,11}12impl<T: 'static> Clone for FooPre<T> {13fn clone(&self) -> Self {14Self {15instance_pre: self.instance_pre.clone(),16indices: self.indices.clone(),17}18}19}20impl<_T: 'static> FooPre<_T> {21/// Creates a new copy of `FooPre` bindings which can then22/// be used to instantiate into a particular store.23///24/// This method may fail if the component behind `instance_pre`25/// does not have the required exports.26pub fn new(27instance_pre: wasmtime::component::InstancePre<_T>,28) -> wasmtime::Result<Self> {29let indices = FooIndices::new(&instance_pre)?;30Ok(Self { instance_pre, indices })31}32pub fn engine(&self) -> &wasmtime::Engine {33self.instance_pre.engine()34}35pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {36&self.instance_pre37}38/// Instantiates a new instance of [`Foo`] within the39/// `store` provided.40///41/// This function will use `self` as the pre-instantiated42/// instance to perform instantiation. Afterwards the preloaded43/// indices in `self` are used to lookup all exports on the44/// resulting instance.45pub fn instantiate(46&self,47mut store: impl wasmtime::AsContextMut<Data = _T>,48) -> wasmtime::Result<Foo> {49let mut store = store.as_context_mut();50let instance = self.instance_pre.instantiate(&mut store)?;51self.indices.load(&mut store, &instance)52}53}54impl<_T: Send + 'static> FooPre<_T> {55/// Same as [`Self::instantiate`], except with `async`.56pub async fn instantiate_async(57&self,58mut store: impl wasmtime::AsContextMut<Data = _T>,59) -> wasmtime::Result<Foo> {60let mut store = store.as_context_mut();61let instance = self.instance_pre.instantiate_async(&mut store).await?;62self.indices.load(&mut store, &instance)63}64}65/// Auto-generated bindings for index of the exports of66/// `foo`.67///68/// This is an implementation detail of [`FooPre`] and can69/// be constructed if needed as well.70///71/// For more information see [`Foo`] as well.72#[derive(Clone)]73pub struct FooIndices {74new: wasmtime::component::ComponentExportIndex,75}76/// Auto-generated bindings for an instance a component which77/// implements the world `foo`.78///79/// This structure can be created through a number of means80/// depending on your requirements and what you have on hand:81///82/// * The most convenient way is to use83/// [`Foo::instantiate`] which only needs a84/// [`Store`], [`Component`], and [`Linker`].85///86/// * Alternatively you can create a [`FooPre`] ahead of87/// time with a [`Component`] to front-load string lookups88/// of exports once instead of per-instantiation. This89/// method then uses [`FooPre::instantiate`] to90/// create a [`Foo`].91///92/// * If you've instantiated the instance yourself already93/// then you can use [`Foo::new`].94///95/// These methods are all equivalent to one another and move96/// around the tradeoff of what work is performed when.97///98/// [`Store`]: wasmtime::Store99/// [`Component`]: wasmtime::component::Component100/// [`Linker`]: wasmtime::component::Linker101pub struct Foo {102new: wasmtime::component::Func,103}104const _: () = {105#[allow(unused_imports)]106use wasmtime::component::__internal::anyhow;107impl FooIndices {108/// Creates a new copy of `FooIndices` bindings which can then109/// be used to instantiate into a particular store.110///111/// This method may fail if the component does not have the112/// required exports.113pub fn new<_T>(114_instance_pre: &wasmtime::component::InstancePre<_T>,115) -> wasmtime::Result<Self> {116let _component = _instance_pre.component();117let _instance_type = _instance_pre.instance_type();118let new = {119let (item, index) = _component120.get_export(None, "new")121.ok_or_else(|| anyhow::anyhow!("no export `new` found"))?;122match item {123wasmtime::component::types::ComponentItem::ComponentFunc(func) => {124anyhow::Context::context(125func.typecheck::<(), ()>(&_instance_type),126"type-checking export func `new`",127)?;128index129}130_ => Err(anyhow::anyhow!("export `new` is not a function"))?,131}132};133Ok(FooIndices { new })134}135/// Uses the indices stored in `self` to load an instance136/// of [`Foo`] from the instance provided.137///138/// Note that at this time this method will additionally139/// perform type-checks of all exports.140pub fn load(141&self,142mut store: impl wasmtime::AsContextMut,143instance: &wasmtime::component::Instance,144) -> wasmtime::Result<Foo> {145let _ = &mut store;146let _instance = instance;147let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func();148Ok(Foo { new })149}150}151impl Foo {152/// Convenience wrapper around [`FooPre::new`] and153/// [`FooPre::instantiate`].154pub fn instantiate<_T>(155store: impl wasmtime::AsContextMut<Data = _T>,156component: &wasmtime::component::Component,157linker: &wasmtime::component::Linker<_T>,158) -> wasmtime::Result<Foo> {159let pre = linker.instantiate_pre(component)?;160FooPre::new(pre)?.instantiate(store)161}162/// Convenience wrapper around [`FooIndices::new`] and163/// [`FooIndices::load`].164pub fn new(165mut store: impl wasmtime::AsContextMut,166instance: &wasmtime::component::Instance,167) -> wasmtime::Result<Foo> {168let indices = FooIndices::new(&instance.instance_pre(&store))?;169indices.load(&mut store, instance)170}171/// Convenience wrapper around [`FooPre::new`] and172/// [`FooPre::instantiate_async`].173pub async fn instantiate_async<_T>(174store: impl wasmtime::AsContextMut<Data = _T>,175component: &wasmtime::component::Component,176linker: &wasmtime::component::Linker<_T>,177) -> wasmtime::Result<Foo>178where179_T: Send,180{181let pre = linker.instantiate_pre(component)?;182FooPre::new(pre)?.instantiate_async(store).await183}184pub async fn call_new<_T, _D>(185&self,186accessor: &wasmtime::component::Accessor<_T, _D>,187) -> wasmtime::Result<()>188where189_T: Send,190_D: wasmtime::component::HasData,191{192let callee = unsafe {193wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new)194};195let () = callee.call_concurrent(accessor, ()).await?;196Ok(())197}198}199};200201202