From 16acdd566faa3c0f36382aae4ab3209a4e316b4b Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Fri, 28 Jan 2022 15:05:57 -0500 Subject: [PATCH 01/21] EventLoop::new now uses results instead of panics --- examples/control_flow.rs | 2 +- examples/cursor.rs | 2 +- examples/cursor_grab.rs | 2 +- examples/custom_events.rs | 2 +- examples/drag_window.rs | 2 +- examples/fullscreen.rs | 2 +- examples/handling_close.rs | 2 +- examples/min_max_size.rs | 2 +- examples/minimize.rs | 2 +- examples/monitor_list.rs | 2 +- examples/mouse_wheel.rs | 2 +- examples/multithreaded.rs | 2 +- examples/multiwindow.rs | 2 +- examples/request_redraw.rs | 2 +- examples/request_redraw_threaded.rs | 2 +- examples/resizable.rs | 2 +- examples/set_ime_position.rs | 2 +- examples/timer.rs | 2 +- examples/transparent.rs | 2 +- examples/video_modes.rs | 2 +- examples/web.rs | 2 +- examples/window.rs | 2 +- examples/window_debug.rs | 2 +- examples/window_icon.rs | 2 +- examples/window_run_return.rs | 2 +- src/event_loop.rs | 17 +++++++++++------ src/platform_impl/linux/mod.rs | 20 ++++++++++++-------- 27 files changed, 48 insertions(+), 39 deletions(-) diff --git a/examples/control_flow.rs b/examples/control_flow.rs index cbb445eebb..f28ec5830a 100644 --- a/examples/control_flow.rs +++ b/examples/control_flow.rs @@ -26,7 +26,7 @@ fn main() { println!("Press 'R' to toggle request_redraw() calls."); println!("Press 'Esc' to close the window."); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("Press 1, 2, 3 to change control flow mode. Press R to toggle redraw requests.") .build(&event_loop) diff --git a/examples/cursor.rs b/examples/cursor.rs index 6ec8a05933..40d99440b4 100644 --- a/examples/cursor.rs +++ b/examples/cursor.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap(); window.set_title("A fantastic window!"); diff --git a/examples/cursor_grab.rs b/examples/cursor_grab.rs index 90a94764de..430a83b599 100644 --- a/examples/cursor_grab.rs +++ b/examples/cursor_grab.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("Super Cursor Grab'n'Hide Simulator 9000") diff --git a/examples/custom_events.rs b/examples/custom_events.rs index 016754b590..bac881851a 100644 --- a/examples/custom_events.rs +++ b/examples/custom_events.rs @@ -13,7 +13,7 @@ fn main() { } SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::::with_user_event(); + let event_loop = EventLoop::::with_user_event().unwrap(); let _window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/drag_window.rs b/examples/drag_window.rs index a408c7c722..c3942b9171 100644 --- a/examples/drag_window.rs +++ b/examples/drag_window.rs @@ -9,7 +9,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window_1 = WindowBuilder::new().build(&event_loop).unwrap(); let window_2 = WindowBuilder::new().build(&event_loop).unwrap(); diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs index 90d41ece12..f24337638d 100644 --- a/examples/fullscreen.rs +++ b/examples/fullscreen.rs @@ -8,7 +8,7 @@ use winit::window::{Fullscreen, WindowBuilder}; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); print!("Please choose the fullscreen mode: (1) exclusive, (2) borderless: "); stdout().flush().unwrap(); diff --git a/examples/handling_close.rs b/examples/handling_close.rs index 8334c1773f..46c055c566 100644 --- a/examples/handling_close.rs +++ b/examples/handling_close.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let _window = WindowBuilder::new() .with_title("Your faithful window") diff --git a/examples/min_max_size.rs b/examples/min_max_size.rs index 9a58ed6c69..1e5f667f4a 100644 --- a/examples/min_max_size.rs +++ b/examples/min_max_size.rs @@ -8,7 +8,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap(); diff --git a/examples/minimize.rs b/examples/minimize.rs index eb02a752c9..02c3b009d7 100644 --- a/examples/minimize.rs +++ b/examples/minimize.rs @@ -7,7 +7,7 @@ use winit::window::WindowBuilder; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/monitor_list.rs b/examples/monitor_list.rs index 9c8b77e61c..1cc1cc8e6b 100644 --- a/examples/monitor_list.rs +++ b/examples/monitor_list.rs @@ -3,7 +3,7 @@ use winit::{event_loop::EventLoop, window::WindowBuilder}; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap(); dbg!(window.available_monitors().collect::>()); diff --git a/examples/mouse_wheel.rs b/examples/mouse_wheel.rs index e61b64af8f..f5b2a9a615 100644 --- a/examples/mouse_wheel.rs +++ b/examples/mouse_wheel.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("Mouse Wheel events") diff --git a/examples/multithreaded.rs b/examples/multithreaded.rs index ba2b9ad240..7fb7303a89 100644 --- a/examples/multithreaded.rs +++ b/examples/multithreaded.rs @@ -14,7 +14,7 @@ fn main() { const WINDOW_SIZE: PhysicalSize = PhysicalSize::new(600, 400); SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let mut window_senders = HashMap::with_capacity(WINDOW_COUNT); for _ in 0..WINDOW_COUNT { let window = WindowBuilder::new() diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs index e946537296..0c2c02aa14 100644 --- a/examples/multiwindow.rs +++ b/examples/multiwindow.rs @@ -9,7 +9,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let mut windows = HashMap::new(); for _ in 0..3 { diff --git a/examples/request_redraw.rs b/examples/request_redraw.rs index 163f6a14d2..de7919c024 100644 --- a/examples/request_redraw.rs +++ b/examples/request_redraw.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/request_redraw_threaded.rs b/examples/request_redraw_threaded.rs index 7a28c23b55..748ac4c07c 100644 --- a/examples/request_redraw_threaded.rs +++ b/examples/request_redraw_threaded.rs @@ -10,7 +10,7 @@ fn main() { }; SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/resizable.rs b/examples/resizable.rs index 17892d8741..01404376fc 100644 --- a/examples/resizable.rs +++ b/examples/resizable.rs @@ -8,7 +8,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let mut resizable = false; diff --git a/examples/set_ime_position.rs b/examples/set_ime_position.rs index 2cd376da0f..6ff90065f7 100644 --- a/examples/set_ime_position.rs +++ b/examples/set_ime_position.rs @@ -8,7 +8,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap(); window.set_title("A fantastic window!"); diff --git a/examples/timer.rs b/examples/timer.rs index 7bbb9685f4..7e84514db2 100644 --- a/examples/timer.rs +++ b/examples/timer.rs @@ -10,7 +10,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let _window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/transparent.rs b/examples/transparent.rs index c9937cd4cd..2adb07b8fb 100644 --- a/examples/transparent.rs +++ b/examples/transparent.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_decorations(false) diff --git a/examples/video_modes.rs b/examples/video_modes.rs index 341f43855b..063b0e0135 100644 --- a/examples/video_modes.rs +++ b/examples/video_modes.rs @@ -3,7 +3,7 @@ use winit::event_loop::EventLoop; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let monitor = match event_loop.primary_monitor() { Some(monitor) => monitor, None => { diff --git a/examples/web.rs b/examples/web.rs index 83ccc45ee2..2c03477bee 100644 --- a/examples/web.rs +++ b/examples/web.rs @@ -5,7 +5,7 @@ use winit::{ }; pub fn main() { - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/window.rs b/examples/window.rs index 783578de65..c4633b5990 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/window_debug.rs b/examples/window_debug.rs index 577ad5cd73..6a543fb179 100644 --- a/examples/window_debug.rs +++ b/examples/window_debug.rs @@ -10,7 +10,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/window_icon.rs b/examples/window_icon.rs index 6c79625505..29a36259fa 100644 --- a/examples/window_icon.rs +++ b/examples/window_icon.rs @@ -19,7 +19,7 @@ fn main() { let icon = load_icon(Path::new(path)); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("An iconic window!") diff --git a/examples/window_run_return.rs b/examples/window_run_return.rs index 6bedfcd82c..498b225721 100644 --- a/examples/window_run_return.rs +++ b/examples/window_run_return.rs @@ -18,7 +18,7 @@ fn main() { platform::run_return::EventLoopExtRunReturn, window::WindowBuilder, }; - let mut event_loop = EventLoop::new(); + let mut event_loop = EventLoop::new().unwrap(); SimpleLogger::new().init().unwrap(); let _window = WindowBuilder::new() diff --git a/src/event_loop.rs b/src/event_loop.rs index 17c01f5f58..54975311f9 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -136,7 +136,7 @@ impl EventLoop<()> { /// ## Platform-specific /// /// - **iOS:** Can only be called on the main thread. - pub fn new() -> EventLoop<()> { + pub fn new() -> Result, String> { EventLoop::<()>::with_user_event() } } @@ -149,11 +149,16 @@ impl EventLoop { /// ## Platform-specific /// /// - **iOS:** Can only be called on the main thread. - pub fn with_user_event() -> EventLoop { - EventLoop { - event_loop: platform_impl::EventLoop::new(), - _marker: ::std::marker::PhantomData, - } + pub fn with_user_event() -> Result, String> { + return match platform_impl::EventLoop::new() { + Ok(e) => return { + Ok(EventLoop { + event_loop: e, + _marker: ::std::marker::PhantomData, + }) + }, + Err(e) => Err(e) + }; } /// Hijacks the calling thread and initializes the winit event loop with the provided diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index b8e233653c..1135342532 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -568,10 +568,13 @@ impl Clone for EventLoopProxy { } impl EventLoop { - pub fn new() -> EventLoop { - assert_is_main_thread("new_any_thread"); + pub fn new() -> Result, String> { + match assert_is_main_thread("new_any_thread") { + Ok(_) => (), + Err(e) => return Err(e) + } - EventLoop::new_any_thread() + Ok(EventLoop::new_any_thread()) } pub fn new_any_thread() -> EventLoop { @@ -750,15 +753,16 @@ fn sticky_exit_callback( } } -fn assert_is_main_thread(suggested_method: &str) { +fn assert_is_main_thread(suggested_method: &str) -> Result<(), String> { if !is_main_thread() { - panic!( - "Initializing the event loop outside of the main thread is a significant \ + return Err( + format!("Initializing the event loop outside of the main thread is a significant \ cross-platform compatibility hazard. If you really, absolutely need to create an \ EventLoop on a different thread, please use the `EventLoopExtUnix::{}` function.", - suggested_method - ); + suggested_method) + ) } + Ok(()) } #[cfg(target_os = "linux")] From c18fcc4f79248ec2ed500702d68cb9c99e52f208 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Fri, 28 Jan 2022 15:13:11 -0500 Subject: [PATCH 02/21] new_wayland and new_x11 --- src/platform/unix.rs | 4 ++-- src/platform_impl/linux/mod.rs | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/platform/unix.rs b/src/platform/unix.rs index 74dc769b67..5f027c2af5 100644 --- a/src/platform/unix.rs +++ b/src/platform/unix.rs @@ -176,14 +176,14 @@ impl EventLoopExtUnix for EventLoop { #[inline] #[cfg(feature = "x11")] fn new_x11() -> Result { - LinuxEventLoop::new_x11().map(wrap_ev) + LinuxEventLoop::new_x11().unwrap().map(wrap_ev) } #[inline] #[cfg(feature = "wayland")] fn new_wayland() -> Self { wrap_ev( - LinuxEventLoop::new_wayland() + LinuxEventLoop::new_wayland().unwrap() // TODO: propagate .expect("failed to open Wayland connection"), ) diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 1135342532..5468c9c577 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -626,10 +626,13 @@ impl EventLoop { } #[cfg(feature = "wayland")] - pub fn new_wayland() -> Result, Box> { - assert_is_main_thread("new_wayland_any_thread"); + pub fn new_wayland() -> Result, Box>, String> { + match assert_is_main_thread("new_wayland_any_thread") { + Ok(_) => (), + Err(e) => return Err(e) + } - EventLoop::new_wayland_any_thread() + Ok(EventLoop::new_wayland_any_thread()) } #[cfg(feature = "wayland")] @@ -638,10 +641,14 @@ impl EventLoop { } #[cfg(feature = "x11")] - pub fn new_x11() -> Result, XNotSupported> { - assert_is_main_thread("new_x11_any_thread"); + pub fn new_x11() -> Result, XNotSupported>, String> { + match assert_is_main_thread("new_x11_any_thread") { + Ok(_) => (), + Err(e) => return Err(e) + } + + Ok(EventLoop::new_x11_any_thread()) - EventLoop::new_x11_any_thread() } #[cfg(feature = "x11")] From 4347c0e4dc934a07105e67429a4638de6a46be38 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Fri, 28 Jan 2022 15:15:53 -0500 Subject: [PATCH 03/21] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eda123815..5f2c793749 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ And please only add new entries to the top of this list, right below the `# Unre - Remove no longer needed `WINIT_LINK_COLORSYNC` environment variable. - **Breaking:** Rename the `Exit` variant of `ControlFlow` to `ExitWithCode`, which holds a value to control the exit code after running. Add an `Exit` constant which aliases to `ExitWithCode(0)` instead to avoid major breakage. This shouldn't affect most existing programs. +- Changed to Results instead of a panic for creating a new EventLoop # 0.26.1 (2022-01-05) From 6463c7ca16ff1926535aea59dbcbc014409a3992 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Fri, 28 Jan 2022 15:30:40 -0500 Subject: [PATCH 04/21] Changed Function name to be more accurate --- src/platform_impl/linux/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 5468c9c577..fd789be8bc 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -569,7 +569,7 @@ impl Clone for EventLoopProxy { impl EventLoop { pub fn new() -> Result, String> { - match assert_is_main_thread("new_any_thread") { + match error_if_not_main_thread("new_any_thread") { Ok(_) => (), Err(e) => return Err(e) } @@ -627,7 +627,7 @@ impl EventLoop { #[cfg(feature = "wayland")] pub fn new_wayland() -> Result, Box>, String> { - match assert_is_main_thread("new_wayland_any_thread") { + match error_if_not_main_thread("new_wayland_any_thread") { Ok(_) => (), Err(e) => return Err(e) } @@ -642,7 +642,7 @@ impl EventLoop { #[cfg(feature = "x11")] pub fn new_x11() -> Result, XNotSupported>, String> { - match assert_is_main_thread("new_x11_any_thread") { + match error_if_not_main_thread("new_x11_any_thread") { Ok(_) => (), Err(e) => return Err(e) } @@ -760,7 +760,7 @@ fn sticky_exit_callback( } } -fn assert_is_main_thread(suggested_method: &str) -> Result<(), String> { +fn error_if_not_main_thread(suggested_method: &str) -> Result<(), String> { if !is_main_thread() { return Err( format!("Initializing the event loop outside of the main thread is a significant \ From 8ea28f7520afdf0e21df740e2309c46aaaa55d3f Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Fri, 28 Jan 2022 15:30:50 -0500 Subject: [PATCH 05/21] Removed Redundant Return --- src/event_loop.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 54975311f9..3d0400a539 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -151,7 +151,7 @@ impl EventLoop { /// - **iOS:** Can only be called on the main thread. pub fn with_user_event() -> Result, String> { return match platform_impl::EventLoop::new() { - Ok(e) => return { + Ok(e) => { Ok(EventLoop { event_loop: e, _marker: ::std::marker::PhantomData, From 46917eef44376c970aa6383421ebdc1a80b96a3d Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Sat, 29 Jan 2022 17:58:07 -0500 Subject: [PATCH 06/21] Changed Docs to be more accurate --- src/event_loop.rs | 2 +- src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 3d0400a539..6093be2c7e 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -122,7 +122,7 @@ impl EventLoop<()> { /// Builds a new event loop with a `()` as the user event type. /// /// ***For cross-platform compatibility, the `EventLoop` must be created on the main thread.*** - /// Attempting to create the event loop on a different thread will panic. This restriction isn't + /// Attempting to create the event loop on a different thread will return an error. This restriction isn't /// strictly necessary on all platforms, but is imposed to eliminate any nasty surprises when /// porting to platforms that require it. `EventLoopExt::new_any_thread` functions are exposed /// in the relevant `platform` module if the target platform supports creating an event loop on diff --git a/src/lib.rs b/src/lib.rs index 13b3c9ac3d..6565d1b217 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,7 +7,7 @@ //! //! ```no_run //! use winit::event_loop::EventLoop; -//! let event_loop = EventLoop::new(); +//! let event_loop = EventLoop::new().unwrap(); //! ``` //! //! Once this is done there are two ways to create a [`Window`]: @@ -48,7 +48,7 @@ //! window::WindowBuilder, //! }; //! -//! let event_loop = EventLoop::new(); +//! let event_loop = EventLoop::new().unwrap(); //! let window = WindowBuilder::new().build(&event_loop).unwrap(); //! //! event_loop.run(move |event, _, control_flow| { From 0c6a9a23a3c0fdd7a41e6c2480e3b2b591b83e3d Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Sun, 20 Feb 2022 11:09:51 -0500 Subject: [PATCH 07/21] Added Result to Creating the EventLoop --- src/event_loop.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 72a4d36663..b9b0d0dc18 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -93,14 +93,14 @@ impl EventLoopBuilder { /// /// - **iOS:** Can only be called on the main thread. #[inline] - pub fn build(&mut self) -> EventLoop { - EventLoop { + pub fn build(&mut self) -> Result, String> { + Ok(EventLoop { event_loop: match platform_impl::EventLoop::new(&self.platform_specific) { Ok(event_loop) => event_loop, - Err(err) => panic!("Error creating event loop: {}", err), + Err(err) => return Err(format!("Error creating event loop: {}", err)), }, _marker: PhantomData, - } + }) } } @@ -180,14 +180,14 @@ impl Default for ControlFlow { impl EventLoop<()> { /// Alias for `EventLoopBuilder::new().build()`. #[inline] - pub fn new() -> EventLoop<()> { + pub fn new() -> Result, String> { EventLoopBuilder::new().build() } } impl EventLoop { #[deprecated = "Use `EventLoopBuilder::::with_user_event().build()` instead."] - pub fn with_user_event() -> EventLoop { + pub fn with_user_event() -> Result, String> { EventLoopBuilder::::with_user_event().build() } From 4b68ba4f99248eb8c1d34ecbc6260309c0c23f2e Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Sun, 20 Feb 2022 11:09:59 -0500 Subject: [PATCH 08/21] Added unwrap back to examples --- examples/control_flow.rs | 2 +- examples/cursor.rs | 2 +- examples/cursor_grab.rs | 2 +- examples/custom_events.rs | 2 +- examples/drag_window.rs | 2 +- examples/fullscreen.rs | 2 +- examples/handling_close.rs | 2 +- examples/min_max_size.rs | 2 +- examples/minimize.rs | 2 +- examples/monitor_list.rs | 2 +- examples/mouse_wheel.rs | 2 +- examples/multithreaded.rs | 2 +- examples/multiwindow.rs | 2 +- examples/request_redraw.rs | 2 +- examples/request_redraw_threaded.rs | 2 +- examples/resizable.rs | 2 +- examples/set_ime_position.rs | 2 +- examples/timer.rs | 2 +- examples/transparent.rs | 2 +- examples/video_modes.rs | 2 +- examples/web.rs | 2 +- examples/window.rs | 2 +- examples/window_debug.rs | 2 +- examples/window_icon.rs | 2 +- examples/window_run_return.rs | 2 +- 25 files changed, 25 insertions(+), 25 deletions(-) diff --git a/examples/control_flow.rs b/examples/control_flow.rs index cbb445eebb..f28ec5830a 100644 --- a/examples/control_flow.rs +++ b/examples/control_flow.rs @@ -26,7 +26,7 @@ fn main() { println!("Press 'R' to toggle request_redraw() calls."); println!("Press 'Esc' to close the window."); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("Press 1, 2, 3 to change control flow mode. Press R to toggle redraw requests.") .build(&event_loop) diff --git a/examples/cursor.rs b/examples/cursor.rs index 6ec8a05933..40d99440b4 100644 --- a/examples/cursor.rs +++ b/examples/cursor.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap(); window.set_title("A fantastic window!"); diff --git a/examples/cursor_grab.rs b/examples/cursor_grab.rs index 90a94764de..430a83b599 100644 --- a/examples/cursor_grab.rs +++ b/examples/cursor_grab.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("Super Cursor Grab'n'Hide Simulator 9000") diff --git a/examples/custom_events.rs b/examples/custom_events.rs index e2ed027176..b0ea050ae7 100644 --- a/examples/custom_events.rs +++ b/examples/custom_events.rs @@ -14,7 +14,7 @@ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoopBuilder::::with_user_event().build(); + let event_loop = EventLoopBuilder::::with_user_event().build().unwrap(); let _window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/drag_window.rs b/examples/drag_window.rs index a408c7c722..c3942b9171 100644 --- a/examples/drag_window.rs +++ b/examples/drag_window.rs @@ -9,7 +9,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window_1 = WindowBuilder::new().build(&event_loop).unwrap(); let window_2 = WindowBuilder::new().build(&event_loop).unwrap(); diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs index 90d41ece12..f24337638d 100644 --- a/examples/fullscreen.rs +++ b/examples/fullscreen.rs @@ -8,7 +8,7 @@ use winit::window::{Fullscreen, WindowBuilder}; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); print!("Please choose the fullscreen mode: (1) exclusive, (2) borderless: "); stdout().flush().unwrap(); diff --git a/examples/handling_close.rs b/examples/handling_close.rs index 8334c1773f..46c055c566 100644 --- a/examples/handling_close.rs +++ b/examples/handling_close.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let _window = WindowBuilder::new() .with_title("Your faithful window") diff --git a/examples/min_max_size.rs b/examples/min_max_size.rs index 9a58ed6c69..1e5f667f4a 100644 --- a/examples/min_max_size.rs +++ b/examples/min_max_size.rs @@ -8,7 +8,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap(); diff --git a/examples/minimize.rs b/examples/minimize.rs index eb02a752c9..02c3b009d7 100644 --- a/examples/minimize.rs +++ b/examples/minimize.rs @@ -7,7 +7,7 @@ use winit::window::WindowBuilder; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/monitor_list.rs b/examples/monitor_list.rs index 9c8b77e61c..1cc1cc8e6b 100644 --- a/examples/monitor_list.rs +++ b/examples/monitor_list.rs @@ -3,7 +3,7 @@ use winit::{event_loop::EventLoop, window::WindowBuilder}; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap(); dbg!(window.available_monitors().collect::>()); diff --git a/examples/mouse_wheel.rs b/examples/mouse_wheel.rs index e61b64af8f..f5b2a9a615 100644 --- a/examples/mouse_wheel.rs +++ b/examples/mouse_wheel.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("Mouse Wheel events") diff --git a/examples/multithreaded.rs b/examples/multithreaded.rs index ba2b9ad240..7fb7303a89 100644 --- a/examples/multithreaded.rs +++ b/examples/multithreaded.rs @@ -14,7 +14,7 @@ fn main() { const WINDOW_SIZE: PhysicalSize = PhysicalSize::new(600, 400); SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let mut window_senders = HashMap::with_capacity(WINDOW_COUNT); for _ in 0..WINDOW_COUNT { let window = WindowBuilder::new() diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs index e946537296..0c2c02aa14 100644 --- a/examples/multiwindow.rs +++ b/examples/multiwindow.rs @@ -9,7 +9,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let mut windows = HashMap::new(); for _ in 0..3 { diff --git a/examples/request_redraw.rs b/examples/request_redraw.rs index 163f6a14d2..de7919c024 100644 --- a/examples/request_redraw.rs +++ b/examples/request_redraw.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/request_redraw_threaded.rs b/examples/request_redraw_threaded.rs index 7a28c23b55..748ac4c07c 100644 --- a/examples/request_redraw_threaded.rs +++ b/examples/request_redraw_threaded.rs @@ -10,7 +10,7 @@ fn main() { }; SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/resizable.rs b/examples/resizable.rs index 17892d8741..01404376fc 100644 --- a/examples/resizable.rs +++ b/examples/resizable.rs @@ -8,7 +8,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let mut resizable = false; diff --git a/examples/set_ime_position.rs b/examples/set_ime_position.rs index 2cd376da0f..6ff90065f7 100644 --- a/examples/set_ime_position.rs +++ b/examples/set_ime_position.rs @@ -8,7 +8,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap(); window.set_title("A fantastic window!"); diff --git a/examples/timer.rs b/examples/timer.rs index 7bbb9685f4..7e84514db2 100644 --- a/examples/timer.rs +++ b/examples/timer.rs @@ -10,7 +10,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let _window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/transparent.rs b/examples/transparent.rs index c9937cd4cd..2adb07b8fb 100644 --- a/examples/transparent.rs +++ b/examples/transparent.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_decorations(false) diff --git a/examples/video_modes.rs b/examples/video_modes.rs index 341f43855b..063b0e0135 100644 --- a/examples/video_modes.rs +++ b/examples/video_modes.rs @@ -3,7 +3,7 @@ use winit::event_loop::EventLoop; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let monitor = match event_loop.primary_monitor() { Some(monitor) => monitor, None => { diff --git a/examples/web.rs b/examples/web.rs index 83ccc45ee2..2c03477bee 100644 --- a/examples/web.rs +++ b/examples/web.rs @@ -5,7 +5,7 @@ use winit::{ }; pub fn main() { - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/window.rs b/examples/window.rs index 783578de65..c4633b5990 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/window_debug.rs b/examples/window_debug.rs index 577ad5cd73..6a543fb179 100644 --- a/examples/window_debug.rs +++ b/examples/window_debug.rs @@ -10,7 +10,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/window_icon.rs b/examples/window_icon.rs index 6c79625505..29a36259fa 100644 --- a/examples/window_icon.rs +++ b/examples/window_icon.rs @@ -19,7 +19,7 @@ fn main() { let icon = load_icon(Path::new(path)); - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let window = WindowBuilder::new() .with_title("An iconic window!") diff --git a/examples/window_run_return.rs b/examples/window_run_return.rs index 6bedfcd82c..498b225721 100644 --- a/examples/window_run_return.rs +++ b/examples/window_run_return.rs @@ -18,7 +18,7 @@ fn main() { platform::run_return::EventLoopExtRunReturn, window::WindowBuilder, }; - let mut event_loop = EventLoop::new(); + let mut event_loop = EventLoop::new().unwrap(); SimpleLogger::new().init().unwrap(); let _window = WindowBuilder::new() From b0264e9fac53eaf7a8fa96a09f33cb78a607d8f5 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 12:39:39 -0500 Subject: [PATCH 09/21] Added Creation Error --- src/error.rs | 5 +++++ src/event_loop.rs | 9 +++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index c039f3b868..075d74e390 100644 --- a/src/error.rs +++ b/src/error.rs @@ -80,3 +80,8 @@ impl fmt::Display for NotSupportedError { impl error::Error for OsError {} impl error::Error for ExternalError {} impl error::Error for NotSupportedError {} + +#[derive(Debug)] +pub enum CreationError { + EventLoop(String), +} \ No newline at end of file diff --git a/src/event_loop.rs b/src/event_loop.rs index b9b0d0dc18..eebd09d742 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -14,6 +14,7 @@ use std::marker::PhantomData; use std::ops::Deref; use std::{error, fmt}; +use crate::error::CreationError; use crate::{event::Event, monitor::MonitorHandle, platform_impl}; /// Provides a way to retrieve events from the system and from the windows that were registered to @@ -93,11 +94,11 @@ impl EventLoopBuilder { /// /// - **iOS:** Can only be called on the main thread. #[inline] - pub fn build(&mut self) -> Result, String> { + pub fn build(&mut self) -> Result, CreationError> { Ok(EventLoop { event_loop: match platform_impl::EventLoop::new(&self.platform_specific) { Ok(event_loop) => event_loop, - Err(err) => return Err(format!("Error creating event loop: {}", err)), + Err(err) => return Err(CreationError::EventLoop(format!("Error creating event loop: {}", err))), }, _marker: PhantomData, }) @@ -180,14 +181,14 @@ impl Default for ControlFlow { impl EventLoop<()> { /// Alias for `EventLoopBuilder::new().build()`. #[inline] - pub fn new() -> Result, String> { + pub fn new() -> Result, CreationError> { EventLoopBuilder::new().build() } } impl EventLoop { #[deprecated = "Use `EventLoopBuilder::::with_user_event().build()` instead."] - pub fn with_user_event() -> Result, String> { + pub fn with_user_event() -> Result, CreationError> { EventLoopBuilder::::with_user_event().build() } From df250b4e09586a54557c8370f0c8fb2fc24ccc85 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 12:45:57 -0500 Subject: [PATCH 10/21] Removed Empty Line --- examples/custom_events.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/custom_events.rs b/examples/custom_events.rs index b0ea050ae7..b2e2a56265 100644 --- a/examples/custom_events.rs +++ b/examples/custom_events.rs @@ -13,7 +13,6 @@ fn main() { } SimpleLogger::new().init().unwrap(); - let event_loop = EventLoopBuilder::::with_user_event().build().unwrap(); let _window = WindowBuilder::new() From 74b180e424f996b609dfc97cacfa2a3aff5f423b Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 12:46:16 -0500 Subject: [PATCH 11/21] Implemented Display + New Error --- src/error.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/error.rs b/src/error.rs index 075d74e390..8a7901e9e6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -84,4 +84,14 @@ impl error::Error for NotSupportedError {} #[derive(Debug)] pub enum CreationError { EventLoop(String), + InitializeBackend(String), +} + +impl fmt::Display for CreationError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + match self { + CreationError::EventLoop(s) => f.write_str(s), + CreationError::InitializeBackend(s) => f.write_str(s), + } + } } \ No newline at end of file From 0f96200b955a175fbd9bd40a3385a92cc0c42050 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 12:46:41 -0500 Subject: [PATCH 12/21] Changed an Err to a panic + Error Enum --- src/platform_impl/linux/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index f07811c3e5..449ee708b8 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -25,7 +25,7 @@ pub use self::x11::XNotSupported; use self::x11::{ffi::XVisualInfo, util::WindowType as XWindowType, XConnection, XError}; use crate::{ dpi::{PhysicalPosition, PhysicalSize, Position, Size}, - error::{ExternalError, NotSupportedError, OsError as RootOsError}, + error::{ExternalError, NotSupportedError, OsError as RootOsError, CreationError}, event::Event, event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW}, icon::Icon, @@ -606,13 +606,13 @@ impl Clone for EventLoopProxy { } impl EventLoop { - pub(crate) fn new(attributes: &PlatformSpecificEventLoopAttributes) -> Result { + pub(crate) fn new(attributes: &PlatformSpecificEventLoopAttributes) -> Result { if !attributes.any_thread && !is_main_thread() { - return Err( + panic!( "Initializing the event loop outside of the main thread is a significant \ cross-platform compatibility hazard. If you absolutely need to create an \ EventLoop on a different thread, you can use the \ - `EventLoopBuilderExtUnix::any_thread` function.".to_string() + `EventLoopBuilderExtUnix::any_thread` function." ); } @@ -635,7 +635,7 @@ impl EventLoop { #[cfg(feature = "x11")] return match EventLoop::new_x11_any_thread() { Ok(event_loop) => Ok(event_loop), - Err(e) => Err(format!("Failed to initialize X11 Backend: {}", e)), + Err(e) => Err(CreationError::InitializeBackend(format!("Failed to initialize X11 Backend: {}", e))), }; #[cfg(not(feature = "x11"))] @@ -645,7 +645,7 @@ impl EventLoop { #[cfg(feature = "wayland")] return match EventLoop::new_wayland_any_thread() { Ok(event_loop) => Ok(event_loop), - Err(e) => Err(format!("Failed to initialize Wayland Backend: {}", e)), + Err(e) => Err(CreationError::InitializeBackend(format!("Failed to initialize Wayland Backend: {}", e))), }; #[cfg(not(feature = "wayland"))] Err("wayland feature is not enabled"); From 6641d3d5254392b9aae0574cbbd8976bb4f5f873 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 12:39:39 -0500 Subject: [PATCH 13/21] Added Creation Error --- src/error.rs | 5 +++++ src/event_loop.rs | 9 +++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index c039f3b868..075d74e390 100644 --- a/src/error.rs +++ b/src/error.rs @@ -80,3 +80,8 @@ impl fmt::Display for NotSupportedError { impl error::Error for OsError {} impl error::Error for ExternalError {} impl error::Error for NotSupportedError {} + +#[derive(Debug)] +pub enum CreationError { + EventLoop(String), +} \ No newline at end of file diff --git a/src/event_loop.rs b/src/event_loop.rs index b9b0d0dc18..eebd09d742 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -14,6 +14,7 @@ use std::marker::PhantomData; use std::ops::Deref; use std::{error, fmt}; +use crate::error::CreationError; use crate::{event::Event, monitor::MonitorHandle, platform_impl}; /// Provides a way to retrieve events from the system and from the windows that were registered to @@ -93,11 +94,11 @@ impl EventLoopBuilder { /// /// - **iOS:** Can only be called on the main thread. #[inline] - pub fn build(&mut self) -> Result, String> { + pub fn build(&mut self) -> Result, CreationError> { Ok(EventLoop { event_loop: match platform_impl::EventLoop::new(&self.platform_specific) { Ok(event_loop) => event_loop, - Err(err) => return Err(format!("Error creating event loop: {}", err)), + Err(err) => return Err(CreationError::EventLoop(format!("Error creating event loop: {}", err))), }, _marker: PhantomData, }) @@ -180,14 +181,14 @@ impl Default for ControlFlow { impl EventLoop<()> { /// Alias for `EventLoopBuilder::new().build()`. #[inline] - pub fn new() -> Result, String> { + pub fn new() -> Result, CreationError> { EventLoopBuilder::new().build() } } impl EventLoop { #[deprecated = "Use `EventLoopBuilder::::with_user_event().build()` instead."] - pub fn with_user_event() -> Result, String> { + pub fn with_user_event() -> Result, CreationError> { EventLoopBuilder::::with_user_event().build() } From 20fdb88f07d4549d5887962bf34eefe4704739a0 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 12:45:57 -0500 Subject: [PATCH 14/21] Removed Empty Line --- examples/custom_events.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/custom_events.rs b/examples/custom_events.rs index b0ea050ae7..b2e2a56265 100644 --- a/examples/custom_events.rs +++ b/examples/custom_events.rs @@ -13,7 +13,6 @@ fn main() { } SimpleLogger::new().init().unwrap(); - let event_loop = EventLoopBuilder::::with_user_event().build().unwrap(); let _window = WindowBuilder::new() From fd8843bbbe0f9cf26759bdabc3345017218722f6 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 12:46:16 -0500 Subject: [PATCH 15/21] Implemented Display + New Error --- src/error.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/error.rs b/src/error.rs index 075d74e390..8a7901e9e6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -84,4 +84,14 @@ impl error::Error for NotSupportedError {} #[derive(Debug)] pub enum CreationError { EventLoop(String), + InitializeBackend(String), +} + +impl fmt::Display for CreationError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + match self { + CreationError::EventLoop(s) => f.write_str(s), + CreationError::InitializeBackend(s) => f.write_str(s), + } + } } \ No newline at end of file From a3b10c4ff9bed8954d4fe5e573e42df85324b7f3 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 12:46:41 -0500 Subject: [PATCH 16/21] Changed an Err to a panic + Error Enum --- src/platform_impl/linux/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index f07811c3e5..449ee708b8 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -25,7 +25,7 @@ pub use self::x11::XNotSupported; use self::x11::{ffi::XVisualInfo, util::WindowType as XWindowType, XConnection, XError}; use crate::{ dpi::{PhysicalPosition, PhysicalSize, Position, Size}, - error::{ExternalError, NotSupportedError, OsError as RootOsError}, + error::{ExternalError, NotSupportedError, OsError as RootOsError, CreationError}, event::Event, event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW}, icon::Icon, @@ -606,13 +606,13 @@ impl Clone for EventLoopProxy { } impl EventLoop { - pub(crate) fn new(attributes: &PlatformSpecificEventLoopAttributes) -> Result { + pub(crate) fn new(attributes: &PlatformSpecificEventLoopAttributes) -> Result { if !attributes.any_thread && !is_main_thread() { - return Err( + panic!( "Initializing the event loop outside of the main thread is a significant \ cross-platform compatibility hazard. If you absolutely need to create an \ EventLoop on a different thread, you can use the \ - `EventLoopBuilderExtUnix::any_thread` function.".to_string() + `EventLoopBuilderExtUnix::any_thread` function." ); } @@ -635,7 +635,7 @@ impl EventLoop { #[cfg(feature = "x11")] return match EventLoop::new_x11_any_thread() { Ok(event_loop) => Ok(event_loop), - Err(e) => Err(format!("Failed to initialize X11 Backend: {}", e)), + Err(e) => Err(CreationError::InitializeBackend(format!("Failed to initialize X11 Backend: {}", e))), }; #[cfg(not(feature = "x11"))] @@ -645,7 +645,7 @@ impl EventLoop { #[cfg(feature = "wayland")] return match EventLoop::new_wayland_any_thread() { Ok(event_loop) => Ok(event_loop), - Err(e) => Err(format!("Failed to initialize Wayland Backend: {}", e)), + Err(e) => Err(CreationError::InitializeBackend(format!("Failed to initialize Wayland Backend: {}", e))), }; #[cfg(not(feature = "wayland"))] Err("wayland feature is not enabled"); From e5d93241a6b3b7b5a8937909490787c9d04efdba Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 13:00:44 -0500 Subject: [PATCH 17/21] Changed new to automatically unwrap --- src/event_loop.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index eebd09d742..3a5102e6c1 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -179,9 +179,14 @@ impl Default for ControlFlow { } impl EventLoop<()> { - /// Alias for `EventLoopBuilder::new().build()`. + /// Alias for `EventLoopBuilder::new().build().unwrap()`. #[inline] - pub fn new() -> Result, CreationError> { + pub fn new() -> EventLoop<()> { + EventLoopBuilder::new().build().unwrap() + } + + /// Alias for `EventLoopBuilder::new().build()`. + pub fn new_result() -> Result, CreationError> { EventLoopBuilder::new().build() } } From c46c783157a313b30d608ceb1ec48ae84227852b Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 13:05:02 -0500 Subject: [PATCH 18/21] Changed the Examples to not be unwrap (again) --- examples/control_flow.rs | 2 +- examples/cursor.rs | 2 +- examples/cursor_grab.rs | 2 +- examples/drag_window.rs | 2 +- examples/fullscreen.rs | 2 +- examples/handling_close.rs | 2 +- examples/min_max_size.rs | 2 +- examples/minimize.rs | 2 +- examples/monitor_list.rs | 2 +- examples/mouse_wheel.rs | 2 +- examples/multithreaded.rs | 2 +- examples/multiwindow.rs | 2 +- examples/request_redraw.rs | 2 +- examples/request_redraw_threaded.rs | 2 +- examples/resizable.rs | 2 +- examples/set_ime_position.rs | 2 +- examples/timer.rs | 2 +- examples/transparent.rs | 2 +- examples/video_modes.rs | 2 +- examples/web.rs | 2 +- examples/window.rs | 2 +- examples/window_debug.rs | 2 +- examples/window_icon.rs | 2 +- examples/window_run_return.rs | 2 +- 24 files changed, 24 insertions(+), 24 deletions(-) diff --git a/examples/control_flow.rs b/examples/control_flow.rs index f28ec5830a..cbb445eebb 100644 --- a/examples/control_flow.rs +++ b/examples/control_flow.rs @@ -26,7 +26,7 @@ fn main() { println!("Press 'R' to toggle request_redraw() calls."); println!("Press 'Esc' to close the window."); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("Press 1, 2, 3 to change control flow mode. Press R to toggle redraw requests.") .build(&event_loop) diff --git a/examples/cursor.rs b/examples/cursor.rs index 40d99440b4..6ec8a05933 100644 --- a/examples/cursor.rs +++ b/examples/cursor.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new().build(&event_loop).unwrap(); window.set_title("A fantastic window!"); diff --git a/examples/cursor_grab.rs b/examples/cursor_grab.rs index 430a83b599..90a94764de 100644 --- a/examples/cursor_grab.rs +++ b/examples/cursor_grab.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("Super Cursor Grab'n'Hide Simulator 9000") diff --git a/examples/drag_window.rs b/examples/drag_window.rs index c3942b9171..a408c7c722 100644 --- a/examples/drag_window.rs +++ b/examples/drag_window.rs @@ -9,7 +9,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window_1 = WindowBuilder::new().build(&event_loop).unwrap(); let window_2 = WindowBuilder::new().build(&event_loop).unwrap(); diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs index f24337638d..90d41ece12 100644 --- a/examples/fullscreen.rs +++ b/examples/fullscreen.rs @@ -8,7 +8,7 @@ use winit::window::{Fullscreen, WindowBuilder}; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); print!("Please choose the fullscreen mode: (1) exclusive, (2) borderless: "); stdout().flush().unwrap(); diff --git a/examples/handling_close.rs b/examples/handling_close.rs index 46c055c566..8334c1773f 100644 --- a/examples/handling_close.rs +++ b/examples/handling_close.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let _window = WindowBuilder::new() .with_title("Your faithful window") diff --git a/examples/min_max_size.rs b/examples/min_max_size.rs index 1e5f667f4a..9a58ed6c69 100644 --- a/examples/min_max_size.rs +++ b/examples/min_max_size.rs @@ -8,7 +8,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new().build(&event_loop).unwrap(); diff --git a/examples/minimize.rs b/examples/minimize.rs index 02c3b009d7..eb02a752c9 100644 --- a/examples/minimize.rs +++ b/examples/minimize.rs @@ -7,7 +7,7 @@ use winit::window::WindowBuilder; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/monitor_list.rs b/examples/monitor_list.rs index 1cc1cc8e6b..9c8b77e61c 100644 --- a/examples/monitor_list.rs +++ b/examples/monitor_list.rs @@ -3,7 +3,7 @@ use winit::{event_loop::EventLoop, window::WindowBuilder}; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new().build(&event_loop).unwrap(); dbg!(window.available_monitors().collect::>()); diff --git a/examples/mouse_wheel.rs b/examples/mouse_wheel.rs index f5b2a9a615..e61b64af8f 100644 --- a/examples/mouse_wheel.rs +++ b/examples/mouse_wheel.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("Mouse Wheel events") diff --git a/examples/multithreaded.rs b/examples/multithreaded.rs index 7fb7303a89..ba2b9ad240 100644 --- a/examples/multithreaded.rs +++ b/examples/multithreaded.rs @@ -14,7 +14,7 @@ fn main() { const WINDOW_SIZE: PhysicalSize = PhysicalSize::new(600, 400); SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let mut window_senders = HashMap::with_capacity(WINDOW_COUNT); for _ in 0..WINDOW_COUNT { let window = WindowBuilder::new() diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs index 0c2c02aa14..e946537296 100644 --- a/examples/multiwindow.rs +++ b/examples/multiwindow.rs @@ -9,7 +9,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let mut windows = HashMap::new(); for _ in 0..3 { diff --git a/examples/request_redraw.rs b/examples/request_redraw.rs index de7919c024..163f6a14d2 100644 --- a/examples/request_redraw.rs +++ b/examples/request_redraw.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/request_redraw_threaded.rs b/examples/request_redraw_threaded.rs index 748ac4c07c..7a28c23b55 100644 --- a/examples/request_redraw_threaded.rs +++ b/examples/request_redraw_threaded.rs @@ -10,7 +10,7 @@ fn main() { }; SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/resizable.rs b/examples/resizable.rs index 01404376fc..17892d8741 100644 --- a/examples/resizable.rs +++ b/examples/resizable.rs @@ -8,7 +8,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let mut resizable = false; diff --git a/examples/set_ime_position.rs b/examples/set_ime_position.rs index 6ff90065f7..2cd376da0f 100644 --- a/examples/set_ime_position.rs +++ b/examples/set_ime_position.rs @@ -8,7 +8,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new().build(&event_loop).unwrap(); window.set_title("A fantastic window!"); diff --git a/examples/timer.rs b/examples/timer.rs index 7e84514db2..7bbb9685f4 100644 --- a/examples/timer.rs +++ b/examples/timer.rs @@ -10,7 +10,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let _window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/transparent.rs b/examples/transparent.rs index 2adb07b8fb..c9937cd4cd 100644 --- a/examples/transparent.rs +++ b/examples/transparent.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_decorations(false) diff --git a/examples/video_modes.rs b/examples/video_modes.rs index 063b0e0135..341f43855b 100644 --- a/examples/video_modes.rs +++ b/examples/video_modes.rs @@ -3,7 +3,7 @@ use winit::event_loop::EventLoop; fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let monitor = match event_loop.primary_monitor() { Some(monitor) => monitor, None => { diff --git a/examples/web.rs b/examples/web.rs index 2c03477bee..83ccc45ee2 100644 --- a/examples/web.rs +++ b/examples/web.rs @@ -5,7 +5,7 @@ use winit::{ }; pub fn main() { - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/window.rs b/examples/window.rs index c4633b5990..783578de65 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -7,7 +7,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/window_debug.rs b/examples/window_debug.rs index 6a543fb179..577ad5cd73 100644 --- a/examples/window_debug.rs +++ b/examples/window_debug.rs @@ -10,7 +10,7 @@ use winit::{ fn main() { SimpleLogger::new().init().unwrap(); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("A fantastic window!") diff --git a/examples/window_icon.rs b/examples/window_icon.rs index 29a36259fa..6c79625505 100644 --- a/examples/window_icon.rs +++ b/examples/window_icon.rs @@ -19,7 +19,7 @@ fn main() { let icon = load_icon(Path::new(path)); - let event_loop = EventLoop::new().unwrap(); + let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_title("An iconic window!") diff --git a/examples/window_run_return.rs b/examples/window_run_return.rs index 498b225721..6bedfcd82c 100644 --- a/examples/window_run_return.rs +++ b/examples/window_run_return.rs @@ -18,7 +18,7 @@ fn main() { platform::run_return::EventLoopExtRunReturn, window::WindowBuilder, }; - let mut event_loop = EventLoop::new().unwrap(); + let mut event_loop = EventLoop::new(); SimpleLogger::new().init().unwrap(); let _window = WindowBuilder::new() From beae37a32f20d67526e725ae639be43d62840824 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 23:12:15 -0500 Subject: [PATCH 19/21] Removed new_result --- src/event_loop.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 3a5102e6c1..1601e34590 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -184,11 +184,6 @@ impl EventLoop<()> { pub fn new() -> EventLoop<()> { EventLoopBuilder::new().build().unwrap() } - - /// Alias for `EventLoopBuilder::new().build()`. - pub fn new_result() -> Result, CreationError> { - EventLoopBuilder::new().build() - } } impl EventLoop { From a8c811245ff1d790937dbfa1b6b5a05ff73db903 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 23:14:02 -0500 Subject: [PATCH 20/21] No longer converting Backend into EventLoop error --- src/event_loop.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 1601e34590..42b13cbf62 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -98,7 +98,7 @@ impl EventLoopBuilder { Ok(EventLoop { event_loop: match platform_impl::EventLoop::new(&self.platform_specific) { Ok(event_loop) => event_loop, - Err(err) => return Err(CreationError::EventLoop(format!("Error creating event loop: {}", err))), + Err(err) => return Err(err), }, _marker: PhantomData, }) From 15cbf927f53b9ff812e4646f7990b3d1f66709a1 Mon Sep 17 00:00:00 2001 From: lonnonjamesd Date: Mon, 21 Feb 2022 23:15:44 -0500 Subject: [PATCH 21/21] Kept Function Signature since deprecated --- src/event_loop.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 42b13cbf62..2d9ab16e56 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -188,8 +188,11 @@ impl EventLoop<()> { impl EventLoop { #[deprecated = "Use `EventLoopBuilder::::with_user_event().build()` instead."] - pub fn with_user_event() -> Result, CreationError> { - EventLoopBuilder::::with_user_event().build() + pub fn with_user_event() -> EventLoop { + match EventLoopBuilder::::with_user_event().build() { + Ok(event_loop) => event_loop, + Err(err) => panic!("Failed to create event loop: {}", err), + } } /// Hijacks the calling thread and initializes the winit event loop with the provided