From 4c78f61a9627790729cef72765699d48bcc85de2 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 1 Oct 2021 21:08:17 +0200 Subject: [PATCH] egui any maps: add get_or + #[inline] --- egui/src/any/any_map.rs | 20 ++++++++++++++------ egui/src/any/element.rs | 2 ++ egui/src/any/serializable/any_map.rs | 18 ++++++++++++------ egui/src/any/serializable/type_id.rs | 2 ++ egui/src/any/serializable/type_map.rs | 16 ++++++++++++---- egui/src/any/type_map.rs | 17 +++++++++++++---- 6 files changed, 55 insertions(+), 20 deletions(-) diff --git a/egui/src/any/any_map.rs b/egui/src/any/any_map.rs index 106c48429..ca7853972 100644 --- a/egui/src/any/any_map.rs +++ b/egui/src/any/any_map.rs @@ -16,16 +16,17 @@ impl Default for AnyMap { // ---------------------------------------------------------------------------- impl AnyMap { + #[inline] pub fn get(&mut self, key: &Key) -> Option<&T> { self.get_mut(key).map(|x| &*x) } + #[inline] pub fn get_mut(&mut self, key: &Key) -> Option<&mut T> { self.0.get_mut(key)?.get_mut() } -} -impl AnyMap { + #[inline] pub fn get_or_insert_with( &mut self, key: Key, @@ -34,10 +35,16 @@ impl AnyMap { &*self.get_mut_or_insert_with(key, or_insert_with) } + #[inline] pub fn get_or_default(&mut self, key: Key) -> &T { self.get_or_insert_with(key, Default::default) } + #[inline] + pub fn get_or(&mut self, key: Key, value: T) -> &T { + &*self.get_mut_or_insert_with(key, || value) + } + pub fn get_mut_or_insert_with( &mut self, key: Key, @@ -53,31 +60,32 @@ impl AnyMap { } } + #[inline] pub fn get_mut_or_default(&mut self, key: Key) -> &mut T { self.get_mut_or_insert_with(key, Default::default) } -} -impl AnyMap { + #[inline] pub fn insert(&mut self, key: Key, element: T) { self.0.insert(key, AnyMapElement::new(element)); } + #[inline] pub fn remove(&mut self, key: &Key) { self.0.remove(key); } + #[inline] pub fn remove_by_type(&mut self) { let key = TypeId::of::(); self.0.retain(|_, v| v.type_id() != key); } + #[inline] pub fn clear(&mut self) { self.0.clear(); } -} -impl AnyMap { /// You could use this function to find is there some leak or misusage. pub fn count(&mut self) -> usize { let key = TypeId::of::(); diff --git a/egui/src/any/element.rs b/egui/src/any/element.rs index a5af4e8b4..93b9146a5 100644 --- a/egui/src/any/element.rs +++ b/egui/src/any/element.rs @@ -39,10 +39,12 @@ impl AnyMapElement { } } + #[inline] pub(crate) fn type_id(&self) -> TypeId { (*self.value).type_id() } + #[inline] pub(crate) fn get_mut(&mut self) -> Option<&mut T> { self.value.downcast_mut() } diff --git a/egui/src/any/serializable/any_map.rs b/egui/src/any/serializable/any_map.rs index 5b11d1165..8fd104929 100644 --- a/egui/src/any/serializable/any_map.rs +++ b/egui/src/any/serializable/any_map.rs @@ -17,16 +17,17 @@ impl Default for AnyMap { // ---------------------------------------------------------------------------- impl AnyMap { + #[inline] pub fn get(&mut self, key: &Key) -> Option<&T> { self.get_mut(key).map(|x| &*x) } + #[inline] pub fn get_mut(&mut self, key: &Key) -> Option<&mut T> { self.0.get_mut(key)?.get_mut() } -} -impl AnyMap { + #[inline] pub fn get_or_insert_with( &mut self, key: Key, @@ -35,10 +36,16 @@ impl AnyMap { &*self.get_mut_or_insert_with(key, or_insert_with) } + #[inline] pub fn get_or_default(&mut self, key: Key) -> &T { self.get_or_insert_with(key, Default::default) } + #[inline] + pub fn get_or(&mut self, key: Key, value: T) -> &T { + &*self.get_mut_or_insert_with(key, || value) + } + pub fn get_mut_or_insert_with( &mut self, key: Key, @@ -57,13 +64,13 @@ impl AnyMap { pub fn get_mut_or_default(&mut self, key: Key) -> &mut T { self.get_mut_or_insert_with(key, Default::default) } -} -impl AnyMap { + #[inline] pub fn insert(&mut self, key: Key, element: T) { self.0.insert(key, AnyMapElement::new(element)); } + #[inline] pub fn remove(&mut self, key: &Key) { self.0.remove(key); } @@ -74,12 +81,11 @@ impl AnyMap { self.0.retain(|_, v| v.type_id() != key); } + #[inline] pub fn clear(&mut self) { self.0.clear(); } -} -impl AnyMap { /// You could use this function to find is there some leak or misusage. Note, that result of this function could break between runs, if you upgraded the Rust version or for other reasons. pub fn count(&mut self) -> usize { let key = TypeId::of::(); diff --git a/egui/src/any/serializable/type_id.rs b/egui/src/any/serializable/type_id.rs index 414670ff7..0c99ad796 100644 --- a/egui/src/any/serializable/type_id.rs +++ b/egui/src/any/serializable/type_id.rs @@ -6,12 +6,14 @@ use std::any::Any; pub struct TypeId(u64); impl TypeId { + #[inline] pub fn of() -> Self { std::any::TypeId::of::().into() } } impl From for TypeId { + #[inline] fn from(id: std::any::TypeId) -> Self { Self(epaint::util::hash(id)) } diff --git a/egui/src/any/serializable/type_map.rs b/egui/src/any/serializable/type_map.rs index b4d019d25..873230335 100644 --- a/egui/src/any/serializable/type_map.rs +++ b/egui/src/any/serializable/type_map.rs @@ -13,24 +13,31 @@ pub struct TypeMap(HashMap); // ---------------------------------------------------------------------------- impl TypeMap { + #[inline] pub fn get(&mut self) -> Option<&T> { self.get_mut().map(|x| &*x) } + #[inline] pub fn get_mut(&mut self) -> Option<&mut T> { self.0.get_mut(&TypeId::of::())?.get_mut() } -} -impl TypeMap { + #[inline] pub fn get_or_insert_with(&mut self, or_insert_with: impl FnOnce() -> T) -> &T { &*self.get_mut_or_insert_with(or_insert_with) } + #[inline] pub fn get_or_default(&mut self) -> &T { self.get_or_insert_with(Default::default) } + #[inline] + pub fn get_or(&mut self, value: T) -> &T { + &*self.get_mut_or_insert_with(|| value) + } + pub fn get_mut_or_insert_with( &mut self, or_insert_with: impl FnOnce() -> T, @@ -48,18 +55,19 @@ impl TypeMap { pub fn get_mut_or_default(&mut self) -> &mut T { self.get_mut_or_insert_with(Default::default) } -} -impl TypeMap { + #[inline] pub fn insert(&mut self, element: T) { self.0 .insert(TypeId::of::(), AnyMapElement::new(element)); } + #[inline] pub fn remove(&mut self) { self.0.remove(&TypeId::of::()); } + #[inline] pub fn clear(&mut self) { self.0.clear(); } diff --git a/egui/src/any/type_map.rs b/egui/src/any/type_map.rs index 6ac8cbd68..2ed59c700 100644 --- a/egui/src/any/type_map.rs +++ b/egui/src/any/type_map.rs @@ -12,24 +12,31 @@ pub struct TypeMap(HashMap); // ---------------------------------------------------------------------------- impl TypeMap { + #[inline] pub fn get(&mut self) -> Option<&T> { self.get_mut().map(|x| &*x) } + #[inline] pub fn get_mut(&mut self) -> Option<&mut T> { self.0.get_mut(&TypeId::of::())?.get_mut() } -} -impl TypeMap { + #[inline] pub fn get_or_insert_with(&mut self, or_insert_with: impl FnOnce() -> T) -> &T { &*self.get_mut_or_insert_with(or_insert_with) } + #[inline] pub fn get_or_default(&mut self) -> &T { self.get_or_insert_with(Default::default) } + #[inline] + pub fn get_or(&mut self, value: T) -> &T { + &*self.get_mut_or_insert_with(|| value) + } + pub fn get_mut_or_insert_with( &mut self, or_insert_with: impl FnOnce() -> T, @@ -44,21 +51,23 @@ impl TypeMap { } } + #[inline] pub fn get_mut_or_default(&mut self) -> &mut T { self.get_mut_or_insert_with(Default::default) } -} -impl TypeMap { + #[inline] pub fn insert(&mut self, element: T) { self.0 .insert(TypeId::of::(), AnyMapElement::new(element)); } + #[inline] pub fn remove(&mut self) { self.0.remove(&TypeId::of::()); } + #[inline] pub fn clear(&mut self) { self.0.clear(); }