I’m trying to create my first Rust game using Wasm and SDL. I’m also playing with Specs to compartmentalize everything in systems. Unfortunately, I’m having some issues with the lack of thread-safety of the SDL context, and am wondering what a good fix would be.

To keep everything compartmentalized, I’ve created a Graphics system that handles all graphical rendering:

error[E0277]: the trait bound `std::rc::Rc<sdl2::SdlDrop>: std::marker::Send` is not satisfied in `Graphics`
--> src/main.rs:105:14
|
105 | .add(graphics, "graphics", &[])
| ^^^ `std::rc::Rc<sdl2::SdlDrop>` cannot be sent between threads safely
|
= help: within `Graphics`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<sdl2::SdlDrop>`
= note: required because it appears within the type `sdl2::Sdl`
= note: required because it appears within the type `Graphics`

Pretty sure I understand what this is doing–the SDL context isn’t safe to pass across thread boundaries, and the compiler is catching that. What I don’t understand is how to fix it. The only place that should access the SDL context is the new method, which creates it, and the System implementation which calls it to do rendering and poll for events. How can I compartmentalize that variable so this error will go away? I tried not creating the system myself and letting Specs do it (I.e. passing Graphics in directly rather than creating an instance) but no change.

I don’t really know anything about SDL either, but my guess is that SDL’s underlying machinery isn’t thread-safe in that context and so that restriction was encoded into the Rust API. Otherwise I would imagine that the underlying type would be using Arc rather than Rc for its reference counting.

Also I’m pretty sure that wasm doesn’t currently have threads anyway, so even if you did do something to fix the compilation error you’d just panic at runtime instead.

I eventually sorted this out by getting rid of Specs as my ECS. Yes, Wasm is single-threaded, but Specs is multi-threaded, and while I think the multi-threading deps were compiled out of Emscripten builds, the type definitions remained and the compiler wasn’t happy. Switched to rustic-ecs and now things seem good.