Skip to content

impl EntityCommand for Arc<dyn Fn(EntityWorldMut) ...> - #25391

Open
Person-93 wants to merge 3 commits into
bevyengine:mainfrom
Person-93:arc-fn-impl-entity-command
Open

impl EntityCommand for Arc<dyn Fn(EntityWorldMut) ...>#25391
Person-93 wants to merge 3 commits into
bevyengine:mainfrom
Person-93:arc-fn-impl-entity-command

Conversation

@Person-93

Copy link
Copy Markdown
Contributor

Objective

A Box<dyn Fn(EntityWorldMut) -> Out + Send + 'static> can already be used as an EntityCommand, but an Arc can't.

Solution

Add an implementation for Arc.

Testing

None

@alice-i-cecile alice-i-cecile 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 labels Aug 13, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in ECS Aug 13, 2026
@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 S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Aug 13, 2026
@alice-i-cecile

Copy link
Copy Markdown
Member

Can you say a bit more about why you wanted this? Presumably a heavy or not thread-safe command?

@ItsDoot ItsDoot 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.

LGTM, although I would make the following addition to relax its bounds a bit.

Also interested to here your motivation!

Comment thread crates/bevy_ecs/src/system/commands/entity_command.rs Outdated
@ItsDoot ItsDoot 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 Aug 14, 2026
Co-authored-by: Christian Hughes <9044780+ItsDoot@users.noreply.github.com>
@Person-93

Copy link
Copy Markdown
Contributor Author

I want to have something like this.

#[derive(Resource)]
struct DynamicEntityCommands(Vec<Arc<dyn Fn(EntityWorldMut) -> Result + Send + Sync + 'static>>);

@chescock

Copy link
Copy Markdown
Contributor

A Box<dyn Fn(EntityWorldMut) -> Out + Send + 'static> can already be used as an EntityCommand, but an Arc can't.

Note that another option is to convert the Arc<dyn Fn...> to an EntityCommand by wrapping it in a closure, like move |e: EntityWorldMut| arc(e).


I want to have something like this.

#[derive(Resource)]
struct DynamicEntityCommands(Vec<Arc<dyn Fn(EntityWorldMut) -> Result + Send + Sync + 'static>>);

I'm not sure whether this will actually be helpful for your use case, but if your commands are cheap to clone and you want to do some premature optimization, then another option is to wrap queue() in an Fn(EntityCommands) before erasing the type. That avoids the atomic operations for Arc::clone and can avoid memory allocation for ZST commands.

#[derive(Resource)]
struct DynamicEntityCommands(Vec<Box<dyn Fn(EntityCommands) + Send + Sync + 'static>>);

impl DynamicEntityCommands {
    fn queue(&mut self, c: impl EntityCommand + Clone + Sync) {
        self.0.push(Box::new(move |mut entity_commands| {
            entity_commands.queue(c.clone());
        }));
    }

    fn apply(&self, mut entity_commands: EntityCommands) {
        for f in &self.0 {
            f(entity_commands.reborrow());
        }
    }
}

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 S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

4 participants