From 43ae99f8380d5212ad1f099723c5bcd8ba46365e Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Sat, 1 Jun 2024 15:15:49 +0200 Subject: [PATCH] web: Use RuffleHandle as a custom key type, not a wrapper over DefaultKey --- web/src/lib.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/web/src/lib.rs b/web/src/lib.rs index 7c51fc1161a22..3675902352a60 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -29,7 +29,7 @@ use ruffle_render::quality::StageQuality; use ruffle_video_software::backend::SoftwareVideoBackend; use ruffle_web_common::JsResult; use serde::{Deserialize, Serialize}; -use slotmap::{DefaultKey, SlotMap}; +use slotmap::{new_key_type, SlotMap}; use std::rc::Rc; use std::str::FromStr; use std::sync::Once; @@ -48,11 +48,19 @@ use web_sys::{ static RUFFLE_GLOBAL_PANIC: Once = Once::new(); +new_key_type! { + /// An opaque handle to a `RuffleInstance` inside the pool. + /// + /// This type is exported to JS, and is used to interact with the library. + #[wasm_bindgen] + pub struct RuffleHandle; +} + thread_local! { /// We store the actual instances of the ruffle core in a static pool. /// This gives us a clear boundary between the JS side and Rust side, avoiding /// issues with lifetimes and type parameters (which cannot be exported with wasm-bindgen). - static INSTANCES: RefCell>> = RefCell::new(SlotMap::new()); + static INSTANCES: RefCell>> = RefCell::new(SlotMap::with_key()); static CURRENT_CONTEXT: RefCell>> = const { RefCell::new(None) }; } @@ -336,13 +344,6 @@ struct MovieMetadata { uncompressed_len: i32, } -/// An opaque handle to a `RuffleInstance` inside the pool. -/// -/// This type is exported to JS, and is used to interact with the library. -#[wasm_bindgen] -#[derive(Clone, Copy)] -pub struct RuffleHandle(DefaultKey); - #[wasm_bindgen] impl RuffleHandle { #[allow(clippy::new_ret_no_self)] @@ -1105,7 +1106,7 @@ impl RuffleHandle { fn add_instance(instance: RuffleInstance) -> Result { INSTANCES.try_with(|instances| { let mut instances = instances.try_borrow_mut()?; - let ruffle = Self(instances.insert(RefCell::new(instance))); + let ruffle = instances.insert(RefCell::new(instance)); Ok(ruffle) })? } @@ -1114,7 +1115,7 @@ impl RuffleHandle { fn remove_instance(&self) -> Result { INSTANCES.try_with(|instances| { let mut instances = instances.try_borrow_mut()?; - if let Some(instance) = instances.remove(self.0) { + if let Some(instance) = instances.remove(*self) { Ok(instance.into_inner()) } else { Err(RuffleInstanceError::InstanceNotFound) @@ -1130,7 +1131,7 @@ impl RuffleHandle { let ret = INSTANCES .try_with(|instances| { let instances = instances.try_borrow()?; - if let Some(instance) = instances.get(self.0) { + if let Some(instance) = instances.get(*self) { let instance = instance.try_borrow()?; let _subscriber = tracing::subscriber::set_default(instance.log_subscriber.clone()); @@ -1155,7 +1156,7 @@ impl RuffleHandle { let ret = INSTANCES .try_with(|instances| { let instances = instances.try_borrow()?; - if let Some(instance) = instances.get(self.0) { + if let Some(instance) = instances.get(*self) { let mut instance = instance.try_borrow_mut()?; let _subscriber = tracing::subscriber::set_default(instance.log_subscriber.clone()); @@ -1180,7 +1181,7 @@ impl RuffleHandle { let ret = INSTANCES .try_with(|instances| { let instances = instances.try_borrow()?; - if let Some(instance) = instances.get(self.0) { + if let Some(instance) = instances.get(*self) { let instance = instance.try_borrow()?; let _subscriber = tracing::subscriber::set_default(instance.log_subscriber.clone()); @@ -1212,7 +1213,7 @@ impl RuffleHandle { let ret = INSTANCES .try_with(|instances| { let instances = instances.try_borrow()?; - if let Some(instance) = instances.get(self.0) { + if let Some(instance) = instances.get(*self) { let instance = instance.try_borrow()?; let _subscriber = tracing::subscriber::set_default(instance.log_subscriber.clone());