Path: blob/main/crates/component-macro/tests/expanded/dead-code_tracing_async.rs
1692 views
/// Auto-generated bindings for a pre-instantiated version of a1/// component which implements the world `imports`.2///3/// This structure is created through [`ImportsPre::new`] which4/// takes a [`InstancePre`](wasmtime::component::InstancePre) that5/// has been created through a [`Linker`](wasmtime::component::Linker).6///7/// For more information see [`Imports`] as well.8pub struct ImportsPre<T: 'static> {9instance_pre: wasmtime::component::InstancePre<T>,10indices: ImportsIndices,11}12impl<T: 'static> Clone for ImportsPre<T> {13fn clone(&self) -> Self {14Self {15instance_pre: self.instance_pre.clone(),16indices: self.indices.clone(),17}18}19}20impl<_T: 'static> ImportsPre<_T> {21/// Creates a new copy of `ImportsPre` 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 = ImportsIndices::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 [`Imports`] 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<Imports> {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> ImportsPre<_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<Imports> {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/// `imports`.67///68/// This is an implementation detail of [`ImportsPre`] and can69/// be constructed if needed as well.70///71/// For more information see [`Imports`] as well.72#[derive(Clone)]73pub struct ImportsIndices {}74/// Auto-generated bindings for an instance a component which75/// implements the world `imports`.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/// [`Imports::instantiate`] which only needs a82/// [`Store`], [`Component`], and [`Linker`].83///84/// * Alternatively you can create a [`ImportsPre`] ahead of85/// time with a [`Component`] to front-load string lookups86/// of exports once instead of per-instantiation. This87/// method then uses [`ImportsPre::instantiate`] to88/// create a [`Imports`].89///90/// * If you've instantiated the instance yourself already91/// then you can use [`Imports::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 Imports {}100const _: () = {101#[allow(unused_imports)]102use wasmtime::component::__internal::anyhow;103impl ImportsIndices {104/// Creates a new copy of `ImportsIndices` bindings which can then105/// be used to instantiate into a particular store.106///107/// This method may fail if the component does not have the108/// required exports.109pub fn new<_T>(110_instance_pre: &wasmtime::component::InstancePre<_T>,111) -> wasmtime::Result<Self> {112let _component = _instance_pre.component();113let _instance_type = _instance_pre.instance_type();114Ok(ImportsIndices {})115}116/// Uses the indices stored in `self` to load an instance117/// of [`Imports`] from the instance provided.118///119/// Note that at this time this method will additionally120/// perform type-checks of all exports.121pub fn load(122&self,123mut store: impl wasmtime::AsContextMut,124instance: &wasmtime::component::Instance,125) -> wasmtime::Result<Imports> {126let _ = &mut store;127let _instance = instance;128Ok(Imports {})129}130}131impl Imports {132/// Convenience wrapper around [`ImportsPre::new`] and133/// [`ImportsPre::instantiate`].134pub fn instantiate<_T>(135store: impl wasmtime::AsContextMut<Data = _T>,136component: &wasmtime::component::Component,137linker: &wasmtime::component::Linker<_T>,138) -> wasmtime::Result<Imports> {139let pre = linker.instantiate_pre(component)?;140ImportsPre::new(pre)?.instantiate(store)141}142/// Convenience wrapper around [`ImportsIndices::new`] and143/// [`ImportsIndices::load`].144pub fn new(145mut store: impl wasmtime::AsContextMut,146instance: &wasmtime::component::Instance,147) -> wasmtime::Result<Imports> {148let indices = ImportsIndices::new(&instance.instance_pre(&store))?;149indices.load(&mut store, instance)150}151/// Convenience wrapper around [`ImportsPre::new`] and152/// [`ImportsPre::instantiate_async`].153pub async fn instantiate_async<_T>(154store: impl wasmtime::AsContextMut<Data = _T>,155component: &wasmtime::component::Component,156linker: &wasmtime::component::Linker<_T>,157) -> wasmtime::Result<Imports>158where159_T: Send,160{161let pre = linker.instantiate_pre(component)?;162ImportsPre::new(pre)?.instantiate_async(store).await163}164pub fn add_to_linker<T, D>(165linker: &mut wasmtime::component::Linker<T>,166host_getter: fn(&mut T) -> D::Data<'_>,167) -> wasmtime::Result<()>168where169D: a::b::interface_with_live_type::HostWithStore170+ a::b::interface_with_dead_type::HostWithStore + Send,171for<'a> D::Data<172'a,173>: a::b::interface_with_live_type::Host174+ a::b::interface_with_dead_type::Host + Send,175T: 'static + Send,176{177a::b::interface_with_live_type::add_to_linker::<T, D>(linker, host_getter)?;178a::b::interface_with_dead_type::add_to_linker::<T, D>(linker, host_getter)?;179Ok(())180}181}182};183pub mod a {184pub mod b {185#[allow(clippy::all)]186pub mod interface_with_live_type {187#[allow(unused_imports)]188use wasmtime::component::__internal::{anyhow, Box};189#[derive(wasmtime::component::ComponentType)]190#[derive(wasmtime::component::Lift)]191#[derive(wasmtime::component::Lower)]192#[component(record)]193#[derive(Clone, Copy)]194pub struct LiveType {195#[component(name = "a")]196pub a: u32,197}198impl core::fmt::Debug for LiveType {199fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {200f.debug_struct("LiveType").field("a", &self.a).finish()201}202}203const _: () = {204assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);205assert!(2064 == < LiveType as wasmtime::component::ComponentType >::ALIGN32207);208};209pub trait HostWithStore: wasmtime::component::HasData + Send {}210impl<_T: ?Sized> HostWithStore for _T211where212_T: wasmtime::component::HasData + Send,213{}214pub trait Host: Send {215fn f(&mut self) -> impl ::core::future::Future<Output = LiveType> + Send;216}217impl<_T: Host + ?Sized + Send> Host for &mut _T {218fn f(219&mut self,220) -> impl ::core::future::Future<Output = LiveType> + Send {221async move { Host::f(*self).await }222}223}224pub fn add_to_linker<T, D>(225linker: &mut wasmtime::component::Linker<T>,226host_getter: fn(&mut T) -> D::Data<'_>,227) -> wasmtime::Result<()>228where229D: HostWithStore,230for<'a> D::Data<'a>: Host,231T: 'static + Send,232{233let mut inst = linker.instance("a:b/interface-with-live-type")?;234inst.func_wrap_async(235"f",236move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {237use tracing::Instrument;238let span = tracing::span!(239tracing::Level::TRACE, "wit-bindgen import", module =240"interface-with-live-type", function = "f",241);242wasmtime::component::__internal::Box::new(243async move {244tracing::event!(tracing::Level::TRACE, "call");245let host = &mut host_getter(caller.data_mut());246let r = Host::f(host).await;247tracing::event!(248tracing::Level::TRACE, result = tracing::field::debug(& r),249"return"250);251Ok((r,))252}253.instrument(span),254)255},256)?;257Ok(())258}259}260#[allow(clippy::all)]261pub mod interface_with_dead_type {262#[allow(unused_imports)]263use wasmtime::component::__internal::{anyhow, Box};264pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;265const _: () = {266assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);267assert!(2684 == < LiveType as wasmtime::component::ComponentType >::ALIGN32269);270};271#[derive(wasmtime::component::ComponentType)]272#[derive(wasmtime::component::Lift)]273#[derive(wasmtime::component::Lower)]274#[component(record)]275#[derive(Clone, Copy)]276pub struct DeadType {277#[component(name = "a")]278pub a: u32,279}280impl core::fmt::Debug for DeadType {281fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {282f.debug_struct("DeadType").field("a", &self.a).finish()283}284}285const _: () = {286assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);287assert!(2884 == < DeadType as wasmtime::component::ComponentType >::ALIGN32289);290};291#[derive(wasmtime::component::ComponentType)]292#[derive(wasmtime::component::Lift)]293#[derive(wasmtime::component::Lower)]294#[component(variant)]295#[derive(Clone, Copy)]296pub enum V {297#[component(name = "a")]298A(LiveType),299#[component(name = "b")]300B(DeadType),301}302impl core::fmt::Debug for V {303fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {304match self {305V::A(e) => f.debug_tuple("V::A").field(e).finish(),306V::B(e) => f.debug_tuple("V::B").field(e).finish(),307}308}309}310const _: () = {311assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);312assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);313};314pub trait HostWithStore: wasmtime::component::HasData {}315impl<_T: ?Sized> HostWithStore for _T316where317_T: wasmtime::component::HasData,318{}319pub trait Host {}320impl<_T: Host + ?Sized> Host for &mut _T {}321pub fn add_to_linker<T, D>(322linker: &mut wasmtime::component::Linker<T>,323host_getter: fn(&mut T) -> D::Data<'_>,324) -> wasmtime::Result<()>325where326D: HostWithStore,327for<'a> D::Data<'a>: Host,328T: 'static,329{330let mut inst = linker.instance("a:b/interface-with-dead-type")?;331Ok(())332}333}334}335}336337338