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

Add missing unsafe to entity_command::insert_by_id and make it more configurable #18052

Merged
merged 3 commits into from
Feb 27, 2025

Conversation

JaySpruce
Copy link
Contributor

@JaySpruce JaySpruce commented Feb 26, 2025

Objective

insert_by_id is unsafe, but I forgot to add that to the manually-queueable version in entity_command.

It also can only insert using InsertMode::Replace, when it could easily be configurable by threading an InsertMode parameter to the final BundleInserter::insert call.

Solution

  • Add unsafe and safety comment.
  • Add InsertMode parameter to entity_command::insert_by_id, EntityWorldMut::insert_by_id_with_caller, and EntityWorldMut::insert_dynamic_bundle.
  • Add InsertMode parameter to entity_command::insert and remove entity_command::insert_if_new, for consistency with the other manually-queued insertion commands.

@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Feb 26, 2025
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events P-Unsound A bug that results in undefined compiler behavior X-Uncontroversial This work is generally agreed upon D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Unsafe Touches with unsafe code in some way labels Feb 26, 2025
pub unsafe fn insert_by_id<T: Send + 'static>(
component_id: ComponentId,
value: T,
mode: InsertMode,
Copy link
Contributor

Choose a reason for hiding this comment

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

Elsewhere the pub methods seem to use insert for InsertMode::Replace and insert_if_new for InsertMode::Keep. Should we add insert_by_id_if_new instead of adding a mode parameter, for consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's sort of already inconsistent, since entity_command::insert_from_world and command::insert_batch have a parameter instead of another method. I wouldn't really mind either way, but that sort of combinatorial API explosion can be contentious

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good! I do prefer the parameter approach to avoid the combinatorial API explosion, I just also like consistency :).

Would it be worth creating a follow-up issue to remove insert_if_new in favor of an InsertMode parameter on insert?

Copy link
Contributor Author

@JaySpruce JaySpruce Feb 26, 2025

Choose a reason for hiding this comment

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

I briefly did that as part of this PR before I changed my mind lol. I could re-add that here, or make another PR if you think it's a big enough change or too unrelated

Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering if we even wanted to change the already-released EntityCommands::insert_if_new, and we wouldn't want to block a soundness fix with a debate about that. The functions in this file are new in this release, though, so if you're just changing these then I'm happy either way!

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 27, 2025
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 27, 2025
Merged via the queue into bevyengine:main with commit 67146bd Feb 27, 2025
31 checks passed
@JaySpruce JaySpruce deleted the insert_by_id_unsafe branch February 27, 2025 15:41
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-Bug An unexpected or incorrect behavior D-Straightforward Simple bug fixes and API improvements, docs, test and examples D-Unsafe Touches with unsafe code in some way P-Unsound A bug that results in undefined compiler behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants