Skip to content

Commit 05f0168

Browse files
committed
make EntityRef lenient
1 parent c3ad453 commit 05f0168

File tree

4 files changed

+13
-35
lines changed

4 files changed

+13
-35
lines changed

crates/bevy_ecs/src/reflect.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,15 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
103103
.entity_mut(destination_entity)
104104
.insert(destination_component);
105105
},
106-
reflect_component: |world, entity| unsafe {
106+
reflect_component: |world, entity| {
107107
world
108108
.get_entity(entity)?
109-
// SAFE: lifetimes force correct usage of returned data
110-
.get_unchecked::<C>()
109+
.get::<C>()
111110
.map(|c| c as &dyn Reflect)
112111
},
113112
reflect_component_mut: |world, entity| unsafe {
114113
world
115114
.get_entity(entity)?
116-
// SAFE: lifetimes force correct usage of returned data
117115
.get_unchecked_mut::<C>(world.last_change_tick(), world.read_change_tick())
118116
.map(|c| ReflectMut {
119117
value: c.value as &mut dyn Reflect,

crates/bevy_ecs/src/system/query.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -868,12 +868,9 @@ where
868868
.archetype_component_access
869869
.has_read(archetype_component)
870870
{
871-
// SAFE: lifetimes enforce correct usage of returned borrow
872-
unsafe {
873-
entity_ref
874-
.get_unchecked::<T>()
875-
.ok_or(QueryComponentError::MissingComponent)
876-
}
871+
entity_ref
872+
.get::<T>()
873+
.ok_or(QueryComponentError::MissingComponent)
877874
} else {
878875
Err(QueryComponentError::MissingReadAccess)
879876
}

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,12 @@ impl<'w> EntityRef<'w> {
6262
}
6363

6464
#[inline]
65-
pub fn get<T: Component>(&self) -> Option<&'_ T> {
66-
// SAFE: lifetimes enforce correct usage of returned borrow
67-
unsafe { self.get_unchecked::<T>() }
68-
}
69-
70-
/// Gets an immutable reference to the component of type `T` associated with
71-
/// this entity without ensuring there are no unique borrows active and without
72-
/// ensuring that the returned reference will stay valid.
73-
///
74-
/// # Safety
75-
///
76-
/// - The returned reference must never alias a mutable borrow of this component.
77-
/// - The returned reference must not be used after this component is moved which
78-
/// may happen from **any** `insert_component`, `remove_component` or `despawn`
79-
/// operation on this world (non-exhaustive list).
80-
#[inline]
81-
pub unsafe fn get_unchecked<T: Component>(&self) -> Option<&'w T> {
65+
pub fn get<T: Component>(&self) -> Option<&'w T> {
8266
// SAFE: entity location is valid and returned component is of type T
83-
get_component_with_type(self.world, TypeId::of::<T>(), self.entity, self.location)
84-
.map(|value| &*value.cast::<T>())
67+
unsafe {
68+
get_component_with_type(self.world, TypeId::of::<T>(), self.entity, self.location)
69+
.map(|value| &*value.cast::<T>())
70+
}
8571
}
8672

8773
/// Gets a mutable reference to the component of type `T` associated with

crates/bevy_ecs/src/world/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ impl World {
197197
/// .insert(Position { x: 0.0, y: 0.0 })
198198
/// .id();
199199
///
200-
/// let entity_ref = world.entity(entity);
201-
/// let position = entity_ref.get::<Position>().unwrap();
200+
/// let position = world.entity(entity).get::<Position>().unwrap();
202201
/// assert_eq!(position.x, 0.0);
203202
/// ```
204203
#[inline]
@@ -338,8 +337,7 @@ impl World {
338337
/// .insert_bundle((Num(1), Label("hello"))) // add a bundle of components
339338
/// .id();
340339
///
341-
/// let entity_ref = world.entity(entity);
342-
/// let position = entity_ref.get::<Position>().unwrap();
340+
/// let position = world.entity(entity).get::<Position>().unwrap();
343341
/// assert_eq!(position.x, 0.0);
344342
/// ```
345343
pub fn spawn(&mut self) -> EntityMut {
@@ -416,8 +414,7 @@ impl World {
416414
/// ```
417415
#[inline]
418416
pub fn get<T: Component>(&self, entity: Entity) -> Option<&T> {
419-
// SAFE: lifetimes enforce correct usage of returned borrow
420-
unsafe { self.get_entity(entity)?.get_unchecked::<T>() }
417+
self.get_entity(entity)?.get()
421418
}
422419

423420
/// Retrieves a mutable reference to the given `entity`'s [Component] of the given type.

0 commit comments

Comments
 (0)