[][src]Crate wasmtime_async


This crate is an extension to the wasmtime crate. It makes it possible to use wasmtime along with futures, without having to consider every WebAssembly call as blocking. Note however that it is designed only for cases where the time spent in WebAssembly is indeed not enough to warrant running it in a worker thread -- it is designed with, in mind, relatively lightweight WebAssembly code that then calls into async callbacks provided from the rust side.

The WebAssembly code will be run on a specific stack and, when it calls an async callback provided by rust, will be interrupted until the future has returned. As such, so long as the time spent inside WebAssembly is not enough to warrant in itself running the whole task on a blocking executor, it should be possible to run WebAssembly code that integrates seamlessly with async code.

The drawback of this approach is that the async callbacks appear as though they were blocking to WebAssembly. This is unfortunately necessary to not require the WebAssembly code itself to be async-ready.


This crate provides two main elements:

  • The Stack struct, that owns a stack on which WebAssembly code can run
  • The FuncExt trait, that provides extension methods for wasmtime::Func for dealing with asynchronous callbacks provided by Rust to WebAssembly.


The Stack struct is easy to use: just use Stack::new, and it will create a stack on which WebAssembly code can run.

Running code on a Stack takes a mutable reference to it so long as the code is running, thus preventing multiple functions from using the same stack without any runtime cost or unsafe usage.


The FuncExt provides most of the interesting methods in this crate.

Wrapping async functions for use from WebAssembly

First, use the methods to wrap async functions in Rust so that they could be used transparently from WebAssembly:

It would be better to have a single FuncExt::wrap_async function, but unfortunately, due to both orphan rules and a probable limitation of rustc, it is not possible to do so at the moment, hence the multiple methods that handle each part of Func::wrap.

Calling WebAssembly that uses async functions

When calling into WebAssembly code that potentially uses async functions, you must use an async-ready calling function, or a panic will arise!

The functions are these:



A “prelude” for users of the wasmtime-async crate.



Helper struct for retrieving native functions out of WebAssembly functions that call async callbacks.


A stack on which to run WebAssembly code


Future that runs WebAssembly code to completion, when the WebAssembly potentially includes async callback calls.



The extension trait to wasmtime::Func, that holds most of the interest of this crate.


Internal trait implemented for all arguments that can be passed to FuncExt::wrap_async.