Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UI data() read mutability #2742

Merged
merged 4 commits into from
Mar 29, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions crates/egui/src/util/id_type_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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).
///
Expand Down Expand Up @@ -355,16 +364,19 @@ 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())
.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> {
Expand Down