Path: blob/main/crates/component-macro/tests/expanded/char_concurrent.rs
3080 views
/// Auto-generated bindings for a pre-instantiated version of a1/// component which implements the world `the-world`.2///3/// This structure is created through [`TheWorldPre::new`] which4/// takes a [`InstancePre`](wasmtime::component::InstancePre) that5/// has been created through a [`Linker`](wasmtime::component::Linker).6///7/// For more information see [`TheWorld`] as well.8pub struct TheWorldPre<T: 'static> {9instance_pre: wasmtime::component::InstancePre<T>,10indices: TheWorldIndices,11}12impl<T: 'static> Clone for TheWorldPre<T> {13fn clone(&self) -> Self {14Self {15instance_pre: self.instance_pre.clone(),16indices: self.indices.clone(),17}18}19}20impl<_T: 'static> TheWorldPre<_T> {21/// Creates a new copy of `TheWorldPre` 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 = TheWorldIndices::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 [`TheWorld`] 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<TheWorld> {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> TheWorldPre<_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<TheWorld> {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/// `the-world`.67///68/// This is an implementation detail of [`TheWorldPre`] and can69/// be constructed if needed as well.70///71/// For more information see [`TheWorld`] as well.72#[derive(Clone)]73pub struct TheWorldIndices {74interface0: exports::foo::foo::chars::GuestIndices,75}76/// Auto-generated bindings for an instance a component which77/// implements the world `the-world`.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/// [`TheWorld::instantiate`] which only needs a84/// [`Store`], [`Component`], and [`Linker`].85///86/// * Alternatively you can create a [`TheWorldPre`] ahead of87/// time with a [`Component`] to front-load string lookups88/// of exports once instead of per-instantiation. This89/// method then uses [`TheWorldPre::instantiate`] to90/// create a [`TheWorld`].91///92/// * If you've instantiated the instance yourself already93/// then you can use [`TheWorld::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 TheWorld {102interface0: exports::foo::foo::chars::Guest,103}104const _: () = {105impl TheWorldIndices {106/// Creates a new copy of `TheWorldIndices` bindings which can then107/// be used to instantiate into a particular store.108///109/// This method may fail if the component does not have the110/// required exports.111pub fn new<_T>(112_instance_pre: &wasmtime::component::InstancePre<_T>,113) -> wasmtime::Result<Self> {114let _component = _instance_pre.component();115let _instance_type = _instance_pre.instance_type();116let interface0 = exports::foo::foo::chars::GuestIndices::new(_instance_pre)?;117Ok(TheWorldIndices { interface0 })118}119/// Uses the indices stored in `self` to load an instance120/// of [`TheWorld`] from the instance provided.121///122/// Note that at this time this method will additionally123/// perform type-checks of all exports.124pub fn load(125&self,126mut store: impl wasmtime::AsContextMut,127instance: &wasmtime::component::Instance,128) -> wasmtime::Result<TheWorld> {129let _ = &mut store;130let _instance = instance;131let interface0 = self.interface0.load(&mut store, &_instance)?;132Ok(TheWorld { interface0 })133}134}135impl TheWorld {136/// Convenience wrapper around [`TheWorldPre::new`] and137/// [`TheWorldPre::instantiate`].138pub fn instantiate<_T>(139store: impl wasmtime::AsContextMut<Data = _T>,140component: &wasmtime::component::Component,141linker: &wasmtime::component::Linker<_T>,142) -> wasmtime::Result<TheWorld> {143let pre = linker.instantiate_pre(component)?;144TheWorldPre::new(pre)?.instantiate(store)145}146/// Convenience wrapper around [`TheWorldIndices::new`] and147/// [`TheWorldIndices::load`].148pub fn new(149mut store: impl wasmtime::AsContextMut,150instance: &wasmtime::component::Instance,151) -> wasmtime::Result<TheWorld> {152let indices = TheWorldIndices::new(&instance.instance_pre(&store))?;153indices.load(&mut store, instance)154}155/// Convenience wrapper around [`TheWorldPre::new`] and156/// [`TheWorldPre::instantiate_async`].157pub async fn instantiate_async<_T>(158store: impl wasmtime::AsContextMut<Data = _T>,159component: &wasmtime::component::Component,160linker: &wasmtime::component::Linker<_T>,161) -> wasmtime::Result<TheWorld>162where163_T: Send,164{165let pre = linker.instantiate_pre(component)?;166TheWorldPre::new(pre)?.instantiate_async(store).await167}168pub fn add_to_linker<T, D>(169linker: &mut wasmtime::component::Linker<T>,170host_getter: fn(&mut T) -> D::Data<'_>,171) -> wasmtime::Result<()>172where173D: foo::foo::chars::HostWithStore + Send,174for<'a> D::Data<'a>: foo::foo::chars::Host + Send,175T: 'static + Send,176{177foo::foo::chars::add_to_linker::<T, D>(linker, host_getter)?;178Ok(())179}180pub fn foo_foo_chars(&self) -> &exports::foo::foo::chars::Guest {181&self.interface0182}183}184};185pub mod foo {186pub mod foo {187#[allow(clippy::all)]188pub mod chars {189#[allow(unused_imports)]190use wasmtime::component::__internal::Box;191pub trait HostWithStore: wasmtime::component::HasData + Send {192/// A function that accepts a character193fn take_char<T: Send>(194accessor: &wasmtime::component::Accessor<T, Self>,195x: char,196) -> impl ::core::future::Future<Output = ()> + Send;197/// A function that returns a character198fn return_char<T: Send>(199accessor: &wasmtime::component::Accessor<T, Self>,200) -> impl ::core::future::Future<Output = char> + Send;201}202pub trait Host: Send {}203impl<_T: Host + ?Sized + Send> Host for &mut _T {}204pub fn add_to_linker<T, D>(205linker: &mut wasmtime::component::Linker<T>,206host_getter: fn(&mut T) -> D::Data<'_>,207) -> wasmtime::Result<()>208where209D: HostWithStore,210for<'a> D::Data<'a>: Host,211T: 'static + Send,212{213let mut inst = linker.instance("foo:foo/chars")?;214inst.func_wrap_concurrent(215"take-char",216move |caller: &wasmtime::component::Accessor<T>, (arg0,): (char,)| {217wasmtime::component::__internal::Box::pin(async move {218let host = &caller.with_getter(host_getter);219let r = <D as HostWithStore>::take_char(host, arg0).await;220Ok(r)221})222},223)?;224inst.func_wrap_concurrent(225"return-char",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>::return_char(host).await;230Ok((r,))231})232},233)?;234Ok(())235}236}237}238}239pub mod exports {240pub mod foo {241pub mod foo {242#[allow(clippy::all)]243pub mod chars {244#[allow(unused_imports)]245use wasmtime::component::__internal::Box;246#[derive(Clone)]247pub struct Guest {248take_char: wasmtime::component::Func,249return_char: wasmtime::component::Func,250}251#[derive(Clone)]252pub struct GuestIndices {253take_char: wasmtime::component::ComponentExportIndex,254return_char: wasmtime::component::ComponentExportIndex,255}256impl GuestIndices {257/// Constructor for [`GuestIndices`] which takes a258/// [`Component`](wasmtime::component::Component) as input and can be executed259/// before instantiation.260///261/// This constructor can be used to front-load string lookups to find exports262/// within a component.263pub fn new<_T>(264_instance_pre: &wasmtime::component::InstancePre<_T>,265) -> wasmtime::Result<GuestIndices> {266let instance = _instance_pre267.component()268.get_export_index(None, "foo:foo/chars")269.ok_or_else(|| {270wasmtime::format_err!(271"no exported instance named `foo:foo/chars`"272)273})?;274let mut lookup = move |name| {275_instance_pre276.component()277.get_export_index(Some(&instance), name)278.ok_or_else(|| {279wasmtime::format_err!(280"instance export `foo:foo/chars` does \281not have export `{name}`"282)283})284};285let _ = &mut lookup;286let take_char = lookup("take-char")?;287let return_char = lookup("return-char")?;288Ok(GuestIndices {289take_char,290return_char,291})292}293pub fn load(294&self,295mut store: impl wasmtime::AsContextMut,296instance: &wasmtime::component::Instance,297) -> wasmtime::Result<Guest> {298let _instance = instance;299let _instance_pre = _instance.instance_pre(&store);300let _instance_type = _instance_pre.instance_type();301let mut store = store.as_context_mut();302let _ = &mut store;303let take_char = *_instance304.get_typed_func::<(char,), ()>(&mut store, &self.take_char)?305.func();306let return_char = *_instance307.get_typed_func::<308(),309(char,),310>(&mut store, &self.return_char)?311.func();312Ok(Guest { take_char, return_char })313}314}315impl Guest {316/// A function that accepts a character317pub async fn call_take_char<_T, _D>(318&self,319accessor: &wasmtime::component::Accessor<_T, _D>,320arg0: char,321) -> wasmtime::Result<()>322where323_T: Send,324_D: wasmtime::component::HasData,325{326let callee = unsafe {327wasmtime::component::TypedFunc::<328(char,),329(),330>::new_unchecked(self.take_char)331};332let ((), _) = callee.call_concurrent(accessor, (arg0,)).await?;333Ok(())334}335/// A function that returns a character336pub async fn call_return_char<_T, _D>(337&self,338accessor: &wasmtime::component::Accessor<_T, _D>,339) -> wasmtime::Result<char>340where341_T: Send,342_D: wasmtime::component::HasData,343{344let callee = unsafe {345wasmtime::component::TypedFunc::<346(),347(char,),348>::new_unchecked(self.return_char)349};350let ((ret0,), _) = callee.call_concurrent(accessor, ()).await?;351Ok(ret0)352}353}354}355}356}357}358359360