General enquiries :
+44 (0)20 7602 6000

Rust + WebAssembly - Is it ready?

Wednesday 29 July 2020 Programming

Chris Manning's picture
By Chris Manning

Rust has quickly become one of my favourite programming languages with its brilliant tooling and rich type system. In recent times, its community has been working towards making it a language for the web, both on the server and in the browser.

As a systems language that may be a bit of a surprise. But how can it run in a browser? Enter WebAssembly.

WebAssembly (wasm) is a nascent technology initially aimed at providing an assembly language and runtime environment for web browsers. This has since been broadened to a portable assembly and runtime, not just the web, with several efforts in creating portable runtimes (wasmtime, Lucet, wasm-micro-runtime, waSCC, et al) with host system interfaces (WASI). This evaluation is however limited to running rust compiled to wasm in the browser.

Wasm enables code written in languages that usually target native platforms (such as C, C++, Go & Rust) to be compiled to portable bytecode like Java or C#. In web browsers, this bytecode is executed in a sandbox environment entirely within the JavaScript VM. Wasm modules can be loaded and invoked from JavaScript and JavaScript code can be invoked from wasm modules, though there are currently some limitations with tooling that make some of this difficult (see below).

As a proof of concept, a few months ago I created a very basic map drawing application written mostly in Rust. It takes OpenStreetMap data as input and plots and draws this on a canvas element, producing a line-drawn map of the area including roads and some natural features.

Here's how part of central Bristol looks:

The map can be zoomed in or out by scrolling within the area and it can be clicked and dragged to move it around. This basic functionality is responsive on my machine at least. The coordinates to plot are calculated in advance and kept in memory as I found this to have the best performance overall once more things were being drawn.

Using the canvas to draw, scale and translate resulted in jittery dragging and poor-quality scaling. I didn't have any success with drawing to a canvas offscreen and then loading this to the onscreen canvas. OffscreenCanvas is only partially supported in Firefox so I didn't get very far with that, but I also couldn't get CanvasRenderingContext2D to work offscreen either.

This has all been a learning experience and I'm sure I've made some (probably obvious) mistakes. Much of what has been done resulted from workarounds of individual issues that I could see being done differently now. Anyway, here is an account of my experience with it based on how things were earlier this year - there may have been improvements made to the language over the last few months.

JavaScript + Rust

The Rust WebAssembly working group has provided some tools for working with this. Rust code targeting wasm can call JS code easily through bindings generated by the wasm-bindgen tool and library. This is typically used to annotate Rust code to export bindings to JS (generates equivalent-ish code and glue), or annotate ffi declarations to import JS code. The wasm-pack tool is used for doing all the steps required for compiling Rust to wasm, generating JS glue code, and packaging it all up, invoking wasm-bindgen as necessary.

Many Rust crates (libraries from crates.io) can be compiled to wasm and so can simply be added to your project's Cargo.toml as a dependency, although there are some limitations to this (see below).

Export to JS

Rust structs are exported as classes to JS when marked with #[wasm_bindgen]. Since Rust doesn't have constructors, an associated function can be marked with #[wasm_bindgen(constructor)] to be exposed as a constructor so that a class can be instantiated with new Example(data) in JS (see image below). There are all sorts of variations on this that can be seen in the wasm-bindgen docs.

Import from JS

JavaScript functions can be called from rust through the use of bindings.

Snippets

Import from a snippet packaged beside Rust code (see image below). Note that the wasm-bindgen tool is not currently capable of allowing module imports within the snippets imported this way.

JavaScript and Web APIs

Built-in JS types and APIs are exposed to Rust with the js-sys crate. Web APIs are exposed in the web-sys crate, using js-sys where necessary. Both of these crates are fairly low-level bindings only and aren't always easy to use from Rust or at least aren't idiomatic.

Notable features:

  • web-sys contains bindings for everything I've tried
  • JS-style inheritance maps quite well to Rust (which has no inheritance)
    • Deref trait is used such that a type may be "dereferenced" automatically to its parent type where necessary, similarly to how smart pointers get dereferenced

