From b732fa6378964ab9ca23fd71d7a82f39f6ae0009 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 10 Aug 2026 21:18:18 +0200 Subject: fix(engine): prevent ignored mouse button input when main loop is slow --- engine/src/util.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'engine/src/util.rs') diff --git a/engine/src/util.rs b/engine/src/util.rs index a8ee893..bd69734 100644 --- a/engine/src/util.rs +++ b/engine/src/util.rs @@ -21,6 +21,15 @@ impl MapVec .insert_at_part_pt_by_key((key, value), |(a_key, _)| a_key); } + pub fn insert_mut(&mut self, key: Key, value: Value) -> &mut Value + { + let insert_index = self + .inner + .partition_point(|(other_key, _other_value)| other_key <= &key); + + &mut self.inner.insert_mut(insert_index, (key, value)).1 + } + pub fn remove(&mut self, key: Key) -> Option { let index = self @@ -61,6 +70,21 @@ impl MapVec Some(value) } + pub fn entry(&mut self, key: Key) -> MapVecEntry<'_, Key, Value> + { + let index = self + .inner + .binary_search_by_key(&&key, |(a_key, _)| a_key) + .ok(); + + MapVecEntry { map: self, key, index } + } + + pub fn iter_mut(&mut self) -> impl Iterator + { + self.inner.iter_mut().map(|(key, value)| (key, value)) + } + pub fn values(&self) -> impl Iterator { self.inner.iter().map(|(_, value)| value) @@ -89,6 +113,28 @@ impl Default for MapVec } } +pub struct MapVecEntry<'map, Key: Ord, Value> +{ + map: &'map mut MapVec, + key: Key, + index: Option, +} + +impl<'map, Key: Ord, Value> MapVecEntry<'map, Key, Value> +{ + pub fn or_insert_with(self, func: impl FnOnce() -> Value) -> &'map mut Value + { + match self.index { + Some(index) => { + let (_, value) = unsafe { self.map.inner.get_unchecked_mut(index) }; + + value + } + None => self.map.insert_mut(self.key, func()), + } + } +} + pub trait OptionExt { /// Substitute for the currently experimental function -- cgit v1.2.3-18-g5258