Skip to content

Commit

Permalink
Add support for async/streams/futures to Rust generator (#1082)
Browse files Browse the repository at this point in the history
* Add support for async/streams/futures to Rust generator

This adds support for generating bindings which use the [Async
ABI](https://github.com/WebAssembly/component-model/blob/main/design/mvp/Async.md)
along with the [`stream`, `future`, and
`error-context`](WebAssembly/component-model#405) types.

By default, normal synchronous bindings are generated, but the user may opt-in
to async bindings for all or some of the imported and/or exported functions in
the target world and interfaces -- provided the default-enabled `async` feature
is enabled.

In addition, we generate `StreamPayload` and/or `FuturePayload` trait
implementations for any types appearing as the `T` in `stream<T>` or `future<T>`
in the WIT files, respectively.  That enables user code to call `new_stream` or
`new_future` to create `stream`s or `future`s with those payload types, then
write to them, read from them, and/or pass the readable end as a parameter to a
component import or return value of a component export.

Note that I've added new `core::abi::Instruction` enum variants to handle async
lifting and lowering, but they're currently tailored to the Rust generator and
will probably change somewhat as we add support for other languages.

This does not include any new tests; I'll add those in a follow-up commit.

Signed-off-by: Joel Dice <[email protected]>

add `async: true` case to Rust `codegen_tests`

This ensures that all the codegen test WIT files produce compile-able bindings
with `async: true` (i.e. all imports lowered and all exports lifted using the
async ABI).  That revealed some issues involving resource methods and
constructors, as well as missing stub support, which I've resolved.

Signed-off-by: Joel Dice <[email protected]>

add codegen tests for futures, streams, and error-contexts

Signed-off-by: Joel Dice <[email protected]>

remove async_support::poll_future

It was both unsafe to use and intended only for testing (and not even good for
that, it turns out).

Signed-off-by: Joel Dice <[email protected]>

add stream/future read/write cancellation support

Also, fix some issues with stream/future payload lifting/lowering which I
_thought_ I had already tested but actually hadn't.

Signed-off-by: Joel Dice <[email protected]>

support callback-less (AKA stackful) async lifts

Signed-off-by: Joel Dice <[email protected]>

revert incorrect test change in flavorful/wasm.rs

I had thoughtlessly removed test code based on a clippy warning, not realizing
it was testing (at compile time) that the generated types implemented `Debug`.

Signed-off-by: Joel Dice <[email protected]>

test `async: true` option in Rust codegen tests

I had meant to do this originally, but apparently forgot to actually use the
option.

Signed-off-by: Joel Dice <[email protected]>

add docs for new `debug` and `async` Rust macro options

Signed-off-by: Joel Dice <[email protected]>

address `cargo check` lifetime warning

Signed-off-by: Joel Dice <[email protected]>

minimize use of features based on PR feedback

Signed-off-by: Joel Dice <[email protected]>

* add comment explaining lack of recursion in `type_id_info`

Signed-off-by: Joel Dice <[email protected]>

* refactor stream and future support for Rust generator

Per a discussion with Alex, this moves most of the stream and future code into
the `wit-bindgen-rt` crate.  That's where I had intended to put it to begin
with, but ran into orphan rule issues given the trait-based approach I was
using.

The new approach uses dynamic dispatch via a vtable type.  Thus we've traded a
small (theoretical) amount of performance for much better compatibility in cases
of separately-generated bindings (e.g. passing `FutureWriter<T>` between crates
should work fine now), easier debugging, etc.

Signed-off-by: Joel Dice <[email protected]>

* break `streams_and_futures` into two modules

Signed-off-by: Joel Dice <[email protected]>

* restore previously-removed feature check in Rust macro

Signed-off-by: Joel Dice <[email protected]>

---------

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej authored Jan 8, 2025
1 parent 19f259b commit 3f6096b
Show file tree
Hide file tree
Showing 41 changed files with 3,443 additions and 348 deletions.
134 changes: 91 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f6096b

Please sign in to comment.