Path: blob/main/crates/bevy_ecs/src/reflect/map_entities.rs
6600 views
use crate::entity::{EntityMapper, MapEntities};1use bevy_reflect::{FromReflect, FromType, PartialReflect};23/// For a specific type of value, this maps any fields with values of type [`Entity`] to a new world.4///5/// Since a given `Entity` ID is only valid for the world it came from, when performing deserialization6/// any stored IDs need to be re-allocated in the destination world.7///8/// See [`EntityMapper`] and [`MapEntities`] for more information.9///10/// [`Entity`]: crate::entity::Entity11/// [`EntityMapper`]: crate::entity::EntityMapper12#[derive(Clone)]13pub struct ReflectMapEntities {14map_entities: fn(&mut dyn PartialReflect, &mut dyn EntityMapper),15}1617impl ReflectMapEntities {18/// A general method for remapping entities in a reflected value via an [`EntityMapper`].19///20/// # Panics21/// Panics if the type of the reflected value doesn't match.22pub fn map_entities(&self, reflected: &mut dyn PartialReflect, mapper: &mut dyn EntityMapper) {23(self.map_entities)(reflected, mapper);24}25}2627impl<C: FromReflect + MapEntities> FromType<C> for ReflectMapEntities {28fn from_type() -> Self {29ReflectMapEntities {30map_entities: |reflected, mut mapper| {31let mut concrete = C::from_reflect(reflected).expect("reflected type should match");32concrete.map_entities(&mut mapper);33reflected.apply(&concrete);34},35}36}37}383940