pub struct DPre<T: 'static> {
instance_pre: wasmtime::component::InstancePre<T>,
indices: DIndices,
}
impl<T: 'static> Clone for DPre<T> {
fn clone(&self) -> Self {
Self {
instance_pre: self.instance_pre.clone(),
indices: self.indices.clone(),
}
}
}
impl<_T: 'static> DPre<_T> {
pub fn new(
instance_pre: wasmtime::component::InstancePre<_T>,
) -> wasmtime::Result<Self> {
let indices = DIndices::new(&instance_pre)?;
Ok(Self { instance_pre, indices })
}
pub fn engine(&self) -> &wasmtime::Engine {
self.instance_pre.engine()
}
pub fn instance_pre(&self) -> &wasmtime::component::InstancePre<_T> {
&self.instance_pre
}
pub fn instantiate(
&self,
mut store: impl wasmtime::AsContextMut<Data = _T>,
) -> wasmtime::Result<D> {
let mut store = store.as_context_mut();
let instance = self.instance_pre.instantiate(&mut store)?;
self.indices.load(&mut store, &instance)
}
}
impl<_T: Send + 'static> DPre<_T> {
pub async fn instantiate_async(
&self,
mut store: impl wasmtime::AsContextMut<Data = _T>,
) -> wasmtime::Result<D> {
let mut store = store.as_context_mut();
let instance = self.instance_pre.instantiate_async(&mut store).await?;
self.indices.load(&mut store, &instance)
}
}
#[derive(Clone)]
pub struct DIndices {}
pub struct D {}
const _: () = {
impl DIndices {
pub fn new<_T>(
_instance_pre: &wasmtime::component::InstancePre<_T>,
) -> wasmtime::Result<Self> {
let _component = _instance_pre.component();
let _instance_type = _instance_pre.instance_type();
Ok(DIndices {})
}
pub fn load(
&self,
mut store: impl wasmtime::AsContextMut,
instance: &wasmtime::component::Instance,
) -> wasmtime::Result<D> {
let _ = &mut store;
let _instance = instance;
Ok(D {})
}
}
impl D {
pub fn instantiate<_T>(
store: impl wasmtime::AsContextMut<Data = _T>,
component: &wasmtime::component::Component,
linker: &wasmtime::component::Linker<_T>,
) -> wasmtime::Result<D> {
let pre = linker.instantiate_pre(component)?;
DPre::new(pre)?.instantiate(store)
}
pub fn new(
mut store: impl wasmtime::AsContextMut,
instance: &wasmtime::component::Instance,
) -> wasmtime::Result<D> {
let indices = DIndices::new(&instance.instance_pre(&store))?;
indices.load(&mut store, instance)
}
pub async fn instantiate_async<_T>(
store: impl wasmtime::AsContextMut<Data = _T>,
component: &wasmtime::component::Component,
linker: &wasmtime::component::Linker<_T>,
) -> wasmtime::Result<D>
where
_T: Send,
{
let pre = linker.instantiate_pre(component)?;
DPre::new(pre)?.instantiate_async(store).await
}
pub fn add_to_linker<T, D>(
linker: &mut wasmtime::component::Linker<T>,
host_getter: fn(&mut T) -> D::Data<'_>,
) -> wasmtime::Result<()>
where
D: foo::foo::a::HostWithStore<T> + foo::foo::b::HostWithStore<T>
+ foo::foo::c::HostWithStore<T> + d::HostWithStore<T>,
for<'a> D::Data<
'a,
>: foo::foo::a::Host + foo::foo::b::Host + foo::foo::c::Host + d::Host,
T: 'static,
{
foo::foo::a::add_to_linker::<T, D>(linker, host_getter)?;
foo::foo::b::add_to_linker::<T, D>(linker, host_getter)?;
foo::foo::c::add_to_linker::<T, D>(linker, host_getter)?;
d::add_to_linker::<T, D>(linker, host_getter)?;
Ok(())
}
}
};
pub mod foo {
pub mod foo {
#[allow(clippy::all)]
pub mod a {
#[allow(unused_imports)]
use wasmtime::component::__internal::Box;
#[derive(wasmtime::component::ComponentType)]
#[derive(wasmtime::component::Lift)]
#[derive(wasmtime::component::Lower)]
#[component(record)]
#[derive(Clone, Copy)]
pub struct Foo {}
impl core::fmt::Debug for Foo {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Foo").finish()
}
}
const _: () = {
assert!(0 == < Foo as wasmtime::component::ComponentType >::SIZE32);
assert!(1 == < Foo as wasmtime::component::ComponentType >::ALIGN32);
};
pub trait HostWithStore<T>: wasmtime::component::HasData {}
impl<H: ?Sized, T> HostWithStore<T> for H
where
H: wasmtime::component::HasData,
{}
pub trait Host {
fn a(&mut self) -> Foo;
}
impl<_T: Host + ?Sized> Host for &mut _T {
fn a(&mut self) -> Foo {
Host::a(*self)
}
}
pub fn add_to_linker_instance<T, D>(
inst: &mut wasmtime::component::LinkerInstance<'_, T>,
host_getter: fn(&mut T) -> D::Data<'_>,
) -> wasmtime::Result<()>
where
D: HostWithStore<T>,
for<'a> D::Data<'a>: Host,
T: 'static,
{
inst.func_wrap(
"a",
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
let host = &mut host_getter(caller.data_mut());
let r = Host::a(host);
Ok((r,))
},
)?;
Ok(())
}
pub fn add_to_linker<T, D>(
linker: &mut wasmtime::component::Linker<T>,
host_getter: fn(&mut T) -> D::Data<'_>,
) -> wasmtime::Result<()>
where
D: HostWithStore<T>,
for<'a> D::Data<'a>: Host,
T: 'static,
{
let mut inst = linker.instance("foo:foo/a")?;
add_to_linker_instance::<T, D>(&mut inst, host_getter)
}
}
#[allow(clippy::all)]
pub mod b {
#[allow(unused_imports)]
use wasmtime::component::__internal::Box;
pub type Foo = super::super::super::foo::foo::a::Foo;
const _: () = {
assert!(0 == < Foo as wasmtime::component::ComponentType >::SIZE32);
assert!(1 == < Foo as wasmtime::component::ComponentType >::ALIGN32);
};
pub trait HostWithStore<T>: wasmtime::component::HasData {}
impl<H: ?Sized, T> HostWithStore<T> for H
where
H: wasmtime::component::HasData,
{}
pub trait Host {
fn a(&mut self) -> Foo;
}
impl<_T: Host + ?Sized> Host for &mut _T {
fn a(&mut self) -> Foo {
Host::a(*self)
}
}
pub fn add_to_linker_instance<T, D>(
inst: &mut wasmtime::component::LinkerInstance<'_, T>,
host_getter: fn(&mut T) -> D::Data<'_>,
) -> wasmtime::Result<()>
where
D: HostWithStore<T>,
for<'a> D::Data<'a>: Host,
T: 'static,
{
inst.func_wrap(
"a",
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
let host = &mut host_getter(caller.data_mut());
let r = Host::a(host);
Ok((r,))
},
)?;
Ok(())
}
pub fn add_to_linker<T, D>(
linker: &mut wasmtime::component::Linker<T>,
host_getter: fn(&mut T) -> D::Data<'_>,
) -> wasmtime::Result<()>
where
D: HostWithStore<T>,
for<'a> D::Data<'a>: Host,
T: 'static,
{
let mut inst = linker.instance("foo:foo/b")?;
add_to_linker_instance::<T, D>(&mut inst, host_getter)
}
}
#[allow(clippy::all)]
pub mod c {
#[allow(unused_imports)]
use wasmtime::component::__internal::Box;
pub type Foo = super::super::super::foo::foo::b::Foo;
const _: () = {
assert!(0 == < Foo as wasmtime::component::ComponentType >::SIZE32);
assert!(1 == < Foo as wasmtime::component::ComponentType >::ALIGN32);
};
pub trait HostWithStore<T>: wasmtime::component::HasData {}
impl<H: ?Sized, T> HostWithStore<T> for H
where
H: wasmtime::component::HasData,
{}
pub trait Host {
fn a(&mut self) -> Foo;
}
impl<_T: Host + ?Sized> Host for &mut _T {
fn a(&mut self) -> Foo {
Host::a(*self)
}
}
pub fn add_to_linker_instance<T, D>(
inst: &mut wasmtime::component::LinkerInstance<'_, T>,
host_getter: fn(&mut T) -> D::Data<'_>,
) -> wasmtime::Result<()>
where
D: HostWithStore<T>,
for<'a> D::Data<'a>: Host,
T: 'static,
{
inst.func_wrap(
"a",
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
let host = &mut host_getter(caller.data_mut());
let r = Host::a(host);
Ok((r,))
},
)?;
Ok(())
}
pub fn add_to_linker<T, D>(
linker: &mut wasmtime::component::Linker<T>,
host_getter: fn(&mut T) -> D::Data<'_>,
) -> wasmtime::Result<()>
where
D: HostWithStore<T>,
for<'a> D::Data<'a>: Host,
T: 'static,
{
let mut inst = linker.instance("foo:foo/c")?;
add_to_linker_instance::<T, D>(&mut inst, host_getter)
}
}
}
}
#[allow(clippy::all)]
pub mod d {
#[allow(unused_imports)]
use wasmtime::component::__internal::Box;
pub type Foo = super::foo::foo::c::Foo;
const _: () = {
assert!(0 == < Foo as wasmtime::component::ComponentType >::SIZE32);
assert!(1 == < Foo as wasmtime::component::ComponentType >::ALIGN32);
};
pub trait HostWithStore<T>: wasmtime::component::HasData {}
impl<H: ?Sized, T> HostWithStore<T> for H
where
H: wasmtime::component::HasData,
{}
pub trait Host {
fn b(&mut self) -> Foo;
}
impl<_T: Host + ?Sized> Host for &mut _T {
fn b(&mut self) -> Foo {
Host::b(*self)
}
}
pub fn add_to_linker_instance<T, D>(
inst: &mut wasmtime::component::LinkerInstance<'_, T>,
host_getter: fn(&mut T) -> D::Data<'_>,
) -> wasmtime::Result<()>
where
D: HostWithStore<T>,
for<'a> D::Data<'a>: Host,
T: 'static,
{
inst.func_wrap(
"b",
move |mut caller: wasmtime::StoreContextMut<'_, T>, (): ()| {
let host = &mut host_getter(caller.data_mut());
let r = Host::b(host);
Ok((r,))
},
)?;
Ok(())
}
pub fn add_to_linker<T, D>(
linker: &mut wasmtime::component::Linker<T>,
host_getter: fn(&mut T) -> D::Data<'_>,
) -> wasmtime::Result<()>
where
D: HostWithStore<T>,
for<'a> D::Data<'a>: Host,
T: 'static,
{
let mut inst = linker.instance("d")?;
add_to_linker_instance::<T, D>(&mut inst, host_getter)
}
}