pub fn clone_from<F: LayoutFilter, M: Merger>(
        &mut self,
        source: &World,
        filter: &F,
        merger: &mut M,
    ) -> HashMap<Entity, Entity, EntityHasher> {
        let mut allocator = EntityAllocator::new();
        let mut reallocated = HashMap::default();

        // assign destination IDs
        for src_arch in source.archetypes.iter().filter(|arch| {
            filter
                .matches_layout(arch.layout().component_types())
                .is_pass()
        }) {
            // find conflicts, and remove the existing entity, to be replaced with that defined in
            // the source
            for src_entity in src_arch.entities() {
                let dst_entity = merger.assign_id(*src_entity, &mut allocator);
                self.remove(dst_entity);
                reallocated.insert(*src_entity, dst_entity);
            }
        }

        let mut reallocated = Some(reallocated);
        let mut mappings = match merger.entity_map() {
            EntityRewrite::Auto(Some(mut overrides)) => {
                for (a, b) in reallocated.as_ref().unwrap().iter() {
                    overrides.entry(*a).or_insert(*b);
                }
                overrides
            }
            EntityRewrite::Auto(None) => reallocated.take().unwrap(),
            EntityRewrite::Explicit(overrides) => overrides,
        };

        // set the entity mappings as context for Entity::clone
        ID_CLONE_MAPPINGS.with(|cell| {
            std::mem::swap(&mut *cell.borrow_mut(), &mut mappings);
        });

        // clone entities
        for src_arch in source.archetypes.iter().filter(|arch| {
            filter
                .matches_layout(arch.layout().component_types())
                .is_pass()
        }) {
            // construct the destination entity layout
            let layout = merger.convert_layout((**src_arch.layout()).clone());

            // find or construct the destination archetype
            let dst_arch_index = if !M::prefers_new_archetype() || src_arch.entities().len() < 32 {
                self.index.search(&layout).next()
            } else {
                None
            };
            let dst_arch_index = dst_arch_index.unwrap_or_else(|| self.insert_archetype(layout));
            let dst_arch = &mut self.archetypes[dst_arch_index.0 as usize];

            // build a writer for the destination archetype
            let mut writer =
                ArchetypeWriter::new(dst_arch_index, dst_arch, self.components.get_multi_mut());

            // push entity IDs into the archetype
            ID_CLONE_MAPPINGS.with(|cell| {
                let map = cell.borrow();
                for entity in src_arch.entities() {
                    let entity = map.get(entity).unwrap_or(entity);
                    writer.push(*entity);
                }
            });

            // merge components into the archetype
            merger.merge_archetype(
                0..src_arch.entities().len(),
                src_arch,
                &source.components,
                &mut writer,
            );

            // record entity locations
            let (base, entities) = writer.inserted();
            self.entities.insert(entities, dst_arch_index, base);
        }

        reallocated.unwrap_or_else(|| {
            // switch the map context back to recover our hashmap
            ID_CLONE_MAPPINGS.with(|cell| {
                std::mem::swap(&mut *cell.borrow_mut(), &mut mappings);
            });
            mappings
        })
    }
/// Clones the entities from a world into this world.
///
/// A [`LayoutFilter`] selects which entities to merge.
/// A [`Merger`] describes how to perform the merge operation.
///
/// If any entity IDs are remapped by the policy, their mappings will be returned in the result.
///
/// More advanced operations such as component type transformations can be performed with the
/// [`Duplicate`] merger.
///
/// # Examples
///
/// Cloning all entities from the source world, converting all `i32` components to `f64`
/// components. ```
/// # use nh_ecs::*;
/// let mut world_a = World::default();
/// let mut world_b = World::default();
///
/// // any component types not registered with Duplicate will be ignored during the merge
/// let mut merger = Duplicate::default();
/// merger.register_copy::<isize>(); // copy is faster than clone
/// merger.register_clone::<String>();
/// merger.register_convert(|comp: &i32| *comp as f32);
///
/// let _ = world_a.clone_from(&world_b, &any(), &mut merger);
/// ```
Clones the entities from a world into this world.
A [`LayoutFilter`] selects which entities to merge.
A [`Merger`] describes how to perform the merge operation.

