From f88fba0253b45de6a2ac0c3cbcf01f50503c9396 Mon Sep 17 00:00:00 2001 From: MultisampledNight Date: Fri, 17 Dec 2021 19:06:24 +0100 Subject: [PATCH] Change i32 as exit code to u8 This avoids nasty surprises with negative numbers on some unix-alikes due to two's complement. --- src/event_loop.rs | 8 +++++--- src/platform/run_return.rs | 7 ++++--- src/platform_impl/linux/mod.rs | 2 +- src/platform_impl/linux/wayland/event_loop/mod.rs | 4 ++-- src/platform_impl/linux/x11/mod.rs | 4 ++-- src/platform_impl/windows/event_loop.rs | 4 ++-- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 756545f4a..8409514b0 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -87,9 +87,11 @@ pub enum ControlFlow { WaitUntil(Instant), /// Send a `LoopDestroyed` event and stop the event loop. This variant is *sticky* - once set, /// `control_flow` cannot be changed from `Exit`, and any future attempts to do so will result - /// in the `control_flow` parameter being reset to `Exit`. The contained number will be used as - /// exit code, if the platform supports that (this means _not_ ios, android and wasm32). - Exit(i32), + /// in the `control_flow` parameter being reset to `Exit`. + /// The contained number will be used as exit code, if the platform supports that (this + /// _excludes_ ios, android and wasm32), it's an [`u8`] to prevent some weird surprises as + /// described in [`std::process::exit`]. + Exit(u8), } impl Default for ControlFlow { diff --git a/src/platform/run_return.rs b/src/platform/run_return.rs index 1e1d0d831..91a359c09 100644 --- a/src/platform/run_return.rs +++ b/src/platform/run_return.rs @@ -22,7 +22,8 @@ pub trait EventLoopExtRunReturn { /// Initializes the `winit` event loop. /// /// Unlike `run`, this function accepts non-`'static` (i.e. non-`move`) closures and returns - /// control flow to the caller when `control_flow` is set to `ControlFlow::Exit`. + /// control flow to the caller when `control_flow` is set to `ControlFlow::Exit`, where its + /// content representing the exit code is the return value. /// /// # Caveats /// Despite its appearance at first glance, this is *not* a perfect replacement for @@ -33,7 +34,7 @@ pub trait EventLoopExtRunReturn { /// underlying OS APIs, which cannot be hidden by `winit` without severe stability repercussions. /// /// You are strongly encouraged to use `run`, unless the use of this is absolutely necessary. - fn run_return(&mut self, event_handler: F) -> i32 + fn run_return(&mut self, event_handler: F) -> u8 where F: FnMut( Event<'_, Self::UserEvent>, @@ -45,7 +46,7 @@ pub trait EventLoopExtRunReturn { impl EventLoopExtRunReturn for EventLoop { type UserEvent = T; - fn run_return(&mut self, event_handler: F) -> i32 + fn run_return(&mut self, event_handler: F) -> u8 where F: FnMut( Event<'_, Self::UserEvent>, diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index d4e803517..bd832dadd 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -655,7 +655,7 @@ impl EventLoop { x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy) } - pub fn run_return(&mut self, callback: F) -> i32 + pub fn run_return(&mut self, callback: F) -> u8 where F: FnMut(crate::event::Event<'_, T>, &RootELW, &mut ControlFlow), { diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index 695cd037c..3b4b18f5f 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -207,10 +207,10 @@ impl EventLoop { F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget, &mut ControlFlow) + 'static, { let exit_code = self.run_return(callback); - process::exit(exit_code); + process::exit(i32::from(exit_code)); } - pub fn run_return(&mut self, mut callback: F) -> i32 + pub fn run_return(&mut self, mut callback: F) -> u8 where F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget, &mut ControlFlow), { diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index e655e6da3..582cdc46d 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -258,7 +258,7 @@ impl EventLoop { &self.target } - pub fn run_return(&mut self, mut callback: F) -> i32 + pub fn run_return(&mut self, mut callback: F) -> u8 where F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), { @@ -391,7 +391,7 @@ impl EventLoop { F: 'static + FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), { let exit_code = self.run_return(callback); - ::std::process::exit(exit_code); + ::std::process::exit(i32::from(exit_code)); } fn drain_events(&mut self, callback: &mut F, control_flow: &mut ControlFlow) diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 0d666b106..f2b2fca16 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -189,10 +189,10 @@ impl EventLoop { F: 'static + FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), { let exit_code = self.run_return(event_handler); - ::std::process::exit(exit_code); + ::std::process::exit(i32::from(exit_code)); } - pub fn run_return(&mut self, mut event_handler: F) -> i32 + pub fn run_return(&mut self, mut event_handler: F) -> u8 where F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), {