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 18, 2023
1 parent c2cc308 commit 23e7376
Show file tree
Hide file tree
Showing 35 changed files with 87 additions and 108 deletions.
2 changes: 1 addition & 1 deletion examples/child_window.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[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 @@ -20,7 +20,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 @@ -113,5 +113,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use winit::{
window::{CursorIcon, WindowBuilder},
};

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

Expand Down Expand Up @@ -48,7 +48,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 @@ -8,7 +8,7 @@ use winit::{
window::{CursorGrabMode, WindowBuilder},
};

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,5 +69,5 @@ fn main() {
},
_ => (),
}
});
})
}
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 @@ -46,7 +46,7 @@ fn main() {
} => control_flow.set_exit(),
_ => (),
}
});
})
}

#[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 @@ -8,7 +8,7 @@ use winit::{
window::{Window, WindowBuilder, WindowId},
};

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

Expand Down Expand Up @@ -59,7 +59,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 @@ -9,7 +9,7 @@ use winit::window::{Fullscreen, WindowBuilder};
#[cfg(target_os = "macos")]
use winit::platform::macos::WindowExtMacOS;

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

Expand Down Expand Up @@ -125,5 +125,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 @@ -8,7 +8,7 @@ use winit::{
window::WindowBuilder,
};

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

Expand Down Expand Up @@ -81,5 +81,5 @@ fn main() {
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/ime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winit::{
window::{ImePurpose, WindowBuilder},
};

fn main() {
fn main() -> std::result::Result<(), impl std::error::Error> {
SimpleLogger::new()
.with_level(LevelFilter::Trace)
.init()
Expand Down Expand Up @@ -99,5 +99,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> {
simple_logger::SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

Expand Down Expand Up @@ -55,5 +55,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 @@ -7,7 +7,7 @@ use winit::{
window::WindowBuilder,
};

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

Expand Down Expand Up @@ -58,5 +58,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 @@ -10,7 +10,7 @@ use winit::{
window::Window,
};

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 @@ -7,7 +7,7 @@ use winit::{
window::WindowBuilder,
};

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

Expand Down Expand Up @@ -37,5 +37,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::{thread, time};

use simple_logger::SimpleLogger;
Expand Down Expand Up @@ -39,7 +39,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 @@ -9,7 +9,7 @@ use winit::{
window::WindowBuilder,
};

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/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use winit::{
window::{Theme, WindowBuilder},
};

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

Expand Down Expand Up @@ -68,5 +68,5 @@ fn main() {
},
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winit::{
window::WindowBuilder,
};

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

Expand Down Expand Up @@ -38,5 +38,5 @@ fn main() {
} => control_flow.set_exit(),
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/touchpad_gestures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use winit::{
window::WindowBuilder,
};

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

Expand Down Expand Up @@ -42,5 +42,5 @@ fn main() {
_ => (),
}
}
});
})
}
4 changes: 2 additions & 2 deletions examples/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use winit::{
window::WindowBuilder,
};

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

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

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

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

#[cfg(wasm_platform)]
Expand Down
9 changes: 6 additions & 3 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use winit::{
window::WindowBuilder,
};

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

Expand All @@ -25,11 +25,14 @@ fn main() {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => control_flow.set_exit(),
} if window_id == window.id() => {
log::warn!("Close button pressed");
control_flow.set_exit()
}
Event::MainEventsCleared => {
window.request_redraw();
}
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/window_buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use winit::{
window::{WindowBuilder, WindowButtons},
};

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

Expand Down Expand Up @@ -65,5 +65,5 @@ fn main() {
} if window_id == window.id() => control_flow.set_exit(),
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/window_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use winit::{
window::{Fullscreen, WindowBuilder},
};

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

Expand Down Expand Up @@ -133,5 +133,5 @@ fn main() {
} if window_id == window.id() => control_flow.set_exit(),
_ => (),
}
});
})
}
4 changes: 2 additions & 2 deletions examples/window_drag_resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winit::{

const BORDER: f64 = 8.0;

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

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

fn cursor_direction_icon(resize_direction: Option<ResizeDirection>) -> CursorIcon {
Expand Down
Loading

0 comments on commit 23e7376

Please sign in to comment.