Path: blob/main/crates/component-macro/tests/expanded/host-world_concurrent.rs
1692 views
/// Auto-generated bindings for a pre-instantiated version of a1/// component which implements the world `host`.2///3/// This structure is created through [`Host_Pre::new`] which4/// takes a [`InstancePre`](wasmtime::component::InstancePre) that5/// has been created through a [`Linker`](wasmtime::component::Linker).6///7/// For more information see [`Host_`] as well.8pub struct Host_Pre<T: 'static> {9instance_pre: wasmtime::component::InstancePre<T>,10indices: Host_Indices,11}12impl<T: 'static> Clone for Host_Pre<T> {13fn clone(&self) -> Self {14Self {15instance_pre: self.instance_pre.clone(),16indices: self.indices.clone(),17}18}19}20impl<_T: 'static> Host_Pre<_T> {21/// Creates a new copy of `Host_Pre` 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 = Host_Indices::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 [`Host_`] 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<Host_> {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> Host_Pre<_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<Host_> {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/// `host`.67///68/// This is an implementation detail of [`Host_Pre`] and can69/// be constructed if needed as well.70///71/// For more information see [`Host_`] as well.72#[derive(Clone)]73pub struct Host_Indices {}74/// Auto-generated bindings for an instance a component which75/// implements the world `host`.76///77/// This structure can be created through a number of means78/// depending on your requirements and what you have on hand:79///80/// * The most convenient way is to use81/// [`Host_::instantiate`] which only needs a82/// [`Store`], [`Component`], and [`Linker`].83///84/// * Alternatively you can create a [`Host_Pre`] ahead of85/// time with a [`Component`] to front-load string lookups86/// of exports once instead of per-instantiation. This87/// method then uses [`Host_Pre::instantiate`] to88/// create a [`Host_`].89///90/// * If you've instantiated the instance yourself already91/// then you can use [`Host_::new`].92///93/// These methods are all equivalent to one another and move94/// around the tradeoff of what work is performed when.95///96/// [`Store`]: wasmtime::Store97/// [`Component`]: wasmtime::component::Component98/// [`Linker`]: wasmtime::component::Linker99pub struct Host_ {}100pub trait Host_ImportsWithStore: wasmtime::component::HasData + Send {101fn foo<T>(102accessor: &wasmtime::component::Accessor<T, Self>,103) -> impl ::core::future::Future<Output = ()> + Send;104}105pub trait Host_Imports: Send {}106impl<_T: Host_Imports + ?Sized + Send> Host_Imports for &mut _T {}107const _: () = {108#[allow(unused_imports)]109use wasmtime::component::__internal::anyhow;110impl Host_Indices {111/// Creates a new copy of `Host_Indices` bindings which can then112/// be used to instantiate into a particular store.113///114/// This method may fail if the component does not have the115/// required exports.116pub fn new<_T>(117_instance_pre: &wasmtime::component::InstancePre<_T>,118) -> wasmtime::Result<Self> {119let _component = _instance_pre.component();120let _instance_type = _instance_pre.instance_type();121Ok(Host_Indices {})122}123/// Uses the indices stored in `self` to load an instance124/// of [`Host_`] from the instance provided.125///126/// Note that at this time this method will additionally127/// perform type-checks of all exports.128pub fn load(129&self,130mut store: impl wasmtime::AsContextMut,131instance: &wasmtime::component::Instance,132) -> wasmtime::Result<Host_> {133let _ = &mut store;134let _instance = instance;135Ok(Host_ {})136}137}138impl Host_ {139/// Convenience wrapper around [`Host_Pre::new`] and140/// [`Host_Pre::instantiate`].141pub fn instantiate<_T>(142store: impl wasmtime::AsContextMut<Data = _T>,143component: &wasmtime::component::Component,144linker: &wasmtime::component::Linker<_T>,145) -> wasmtime::Result<Host_> {146let pre = linker.instantiate_pre(component)?;147Host_Pre::new(pre)?.instantiate(store)148}149/// Convenience wrapper around [`Host_Indices::new`] and150/// [`Host_Indices::load`].151pub fn new(152mut store: impl wasmtime::AsContextMut,153instance: &wasmtime::component::Instance,154) -> wasmtime::Result<Host_> {155let indices = Host_Indices::new(&instance.instance_pre(&store))?;156indices.load(&mut store, instance)157}158/// Convenience wrapper around [`Host_Pre::new`] and159/// [`Host_Pre::instantiate_async`].160pub async fn instantiate_async<_T>(161store: impl wasmtime::AsContextMut<Data = _T>,162component: &wasmtime::component::Component,163linker: &wasmtime::component::Linker<_T>,164) -> wasmtime::Result<Host_>165where166_T: Send,167{168let pre = linker.instantiate_pre(component)?;169Host_Pre::new(pre)?.instantiate_async(store).await170}171pub fn add_to_linker_imports<T, D>(172linker: &mut wasmtime::component::Linker<T>,173host_getter: fn(&mut T) -> D::Data<'_>,174) -> wasmtime::Result<()>175where176D: Host_ImportsWithStore,177for<'a> D::Data<'a>: Host_Imports,178T: 'static + Send,179{180let mut linker = linker.root();181linker182.func_wrap_concurrent(183"foo",184move |caller: &wasmtime::component::Accessor<T>, (): ()| {185wasmtime::component::__internal::Box::pin(async move {186let host = &caller.with_getter(host_getter);187let r = <D as Host_ImportsWithStore>::foo(host).await;188Ok(r)189})190},191)?;192Ok(())193}194pub fn add_to_linker<T, D>(195linker: &mut wasmtime::component::Linker<T>,196host_getter: fn(&mut T) -> D::Data<'_>,197) -> wasmtime::Result<()>198where199D: Host_ImportsWithStore + Send,200for<'a> D::Data<'a>: Host_Imports + Send,201T: 'static + Send,202{203Self::add_to_linker_imports::<T, D>(linker, host_getter)?;204Ok(())205}206}207};208209210