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 eb129b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
39 changes: 34 additions & 5 deletions crates/bevy_ecs/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ pub struct ReflectComponentFns {
/// Function pointer implementing [`ReflectComponent::remove()`].
pub remove: fn(&mut 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 @@ -108,12 +110,12 @@ impl ReflectComponent {
}

/// 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 @@ -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 eb129b7

Please sign in to comment.