dimforge / bevy_rapier

Official Rapier plugin for the Bevy game engine.
https://rapier.rs
Apache License 2.0
1.19k stars 256 forks source link

WASM: `dyn PhysicsHooks` cannot be shared between threads safely #315

Open smessmer opened 1 year ago

smessmer commented 1 year ago

Building for desktop works fine, but when trying to build with trunk for WASM, I'm getting a compiler error in bevy-rapier2d. The wasm-bindgen feature is enabled.

Bevy version: 0.9.1 Bevy_rapier2d version: 0.20.0

error[E0277]: `dyn PhysicsHooks` cannot be shared between threads safely
   --> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.17.0/src/geometry/narrow_phase.rs:695:74
    |
695 |           par_iter_mut!(&mut self.intersection_graph.graph.edges).for_each(|edge| {
    |                                                                   -------- ^-----
    |                                                                   |        |
    |  _________________________________________________________________|________within this `[closure@/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.17.0/src/geometry/narrow_phase.rs:695:74: 695:80]`
    | |                                                                 |
    | |                                                                 required by a bound introduced by this call
696 | |             let handle1 = nodes[edge.source().index()].weight;
697 | |             let handle2 = nodes[edge.target().index()].weight;
698 | |             let had_intersection = edge.weight.intersecting;
...   |
775 | |             }
776 | |         });
    | |_________^ `dyn PhysicsHooks` cannot be shared between threads safely
    |
    = help: within `[closure@/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.17.0/src/geometry/narrow_phase.rs:695:74: 695:80]`, the trait `Sync` is not implemented for `dyn PhysicsHooks`
    = note: required because it appears within the type `&dyn PhysicsHooks`
note: required because it's used within this closure
   --> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/rapier2d-0.17.0/src/geometry/narrow_phase.rs:695:74
    |
695 |         par_iter_mut!(&mut self.intersection_graph.graph.edges).for_each(|edge| {
    |                                                                          ^^^^^^
note: required by a bound in `rayon::iter::ParallelIterator::for_each`
   --> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/rayon-1.6.1/src/iter/mod.rs:369:30
    |
369 |         OP: Fn(Self::Item) + Sync + Send,
    |                              ^^^^ required by this bound in `ParallelIterator::for_each`
smessmer commented 1 year ago

Oh nvm, it's because I had enabled the parallel feature which doesn't seem to work on WASM. Removing that feature made it work.

Not closing the issue just yet because maybe there is a way to make parallel work on WASM? Feel free to close the issue if this isn't a goal.