Path: blob/main/crates/component-macro/tests/expanded/char.rs
3070 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,174for<'a> D::Data<'a>: foo::foo::chars::Host,175T: 'static,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 {}192impl<_T: ?Sized> HostWithStore for _T193where194_T: wasmtime::component::HasData,195{}196pub trait Host {197/// A function that accepts a character198fn take_char(&mut self, x: char) -> ();199/// A function that returns a character200fn return_char(&mut self) -> char;201}202impl<_T: Host + ?Sized> Host for &mut _T {203/// A function that accepts a character204fn take_char(&mut self, x: char) -> () {205Host::take_char(*self, x)206}207/// A function that returns a character208fn return_char(&mut self) -> char {209Host::return_char(*self)210}211}212pub fn add_to_linker<T, D>(213linker: &mut wasmtime::component::Linker<T>,214host_getter: fn(&mut T) -> D::Data<'_>,215) -> wasmtime::Result<()>216where217D: HostWithStore,218for<'a> D::Data<'a>: Host,219T: 'static,220{221let mut inst = linker.instance("foo:foo/chars")?;222inst.func_wrap(223"take-char",224move |225mut caller: wasmtime::StoreContextMut<'_, T>,226(arg0,): (char,)|227{228let host = &mut host_getter(caller.data_mut());229let r = Host::take_char(host, arg0);230Ok(r)231},232)?;233inst.func_wrap(234"return-char",235move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {236let host = &mut host_getter(caller.data_mut());237let r = Host::return_char(host);238Ok((r,))239},240)?;241Ok(())242}243}244}245}246pub mod exports {247pub mod foo {248pub mod foo {249#[allow(clippy::all)]250pub mod chars {251#[allow(unused_imports)]252use wasmtime::component::__internal::Box;253#[derive(Clone)]254pub struct Guest {255take_char: wasmtime::component::Func,256return_char: wasmtime::component::Func,257}258#[derive(Clone)]259pub struct GuestIndices {260take_char: wasmtime::component::ComponentExportIndex,261return_char: wasmtime::component::ComponentExportIndex,262}263impl GuestIndices {264/// Constructor for [`GuestIndices`] which takes a265/// [`Component`](wasmtime::component::Component) as input and can be executed266/// before instantiation.267///268/// This constructor can be used to front-load string lookups to find exports269/// within a component.270pub fn new<_T>(271_instance_pre: &wasmtime::component::InstancePre<_T>,272) -> wasmtime::Result<GuestIndices> {273let instance = _instance_pre274.component()275.get_export_index(None, "foo:foo/chars")276.ok_or_else(|| {277wasmtime::format_err!(278"no exported instance named `foo:foo/chars`"279)280})?;281let mut lookup = move |name| {282_instance_pre283.component()284.get_export_index(Some(&instance), name)285.ok_or_else(|| {286wasmtime::format_err!(287"instance export `foo:foo/chars` does \288not have export `{name}`"289)290})291};292let _ = &mut lookup;293let take_char = lookup("take-char")?;294let return_char = lookup("return-char")?;295Ok(GuestIndices {296take_char,297return_char,298})299}300pub fn load(301&self,302mut store: impl wasmtime::AsContextMut,303instance: &wasmtime::component::Instance,304) -> wasmtime::Result<Guest> {305let _instance = instance;306let _instance_pre = _instance.instance_pre(&store);307let _instance_type = _instance_pre.instance_type();308let mut store = store.as_context_mut();309let _ = &mut store;310let take_char = *_instance311.get_typed_func::<(char,), ()>(&mut store, &self.take_char)?312.func();313let return_char = *_instance314.get_typed_func::<315(),316(char,),317>(&mut store, &self.return_char)?318.func();319Ok(Guest { take_char, return_char })320}321}322impl Guest {323/// A function that accepts a character324pub fn call_take_char<S: wasmtime::AsContextMut>(325&self,326mut store: S,327arg0: char,328) -> wasmtime::Result<()> {329let callee = unsafe {330wasmtime::component::TypedFunc::<331(char,),332(),333>::new_unchecked(self.take_char)334};335let () = callee.call(store.as_context_mut(), (arg0,))?;336Ok(())337}338/// A function that returns a character339pub fn call_return_char<S: wasmtime::AsContextMut>(340&self,341mut store: S,342) -> wasmtime::Result<char> {343let callee = unsafe {344wasmtime::component::TypedFunc::<345(),346(char,),347>::new_unchecked(self.return_char)348};349let (ret0,) = callee.call(store.as_context_mut(), ())?;350Ok(ret0)351}352}353}354}355}356}357358359