Skip to content

Commit 805eb0e

Browse files
committed
Docks ⛵
1 parent 9242ad1 commit 805eb0e

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,29 @@ impl<'w> EntityRef<'w> {
6767
unsafe { self.get_unchecked::<T>() }
6868
}
6969

70-
/// This allows aliased mutability use after free bugs.
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.
7173
/// # Safety
72-
/// - Must not be a mutable reference to the component.
73-
/// - Must not use the returned reference after the component is removed
74+
/// - The returned reference must never alias a mutable borrow of this component.
75+
/// - The returned reference must not be used after this component is moved which
76+
/// may happen from **any** `insert_component`, `remove_component` or `despawn`
77+
/// operation on this world (non-exhaustive list).
7478
#[inline]
7579
pub unsafe fn get_unchecked<T: Component>(&self) -> Option<&'w T> {
7680
// SAFE: entity location is valid and returned component is of type T
7781
get_component_with_type(self.world, TypeId::of::<T>(), self.entity, self.location)
7882
.map(|value| &*value.cast::<T>())
7983
}
8084

81-
/// This allows aliased mutability and use after free bugs.
85+
/// Gets a mutable reference to the component of type `T` associated with
86+
/// this entity without ensuring there are no other borrows active and without
87+
/// ensuring that the returned reference will stay valid.
8288
/// # Safety
83-
/// - Must not be any other references to the component.
84-
/// - Must not use the returned reference after the component is removed.
89+
/// - The returned reference must never alias a mutable borrow of this component.
90+
/// - The returned reference must not be used after this component is moved which
91+
/// may happen from **any** `insert_component`, `remove_component` or `despawn`
92+
/// operation on this world (non-exhaustive list).
8593
#[inline]
8694
pub unsafe fn get_unchecked_mut<T: Component>(
8795
&self,
@@ -165,21 +173,29 @@ impl<'w> EntityMut<'w> {
165173
unsafe { self.get_unchecked_mut::<T>() }
166174
}
167175

168-
/// This allows aliased mutability use after free bugs.
176+
/// Gets an immutable reference to the component of type `T` associated with
177+
/// this entity without ensuring there are no unique borrows active and without
178+
/// ensuring that the returned reference will stay valid.
169179
/// # Safety
170-
/// - Must not be a mutable reference to the component.
171-
/// - Must not use the returned reference after the component is removed
180+
/// - The returned reference must never alias a mutable borrow of this component.
181+
/// - The returned reference must not be used after this component is moved which
182+
/// may happen from **any** `insert_component`, `remove_component` or `despawn`
183+
/// operation on this world (non-exhaustive list).
172184
#[inline]
173185
pub unsafe fn get_unchecked<T: Component>(&self) -> Option<&'w T> {
174186
// SAFE: entity location is valid and returned component is of type T
175187
get_component_with_type(self.world, TypeId::of::<T>(), self.entity, self.location)
176188
.map(|value| &*value.cast::<T>())
177189
}
178190

179-
/// This allows aliased mutability and use after free bugs.
191+
/// Gets a mutable reference to the component of type `T` associated with
192+
/// this entity without ensuring there are no other borrows active and without
193+
/// ensuring that the returned reference will stay valid.
180194
/// # Safety
181-
/// - Must not be any other references to the component.
182-
/// - Must not use the returned reference after the component is removed.
195+
/// - The returned reference must never alias a mutable borrow of this component.
196+
/// - The returned reference must not be used after this component is moved which
197+
/// may happen from **any** `insert_component`, `remove_component` or `despawn`
198+
/// operation on this world (non-exhaustive list).
183199
#[inline]
184200
pub unsafe fn get_unchecked_mut<T: Component>(&self) -> Option<Mut<'w, T>> {
185201
get_component_and_ticks_with_type(self.world, TypeId::of::<T>(), self.entity, self.location)

0 commit comments

Comments
 (0)