Skip to content

Commit

Permalink
Remove EntityMut::get_unchecked (bevyengine#4547)
Browse files Browse the repository at this point in the history
The only way to soundly use this API is already encapsulated within `EntityMut::get`, so this api is removed.

# Migration guide

Replace calls to `EntityMut::get_unchecked` with calls to `EntityMut::get`.
  • Loading branch information
TheRawMeatball authored and exjam committed May 22, 2022
1 parent 95cc262 commit 572dcb7
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions crates/bevy_ecs/src/world/entity_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ impl<'w> EntityMut<'w> {
#[inline]
pub fn get<T: Component>(&self) -> Option<&'_ T> {
// SAFE: lifetimes enforce correct usage of returned borrow
unsafe { self.get_unchecked::<T>() }
unsafe {
get_component_with_type(self.world, TypeId::of::<T>(), self.entity, self.location)
.map(|value| &*value.cast::<T>())
}
}

#[inline]
Expand All @@ -163,23 +166,6 @@ impl<'w> EntityMut<'w> {
unsafe { self.get_unchecked_mut::<T>() }
}

/// 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
///
/// - 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<T: Component>(&self) -> Option<&'w T> {
// SAFE: entity location is valid and returned component is of type T
get_component_with_type(self.world, TypeId::of::<T>(), self.entity, self.location)
.map(|value| &*value.cast::<T>())
}

/// 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.
Expand Down

0 comments on commit 572dcb7

Please sign in to comment.