Skip to content

Safely insert type erased component - #25320

Open
Person-93 wants to merge 5 commits into
bevyengine:mainfrom
Person-93:safely-insert-component-by-id
Open

Safely insert type erased component#25320
Person-93 wants to merge 5 commits into
bevyengine:mainfrom
Person-93:safely-insert-component-by-id

Conversation

@Person-93

Copy link
Copy Markdown
Contributor

Objective

Bevy has a public API for inserting components by id, but it's unsafe. https://docs.rs/bevy/latest/bevy/prelude/struct.EntityCommands.html#method.insert_by_id

Solution

This PR adds a safe wrapper around it. The user creates a ComponentInserter when the type is known. Later, the user can use it to insert the component.

Testing

I added an example in the docs which will be run as a doctest.

@Person-93
Person-93 force-pushed the safely-insert-component-by-id branch from 51142fe to 6f731e6 Compare August 7, 2026 05:06
@Person-93
Person-93 force-pushed the safely-insert-component-by-id branch from 6f731e6 to 0bb6d39 Compare August 7, 2026 05:10
@JaySpruce JaySpruce added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 7, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in ECS Aug 7, 2026

@urben1680 urben1680 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you write more about the motivation why you want/need this?

I would also like more unit tests:

  • Does dropping work as intended? For both the cases that the BackingPtr is consumed and if not.
  • Is the constructor panic safe?

Comment thread crates/bevy_ecs/src/component/inserter.rs
@Person-93
Person-93 requested a review from urben1680 August 7, 2026 21:10
@Person-93

Copy link
Copy Markdown
Contributor Author

Could you write more about the motivation why you want/need this?

Like I said, I want a safe way to insert type-erased components. Two use cases:

  1. utility AI: I can store a Vec<(SystemId<Entity, f32>, ComponentInserter)> in a resource. Then I can iterate over a list of entities and for each one I run each of the systems and insert the component that returns the greatest number.
  2. console input: I can store a HashMap<String, ComponentInserter> in a resource and look up a component with a string that comes from user input.

@Person-93

Person-93 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

I would also like more unit tests:

  • Does dropping work as intended? For both the cases that the BackingPtr is consumed and if not.
  • Is the constructor panic safe?

I've added the unit tests and I've fixed a segfault when the component is a ZST.

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-Feature A new feature, making something new possible D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

3 participants