Propagate error from EventLoop creation

Inner panics could make it hard to trouble shoot the issues and for some
users ints not desirable.

The inner panics were left only when they are used to `assert!` during
development.
This commit is contained in:
Kirill Chibisov 2023-08-06 06:03:54 +04:00 committed by GitHub
parent 584aab4cd0
commit ed26dd58fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
57 changed files with 344 additions and 291 deletions

View file

@ -30,7 +30,7 @@ use crate::{
self, CursorGrabMode, ImePurpose, ResizeDirection, Theme, WindowButtons, WindowLevel,
},
};
use crate::{error::RunLoopError, platform_impl::Fullscreen};
use crate::{error::EventLoopError, platform_impl::Fullscreen};
mod keycodes;
@ -186,13 +186,15 @@ fn sticky_exit_callback<T, F>(
}
impl<T: 'static> EventLoop<T> {
pub(crate) fn new(attributes: &PlatformSpecificEventLoopAttributes) -> Self {
pub(crate) fn new(
attributes: &PlatformSpecificEventLoopAttributes,
) -> Result<Self, EventLoopError> {
let (user_events_sender, user_events_receiver) = mpsc::channel();
let android_app = attributes.android_app.as_ref().expect("An `AndroidApp` as passed to android_main() is required to create an `EventLoop` on Android");
let redraw_flag = SharedFlag::new();
Self {
Ok(Self {
android_app: android_app.clone(),
window_target: event_loop::EventLoopWindowTarget {
p: EventLoopWindowTarget {
@ -214,7 +216,7 @@ impl<T: 'static> EventLoop<T> {
control_flow: Default::default(),
cause: StartCause::Init,
ignore_volume_keys: attributes.ignore_volume_keys,
}
})
}
fn single_iteration<F>(&mut self, main_event: Option<MainEvent<'_>>, callback: &mut F)
@ -524,19 +526,19 @@ impl<T: 'static> EventLoop<T> {
self.pending_redraw = pending_redraw;
}
pub fn run<F>(mut self, event_handler: F) -> Result<(), RunLoopError>
pub fn run<F>(mut self, event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(event::Event<T>, &event_loop::EventLoopWindowTarget<T>, &mut ControlFlow),
{
self.run_ondemand(event_handler)
}
pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), RunLoopError>
pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(event::Event<T>, &event_loop::EventLoopWindowTarget<T>, &mut ControlFlow),
{
if self.loop_running {
return Err(RunLoopError::AlreadyRunning);
return Err(EventLoopError::AlreadyRunning);
}
loop {
@ -545,7 +547,7 @@ impl<T: 'static> EventLoop<T> {
break Ok(());
}
PumpStatus::Exit(code) => {
break Err(RunLoopError::ExitFailure(code));
break Err(EventLoopError::ExitFailure(code));
}
_ => {
continue;