Skip to content

Changing the return type of add_to_linker closures #8382

Open
@lann

Description

@lann

From this Zulip discussion:

I've been experimenting with ways to provide reusable bits of host functionality and have run into a stumbling block with wasmtime's generated add_to_linker functions, which take getclosures with types (roughly) like:

get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static
    ...
    where U: Host,

These closures are usually trivial field accesses like |data| &mut data.some_host_impl, but this is somewhat limiting because you can only return &muts borrowing from T. As a specific example, the wasmtime-wasi WasiView::table needs to be shared with wasmtime-wasi-http's WasiHttpView::table, but this currently requires both *View implementations to use the same concrete type within T, making the two host implementations uncomposable.

There are some unsafe approaches to deal with this but I'm wondering if it would be possible to change the add_to_linker closure signature to permit returning non-ref types, e.g. something that impls AsMut<U>. I've been trying to find a way to do this backward-compatibility but I'm running into the limits of my understanding of closure lifetime

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions