Arc works when using wasm_bindgen for HTML5?

Does Arc work when targetting the HTML5 using wasm_bindgen?

If there was a playground for that (not play.rust-lang.org), I'd not need to ask this. :face_with_diagonal_mouth:

What ChatGPT says:

As of my last update in September 2021, the Rust Arc type is specific to the Rust programming language and is not natively available in web browsers like Chrome, Firefox, or Safari. The Arc type stands for "atomic reference-counted smart pointer" and is used for managing shared ownership of data across threads in Rust.

Try this: Compiler Explorer

1 Like

Interesting... but the compiler explorer has no wasm32-unknown-unknown target installed. Even if I used wasm_bindgen though, what do atomic loads map to in the standard library anyway when target_arch = "wasm32"?

My understanding is that yes and no:

  • Yes: if you write code using it, it will run.

  • No: it does not actually provide thread-safety, which is fine as long as you don't use any threads (which is easy to obey because std doesn't offer creating threads).

The std for wasm32-unknown-unknown was written at a time before “threads in wasm in the browser” was a practical thing to accomplish. Therefore, all of the std::sync operations are stubbed out: Arc works like Rc, and Mutex and RwLock work like RefCell.

Making these things work will require fully supporting atomics-in-WASM:

3 Likes

I'll only create threads in targets other than the browser for running concurrent systems, so no problem.

But std::thread allows spawning threads, no?

No, it is not available on wasm targets.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.