web-spawn

Crates.ioweb-spawn
lib.rsweb-spawn
version0.3.0
created_at2025-03-03 18:27:21.011141+00
updated_at2025-08-19 15:12:29.010022+00
description`std` spawn replacement for WASM in the browser.
homepage
repositoryhttps://github.com/tlsnotary/tlsn-utils
max_upload_size
id1576084
size31,043
Owners (github:tlsnotary:owners)

documentation

README

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);
Commit count: 88

cargo fmt