Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax documented safety requirements for *_assume_mut #17140

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions crates/bevy_ecs/src/world/entity_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,11 @@ impl<'w> EntityMut<'w> {
///
/// # Safety
///
/// - `T` must be a mutable component
/// - One of the following should be true:
/// - Either `T` is mutable, or
/// - `OnReplace` hooks and observers for that component on that entity should trigger immediately before the mutation
/// and `OnInsert` should trigger immediately after the mutation, or
/// - The user should uphold documented invariants of `T`. If no such documentation provided, it is impossible for the end user to uphold this.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this requirement necessary? If it is, then I think fn modify_component() would need to be unsafe and include a similar requirement.

But it seems like this could be handled by privacy: Anything that would violate the invariants of T just shouldn't be pub. That just leaves things like mem::swap, but as long as you're firing triggers that should be indistinguishable from overwriting the component with insert.

#[inline]
pub unsafe fn get_mut_assume_mutable<T: Component>(&mut self) -> Option<Mut<'_, T>> {
// SAFETY: &mut self implies exclusive access for duration of returned value
Expand Down Expand Up @@ -1262,7 +1266,11 @@ impl<'w> EntityWorldMut<'w> {
///
/// # Safety
///
/// - `T` must be a mutable component
/// - One of the following should be true:
/// - Either `T` is mutable, or
/// - `OnReplace` hooks and observers for that component on that entity should trigger immediately before the mutation
/// and `OnInsert` should trigger immediately after the mutation, or
/// - The user should uphold documented invariants of `T`. If no such documentation provided, it is impossible for the end user to uphold this.
#[inline]
pub unsafe fn get_mut_assume_mutable<T: Component>(&mut self) -> Option<Mut<'_, T>> {
// SAFETY:
Expand Down Expand Up @@ -3313,7 +3321,11 @@ impl<'w> FilteredEntityMut<'w> {
///
/// # Safety
///
/// - `T` must be a mutable component
/// - One of the following should be true:
/// - Either `T` is mutable, or
/// - `OnReplace` hooks and observers for that component on that entity should trigger immediately before the mutation
/// and `OnInsert` should trigger immediately after the mutation, or
/// - The user should uphold documented invariants of `T`. If no such documentation provided, it is impossible for the end user to uphold this.
#[inline]
pub unsafe fn into_mut_assume_mutable<T: Component>(self) -> Option<Mut<'w, T>> {
let id = self.entity.world().components().get_id(TypeId::of::<T>())?;
Expand Down
12 changes: 10 additions & 2 deletions crates/bevy_ecs/src/world/unsafe_world_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,11 @@ impl<'w> UnsafeEntityCell<'w> {
/// It is the callers responsibility to ensure that
/// - the [`UnsafeEntityCell`] has permission to access the component mutably
/// - no other references to the component exist at the same time
/// - the component `T` is mutable
/// - One of the following should be true:
/// - Either `T` is mutable, or
/// - `OnReplace` hooks and observers for that component on that entity should trigger immediately before the mutation
/// and `OnInsert` should trigger immediately after the mutation, or
/// - The user should uphold documented invariants of `T`. If no such documentation provided, it is impossible for the end user to uphold this.
#[inline]
pub unsafe fn get_mut_assume_mutable<T: Component>(self) -> Option<Mut<'w, T>> {
// SAFETY: same safety requirements
Expand All @@ -879,7 +883,11 @@ impl<'w> UnsafeEntityCell<'w> {
/// It is the callers responsibility to ensure that
/// - the [`UnsafeEntityCell`] has permission to access the component mutably
/// - no other references to the component exist at the same time
/// - The component `T` is mutable
/// - One of the following should be true:
/// - Either `T` is mutable, or
/// - `OnReplace` hooks and observers for that component on that entity should trigger immediately before the mutation
/// and `OnInsert` should trigger immediately after the mutation, or
/// - The user should uphold documented invariants of `T`. If no such documentation provided, it is impossible for the end user to uphold this.
#[inline]
pub(crate) unsafe fn get_mut_using_ticks_assume_mutable<T: Component>(
&self,
Expand Down
Loading