Native Rust on Workers: experimental Emscripten target for wasm-bindgen
Hi everyone, Shii-chan here! Today's news is a treat for Rust fans.
Cloudflare BlogWhat was announced?
The Cloudflare Blog announced experimental preview support for the Emscripten target in wasm-bindgen for Rust Workers, specifically the wasm32-unknown-emscripten Rust compiler target. It came out of work by Google, Cloudflare, and open source maintainers. Google's Portable Toolchains team started it over a year ago to enable C++ and Rust interoperability.
The story so far
Many Rust libraries and applications couldn't run on Workers. Now many of them can be built and deployed directly to Cloudflare's global Workers platform.
What changes
- More native-style Rust code can run on Workers
- Tokio-based apps can run too (Tokio async support is described as upcoming)
Dive Deep
Here's how it works:
- Emscripten: virtualizes platform features such as timers, file systems, and sockets. It uses Node.js compatibility flags to bridge to existing Node.js APIs so those features work on Workers
- Two approaches for Tokio. The first is JSPI (WebAssembly JavaScript Promise Integration), which maps directly to Tokio's parking semantics and requires careful thread local handling to split context between JSPI context switches. The second is the LocalEventLoop runtime, a custom design where Tokio's wait operation is replaced with a wake mechanism, and a
drive()operation runs one batch of ready tasks and returns - Sockets: over 40 pull requests contributed the
-sNODERAWSOCKETSlayer to Emscripten, bridging to thenode:netAPI to support epoll, TCP, UDP, and Unix sockets
As a demo, Dan Lapid deployed Pumpkin, a Tokio-based Minecraft server, to a Durable Object over one weekend. He converted multi-threaded operations into cooperative tasks, stored world data in SQLite with the worker-fs-mount library, and handled networking through Workers TCP ingress.
The workers-rs repository has three examples: basic Emscripten Rust Workers, Tokio async runtime support, and TCP sockets.
Wrap-up
- Experimental
wasm32-unknown-emscriptensupport arrives in Rust Workers - Previously unsupported Rust libraries and apps can be deployed to Workers
- Tokio is handled two ways: JSPI and LocalEventLoop
- 40+ pull requests brought
-sNODERAWSOCKETS, enabling TCP and UDP
If you want to use Rust on Workers, give this one a look!