Path: blob/main/crates/component-macro/tests/expanded/dead-code_concurrent.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 {208fn f<T: Send>(209accessor: &wasmtime::component::Accessor<T, Self>,210) -> impl ::core::future::Future<Output = LiveType> + Send;211}212pub trait Host: Send {}213impl<_T: Host + ?Sized + Send> Host for &mut _T {}214pub fn add_to_linker<T, D>(215linker: &mut wasmtime::component::Linker<T>,216host_getter: fn(&mut T) -> D::Data<'_>,217) -> wasmtime::Result<()>218where219D: HostWithStore,220for<'a> D::Data<'a>: Host,221T: 'static + Send,222{223let mut inst = linker.instance("a:b/interface-with-live-type")?;224inst.func_wrap_concurrent(225"f",226move |caller: &wasmtime::component::Accessor<T>, (): ()| {227wasmtime::component::__internal::Box::pin(async move {228let host = &caller.with_getter(host_getter);229let r = <D as HostWithStore>::f(host).await;230Ok((r,))231})232},233)?;234Ok(())235}236}237#[allow(clippy::all)]238pub mod interface_with_dead_type {239#[allow(unused_imports)]240use wasmtime::component::__internal::Box;241pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType;242const _: () = {243assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32);244assert!(2454 == < LiveType as wasmtime::component::ComponentType >::ALIGN32246);247};248#[derive(wasmtime::component::ComponentType)]249#[derive(wasmtime::component::Lift)]250#[derive(wasmtime::component::Lower)]251#[component(record)]252#[derive(Clone, Copy)]253pub struct DeadType {254#[component(name = "a")]255pub a: u32,256}257impl core::fmt::Debug for DeadType {258fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {259f.debug_struct("DeadType").field("a", &self.a).finish()260}261}262const _: () = {263assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32);264assert!(2654 == < DeadType as wasmtime::component::ComponentType >::ALIGN32266);267};268#[derive(wasmtime::component::ComponentType)]269#[derive(wasmtime::component::Lift)]270#[derive(wasmtime::component::Lower)]271#[component(variant)]272#[derive(Clone, Copy)]273pub enum V {274#[component(name = "a")]275A(LiveType),276#[component(name = "b")]277B(DeadType),278}279impl core::fmt::Debug for V {280fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {281match self {282V::A(e) => f.debug_tuple("V::A").field(e).finish(),283V::B(e) => f.debug_tuple("V::B").field(e).finish(),284}285}286}287const _: () = {288assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32);289assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32);290};291pub trait HostWithStore: wasmtime::component::HasData {}292impl<_T: ?Sized> HostWithStore for _T293where294_T: wasmtime::component::HasData,295{}296pub trait Host {}297impl<_T: Host + ?Sized> Host for &mut _T {}298pub fn add_to_linker<T, D>(299linker: &mut wasmtime::component::Linker<T>,300host_getter: fn(&mut T) -> D::Data<'_>,301) -> wasmtime::Result<()>302where303D: HostWithStore,304for<'a> D::Data<'a>: Host,305T: 'static,306{307let mut inst = linker.instance("a:b/interface-with-dead-type")?;308Ok(())309}310}311}312}313314315