pub struct TraitMap { /* private fields */ }
Expand description
Map structure that allows types to be dynamically queries by trait.
Values must implement the TraitMapEntry trait to be added to the map.
The on_create()
method should be used to specify which traits are exposed to the map.
Implementations§
source§impl TraitMap
impl TraitMap
pub fn new() -> Self
sourcepub fn add_entry<Entry: TraitMapEntry + 'static>(
&mut self,
entry: Entry
) -> EntryID
pub fn add_entry<Entry: TraitMapEntry + 'static>(
&mut self,
entry: Entry
) -> EntryID
Add an entry to the map.
sourcepub fn remove_entry(&mut self, entry_id: EntryID) -> bool
pub fn remove_entry(&mut self, entry_id: EntryID) -> bool
Remove an entry from the map using its unique ID.
Any cleanup should be handled by the Drop
trait.
Returns true
if the entry was removed, or false
otherwise.
sourcepub fn update_entry(&mut self, entry_id: EntryID) -> bool
pub fn update_entry(&mut self, entry_id: EntryID) -> bool
Call the on_update()
handler for an entry.
Returns true
if the entry exists and was updated, or false
otherwise.
sourcepub fn get_entry_type(&self, entry_id: EntryID) -> Option<TypeId>
pub fn get_entry_type(&self, entry_id: EntryID) -> Option<TypeId>
Get the concrete type for an entry in the map
sourcepub fn all_entries(&self) -> HashMap<EntryID, &dyn TraitMapEntry>
pub fn all_entries(&self) -> HashMap<EntryID, &dyn TraitMapEntry>
Get the list of all entries as an immutable reference
sourcepub fn all_entries_mut(&mut self) -> HashMap<EntryID, &mut dyn TraitMapEntry>
pub fn all_entries_mut(&mut self) -> HashMap<EntryID, &mut dyn TraitMapEntry>
Get the list of all entries as a mutable reference
sourcepub fn get_entities<Trait>(&self) -> HashMap<EntryID, &Trait>where
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
pub fn get_entities<Trait>(&self) -> HashMap<EntryID, &Trait>where
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
Returns all entries that are registered with a specific trait. Returns an immutable reference.
Examples
for (entry_id, entry) in map.get_entries::<dyn MyTrait>() {
entry.trait_method(1, "hello");
}
sourcepub fn get_entities_mut<Trait>(&mut self) -> HashMap<EntryID, &mut Trait>where
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
pub fn get_entities_mut<Trait>(&mut self) -> HashMap<EntryID, &mut Trait>where
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
Returns all entries that are registered with a specific trait. Returns a mutable reference.
Examples
for (entry_id, entry) in map.get_entries_mut::<dyn MyTrait>() {
entry.trait_method_mut("hello");
}
sourcepub fn get_entry<Trait>(&self, entry_id: EntryID) -> Option<&Trait>where
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
pub fn get_entry<Trait>(&self, entry_id: EntryID) -> Option<&Trait>where
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
Get a specific entry that implements a trait. Returns an immutable reference.
Examples
let my_ref: Option<&dyn MyTrait> = map.get_entry::<dyn MyTrait>(entry_id);
sourcepub fn get_entry_mut<Trait>(&mut self, entry_id: EntryID) -> Option<&mut Trait>where
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
pub fn get_entry_mut<Trait>(&mut self, entry_id: EntryID) -> Option<&mut Trait>where
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
sourcepub fn get_entry_downcast<T: TraitMapEntry + 'static>(
&self,
entry_id: EntryID
) -> Option<&T>
pub fn get_entry_downcast<T: TraitMapEntry + 'static>(
&self,
entry_id: EntryID
) -> Option<&T>
Get a specific entry and downcast to an immutable reference of its concrete type.
Errors
Returns None
if the entry no longer exists in the map.
Panics
This method panics if the type parameter T
does not match the concrete type.
Examples
let entry_id = map.add_entry(MyStruct { /* ... */ });
// ...
let my_struct: Option<&MyStruct> = map.get_entry_downcast::<MyStruct>(entry_id);
sourcepub fn get_entry_downcast_mut<T: TraitMapEntry + 'static>(
&mut self,
entry_id: EntryID
) -> Option<&mut T>
pub fn get_entry_downcast_mut<T: TraitMapEntry + 'static>(
&mut self,
entry_id: EntryID
) -> Option<&mut T>
Get a specific entry and downcast to a mutable reference of its concrete type.
Errors
Returns None
if the entry no longer exists in the map.
Panics
This method panics if the type parameter T
does not match the concrete type.
Examples
let entry_id = map.add_entry(MyStruct { /* ... */ });
// ...
let my_struct: Option<&mut MyStruct> = map.get_entry_downcast_mut::<MyStruct>(entry_id);
sourcepub fn take_entry_downcast<T: TraitMapEntry + 'static>(
&mut self,
entry_id: EntryID
) -> Option<T>
pub fn take_entry_downcast<T: TraitMapEntry + 'static>(
&mut self,
entry_id: EntryID
) -> Option<T>
Remove an entry from the map as its concrete type.
Errors
Returns None
if the entry no longer exists in the map.
Panics
This method panics if the type parameter T
does not match the concrete type.
Examples
let entry_id = map.add_entry(MyStruct { /* ... */ });
// ...
let my_struct: Option<MyStruct> = map.take_entry_downcast::<MyStruct>(entry_id);
sourcepub fn try_get_entry_downcast<T: TraitMapEntry + 'static>(
&self,
entry_id: EntryID
) -> Option<Option<&T>>
pub fn try_get_entry_downcast<T: TraitMapEntry + 'static>(
&self,
entry_id: EntryID
) -> Option<Option<&T>>
Get a specific entry and downcast to an immutable reference of its concrete type.
Errors
Returns None
if the entry no longer exists in the map.
Returns Some(None)
if the type parameter T
does not match the concrete type.
Examples
let entry_id = map.add_entry(MyStruct { /* ... */ });
// ...
let my_struct: Option<Option<&MyStruct>> = map.try_get_entry_downcast::<MyStruct>(entry_id);
sourcepub fn try_get_entry_downcast_mut<T: TraitMapEntry + 'static>(
&mut self,
entry_id: EntryID
) -> Option<Option<&mut T>>
pub fn try_get_entry_downcast_mut<T: TraitMapEntry + 'static>(
&mut self,
entry_id: EntryID
) -> Option<Option<&mut T>>
Get a specific entry and downcast to a mutable reference of its concrete type.
Errors
Returns None
if the entry no longer exists in the map.
Returns Some(None)
if the type parameter T
does not match the concrete type.
Examples
let entry_id = map.add_entry(MyStruct { /* ... */ });
// ...
let my_struct: Option<Option<&mut MyStruct>> = map.try_get_entry_downcast_mut::<MyStruct>(entry_id);
sourcepub fn try_take_entry_downcast<T: TraitMapEntry + 'static>(
&mut self,
entry_id: EntryID
) -> Option<Option<T>>
pub fn try_take_entry_downcast<T: TraitMapEntry + 'static>(
&mut self,
entry_id: EntryID
) -> Option<Option<T>>
Remove an entry from the map as its concrete type. If the downcast is invalid, the entry will not be removed from the map.
Errors
Returns None
if the entry no longer exists in the map.
Returns Some(None)
if the type parameter T
does not match the concrete type.
If this happens the type will not be removed from the map.
Examples
let entry_id = map.add_entry(MyStruct { /* ... */ });
// ...
let my_struct: Option<Option<MyStruct>> = map.try_take_entry_downcast::<MyStruct>(entry_id);