Struct trait_map::TraitMap

source ·
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

source

pub fn new() -> Self

source

pub fn add_entry<Entry: TraitMapEntry + 'static>(
&mut self,
entry: Entry
) -> EntryID

Add an entry to the map.

source

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.

source

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.

source

pub fn get_entry_type(&self, entry_id: EntryID) -> Option<TypeId>

Get the concrete type for an entry in the map

source

pub fn all_entries(&self) -> HashMap<EntryID, &dyn TraitMapEntry>

Get the list of all entries as an immutable reference

source

pub fn all_entries_mut(&mut self) -> HashMap<EntryID, &mut dyn TraitMapEntry>

Get the list of all entries as a mutable reference

source

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");
}
source

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");
}
source

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);
source

pub fn get_entry_mut<Trait>(&mut self, entry_id: EntryID) -> Option<&mut Trait>where
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,

Get a specific entry that implements a trait. Returns a mutable reference.

Errors

Returns None if the entry no longer exists in the map.

Examples
let my_mut_ref: Option<&mut dyn MyTrait> = map.get_entry_mut::<dyn MyTrait>(entry_id);
source

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);
source

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);
source

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);
source

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);
source

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);
source

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);

Trait Implementations§

source§

impl Debug for TraitMap

source§

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

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

impl Default for TraitMap

source§

fn default() -> TraitMap

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

impl Drop for TraitMap

source§

fn drop(&mut self)

Executes the destructor for this type. 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, 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, 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.