If any entity IDs are remapped by the policy, their mappings will be returned in the result.

More advanced operations such as component type transformations can be performed with the
[`Duplicate`] merger.
Clones the entities from a world into this world . A [ ` LayoutFilter ` ] selects which entities to merge . A [ ` Merger ` ] describes how to perform the merge operation . If any entity IDs are remapped by the policy their mappings will be returned in the result . More advanced operations such as component type transformations can be performed with the [ ` Duplicate ` ] merger .
pub fn clone_from<F: LayoutFilter, M: Merger>(
        &mut self,
        source: &World,
        filter: &F,
        merger: &mut M,
    ) -> HashMap<Entity, Entity, EntityHasher> {
        let mut allocator = EntityAllocator::new();
        let mut reallocated = HashMap::default();
        // assign destination IDs
        for src_arch in source.archetypes.iter().filter(|arch| {
            filter
                .matches_layout(arch.layout().component_types())
                .is_pass()
        }) {
            // find conflicts, and remove the existing entity, to be replaced with that defined in
            // the source
            for src_entity in src_arch.entities() {
                let dst_entity = merger.assign_id(*src_entity, &mut allocator);
                self.remove(dst_entity);
                reallocated.insert(*src_entity, dst_entity);
            }
        }
        let mut reallocated = Some(reallocated);
        let mut mappings = match merger.entity_map() {
            EntityRewrite::Auto(Some(mut overrides)) => {
                for (a, b) in reallocated.as_ref().unwrap().iter() {
                    overrides.entry(*a).or_insert(*b);
                }
                overrides
            }
            EntityRewrite::Auto(None) => reallocated.take().unwrap(),
            EntityRewrite::Explicit(overrides) => overrides,
        };
        // set the entity mappings as context for Entity::clone
        ID_CLONE_MAPPINGS.with(|cell| {
            std::mem::swap(&mut *cell.borrow_mut(), &mut mappings);
        });
        // clone entities
        for src_arch in source.archetypes.iter().filter(|arch| {
            filter
                .matches_layout(arch.layout().component_types())
                .is_pass()
        }) {
            // construct the destination entity layout
            let layout = merger.convert_layout((**src_arch.layout()).clone());
            // find or construct the destination archetype
            let dst_arch_index = if !M::prefers_new_archetype() || src_arch.entities().len() < 32 {
                self.index.search(&layout).next()
            } else {
                None
            };
            let dst_arch_index = dst_arch_index.unwrap_or_else(|| self.insert_archetype(layout));
            let dst_arch = &mut self.archetypes[dst_arch_index.0 as usize];
            // build a writer for the destination archetype
            let mut writer =
                ArchetypeWriter::new(dst_arch_index, dst_arch, self.components.get_multi_mut());
            // push entity IDs into the archetype
            ID_CLONE_MAPPINGS.with(|cell| {
                let map = cell.borrow();
                for entity in src_arch.entities() {
                    let entity = map.get(entity).unwrap_or(entity);
                    writer.push(*entity);
                }
            });
            // merge components into the archetype
            merger.merge_archetype(
                0..src_arch.entities().len(),
                src_arch,
                &source.components,
                &mut writer,
            );
            // record entity locations
            let (base, entities) = writer.inserted();
            self.entities.insert(entities, dst_arch_index, base);
        }
        reallocated.unwrap_or_else(|| {
            // switch the map context back to recover our hashmap
            ID_CLONE_MAPPINGS.with(|cell| {
                std::mem::swap(&mut *cell.borrow_mut(), &mut mappings);
            });
            mappings
        })
    }
