Path: blob/main/crates/component-macro/tests/expanded/function-new_concurrent.rs
3064 views
/// Auto-generated bindings for a pre-instantiated version of a1/// component which implements the world `foo`.2///3/// This structure is created through [`FooPre::new`] which4/// takes a [`InstancePre`](wasmtime::component::InstancePre) that5/// has been created through a [`Linker`](wasmtime::component::Linker).6///7/// For more information see [`Foo`] as well.8pub struct FooPre<T: 'static> {9instance_pre: wasmtime::component::InstancePre<T>,10indices: FooIndices,11}12impl<T: 'static> Clone for FooPre<T> {13fn clone(&self) -> Self {14Self {15instance_pre: self.instance_pre.clone(),16indices: self.indices.clone(),17}18}19}20impl<_T: 'static> FooPre<_T> {21/// Creates a new copy of `FooPre` 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 = FooIndices::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 [`Foo`] 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<Foo> {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> FooPre<_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<Foo> {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/// `foo`.67///68/// This is an implementation detail of [`FooPre`] and can69/// be constructed if needed as well.70///71/// For more information see [`Foo`] as well.72#[derive(Clone)]73pub struct FooIndices {74new: wasmtime::component::ComponentExportIndex,75}76/// Auto-generated bindings for an instance a component which77/// implements the world `foo`.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/// [`Foo::instantiate`] which only needs a84/// [`Store`], [`Component`], and [`Linker`].85///86/// * Alternatively you can create a [`FooPre`] ahead of87/// time with a [`Component`] to front-load string lookups88/// of exports once instead of per-instantiation. This89/// method then uses [`FooPre::instantiate`] to90/// create a [`Foo`].91///92/// * If you've instantiated the instance yourself already93/// then you can use [`Foo::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 Foo {102new: wasmtime::component::Func,103}104const _: () = {105impl FooIndices {106/// Creates a new copy of `FooIndices` 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 new = {117let (item, index) = _component118.get_export(None, "new")119.ok_or_else(|| wasmtime::format_err!("no export `new` found"))?;120match item {121wasmtime::component::types::ComponentItem::ComponentFunc(func) => {122wasmtime::error::Context::context(123func.typecheck::<(), ()>(&_instance_type),124"type-checking export func `new`",125)?;126index127}128_ => Err(wasmtime::format_err!("export `new` is not a function"))?,129}130};131Ok(FooIndices { new })132}133/// Uses the indices stored in `self` to load an instance134/// of [`Foo`] from the instance provided.135///136/// Note that at this time this method will additionally137/// perform type-checks of all exports.138pub fn load(139&self,140mut store: impl wasmtime::AsContextMut,141instance: &wasmtime::component::Instance,142) -> wasmtime::Result<Foo> {143let _ = &mut store;144let _instance = instance;145let new = *_instance.get_typed_func::<(), ()>(&mut store, &self.new)?.func();146Ok(Foo { new })147}148}149impl Foo {150/// Convenience wrapper around [`FooPre::new`] and151/// [`FooPre::instantiate`].152pub fn instantiate<_T>(153store: impl wasmtime::AsContextMut<Data = _T>,154component: &wasmtime::component::Component,155linker: &wasmtime::component::Linker<_T>,156) -> wasmtime::Result<Foo> {157let pre = linker.instantiate_pre(component)?;158FooPre::new(pre)?.instantiate(store)159}160/// Convenience wrapper around [`FooIndices::new`] and161/// [`FooIndices::load`].162pub fn new(163mut store: impl wasmtime::AsContextMut,164instance: &wasmtime::component::Instance,165) -> wasmtime::Result<Foo> {166let indices = FooIndices::new(&instance.instance_pre(&store))?;167indices.load(&mut store, instance)168}169/// Convenience wrapper around [`FooPre::new`] and170/// [`FooPre::instantiate_async`].171pub async fn instantiate_async<_T>(172store: impl wasmtime::AsContextMut<Data = _T>,173component: &wasmtime::component::Component,174linker: &wasmtime::component::Linker<_T>,175) -> wasmtime::Result<Foo>176where177_T: Send,178{179let pre = linker.instantiate_pre(component)?;180FooPre::new(pre)?.instantiate_async(store).await181}182pub async fn call_new<_T, _D>(183&self,184accessor: &wasmtime::component::Accessor<_T, _D>,185) -> wasmtime::Result<()>186where187_T: Send,188_D: wasmtime::component::HasData,189{190let callee = unsafe {191wasmtime::component::TypedFunc::<(), ()>::new_unchecked(self.new)192};193let ((), _) = callee.call_concurrent(accessor, ()).await?;194Ok(())195}196}197};198199200