Trait EntityMapper

Source
pub trait EntityMapper {
    // Required methods
    fn get_mapped(&mut self, source: Entity) -> Entity;
    fn set_mapped(&mut self, source: Entity, target: Entity);
}
Expand description

An implementor of this trait knows how to map an Entity into another Entity.

Usually this is done by using an EntityHashMap<Entity> to map source entities (mapper inputs) to the current world’s entities (mapper outputs).

More generally, this can be used to map Entity references between any two Worlds.

This is used by MapEntities implementors.

§Example

pub struct SimpleEntityMapper {
  map: EntityHashMap<Entity>,
}

// Example implementation of EntityMapper where we map an entity to another entity if it exists
// in the underlying `EntityHashMap`, otherwise we just return the original entity.
impl EntityMapper for SimpleEntityMapper {
    fn get_mapped(&mut self, entity: Entity) -> Entity {
        self.map.get(&entity).copied().unwrap_or(entity)
    }
     
    fn set_mapped(&mut self, source: Entity, target: Entity) {
        self.map.insert(source, target);
    }
}

Required Methods§

Source

fn get_mapped(&mut self, source: Entity) -> Entity

Returns the “target” entity that maps to the given source.

Source

fn set_mapped(&mut self, source: Entity, target: Entity)

Maps the target entity to the given source. For some implementations this might not actually determine the result of EntityMapper::get_mapped.

Trait Implementations§

Source§

impl EntityMapper for &mut dyn EntityMapper

Source§

fn get_mapped(&mut self, source: Entity) -> Entity

Returns the “target” entity that maps to the given source.
Source§

fn set_mapped(&mut self, source: Entity, target: Entity)

Maps the target entity to the given source. For some implementations this might not actually determine the result of EntityMapper::get_mapped.

Implementations on Foreign Types§

Source§

impl EntityMapper for (Entity, Entity)

Source§

fn get_mapped(&mut self, source: Entity) -> Entity

Source§

fn set_mapped(&mut self, _source: Entity, _target: Entity)

Source§

impl EntityMapper for ()

Source§

fn get_mapped(&mut self, source: Entity) -> Entity

Source§

fn set_mapped(&mut self, _source: Entity, _target: Entity)

Implementors§