Skip to content

Commit

Permalink
First pass at adding explicit non-'static lifetime to AppCreator plus…
Browse files Browse the repository at this point in the history
… Glow/Winit things
  • Loading branch information
timstr committed Sep 2, 2024
1 parent 7bac528 commit 7e4003b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ type DynError = Box<dyn std::error::Error + Send + Sync>;
/// This is how your app is created.
///
/// You can use the [`CreationContext`] to setup egui, restore state, setup OpenGL things, etc.
pub type AppCreator = Box<dyn FnOnce(&CreationContext<'_>) -> Result<Box<dyn App>, DynError>>;
pub type AppCreator<'a> =
Box<dyn 'a + FnOnce(&CreationContext<'_>) -> Result<Box<dyn 'a + App>, DynError>>;

/// Data that is passed to [`AppCreator`] that can be used to setup and initialize your app.
pub struct CreationContext<'s> {
Expand Down
2 changes: 1 addition & 1 deletion crates/eframe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub mod icon_data;
pub fn run_native(
app_name: &str,
mut native_options: NativeOptions,
app_creator: AppCreator,
app_creator: AppCreator<'_>,
) -> Result {
#[cfg(not(feature = "__screenshot"))]
assert!(
Expand Down
25 changes: 14 additions & 11 deletions crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ use super::{
// ----------------------------------------------------------------------------
// Types:

pub struct GlowWinitApp {
pub struct GlowWinitApp<'a> {
repaint_proxy: Arc<egui::mutex::Mutex<EventLoopProxy<UserEvent>>>,
app_name: String,
native_options: NativeOptions,
running: Option<GlowWinitRunning>,
running: Option<GlowWinitRunning<'a>>,

// Note that since this `AppCreator` is FnOnce we are currently unable to support
// re-initializing the `GlowWinitRunning` state on Android if the application
// suspends and resumes.
app_creator: Option<AppCreator>,
app_creator: Option<AppCreator<'a>>,
}

/// State that is initialized when the application is first starts running via
/// a Resumed event. On Android this ensures that any graphics state is only
/// initialized once the application has an associated `SurfaceView`.
struct GlowWinitRunning {
struct GlowWinitRunning<'a> {
integration: EpiIntegration,
app: Box<dyn App>,
app: Box<dyn 'a + App>,

// These needs to be shared with the immediate viewport renderer, hence the Rc/Arc/RefCells:
glutin: Rc<RefCell<GlutinWindowContext>>,
Expand Down Expand Up @@ -126,12 +126,12 @@ struct Viewport {

// ----------------------------------------------------------------------------

impl GlowWinitApp {
impl<'a> GlowWinitApp<'a> {
pub fn new(
event_loop: &EventLoop<UserEvent>,
app_name: &str,
native_options: NativeOptions,
app_creator: AppCreator,
app_creator: AppCreator<'a>,
) -> Self {
crate::profile_function!();
Self {
Expand Down Expand Up @@ -195,7 +195,10 @@ impl GlowWinitApp {
Ok((glutin_window_context, painter))
}

fn init_run_state(&mut self, event_loop: &ActiveEventLoop) -> Result<&mut GlowWinitRunning> {
fn init_run_state(
&mut self,
event_loop: &ActiveEventLoop,
) -> Result<&mut GlowWinitRunning<'a>> {
crate::profile_function!();

let storage = if let Some(file) = &self.native_options.persistence_path {
Expand Down Expand Up @@ -292,7 +295,7 @@ impl GlowWinitApp {
let app_creator = std::mem::take(&mut self.app_creator)
.expect("Single-use AppCreator has unexpectedly already been taken");

let app = {
let app: Box<dyn 'a + App> = {
// Use latest raw_window_handle for eframe compatibility
use raw_window_handle::{HasDisplayHandle as _, HasWindowHandle as _};

Expand Down Expand Up @@ -346,7 +349,7 @@ impl GlowWinitApp {
}
}

impl WinitApp for GlowWinitApp {
impl<'a> WinitApp for GlowWinitApp<'a> {
fn frame_nr(&self, viewport_id: ViewportId) -> u64 {
self.running
.as_ref()
Expand Down Expand Up @@ -483,7 +486,7 @@ impl WinitApp for GlowWinitApp {
}
}

impl GlowWinitRunning {
impl<'a> GlowWinitRunning<'a> {
fn run_ui_and_paint(
&mut self,
event_loop: &ActiveEventLoop,
Expand Down
6 changes: 3 additions & 3 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ fn run_and_return(event_loop: &mut EventLoop<UserEvent>, winit_app: impl WinitAp
app.return_result
}

fn run_and_exit(event_loop: EventLoop<UserEvent>, winit_app: impl WinitApp + 'static) -> Result {
fn run_and_exit(event_loop: EventLoop<UserEvent>, winit_app: impl WinitApp) -> Result {
log::trace!("Entering the winit event loop (run_app)…");

// When to repaint what window
Expand All @@ -314,7 +314,7 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, winit_app: impl WinitApp + 'st
pub fn run_glow(
app_name: &str,
mut native_options: epi::NativeOptions,
app_creator: epi::AppCreator,
app_creator: epi::AppCreator<'_>,
) -> Result {
#![allow(clippy::needless_return_with_question_mark)] // False positive

Expand All @@ -339,7 +339,7 @@ pub fn run_glow(
pub fn run_wgpu(
app_name: &str,
mut native_options: epi::NativeOptions,
app_creator: epi::AppCreator,
app_creator: epi::AppCreator<'_>,
) -> Result {
#![allow(clippy::needless_return_with_question_mark)] // False positive

Expand Down

0 comments on commit 7e4003b

Please sign in to comment.