Typical issues:

  • data type conversion to JsValue
    • often via JSON strings for non-primitives
  • every data type in web-sys is behind feature flags
    • great for generated code size
    • less great when you have to keep adding flags to the build for every little thing
    • makes auto-complete not work(!!)
  • wrapping closures is cumbersome
  • overloaded JS functions have to be separate in Rust
    • eg. CanvasRenderingContext2D.createImageData() in JS is create_image_data_with_imagedata and create_image_data_with_sw_and_sh in Rust for each overload
  • not an issue exactly, but there isn't much integration with rust's mutability and ownership model
    • everything is &self even though state may clearly be modified

stdweb

Interestingly there is also the stdweb crate. Whereas web-sys aims to provide raw bindings to the Web APIs, std-web aims to bring more idiomatic Rust APIs and doesn't utilise web-sys at all. I opted to stick with plain web-sys for a couple of reasons: stdweb doesn't have IndexedDB support so I had to use web-sys anyway, and web-sys is the official Rust wasm library for this, and there isn't interoperability between these. This situation may change in the future and I did read some comments from the author that hint at basing stdweb on top of web-sys.

Limitations

Crates

Many crates are written under the assumption that they will be used in a proper operating system environment, not a web browser sandbox. IO (emscripten manages with emulation, why not Rust?), processes, multi-threading (there is some experimental support using Web Workers) are among the things that will fail at runtime with wasm compiled rust code. This was pretty frustrating. I would add a dependency and write code to integrate it, only to have it fail at runtime because it parallelised some computation, or read from or wrote to a file for example.

NPM packages

It's possible to include npm packages and generated bindings for use in Rust code but I had difficulty getting this to work reliably and ended up avoiding it. See https://github.com/rustwasm/wasm-pack/issues/606 and https://github.com/rustwasm/wasm-bindgen/pull/1305

I believe a better approach given the current status of tooling would be to use a webpack with a wasm-pack plugin and some other plugin to generate a Rust crate from an npm package, rather than use wasm-pack directly. This hasn't yet been explored but I've seen at least one example somewhere that does something like this.

Alternatively an stdweb solution is able to interact with npm packages using browserify, see https://www.steadylearner.com/blog/read/How-to-use-NPM-packages-with-Rust-Frontend. With this method it looks like you have to wrap every function with inline JavaScript inside a rust function.

Async

Asynchronous functions in Rust have recently been stabilised using futures as the building blocks. This works well and even integrates with JS using Promises, thus opening the door to interfacing with asynchronous Web APIs in a natural way. The main problem I faced was that to use async methods, I had to have this method consume self not take by reference due to Rust's borrow checker (even though most of the time these functions were fine when not marked #[wasm_bindgen]). This is OK for one-off function calls for one-off objects, but once this function is called, the object cannot be used again. Rust side gives compile time error. JS side the pointer is nulled and a runtime error saying "a null pointer was passed to Rust" will be given.

The best method I've found so far for dealing with this is shown in the image below - where A is the struct with the async function(s) to expose. This is far from ideal, but it's mostly the result of exposing these async functions to JS. I'm certain there are better solutions to this.

IndexedDB

IndexedDB is a browser-based NoSQL database with a JavaScript API. This has been used to store OpenStreetMap data in a lightly transformed state, which the application then reads on load to feed the map plotting. This works well and IndexedDB is promising for offline storage, though I've not explored some of its actual database features, namely indexes and queries. Since this is browser technology it is available to any language that can access browser APIs.

