impl EntityCommand for Arc<dyn Fn(EntityWorldMut) ...> - #25391
Conversation
|
Can you say a bit more about why you wanted this? Presumably a heavy or not thread-safe command? |
ItsDoot
left a comment
There was a problem hiding this comment.
LGTM, although I would make the following addition to relax its bounds a bit.
Also interested to here your motivation!
Co-authored-by: Christian Hughes <9044780+ItsDoot@users.noreply.github.com>
|
I want to have something like this. #[derive(Resource)]
struct DynamicEntityCommands(Vec<Arc<dyn Fn(EntityWorldMut) -> Result + Send + Sync + 'static>>); |
Note that another option is to convert the
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 #[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());
}
}
} |
Objective
A
Box<dyn Fn(EntityWorldMut) -> Out + Send + 'static>can already be used as anEntityCommand, but anArccan't.Solution
Add an implementation for
Arc.Testing
None