Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I love egui! Thank you Emil <3 This request specifically enables an `eframe::App` which stores a lifetime. In general, I believe this is necessary because `eframe::App` currently does not seem to provide a good place to allocate and then borrow from long-lived data between `update()` calls. To attempt to borrow such long-lived data from a field of the `App` itself would be to create a self-referential struct. A hacky alternative is to allocate long-lived data with `Box::leak`, but that's a code smell and would cause problems if a program ever creates multiple Apps. As a more specific motivating example, I am developing with the [inkwell](https://github.com/TheDan64/inkwell/) crate which requires creating a `inkwell::context::Context` instance which is then borrowed from by a bazillion things with a dedicated `'ctx` lifetime. I need such a `inkwell::context::Context` for the duration of my `eframe::App` but I can't store it as a field of the app. The most natural solution to me is to simply to lift the inkwell context outside of the App and borrow from it, but that currently fails because the AppCreator implicitly has a `'static` lifetime requirement due to the use of `dyn` trait objects. Here is a simpler, self-contained motivating example adapted from the current [hello world example](https://docs.rs/eframe/latest/eframe/): ```rust use eframe::egui; struct LongLivedThing { message: String, } fn main() { let long_lived_thing = LongLivedThing { message: "Hello World!".to_string(), }; let native_options = eframe::NativeOptions::default(); eframe::run_native( "My egui App", native_options, Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc, &long_lived_thing)))), // ^^^^^^^^^^^^^^^^^ // BORROWING from long_lived_thing in App ); } struct MyEguiApp<'a> { long_lived_thing: &'a LongLivedThing, } impl<'a> MyEguiApp<'a> { fn new(cc: &eframe::CreationContext<'_>, long_lived_thing: &'a LongLivedThing) -> Self { // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals. // Restore app state using cc.storage (requires the "persistence" feature). // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use // for e.g. egui::PaintCallback. MyEguiApp { long_lived_thing } } } impl<'a> eframe::App for MyEguiApp<'a> { fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { egui::CentralPanel::default().show(ctx, |ui| { ui.heading(&self.long_lived_thing.message); }); } } ``` This currently fails to compile with: ```plaintext error[E0597]: `long_lived_thing` does not live long enough --> src/main.rs:16:55 | 16 | Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc, &long_lived_thing)))), | ----------------------------------------------^^^^^^^^^^^^^^^^---- | | | | | | | borrowed value does not live long enough | | value captured here | cast requires that `long_lived_thing` is borrowed for `'static` 17 | ); 18 | } | - `long_lived_thing` dropped here while still borrowed | = note: due to object lifetime defaults, `Box<dyn for<'a, 'b> FnOnce(&'a CreationContext<'b>) -> Result<Box<dyn App>, Box<dyn std::error::Error + Send + Sync>>>` actually means `Box<(dyn for<'a, 'b> FnOnce(&'a CreationContext<'b>) -> Result<Box<dyn App>, Box<dyn std::error::Error + Send + Sync>> + 'static)>` ``` With the added lifetimes in this request, I'm able to compile and run this as expected on Ubuntu + Wayland. I see the CI has been emailing me about some build failures and I'll do what I can to address those. Currently running the check.sh script as well. This is intended to resolve #2152 <!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> * Closes #2152 * [x] I have followed the instructions in the PR template
- Loading branch information