From 1a5edee7515ec6d36183053e3b06221418b659c9 Mon Sep 17 00:00:00 2001 From: Nova Date: Sat, 14 Jan 2023 20:29:33 -0500 Subject: [PATCH] refactor(input): more compact registry contains --- src/core/registry.rs | 10 ++++++++++ src/nodes/input/mod.rs | 14 ++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/core/registry.rs b/src/core/registry.rs index d0f0c02..eb67879 100644 --- a/src/core/registry.rs +++ b/src/core/registry.rs @@ -26,6 +26,11 @@ impl Registry { .lock() .insert(Arc::as_ptr(t) as *const () as usize, Arc::downgrade(t)); } + pub fn contains(&self, t: &T) -> bool { + self.0 + .lock() + .contains_key(&(ptr::addr_of!(*t) as *const () as usize)) + } pub fn get_valid_contents(&self) -> Vec> { self.0 .lock() @@ -65,6 +70,11 @@ impl OwnedRegistry { pub fn get_vec(&self) -> Vec> { self.0.lock().values().cloned().collect::>() } + pub fn contains(&self, t: &T) -> bool { + self.0 + .lock() + .contains_key(&(ptr::addr_of!(*t) as *const () as usize)) + } pub fn remove(&self, t: &T) { self.0 .lock() diff --git a/src/nodes/input/mod.rs b/src/nodes/input/mod.rs index e69dbb4..cebc5e1 100644 --- a/src/nodes/input/mod.rs +++ b/src/nodes/input/mod.rs @@ -201,9 +201,9 @@ impl InputHandler { .and_then(|data| data.get_bool()) .unwrap_or(false); - if let Some(method) = method.upgrade() { - if let Some(handler) = handler.upgrade() { - if capture { + if capture { + if let Some(method) = method.upgrade() { + if let Some(handler) = handler.upgrade() { method.captures.add_raw(&handler); } } @@ -272,19 +272,13 @@ pub fn process_input() { // Get the current frame let frame = FRAME.load(Ordering::Relaxed); - // Get the list of captured input handlers for this method - let captures = method.captures.get_valid_contents(); - // Iterate over the distance links and send input to them for distance_link in distance_links { distance_link.send_input(frame, method.datamap.lock().clone().unwrap()); // If the current distance link is in the list of captured input handlers, // break out of the loop to avoid sending input to the remaining distance links - if captures - .iter() - .any(|c| Arc::ptr_eq(c, &distance_link.handler)) - { + if method.captures.contains(&distance_link.handler) { break; } }