Skip to content

Instantly share code, notes, and snippets.

@dicej
Created October 4, 2024 23:41
Show Gist options
  • Save dicej/9dd0adc2386a06403cca70cc16ef1fe8 to your computer and use it in GitHub Desktop.
Save dicej/9dd0adc2386a06403cca70cc16ef1fe8 to your computer and use it in GitHub Desktop.
wasmtime-wit-bindgen lifetime fun
pub fn for_any<F, R, T>(fun: F) -> F
where
F: FnOnce(StoreContextMut<T>) -> R + 'static,
R: 'static,
{
fun
}
pub fn for_any_future<F, R, T, FF>(fut: FF) -> FF
where
F: FnOnce(StoreContextMut<T>) -> R + 'static,
FF: Future<Output = F> + 'static,
R: 'static,
{
fut
}
pub fn for_any_concurrent<F, P, R, T, FF, FFF>(fun: FFF) -> FFF
where
F: FnOnce(StoreContextMut<T>) -> R + 'static,
FF: Future<Output = F> + 'static,
FFF: Fn(StoreContextMut<T>, P) -> FF + 'static,
R: 'static,
{
fun
}
pub fn add_to_linker_get_host<
T,
G: for<'a> GetHost<&'a mut T, T, Host: Host<BodyData = T> + Send>,
>(
linker: &mut wasmtime::component::Linker<T>,
host_getter: G,
) -> wasmtime::Result<()>
where
T: Send,
{
inst.func_wrap_concurrent("[static]body.finish", wasmtime::component::for_any_concurrent(move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,) : (wasmtime::component::Resource<Body>, )| {
let host = caller;
let r = <G::Host as HostBody>::finish(host, arg0,);
wasmtime::component::for_any_future(async move {
let fun = r.await;
wasmtime::component::for_any(move |mut caller: wasmtime::StoreContextMut<'_, T>| {
let r = fun(caller);
Ok((r?,))
})
})
}
))?;
}
// error: lifetime may not live long enough
// --> /home/dicej/p/async-demo/target/debug/build/wasmtime-component-macro-57958afb0f63a808/out/interfaces0.rs:1620:21
// |
// 1617 | inst.func_wrap_concurrent("[static]body.finish", wasmtime::component::for_any_concurrent(move |mut caller: wasmtime::StoreContextMut<'_, T>, (arg0,) : (wasmtime::component::Resource<Body>, )...
// | ---------- has type `StoreContextMut<'1, T>` - return type of closure `{async block@/home/dicej/p/async-demo/target/debug/build/wasmtime-component-macro-57958afb0f63a808/out/interfaces0.rs:1620:57: 1620:67}` contains a lifetime `'2`
// ...
// 1620 | / wasmtime::component::for_any_future(async move {
// 1621 | | let fun = r.await;
// 1622 | | wasmtime::component::for_any(move |mut caller: wasmtime::StoreContextMut<'_, T>| {
// 1623 | | let r = fun(caller);
// 1624 | | Ok((r?,))
// 1625 | | })
// 1626 | | })
// | |______________________^ returning this value requires that `'1` must outlive `'2`
// |
// = note: requirement occurs because of a mutable reference to `T`
// = note: mutable references are invariant over their type parameter
// = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment