Skip to content

Commit

Permalink
Review items
Browse files Browse the repository at this point in the history
  • Loading branch information
Suficio committed Jan 19, 2023
1 parent ba09058 commit a975ef5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
51 changes: 40 additions & 11 deletions crates/bevy_ecs/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ pub struct ReflectComponentFns {
/// Function pointer implementing [`ReflectComponent::insert()`].
pub insert: fn(&mut World, Entity, &dyn Reflect),
/// Function pointer implementing [`ReflectComponent::apply()`].
pub apply: fn(&mut EntityMut, &dyn Reflect),
pub apply: fn(EntityMut, &dyn Reflect),
/// Function pointer implementing [`ReflectComponent::apply_or_insert()`].
pub apply_or_insert: fn(&mut World, Entity, &dyn Reflect),
/// Function pointer implementing [`ReflectComponent::remove()`].
pub remove: fn(&mut EntityMut),
pub remove: fn(EntityMut),
/// Function pointer implementing [`ReflectComponent::contains()`].
pub contains: fn(&EntityRef) -> bool,
pub contains: fn(EntityRef) -> bool,
/// Function pointer implementing [`ReflectComponent::reflect()`].
pub reflect: for<'a> fn(&'a EntityRef) -> Option<&'a dyn Reflect>,
pub reflect: fn(EntityRef) -> Option<&dyn Reflect>,
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
pub reflect_mut: for<'a> fn(&'a mut EntityMut) -> Option<Mut<'a, dyn Reflect>>,
pub reflect_mut: for<'a> fn(&'a mut EntityMut<'a>) -> Option<Mut<'a, dyn Reflect>>,
/// Function pointer implementing [`ReflectComponent::reflect_unchecked_mut()`].
pub reflect_unchecked_mut: fn(&World, Entity) -> Option<Mut<dyn Reflect>>,
/// Function pointer implementing [`ReflectComponent::copy()`].
pub copy: fn(&World, &mut World, Entity, Entity),
}
Expand Down Expand Up @@ -85,7 +87,7 @@ impl ReflectComponent {
/// # Panics
///
/// Panics if there is no [`Component`] of the given type.
pub fn apply(&self, entity: &mut EntityMut, component: &dyn Reflect) {
pub fn apply(&self, entity: EntityMut, component: &dyn Reflect) {
(self.0.apply)(entity, component);
}

Expand All @@ -103,17 +105,17 @@ impl ReflectComponent {
/// # Panics
///
/// Panics if there is no [`Component`] of the given type.
pub fn remove(&self, entity: &mut EntityMut) {
pub fn remove(&self, entity: EntityMut) {
(self.0.remove)(entity);
}

/// Returns whether entity contains this [`Component`]
pub fn contains(&self, entity: &EntityRef) -> bool {
pub fn contains(&self, entity: EntityRef) -> bool {
(self.0.contains)(entity)
}

/// Gets the value of this [`Component`] type from the entity as a reflected reference.
pub fn reflect<'a>(&self, entity: &'a EntityRef<'a>) -> Option<&'a dyn Reflect> {
pub fn reflect<'a>(&self, entity: EntityRef<'a>) -> Option<&'a dyn Reflect> {
(self.0.reflect)(entity)
}

Expand All @@ -122,6 +124,20 @@ impl ReflectComponent {
(self.0.reflect_mut)(entity)
}

/// # Safety
/// This method does not prevent you from having two mutable pointers to the same data,
/// violating Rust's aliasing rules. To avoid this:
/// * Only call this method in an exclusive system to avoid sharing across threads (or use a
/// scheduler that enforces safe memory access).
/// * Don't call this method more than once in the same scope for a given [`Component`].
pub unsafe fn reflect_unchecked_mut<'a>(
&self,
world: &'a World,
entity: Entity,
) -> Option<Mut<'a, dyn Reflect>> {
(self.0.reflect_unchecked_mut)(world, entity)
}

/// Gets the value of this [`Component`] type from entity from `source_world` and [applies](Self::apply()) it to the value of this [`Component`] type in entity in `destination_world`.
///
/// # Panics
Expand Down Expand Up @@ -166,7 +182,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
component.apply(reflected_component);
world.entity_mut(entity).insert(component);
},
apply: |entity, reflected_component| {
apply: |mut entity, reflected_component| {
let mut component = entity.get_mut::<C>().unwrap();
component.apply(reflected_component);
},
Expand All @@ -179,7 +195,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
world.entity_mut(entity).insert(component);
}
},
remove: |entity| {
remove: |mut entity| {
entity.remove::<C>();
},
contains: |entity| entity.contains::<C>(),
Expand All @@ -198,6 +214,19 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
ticks: c.ticks,
})
},
reflect_unchecked_mut: |world, entity| {
// SAFETY: reflect_mut is an unsafe function pointer used by `reflect_unchecked_mut` which promises to never
// produce aliasing mutable references, and reflect_mut, which has mutable world access
unsafe {
world
.get_entity(entity)?
.get_unchecked_mut::<C>(world.last_change_tick(), world.read_change_tick())
.map(|c| Mut {
value: c.value as &mut dyn Reflect,
ticks: c.ticks,
})
}
},
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_scene/src/dynamic_scene_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<'w> DynamicSceneBuilder<'w> {
.get_info(component_id)
.and_then(|info| type_registry.get(info.type_id().unwrap()))
.and_then(|registration| registration.data::<ReflectComponent>())
.and_then(|reflect_component| reflect_component.reflect(&entity));
.and_then(|reflect_component| reflect_component.reflect(entity));

if let Some(reflect_component) = reflect_component {
entry.components.push(reflect_component.clone_value());
Expand Down

0 comments on commit a975ef5

Please sign in to comment.