pub fn clone_from < F : LayoutFilter , M : Merger > ( & mut self , source : & World , filter : & F , merger : & mut M , ) -> HashMap < Entity , Entity , EntityHasher > { let mut allocator = EntityAllocator :: new ( ) ; let mut reallocated = HashMap :: default ( ) ; // assign destination IDs for src_arch in source . archetypes . iter ( ) . filter ( | arch | { filter . matches_layout ( arch . layout ( ) . component_types ( ) ) . is_pass ( ) } ) { // find conflicts, and remove the existing entity, to be replaced with that defined in // the source for src_entity in src_arch . entities ( ) { let dst_entity = merger . assign_id ( * src_entity , & mut allocator ) ; self . remove ( dst_entity ) ; reallocated . insert ( * src_entity , dst_entity ) ; } } let mut reallocated = Some ( reallocated ) ; let mut mappings = match merger . entity_map ( ) { EntityRewrite :: Auto ( Some ( mut overrides ) ) => { for ( a , b ) in reallocated . as_ref ( ) . unwrap ( ) . iter ( ) { overrides . entry ( * a ) . or_insert ( * b ) ; } overrides } EntityRewrite :: Auto ( None ) => reallocated . take ( ) . unwrap ( ) , EntityRewrite :: Explicit ( overrides ) => overrides , } ; // set the entity mappings as context for Entity::clone ID_CLONE_MAPPINGS . with ( | cell | { std :: mem :: swap ( & mut * cell . borrow_mut ( ) , & mut mappings ) ; } ) ; // clone entities for src_arch in source . archetypes . iter ( ) . filter ( | arch | { filter . matches_layout ( arch . layout ( ) . component_types ( ) ) . is_pass ( ) } ) { // construct the destination entity layout let layout = merger . convert_layout ( ( * * src_arch . layout ( ) ) . clone ( ) ) ; // find or construct the destination archetype let dst_arch_index = if ! M :: prefers_new_archetype ( ) || src_arch . entities ( ) . len ( ) < 32 { self . index . search ( & layout ) . next ( ) } else { None } ; let dst_arch_index = dst_arch_index . unwrap_or_else ( | | self . insert_archetype ( layout ) ) ; let dst_arch = & mut self . archetypes [ dst_arch_index . 0 as usize ] ; // build a writer for the destination archetype let mut writer = ArchetypeWriter :: new ( dst_arch_index , dst_arch , self . components . get_multi_mut ( ) ) ; // push entity IDs into the archetype ID_CLONE_MAPPINGS . with ( | cell | { let map = cell . borrow ( ) ; for entity in src_arch . entities ( ) { let entity = map . get ( entity ) . unwrap_or ( entity ) ; writer . push ( * entity ) ; } } ) ; // merge components into the archetype merger . merge_archetype ( 0 .. src_arch . entities ( ) . len ( ) , src_arch , & source . components , & mut writer , ) ; // record entity locations let ( base , entities ) = writer . inserted ( ) ; self . entities . insert ( entities , dst_arch_index , base ) ; } reallocated . unwrap_or_else ( | | { // switch the map context back to recover our hashmap ID_CLONE_MAPPINGS . with ( | cell | { std :: mem :: swap ( & mut * cell . borrow_mut ( ) , & mut mappings ) ; } ) ; mappings } ) }
Clones the entities from a world into this world.
Clones the entities from a world into this world .
&mut self
source
&World
filter
&F
merger
&mut M
&mut self
source
&World
filter
&F
merger
&mut M
examples
Cloning all entities from the source world, converting all `i32` components to `f64`
components.
Cloning all entities from the source world converting all ` i32 ` components to ` f64 ` components .
use nh_ecs::*;
let mut world_a = World::default();
let mut world_b = World::default().

any component types not registered with Duplicate will be ignored during the merge
let mut merger = Duplicate::default();
merger.register_copy::(); // copy is faster than clone
merger.register_clone::();
merger.register_convert(|comp: &i32| *comp as f32).

let _ = world_a.clone_from(&world_b, &any(), &mut merger);
```
let mut world_a = World :: default () ; let mut world_b = World :: default () . any component types not registered with Duplicate will be ignored during the merge let mut merger = Duplicate :: default () ; merger . register_copy :: () ; // copy is faster than clone merger . register_clone :: () ; merger . register_convert ( |comp : &i32| * comp as f32 ) . let _ = world_a . clone_from ( &world_b &any () &mut merger ) ; ` ` `