Struct trait_map::TypedContext
source · pub struct TypedContext<'a, E: ?Sized> { /* private fields */ }
Expand description
Implementations§
source§impl<'a, Entry> TypedContext<'a, Entry>where
Entry: 'static,
impl<'a, Entry> TypedContext<'a, Entry>where
Entry: 'static,
sourcepub fn add_trait<Trait>(&mut self) -> &mut Selfwhere
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
Entry: Unsize<Trait>,
pub fn add_trait<Trait>(&mut self) -> &mut Selfwhere
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
Entry: Unsize<Trait>,
Add a trait to the type map. This method is idempotent, so adding a trait multiple times will only register it once.
By default, every type is associated with the TraitMapEntry trait.
Examples
impl TraitMapEntry for MyStruct {
fn on_create<'a>(&mut self, context: Context<'a>) {
context
.downcast::<Self>()
.add_trait::<dyn ExampleTrait>()
.add_trait::<dyn ExampleTraitTwo>();
}
}
sourcepub fn remove_trait<Trait>(&mut self) -> &mut Selfwhere
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
Entry: Unsize<Trait>,
pub fn remove_trait<Trait>(&mut self) -> &mut Selfwhere
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
Entry: Unsize<Trait>,
Remove a trait from the type map. This method is idempotent, so removing a trait multiple times is a no-op.
By default, every type is associated with the TraitMapEntry trait.
As such, this trait cannot be removed from an entry.
Trying to call .remove_trait::<dyn TraitMapEntry>()
is a no-op.
Examples
impl TraitMapEntry for MyStruct {
// ...
fn on_update<'a>(&mut self, context: Context<'a>) {
context
.downcast::<Self>()
.remove_trait::<dyn ExampleTrait>()
.remove_trait::<dyn ExampleTraitTwo>();
}
}
sourcepub fn has_trait<Trait>(&self) -> boolwhere
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
Entry: Unsize<Trait>,
pub fn has_trait<Trait>(&self) -> boolwhere
Trait: ?Sized + Pointee<Metadata = DynMetadata<Trait>> + 'static,
Entry: Unsize<Trait>,
Test if the trait is registered with the type map.
Examples
impl TraitMapEntry for MyStruct {
// ...
fn on_update<'a>(&mut self, context: Context<'a>) {
let mut context = context.downcast::<Self>();
if !context.has_trait::<dyn ExampleTrait>() {
context.add_trait<dyn ExampleTrait>();
}
}
}