Skip to content

Application should be returned when Bevy runner terminate #2937

@arialpew

Description

@arialpew

What problem does this solve or what need does it fill?

Bevy can be run in headless mode, where you often want to introspect the state of the world (ressource, query, ...) and assert something for test and debugging. Some usefull data are present in Bevy, like time since startup, the whole world state, ...

When an application is constructed and App::run() is called, the application never terminate unless :

  • The runner is set to "run once".
  • The application emit an event called AppExit.
  • The user have setup a custom runner with his own rule.

When the application terminate, App::run() return nothing and you have no way to introspect the final state because the application is now empty due to std::mem::replace.

// impl App
pub fn run(&mut self) {
    #[cfg(feature = "trace")]
    let bevy_app_run_span = info_span!("bevy_app");
    #[cfg(feature = "trace")]
    let _bevy_app_run_guard = bevy_app_run_span.enter();

    let mut app = std::mem::replace(self, App::empty());
    let runner = std::mem::replace(&mut app.runner, Box::new(run_once));
    (runner)(app);
}
// App
pub struct App {
    pub world: World,
    pub runner: Box<dyn Fn(App)>, // Desugar into ---> Box<Dyn Fn(App) -> ()>
    pub schedule: Schedule,
}

What solution would you like?

Since there's a clear owner of the Application (the runner), the runner should be able to return the Application and borrow checker should not complain about that.

We can either :

  1. Replace self with the returned application (should be favored in my opinion, since we are mutating the world when we run the application).
  2. Return the application and let the empty application like that.

What alternative(s) have you considered?

Don't return the Application and let the user with an empty application. If user don't have any background with std::mem::replace, confusion can happen. The user expect the application to be in final state, they don't expect an empty application.

Additional context

/

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-AppBevy apps and pluginsC-ExamplesAn addition or correction to our examplesC-UsabilityA targeted quality-of-life change that makes Bevy easier to use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions