4 releases (2 breaking)

Uses new Rust 2024

0.3.0 Aug 19, 2025
0.2.1 Aug 12, 2025
0.2.0 Mar 7, 2025
0.1.0 Mar 3, 2025

#1482 in Web programming

Download history 115/week @ 2025-11-10 287/week @ 2025-11-17 275/week @ 2025-11-24 517/week @ 2025-12-01 415/week @ 2025-12-08 499/week @ 2025-12-15 294/week @ 2025-12-22 86/week @ 2025-12-29 288/week @ 2026-01-05 312/week @ 2026-01-12 389/week @ 2026-01-19 344/week @ 2026-01-26 236/week @ 2026-02-02 194/week @ 2026-02-09 154/week @ 2026-02-16 170/week @ 2026-02-23

765 downloads per month

MIT/Apache

17KB
294 lines

web-spawn

Crates.io Docs.rs

This crate provides a std::thread shim for WASM builds targeting web browsers.

It borrows from and is heavily inspired by both wasm-bindgen-rayon and wasm_thread but makes a couple different design choices.

Most notably, spawning is explicitly delegated to run in a background task and must be initialized at the start of the program. This task can either run on the main browser thread, or be moved to a dedicated worker to avoid potential interference from other loads.

Usage

Add web-spawn as a dependency in your Cargo.toml:

web-spawn = { version = "0.3" }

Then you must ensure that spawning is initialized. One way to do this is to start the spawner from within your WASM module:

use wasm_bindgen_futures::JsFuture;

JsFuture::from(web_spawn::start_spawner()).await.unwrap();

Alternatively, this can be done on the javascript side:

import init, { startSpawner } from /* your package */;

await init();

// Runs the spawner on a dedicated web worker.
await startSpawner();

Now, in the rest of your Rust code you can use web_spawn::spawn anywhere you would otherwise use std::thread::spawn:

use web_spawn::spawn;

assert_eq!(spawn(|| 1 + 1).join().unwrap(), 2);

Dependencies

~0–2.6MB
~47K SLoC