I'm trying to implement a custom EntityCommand and I'm running into trouble with lifetimes. Can anyone help?
struct ReplaceIf<'w, C: Send> {
entity: Entity,
predicate: dyn Fn(&'w Option<C>) -> bool + Send
}
impl<'w, 's, C: Send> EntityCommand for ReplaceIf<'w, C>{ //Error: lifetime parameter instantiated with the lifetime `'w` as defined here
fn apply(self, entity: Entity, world: &mut World ){
if self.predicate(entity.get::<C>()) {
println!("hello world!");
}
}
}
trait CommandsExt {
fn replace_if<C: Send + Sync>(&mut self, entity: Entity, predicate: dyn Fn(Option<&C>)->bool + Send) -> &mut Self;
}
impl<'a> CommandsExt for EntityCommands<'a> {
fn replace_if<C: Send + Sync>(&mut self, entity: Entity, predicate: dyn Fn(Option<&C>)->bool + Send) -> &mut Self {
self.add(ReplaceIf{ entity, predicate, });
self
}
}