diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 67097d3353ccd6..1777ba9d926de5 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -67,10 +67,14 @@ impl<'w> EntityRef<'w> { unsafe { self.get_unchecked::() } } - /// This allows aliased mutability use after free bugs. + /// Gets an immutable reference to the component of type `T` associated with + /// this entity without ensuring there are no unique borrows active and without + /// ensuring that the returned reference will stay valid. /// # Safety - /// - Must not be a mutable reference to the component. - /// - Must not use the returned reference after the component is removed + /// - The returned reference must never alias a mutable borrow of this component. + /// - The returned reference must not be used after this component is moved which + /// may happen from **any** `insert_component`, `remove_component` or `despawn` + /// operation on this world (non-exhaustive list). #[inline] pub unsafe fn get_unchecked(&self) -> Option<&'w T> { // SAFE: entity location is valid and returned component is of type T @@ -78,10 +82,14 @@ impl<'w> EntityRef<'w> { .map(|value| &*value.cast::()) } - /// This allows aliased mutability and use after free bugs. + /// Gets a mutable reference to the component of type `T` associated with + /// this entity without ensuring there are no other borrows active and without + /// ensuring that the returned reference will stay valid. /// # Safety - /// - Must not be any other references to the component. - /// - Must not use the returned reference after the component is removed. + /// - The returned reference must never alias a mutable borrow of this component. + /// - The returned reference must not be used after this component is moved which + /// may happen from **any** `insert_component`, `remove_component` or `despawn` + /// operation on this world (non-exhaustive list). #[inline] pub unsafe fn get_unchecked_mut( &self, @@ -165,10 +173,14 @@ impl<'w> EntityMut<'w> { unsafe { self.get_unchecked_mut::() } } - /// This allows aliased mutability use after free bugs. + /// Gets an immutable reference to the component of type `T` associated with + /// this entity without ensuring there are no unique borrows active and without + /// ensuring that the returned reference will stay valid. /// # Safety - /// - Must not be a mutable reference to the component. - /// - Must not use the returned reference after the component is removed + /// - The returned reference must never alias a mutable borrow of this component. + /// - The returned reference must not be used after this component is moved which + /// may happen from **any** `insert_component`, `remove_component` or `despawn` + /// operation on this world (non-exhaustive list). #[inline] pub unsafe fn get_unchecked(&self) -> Option<&'w T> { // SAFE: entity location is valid and returned component is of type T @@ -176,10 +188,14 @@ impl<'w> EntityMut<'w> { .map(|value| &*value.cast::()) } - /// This allows aliased mutability and use after free bugs. + /// Gets a mutable reference to the component of type `T` associated with + /// this entity without ensuring there are no other borrows active and without + /// ensuring that the returned reference will stay valid. /// # Safety - /// - Must not be any other references to the component. - /// - Must not use the returned reference after the component is removed. + /// - The returned reference must never alias a mutable borrow of this component. + /// - The returned reference must not be used after this component is moved which + /// may happen from **any** `insert_component`, `remove_component` or `despawn` + /// operation on this world (non-exhaustive list). #[inline] pub unsafe fn get_unchecked_mut(&self) -> Option> { get_component_and_ticks_with_type(self.world, TypeId::of::(), self.entity, self.location)