From 4a7271514812c37fcafd84ea3abf51d118df3da6 Mon Sep 17 00:00:00 2001 From: Max Whitehead <35712032+MaxCWhitehead@users.noreply.github.com> Date: Sun, 6 Oct 2024 21:43:41 -0700 Subject: [PATCH] fix(input): Remove SendWrapper from GILRS_CONTEXT (unless on wasm) (#477) I don't think we want SendWrapper here - this will panic if accessed from a different thread than it was initialized with, was panicking on start updating bones in jumpy. Bevy systems may be scheduled to other threads. Arc + Mutex should make us sync + send here anyway. Lmk if any concerns with this @RockasMockas --- .../bones_framework/src/input/gilrs.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/framework_crates/bones_framework/src/input/gilrs.rs b/framework_crates/bones_framework/src/input/gilrs.rs index ce9662713b..36a8f979e6 100644 --- a/framework_crates/bones_framework/src/input/gilrs.rs +++ b/framework_crates/bones_framework/src/input/gilrs.rs @@ -2,10 +2,24 @@ use crate::prelude::*; use gilrs::{ev::filter::axis_dpad_to_button, EventType, Filter, Gilrs as GilrsContext}; use once_cell::sync::Lazy; -use send_wrapper::SendWrapper; use std::sync::{Arc, Mutex}; +#[cfg(target_arch = "wasm32")] +use send_wrapper::SendWrapper; + +/// Lazy-initialized GilrsContext +#[cfg(not(target_arch = "wasm32"))] +static GILRS_CONTEXT: Lazy>> = Lazy::new(|| { + Arc::new(Mutex::new( + GilrsContext::new().expect("Failed to initialize GilrsContext"), + )) +}); + +// Use SendWrapper when on wasm - GilrsContext is not Sync/Send on wasm, +// this is ok because bevy is single threaded on wasm, it will only be +// accessed from one thread. /// Lazy-initialized GilrsContext +#[cfg(target_arch = "wasm32")] static GILRS_CONTEXT: Lazy>>> = Lazy::new(|| { Arc::new(Mutex::new(SendWrapper::new( GilrsContext::new().expect("Failed to initialize GilrsContext"),