Allow non-static eframe::App lifetime#5060
Merged
emilk merged 4 commits intoemilk:masterfrom Sep 3, 2024
Merged
Conversation
… Glow/Winit things
Contributor
Author
|
I've added lifetime parameters to the |
static eframe::App lifetime
emilk
approved these changes
Sep 2, 2024
hacknus
pushed a commit
to hacknus/egui
that referenced
this pull request
Oct 30, 2024
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 emilk#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 emilk#2152 * [x] I have followed the instructions in the PR template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I love egui! Thank you Emil <3
This request specifically enables an
eframe::Appwhich stores a lifetime.In general, I believe this is necessary because
eframe::Appcurrently does not seem to provide a good place to allocate and then borrow from long-lived data betweenupdate()calls. To attempt to borrow such long-lived data from a field of theAppitself would be to create a self-referential struct. A hacky alternative is to allocate long-lived data withBox::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 crate which requires creating a
inkwell::context::Contextinstance which is then borrowed from by a bazillion things with a dedicated'ctxlifetime. I need such ainkwell::context::Contextfor the duration of myeframe::Appbut 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'staticlifetime requirement due to the use ofdyntrait objects.Here is a simpler, self-contained motivating example adapted from the current hello world example:
This currently fails to compile with:
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