Skip to content

Return the passed EntityWorldMut from EntityCommand::apply #20287

@gwafotapa

Description

@gwafotapa

For reference here is the trait EntityCommand:

pub trait EntityCommand<Out = ()>: Send + 'static {
    fn apply(self, entity: EntityWorldMut) -> Out;
}

The method consumes the EntityWorldMut it takes. I thought an implementation of the trait with Out as EntityWorldMut should be able to return its owned argument so I could continue using it afterwards. In its simplest form that would be

struct MyEntityCommand;

impl EntityCommand<EntityWorldMut<'_>> for MyEntityCommand {
    fn apply(self, entity: EntityWorldMut) -> EntityWorldMut {
        entity
    }
}

but this doesn't compile. The compiler complains that the lifetimes of the method argument and its return must be different per the trait signature. I've tried

struct MyEntityCommand;

impl<'a> EntityCommand<EntityWorldMut<'a>> for MyEntityCommand {
    fn apply<'b: 'a>(self, entity: EntityWorldMut<'b>) -> EntityWorldMut<'a> {
        entity
    }
}

Here the compiler complains that b is early bound whereas according to the trait signature it should be late bound. Lifting b at the impl level and the method argument may not live as long.

After research I found this post which seems to solve this exact problem. As suggested I've tried lifting the lifetime to the trait to add the feature in Bevy but I've run into many compiler complaints that I am unable to solve for now. I was wondering if I'm on the right track or if I've missed something.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-UsabilityA targeted quality-of-life change that makes Bevy easier to useS-Needs-DesignThis issue requires design work to think about how it would best be accomplishedX-ContentiousThere are nontrivial implications that should be thought through

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions