Skip to content

Conversation

@ItsDoot
Copy link
Contributor

@ItsDoot ItsDoot commented Sep 6, 2024

Objective

Closes #15038.

Solution

Added:

  • DeferredWorld::get_many_entities_mut
  • DeferredWorld::many_entities_mut
  • DeferredWorld::get_many_entities_dynamic_mut
  • DeferredWorld::get_many_entities_from_set_mut
  • UnsafeWorldCell::get_entities (used by the above functions)
  • UnsafeWorldCell::get_entities_dynamic (used by the above functions)

Changed:

  • Made equivalent functions on World reuse the newly introduced methods on UnsafeWorldCell for their implementations.
  • Fixed the safety comment on DeferredWorld::get_entity_mut.

Testing

  • Added 3 tests for the World functions since they were previously missing.
  • Added 3 tests for the DeferredWorld functions.

Migration Guide

  • World::get_many_entities and World::get_many_entities_dynamic now return QueryEntityError instead of Entity in the Err path to match the signatures of their _mut counterparts.

@ItsDoot ItsDoot added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 6, 2024
Copy link
Contributor

@chescock chescock left a comment

Choose a reason for hiding this comment

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

It's nice to have a little more API consistency between the various World variants! I like how you were able to share more code between the mut and non-mut versions by putting common code in UnsafeWorldCell.

entities: [Entity; N],
) -> Result<[EntityMut<'_>; N], QueryEntityError> {
let world_cell = self.as_unsafe_world_cell();
// SAFETY: The `world_cell` has exclusive access to the entire world,
Copy link
Contributor

Choose a reason for hiding this comment

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

The _mut methods on World look the same as the ones on DeferredWorld. Could you share the implementation by doing something like DeferredWorld::from(self).get_many_entities_mut();? (Or maybe there is a simpler way to get from &mut World to DeferredWorld?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

DeferredWorld::from(self).get_many_entities_mut();

This doesn't work unfortunately:

error[E0515]: cannot return value referencing temporary value
   --> crates\bevy_ecs\src\world\mod.rs:766:9
    |
766 |         DeferredWorld::from(self).get_many_entities_mut(entities)
    |         -------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |         |
    |         returns a value referencing data owned by the current function
    |         temporary value created here

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see, &mut DeferredWorld is only a borrow of a borrow. To make that work, you'd need DeferredWorld.get_many_entities_mut() to take self by value so that it can return values with the 'w lifetime, but then you'd need to call .reborrow() almost every time you use it.

assert!(world
.get_many_entities_from_set_mut(&EntityHashSet::from_iter([entity1, entity2]))
.is_ok());
assert!(world
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have an assertion to a error of non-existent entity? Entity::PLACEHOLDER is probably useful for this.

@BenjaminBrienen BenjaminBrienen added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 25, 2024
@ItsDoot
Copy link
Contributor Author

ItsDoot commented Oct 3, 2024

Closed in favor of #15614

@ItsDoot ItsDoot closed this Oct 3, 2024
@ItsDoot ItsDoot deleted the fix/get_entity_mut-safety branch November 27, 2024 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples D-Unsafe Touches with unsafe code in some way S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add many_entities_mut function variants to DeferredWorld

4 participants