IndexedDB has a completely asynchronous API but pre-dates Promises and is quite awkward in JavaScript, let alone Rust. A wrapper library indexeddb-rs has been used for interacting with IndexedDB, but this was incomplete (and doesn't compile in its current state at 371853197233df50069d67f332b3aaa3b555b78c). I've filled in some gaps to get it to compile, upgraded to the standardised futures version, and implemented basic transaction support so that I could manipulate database structure and insert and retrieve data. My fork is a submodule in this repo in indexeddb-rs and available on gitlab. Ideally this would be contributed upstream.

I had initially used Dexie for interacting with IndexedDB but this proved too cumbersome no matter which way I tried it (binding Dexie directly in rust, creating small wrappers in JS and binding these, doing all DB work JS side and sending this over to Rust).

Pure Rust Application

I've found that many of the issues mentioned here stem from trying to combine JavaScript and Rust application code rather than writing all in one or the other. The official tutorial goes this route of having JavaScript driving the application so I attempted to follow. When I began I wasn't quite sure how to make the jump to a pure Rust application given the constraints of the wasm runtime. You can define an entry point function (or simply call this entry point from JS) with some kind of application loop but dealing with state becomes quite difficult within this, particularly when you bring async functions into it (required for IndexedDB interaction). Although now async functions can be exposed as start functions so the situation has improved since this began.

Some Rust application frameworks exist that seem promising for this purpose, some taking very different approaches from each other: Seed (web front-end framework), OrbTK (multi-platform GUI toolkit with wasm support), Yew, Percy, and more. Yew seems the most mature and has some traction in the community, but it's based on stdweb and also doesn't integrate well with futures or async functions, instead using an actor model for concurrency. Sadly I don't think any of these are ready for production.

Seed

Seed is a Rust front-end framework targeting WebAssembly, strongly influenced by Elm and React. It's based on the official libraries/tools web-sys and wasm-bindgen so integration with other things is/will become easier. It also has an easy way to invoke async functions so that's nice. Unfortunately seed is quite immature overall and gives no stability guarantees right now. Though it is quite approachable if comfortable with the Elm model.

OrbTK

OrbTK is a GUI widget toolkit aiming to be cross-platform for desktop, mobile and web applications. Whilst it's early days it is usable already and the examples are quite understandable. What's interesting about OrbTK is not just its platform-independence, but also that it's backed by some quite interesting technology like an ECS (entity component system) to manage state and a FRP(functional reactive programming)-influenced API. It was very easy to compile some of the examples to target wasm and the widget-based examples run almost as well as they do as native desktop applications. The canvas example has very poor performance in browser however (looking at the code it appears to be using both a software renderer "euc" and its own bitmap renderer, not web canvas). Still clearly a long way to go for OrbTK but it's a promising GUI framework, something which Rust is lacking.

Gloo

Another that's worth mentioning is Gloo, also from the Rust Wasm group. Gloo is a modular toolkit to help with building web applications rather than a full application framework. Currently it has modules for events and timers, basic building blocks but a good start. Seed already uses some of Gloo and has stated it will continue to do so as it develops.

Conclusion

As it stands, many of the pieces are in place for WebAssembly to take off as a platform for secure and performant code execution on the web. It appears to be getting more and more popular outside of the web too, especially in cloud and serverless environments. It reminds me a bit of the JVM in some ways, just with a much lower level execution model which allows languages like C, C++, Rust and Go to target it.

For web applications, I think that Rust + Wasm is an option, with some caveats. Rust has a steep learning curve itself, but since most of this ecosystem is new or abstracting old with something new, it all has a learning curve. The Elm-like model adopted by front-end frameworks like Yew and Seed does seem to work well with the Rust way of doing things. But I couldn't say whether it has any advantage over Elm or React with JavaScript or TypeScript, other than not having to write much or any Javascript and a different set of tooling. Rust's strictness with ownership got in my way quite a bit, though as mentioned above I think this can largely be attributed to trying to use both JavaScript and Rust together with async functions. For offline apps, IndexedDB makes persistent data storage in-browser a breeze whether from JavaScript or WebAssembly. So really, Rust is just another language that can run in the browser. Rust itself has some great language features, but I haven't gone into any of that here.

So in answer to the title question - not yet. But watch the space.

Rust is becoming some of our team's favourite programming language. But how can make it a language for the web? In this technical blog we discuss how.

Rust + WebAssembly - Is it ready?