pub struct IdTypeMap(_);
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<…>>.

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

source

pub fn insert_temp<T: 'static + Any + Clone + Send + Sync>(
&mut self,
id: Id,
value: T
)

Insert a value that will not be persisted.

source

pub fn insert_persisted<T: SerializableAny>(&mut self, id: Id, value: T)

Insert a value that will be persisted next time you start the app.

source

pub fn get_temp<T: 'static + Clone>(&mut self, id: Id) -> Option<T>

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!

source

pub fn get_persisted<T: SerializableAny>(&mut self, id: Id) -> Option<T>

Read a value, optionally deserializing it if available.

The call clones the value (if found), so make sure it is cheap to clone!

source

pub fn get_temp_mut_or<T: 'static + Any + Clone + Send + Sync>(
&mut self,
id: Id,
or_insert: T
) -> &mut T

source

pub fn get_persisted_mut_or<T: SerializableAny>(
&mut self,
id: Id,
or_insert: T
) -> &mut T

source

pub fn get_temp_mut_or_default<T: 'static + Any + Clone + Send + Sync + Default>(
&mut self,
id: Id
) -> &mut T

source

pub fn get_persisted_mut_or_default<T: SerializableAny + Default>(
&mut self,
id: Id
) -> &mut T

source

pub fn get_temp_mut_or_insert_with<T: 'static + Any + Clone + Send + Sync>(
&mut self,
id: Id,
insert_with: impl FnOnce() -> T
) -> &mut T

source

pub fn get_persisted_mut_or_insert_with<T: SerializableAny>(
&mut self,
id: Id,
insert_with: impl FnOnce() -> T
) -> &mut T

source

pub fn remove<T: 'static>(&mut self, id: Id)

Remove the state of this type an id.

source

pub fn remove_by_type<T: 'static>(&mut self)

Note all state of the given type.

source

pub fn clear(&mut self)

source

pub fn is_empty(&self) -> bool

source

pub fn len(&self) -> usize

source

pub fn count_serialized(&self) -> usize

Count how many values are stored but not yet deserialized.

source

pub fn count<T: 'static>(&self) -> usize

Count the number of values are stored with the given type.

Trait Implementations§

source§

impl Clone for IdTypeMap

source§

fn clone(&self) -> IdTypeMap

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for IdTypeMap

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for IdTypeMap

source§

fn default() -> IdTypeMap

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for IdTypeMap

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for IdTypeMap

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere
U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere
T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,

source§

impl<T> SerializableAny for Twhere
T: 'static + Any + Clone + Serialize + for<'a> Deserialize<'a> + Send + Sync,