Path: blob/main/crates/component-macro/tests/expanded/dead-code_tracing_async.rs
3068 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 _: () = {101impl ImportsIndices {102/// Creates a new copy of `ImportsIndices` bindings which can then103/// be used to instantiate into a particular store.104///105/// This method may fail if the component does not have the106/// required exports.107pub fn new<_T>(108_instance_pre: &wasmtime::component::InstancePre<_T>,109) -> wasmtime::Result<Self> {110let _component = _instance_pre.component();111let _instance_type = _instance_pre.instance_type();112Ok(ImportsIndices {})113}114/// Uses the indices stored in `self` to load an instance115/// of [`Imports`] from the instance provided.116///117/// Note that at this time this method will additionally118/// perform type-checks of all exports.119pub fn load(120&self,121mut store: impl wasmtime::AsContextMut,122instance: &wasmtime::component::Instance,123) -> wasmtime::Result<Imports> {124let _ = &mut store;125let _instance = instance;126Ok(Imports {})127}128}129impl Imports {130/// Convenience wrapper around [`ImportsPre::new`] and131/// [`ImportsPre::instantiate`].132pub fn instantiate<_T>(133store: impl wasmtime::AsContextMut<Data = _T>,134component: &wasmtime::component::Component,135linker: &wasmtime::component::Linker<_T>,136) -> wasmtime::Result<Imports> {137let pre = linker.instantiate_pre(component)?;138ImportsPre::new(pre)?.instantiate(store)139}140/// Convenience wrapper around [`ImportsIndices::new`] and141/// [`ImportsIndices::load`].142pub fn new(143mut store: impl wasmtime::AsContextMut,144instance: &wasmtime::component::Instance,145) -> wasmtime::Result<Imports> {146let indices = ImportsIndices::new(&instance.instance_pre(&store))?;147indices.load(&mut store, instance)148}149/// Convenience wrapper around [`ImportsPre::new`] and150/// [`ImportsPre::instantiate_async`].151pub async fn instantiate_async<_T>(152store: impl wasmtime::AsContextMut<Data = _T>,153component: &wasmtime::component::Component,154linker: &wasmtime::component::Linker<_T>,155) -> wasmtime::Result<Imports>156where157_T: Send,158{159let pre = linker.instantiate_pre(component)?;160ImportsPre::new(pre)?.instantiate_async(store).await161}162pub fn add_to_linker<T, D>(163linker: &mut wasmtime::component::Linker<T>,164host_getter: fn(&mut T) -> D::Data<'_>,165) -> wasmtime::Result<()>166where167D: a::b::interface_with_live_type::HostWithStore168+ a::b::interface_with_dead_type::HostWithStore + Send,169for<'a> D::Data<170'a,171>: a::b::interface_with_live_type::Host172+ a::b::interface_with_dead_type::Host + Send,173T: 'static + Send,174{175a::b::interface_with_live_type::add_to_linker::<T, D>(linker, host_getter)?;176a::b::interface_with_dead_type::add_to_linker::<T, D>(linker, host_getter)?;177Ok(())178}179}180};181pub mod a {182pub mod b {183#[allow(clippy::all)]184pub mod interface_with_live_type {185#[allow(unused_imports)]186use wasmtime::component::__internal::Box;187#[derive(wasmtime::component::ComponentType)]188#[derive(wasmtime::component::Lift)]189#[derive(wasmtime::component::Lower)]190#[component(record)]191#[derive(Clone, Copy)]192pub struct LiveType {193#[component(name = "a")]194pub a: u32,195}196impl core::fmt::Debug for LiveType {197fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {198f.debug_struct("LiveType").field("a", &self.a).finish()199}200}201const _: () = {202assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);203assert!(2044 == < LiveType as wasmtime::component::ComponentType >::ALIGN32205);206};207pub trait HostWithStore: wasmtime::component::HasData + Send {}208impl<_T: ?Sized> HostWithStore for _T209where210_T: wasmtime::component::HasData + Send,211{}212pub trait Host: Send {213fn f(&mut self) -> impl ::core::future::Future<Output = LiveType> + Send;214}215impl<_T: Host + ?Sized + Send> Host for &mut _T {216fn f(217&mut self,218) -> impl ::core::future::Future<Output = LiveType> + Send {219async move { Host::f(*self).await }220}221}222pub fn add_to_linker<T, D>(223linker: &mut wasmtime::component::Linker<T>,224host_getter: fn(&mut T) -> D::Data<'_>,225) -> wasmtime::Result<()>226where227D: HostWithStore,228for<'a> D::Data<'a>: Host,229T: 'static + Send,230{231let mut inst = linker.instance("a:b/interface-with-live-type")?;232inst.func_wrap_async(233"f",234move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {235use tracing::Instrument;236let span = tracing::span!(237tracing::Level::TRACE, "wit-bindgen import", module =238"interface-with-live-type", function = "f",239);240wasmtime::component::__internal::Box::new(241async move {242tracing::event!(tracing::Level::TRACE, "call");243let host = &mut host_getter(caller.data_mut());244let r = Host::f(host).await;245tracing::event!(246tracing::Level::TRACE, result = tracing::field::debug(& r),247"return"248);249Ok((r,))250}251.instrument(span),252)253},254)?;255Ok(())256}257}258#[allow(clippy::all)]259pub mod interface_with_dead_type {260#[allow(unused_imports)]261use wasmtime::component::__internal::Box;262pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;263const _: () = {264assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);265assert!(2664 == < LiveType as wasmtime::component::ComponentType >::ALIGN32267);268};269#[derive(wasmtime::component::ComponentType)]270#[derive(wasmtime::component::Lift)]271#[derive(wasmtime::component::Lower)]272#[component(record)]273#[derive(Clone, Copy)]274pub struct DeadType {275#[component(name = "a")]276pub a: u32,277}278impl core::fmt::Debug for DeadType {279fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {280f.debug_struct("DeadType").field("a", &self.a).finish()281}282}283const _: () = {284assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);285assert!(2864 == < DeadType as wasmtime::component::ComponentType >::ALIGN32287);288};289#[derive(wasmtime::component::ComponentType)]290#[derive(wasmtime::component::Lift)]291#[derive(wasmtime::component::Lower)]292#[component(variant)]293#[derive(Clone, Copy)]294pub enum V {295#[component(name = "a")]296A(LiveType),297#[component(name = "b")]298B(DeadType),299}300impl core::fmt::Debug for V {301fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {302match self {303V::A(e) => f.debug_tuple("V::A").field(e).finish(),304V::B(e) => f.debug_tuple("V::B").field(e).finish(),305}306}307}308const _: () = {309assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);310assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);311};312pub trait HostWithStore: wasmtime::component::HasData {}313impl<_T: ?Sized> HostWithStore for _T314where315_T: wasmtime::component::HasData,316{}317pub trait Host {}318impl<_T: Host + ?Sized> Host for &mut _T {}319pub fn add_to_linker<T, D>(320linker: &mut wasmtime::component::Linker<T>,321host_getter: fn(&mut T) -> D::Data<'_>,322) -> wasmtime::Result<()>323where324D: HostWithStore,325for<'a> D::Data<'a>: Host,326T: 'static,327{328let mut inst = linker.instance("a:b/interface-with-dead-type")?;329Ok(())330}331}332}333}334335336