diff --git a/crates/egui/src/util/id_type_map.rs b/crates/egui/src/util/id_type_map.rs index c5fcae0d1a6..77e70a2ba28 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 { @@ -301,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). /// @@ -355,16 +364,16 @@ 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()) - .cloned() + self.0.get(&hash).and_then(|x| x.get_temp()).cloned() } /// 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 {