Trait TraitMapEntry

Source
pub trait TraitMapEntry: 'static {
    // Required method
    fn on_create<'a>(&mut self, context: Context<'a>);

    // Provided method
    fn on_update<'a>(&mut self, context: Context<'a>) { ... }
}
Expand description

Rust type that can be stored inside of a TraitMap.

If the derive feature flag is enabled, you can derive this trait on types. See the crate documentation.

Required Methods§

Source

fn on_create<'a>(&mut self, context: Context<'a>)

Called when the type is first added to the TraitMap. This should be use to specify which implemented traits are exposed to the map.

§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>();
  }
}

Provided Methods§

Source

fn on_update<'a>(&mut self, context: Context<'a>)

Hook that allows exposed traits to be dynamically updated inside the map. It is called by the update_entry() method. The default implementation does nothing.

§Examples:
impl TraitMapEntry for MyStruct {
  // ...

  fn on_update<'a>(&mut self, context: Context<'a>) {
    context
     .downcast::<Self>()
     .remove_trait::<dyn ExampleTrait>()
     .add_trait::<dyn ExampleTraitTwo>();
  }
}

fn main() {
  let mut map = TraitMap::new();
  let entry_id = map.add_entry(MyStruct { /* ... */ });
  // ...
  map.update_entry(entry_id);
}

Implementors§