Skip to content

Commit 0d366ff

Browse files
ribkchibisov
authored andcommitted
Re-work event loop run() API so it can return a Result
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: #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: #2709
1 parent a6f414d commit 0d366ff

39 files changed

+98
-122
lines changed

examples/child_window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod fill;
44

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

99
use raw_window_handle::HasRawWindowHandle;

examples/control_flow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum Mode {
2727
const WAIT_TIME: time::Duration = time::Duration::from_millis(100);
2828
const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(100);
2929

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

3333
println!("Press '1' to switch to Wait mode.");
@@ -122,5 +122,5 @@ fn main() {
122122
}
123123
_ => (),
124124
}
125-
});
125+
})
126126
}

examples/cursor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use winit::{
1010
#[path = "util/fill.rs"]
1111
mod fill;
1212

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

@@ -54,7 +54,7 @@ fn main() {
5454
}
5555
_ => (),
5656
}
57-
});
57+
})
5858
}
5959

6060
const CURSORS: &[CursorIcon] = &[

examples/cursor_grab.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use winit::{
1111
#[path = "util/fill.rs"]
1212
mod fill;
1313

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

@@ -73,5 +73,5 @@ fn main() {
7373
Event::RedrawRequested(_) => fill::fill_window(&window),
7474
_ => (),
7575
}
76-
});
76+
})
7777
}

examples/custom_events.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::single_match)]
22

33
#[cfg(not(wasm_platform))]
4-
fn main() {
4+
fn main() -> Result<(), impl std::error::Error> {
55
use simple_logger::SimpleLogger;
66
use winit::{
77
event::{Event, WindowEvent},
@@ -52,7 +52,7 @@ fn main() {
5252
}
5353
_ => (),
5454
}
55-
});
55+
})
5656
}
5757

5858
#[cfg(wasm_platform)]

examples/drag_window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use winit::{
1111
#[path = "util/fill.rs"]
1212
mod fill;
1313

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

@@ -69,7 +69,7 @@ fn main() {
6969
}
7070
}
7171
_ => (),
72-
});
72+
})
7373
}
7474

7575
fn name_windows(window_id: WindowId, switched: bool, window_1: &Window, window_2: &Window) {

examples/fullscreen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use winit::platform::macos::WindowExtMacOS;
1212
#[path = "util/fill.rs"]
1313
mod fill;
1414

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

@@ -131,5 +131,5 @@ fn main() {
131131
}
132132
_ => {}
133133
}
134-
});
134+
})
135135
}

examples/handling_close.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use winit::{
1111
#[path = "util/fill.rs"]
1212
mod fill;
1313

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

@@ -87,5 +87,5 @@ fn main() {
8787
}
8888
_ => (),
8989
}
90-
});
90+
})
9191
}

examples/ime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use winit::{
1313
#[path = "util/fill.rs"]
1414
mod fill;
1515

16-
fn main() {
16+
fn main() -> Result<(), impl std::error::Error> {
1717
SimpleLogger::new()
1818
.with_level(LevelFilter::Trace)
1919
.init()
@@ -105,5 +105,5 @@ fn main() {
105105
}
106106
_ => (),
107107
}
108-
});
108+
})
109109
}

examples/key_binding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {
1717
}
1818

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

@@ -61,5 +61,5 @@ fn main() {
6161
}
6262
_ => (),
6363
};
64-
});
64+
})
6565
}

0 commit comments

Comments
 (0)