Fix UI `data()` read mutability (#2742)

* Make `IdTypeMap::get_temp` use immutable `self`

* Add note to `IdTypeMap::get_persisted` about mutability

* Add mention of `ArcSwap` in `IdTypeMap` docs

* Fix formatting with `cargo fmt`
This commit is contained in:
Ivan Sotnikov 2023-03-29 16:59:24 +03:00 committed by GitHub
parent 2946ed74e3
commit 74d43bfa17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 5 deletions

View File

@ -169,6 +169,14 @@ impl Element {
}
}
#[inline]
pub(crate) fn get_temp<T: 'static>(&self) -> Option<&T> {
match self {
Self::Value { value, .. } => value.downcast_ref(),
Self::Serialized { .. } => None,
}
}
#[inline]
pub(crate) fn get_mut_temp<T: 'static>(&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<Mutex<…>>`.
/// Also try `Arc<ArcSwap<…>>`.
///
/// 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<T: 'static + Clone>(&mut self, id: Id) -> Option<T> {
pub fn get_temp<T: 'static + Clone>(&self, id: Id) -> Option<T> {
let hash = hash(TypeId::of::<T>(), 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<T: SerializableAny>(&mut self, id: Id) -> Option<T> {