pub struct IdTypeMap { /* private fields */ }
Expand description
Stores values identified by an Id
AND a the std::any::TypeId
of the value.
In other words, it maps (Id, TypeId)
to any value you want.
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).
You can store state using the key Id::NULL
. The state will then only be identified by its type.
let a = Id::new("a");
let b = Id::new("b");
let mut map: IdTypeMap = Default::default();
// `a` associated with an f64 and an i32
map.insert_persisted(a, 3.14);
map.insert_temp(a, 42);
// `b` associated with an f64 and a `&'static str`
map.insert_persisted(b, 13.37);
map.insert_temp(b, "Hello World".to_owned());
// we can retrieve all four values:
assert_eq!(map.get_temp::<f64>(a), Some(3.14));
assert_eq!(map.get_temp::<i32>(a), Some(42));
assert_eq!(map.get_temp::<f64>(b), Some(13.37));
assert_eq!(map.get_temp::<String>(b), Some("Hello World".to_owned()));
// we can retrieve them like so also:
assert_eq!(map.get_persisted::<f64>(a), Some(3.14));
assert_eq!(map.get_persisted::<i32>(a), Some(42));
assert_eq!(map.get_persisted::<f64>(b), Some(13.37));
assert_eq!(map.get_temp::<String>(b), Some("Hello World".to_owned()));
Implementations§
source§impl IdTypeMap
impl IdTypeMap
sourcepub fn insert_temp<T>(&mut self, id: Id, value: T)
pub fn insert_temp<T>(&mut self, id: Id, value: T)
Insert a value that will not be persisted.
sourcepub fn insert_persisted<T>(&mut self, id: Id, value: T)where
T: SerializableAny,
pub fn insert_persisted<T>(&mut self, id: Id, value: T)where
T: SerializableAny,
Insert a value that will be persisted next time you start the app.
sourcepub fn get_temp<T>(&self, id: Id) -> Option<T>where
T: 'static + Clone,
pub fn get_temp<T>(&self, id: Id) -> Option<T>where
T: 'static + Clone,
Read a value without trying to deserialize a persisted value.
The call clones the value (if found), so make sure it is cheap to clone!
sourcepub fn get_persisted<T>(&mut self, id: Id) -> Option<T>where
T: SerializableAny,
pub fn get_persisted<T>(&mut self, id: Id) -> Option<T>where
T: SerializableAny,
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!
pub fn get_temp_mut_or<T>(&mut self, id: Id, or_insert: T) -> &mut T
pub fn get_persisted_mut_or<T>(&mut self, id: Id, or_insert: T) -> &mut Twhere
T: SerializableAny,
pub fn get_temp_mut_or_default<T>(&mut self, id: Id) -> &mut T
pub fn get_persisted_mut_or_default<T>(&mut self, id: Id) -> &mut Twhere
T: SerializableAny + Default,
pub fn get_temp_mut_or_insert_with<T>( &mut self, id: Id, insert_with: impl FnOnce() -> T ) -> &mut T
pub fn get_persisted_mut_or_insert_with<T>(
&mut self,
id: Id,
insert_with: impl FnOnce() -> T
) -> &mut Twhere
T: SerializableAny,
sourcepub fn remove_temp<T>(&mut self, id: Id) -> Option<T>where
T: 'static + Default,
pub fn remove_temp<T>(&mut self, id: Id) -> Option<T>where
T: 'static + Default,
Remove and fetch the state of this type and id.
sourcepub fn remove_by_type<T>(&mut self)where
T: 'static,
pub fn remove_by_type<T>(&mut self)where
T: 'static,
Note all state of the given type.
pub fn clear(&mut self)
pub fn is_empty(&self) -> bool
pub fn len(&self) -> usize
sourcepub fn count_serialized(&self) -> usize
pub fn count_serialized(&self) -> usize
Count how many values are stored but not yet deserialized.
sourcepub fn count<T>(&self) -> usizewhere
T: 'static,
pub fn count<T>(&self) -> usizewhere
T: 'static,
Count the number of values are stored with the given type.
sourcepub fn max_bytes_per_type(&self) -> usize
pub fn max_bytes_per_type(&self) -> usize
The maximum number of bytes that will be used to store the persisted state of a single widget type.
Some egui widgets store persisted state that is
serialized to disk by some backends (e.g. eframe
).
Example of such widgets is CollapsingHeader
and Window
.
If you keep creating widgets with unique ids (e.g. Windows
with many different names),
egui will use up more and more space for these widgets, until this limit is reached.
Once this limit is reached, the state that was read the longest time ago will be dropped first.
This value in itself will not be serialized.
sourcepub fn set_max_bytes_per_type(&mut self, max_bytes_per_type: usize)
pub fn set_max_bytes_per_type(&mut self, max_bytes_per_type: usize)
Trait Implementations§
source§impl<'de> Deserialize<'de> for IdTypeMap
impl<'de> Deserialize<'de> for IdTypeMap
source§fn deserialize<D>(
deserializer: D
) -> Result<IdTypeMap, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D
) -> Result<IdTypeMap, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
source§impl Serialize for IdTypeMap
impl Serialize for IdTypeMap
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Auto Trait Implementations§
impl Freeze for IdTypeMap
impl !RefUnwindSafe for IdTypeMap
impl Send for IdTypeMap
impl Sync for IdTypeMap
impl Unpin for IdTypeMap
impl !UnwindSafe for IdTypeMap
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.