Skip to content

Commit

Permalink
Re-work event loop run() API so it can return a Result
Browse files Browse the repository at this point in the history
This re-works the portable `run()` API that consumes the `EventLoop` and
runs the loop on the calling thread until the app exits.

This can be supported across _all_ platforms and compared to the
previous `run() -> !` API is now able to return a `Result` status on all
platforms except iOS and Web. Fixes: rust-windowing#2709

By moving away from `run() -> !` we stop calling `std::process::exit()`
internally as a means to kill the process without returning which means
it's possible to return an exit status and applications can return from
their `main()` function normally.

This also fixes Android support where an Activity runs in a thread but
we can't assume to have full ownership of the process (other services
could be running in separate threads).

Additionally all examples have generally been updated so that `main()`
returns a `Result` from `run()`

Fixes: rust-windowing#2709
  • Loading branch information
rib committed Jun 25, 2023
1 parent 40fa2b1 commit 63c5f4c
Show file tree
Hide file tree
Showing 37 changed files with 88 additions and 111 deletions.
2 changes: 1 addition & 1 deletion examples/child_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod fill;

#[cfg(any(x11_platform, macos_platform, windows_platform))]
fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
use std::collections::HashMap;

use raw_window_handle::HasRawWindowHandle;
Expand Down
4 changes: 2 additions & 2 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum Mode {
const WAIT_TIME: time::Duration = time::Duration::from_millis(100);
const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(100);

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();

println!("Press '1' to switch to Wait mode.");
Expand Down Expand Up @@ -122,5 +122,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -54,7 +54,7 @@ fn main() {
}
_ => (),
}
});
})
}

const CURSORS: &[CursorIcon] = &[
Expand Down
4 changes: 2 additions & 2 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -73,5 +73,5 @@ fn main() {
Event::RedrawRequested(_) => fill::fill_window(&window),
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/custom_events.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::single_match)]

#[cfg(not(wasm_platform))]
fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
Expand Down Expand Up @@ -52,7 +52,7 @@ fn main() {
}
_ => (),
}
});
})
}

#[cfg(wasm_platform)]
Expand Down
4 changes: 2 additions & 2 deletions examples/drag_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -69,7 +69,7 @@ fn main() {
}
}
_ => (),
});
})
}

fn name_windows(window_id: WindowId, switched: bool, window_1: &Window, window_2: &Window) {
Expand Down
4 changes: 2 additions & 2 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use winit::platform::macos::WindowExtMacOS;
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -131,5 +131,5 @@ fn main() {
}
_ => {}
}
});
})
}
4 changes: 2 additions & 2 deletions examples/handling_close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -87,5 +87,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/ime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new()
.with_level(LevelFilter::Trace)
.init()
Expand Down Expand Up @@ -105,5 +105,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/key_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
}

#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
#[path = "util/fill.rs"]
mod fill;

Expand Down Expand Up @@ -61,5 +61,5 @@ fn main() {
}
_ => (),
};
});
})
}
4 changes: 2 additions & 2 deletions examples/mouse_wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -64,5 +64,5 @@ In other words, the deltas indicate the direction in which to move the content (
}
_ => (),
}
});
})
}
2 changes: 1 addition & 1 deletion examples/multithreaded.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::single_match)]

#[cfg(not(wasm_platform))]
fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};

use simple_logger::SimpleLogger;
Expand Down
2 changes: 1 addition & 1 deletion examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down
4 changes: 2 additions & 2 deletions examples/request_redraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -41,5 +41,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/request_redraw_threaded.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::single_match)]

#[cfg(not(wasm_platform))]
fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
use std::{sync::Arc, thread, time};

use simple_logger::SimpleLogger;
Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() {
}
_ => (),
}
});
})
}

#[cfg(wasm_platform)]
Expand Down
4 changes: 2 additions & 2 deletions examples/resizable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -53,5 +53,5 @@ fn main() {
}
_ => (),
};
});
})
}
4 changes: 2 additions & 2 deletions examples/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -75,5 +75,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -47,5 +47,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/touchpad_gestures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -47,5 +47,5 @@ fn main() {
} else if let Event::RedrawRequested(_) = event {
fill::fill_window(&window);
}
});
})
}
4 changes: 2 additions & 2 deletions examples/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand All @@ -36,5 +36,5 @@ fn main() {
}
_ => (),
}
});
})
}
6 changes: 3 additions & 3 deletions examples/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use winit::{
window::{Fullscreen, WindowBuilder},
};

pub fn main() {
pub fn main() -> std::result::Result<(), impl std::error::Error> {
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn main() {
}
_ => (),
}
});
})
}

#[cfg(wasm_platform)]
Expand All @@ -66,7 +66,7 @@ mod wasm {
console_log::init_with_level(log::Level::Debug).expect("error initializing logger");

#[allow(clippy::main_recursion)]
super::main();
let _ = super::main();
}

pub fn insert_canvas_and_create_log_list(window: &Window) -> web_sys::Element {
Expand Down
2 changes: 1 addition & 1 deletion examples/web_aspect_ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This example demonstrates the desired future functionality which will possibly b
// Render once with the size info we currently have
render_circle(&canvas, window.inner_size());

event_loop.run(move |event, _, control_flow| {
let _ = event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;

match event {
Expand Down
4 changes: 2 additions & 2 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand All @@ -37,5 +37,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/window_buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -71,5 +71,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/window_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use winit::{
#[path = "util/fill.rs"]
mod fill;

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -139,5 +139,5 @@ fn main() {
}
_ => (),
}
});
})
}
Loading

0 comments on commit 63c5f4c

Please sign in to comment.