From c2239f8d149776552155ae3506f322a3571b7990 Mon Sep 17 00:00:00 2001 From: IS2511 Date: Wed, 15 Feb 2023 21:38:56 +0300 Subject: [PATCH 1/4] Make `IdTypeMap::get_temp` use immutable `self` --- crates/egui/src/util/id_type_map.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/egui/src/util/id_type_map.rs b/crates/egui/src/util/id_type_map.rs index c5fcae0d1a6..6f75ff25040 100644 --- a/crates/egui/src/util/id_type_map.rs +++ b/crates/egui/src/util/id_type_map.rs @@ -169,6 +169,14 @@ impl Element { } } + #[inline] + pub(crate) fn get_temp(&self) -> Option<&T> { + match self { + Self::Value { value, .. } => value.downcast_ref(), + Self::Serialized { .. } => None, + } + } + #[inline] pub(crate) fn get_mut_temp(&mut self) -> Option<&mut T> { match self { @@ -355,11 +363,11 @@ impl IdTypeMap { /// /// The call clones the value (if found), so make sure it is cheap to clone! #[inline] - pub fn get_temp(&mut self, id: Id) -> Option { + pub fn get_temp(&self, id: Id) -> Option { let hash = hash(TypeId::of::(), id); self.0 - .get_mut(&hash) - .and_then(|x| x.get_mut_temp()) + .get(&hash) + .and_then(|x| x.get_temp()) .cloned() } From e76c7918ea677e009f1916fe49652c5594e58eed Mon Sep 17 00:00:00 2001 From: IS2511 Date: Wed, 15 Feb 2023 21:41:00 +0300 Subject: [PATCH 2/4] Add note to `IdTypeMap::get_persisted` about mutability --- crates/egui/src/util/id_type_map.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/egui/src/util/id_type_map.rs b/crates/egui/src/util/id_type_map.rs index 6f75ff25040..ce5e6d3cefc 100644 --- a/crates/egui/src/util/id_type_map.rs +++ b/crates/egui/src/util/id_type_map.rs @@ -373,6 +373,9 @@ impl IdTypeMap { /// Read a value, optionally deserializing it if available. /// + /// NOTE: A mutable `self` is needed because internally this deserializes on first call + /// and caches the result (caching requires self-mutability). + /// /// The call clones the value (if found), so make sure it is cheap to clone! #[inline] pub fn get_persisted(&mut self, id: Id) -> Option { From 215897b9d51249a87d65611702e7454e37440f6c Mon Sep 17 00:00:00 2001 From: IS2511 Date: Wed, 15 Feb 2023 21:45:46 +0300 Subject: [PATCH 3/4] Add mention of `ArcSwap` in `IdTypeMap` docs --- crates/egui/src/util/id_type_map.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/egui/src/util/id_type_map.rs b/crates/egui/src/util/id_type_map.rs index ce5e6d3cefc..d4aa938e09e 100644 --- a/crates/egui/src/util/id_type_map.rs +++ b/crates/egui/src/util/id_type_map.rs @@ -309,6 +309,7 @@ use crate::Id; /// /// Values are cloned when read, so keep them small and light. /// If you want to store something bigger, wrap them in `Arc>`. +/// Also try `Arc>`. /// /// Values can either be "persisted" (serializable) or "temporary" (cleared when egui is shut down). /// From 1844b64646c2a834b6dbdd2f528b534c33eff192 Mon Sep 17 00:00:00 2001 From: IS2511 Date: Sun, 5 Mar 2023 13:49:56 +0300 Subject: [PATCH 4/4] Fix formatting with `cargo fmt` --- crates/egui/src/util/id_type_map.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/crates/egui/src/util/id_type_map.rs b/crates/egui/src/util/id_type_map.rs index d4aa938e09e..77e70a2ba28 100644 --- a/crates/egui/src/util/id_type_map.rs +++ b/crates/egui/src/util/id_type_map.rs @@ -366,10 +366,7 @@ impl IdTypeMap { #[inline] pub fn get_temp(&self, id: Id) -> Option { let hash = hash(TypeId::of::(), id); - self.0 - .get(&hash) - .and_then(|x| x.get_temp()) - .cloned() + self.0.get(&hash).and_then(|x| x.get_temp()).cloned() } /// Read a value, optionally deserializing it if available.