Remove EventLoopExtRunReturn

This commit is contained in:
Robert Bragg 2023-06-18 12:59:53 +01:00 committed by Kirill Chibisov
parent c47d0846fa
commit a6f414d732
12 changed files with 7 additions and 212 deletions

View file

@ -1,71 +0,0 @@
#![allow(clippy::single_match)]
// Limit this example to only compatible platforms.
#[cfg(any(
windows_platform,
macos_platform,
x11_platform,
wayland_platform,
android_platform,
orbital_platform,
))]
fn main() {
use std::{thread::sleep, time::Duration};
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::EventLoop,
platform::run_return::EventLoopExtRunReturn,
window::WindowBuilder,
};
#[path = "util/fill.rs"]
mod fill;
let mut event_loop = EventLoop::new();
SimpleLogger::new().init().unwrap();
let window = WindowBuilder::new()
.with_title("A fantastic window!")
.build(&event_loop)
.unwrap();
let mut quit = false;
while !quit {
event_loop.run_return(|event, _, control_flow| {
control_flow.set_wait();
if let Event::WindowEvent { event, .. } = &event {
// Print only Window events to reduce noise
println!("{event:?}");
}
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
quit = true;
}
Event::MainEventsCleared => {
control_flow.set_exit();
}
Event::RedrawRequested(_) => {
fill::fill_window(&window);
}
_ => (),
}
});
// Sleep for 1/60 second to simulate rendering
println!("rendering");
sleep(Duration::from_millis(16));
}
}
#[cfg(any(ios_platform, wasm_platform))]
fn main() {
println!("This platform doesn't support run_return.");
}

View file

@ -156,7 +156,7 @@ impl<T> fmt::Debug for EventLoopWindowTarget<T> {
/// ///
/// Almost every change is persistent between multiple calls to the event loop closure within a /// Almost every change is persistent between multiple calls to the event loop closure within a
/// given run loop. The only exception to this is [`ExitWithCode`] which, once set, cannot be unset. /// given run loop. The only exception to this is [`ExitWithCode`] which, once set, cannot be unset.
/// Changes are **not** persistent between multiple calls to `run_return` - issuing a new call will /// Changes are **not** persistent between multiple calls to `run_ondemand` - issuing a new call will
/// reset the control flow to [`Poll`]. /// reset the control flow to [`Poll`].
/// ///
/// [`ExitWithCode`]: Self::ExitWithCode /// [`ExitWithCode`]: Self::ExitWithCode

View file

@ -37,7 +37,7 @@
//! Winit no longer uses a `EventLoop::poll_events() -> impl Iterator<Event>`-based event loop //! Winit no longer uses a `EventLoop::poll_events() -> impl Iterator<Event>`-based event loop
//! model, since that can't be implemented properly on some platforms (e.g web, iOS) and works poorly on //! model, since that can't be implemented properly on some platforms (e.g web, iOS) and works poorly on
//! most other platforms. However, this model can be re-implemented to an extent with //! most other platforms. However, this model can be re-implemented to an extent with
//! [`EventLoopExtRunReturn::run_return`]. See that method's documentation for more reasons about why //! [`EventLoopExtPumpEvents::pump_events`]. See that method's documentation for more reasons about why
//! it's discouraged, beyond compatibility reasons. //! it's discouraged, beyond compatibility reasons.
//! //!
//! //!
@ -109,7 +109,7 @@
//! window visible only once you're ready to render into it. //! window visible only once you're ready to render into it.
//! //!
//! [`EventLoop`]: event_loop::EventLoop //! [`EventLoop`]: event_loop::EventLoop
//! [`EventLoopExtRunReturn::run_return`]: ./platform/run_return/trait.EventLoopExtRunReturn.html#tymethod.run_return //! [`EventLoopExtPumpEvents::pump_events`]: ./platform/pump_events/trait.EventLoopExtPumpEvents.html#tymethod.pump_events
//! [`EventLoop::new()`]: event_loop::EventLoop::new //! [`EventLoop::new()`]: event_loop::EventLoop::new
//! [event_loop_run]: event_loop::EventLoop::run //! [event_loop_run]: event_loop::EventLoop::run
//! [`ControlFlow`]: event_loop::ControlFlow //! [`ControlFlow`]: event_loop::ControlFlow

View file

@ -13,7 +13,6 @@
//! //!
//! - `run_ondemand` (available on `windows`, `unix`, `macos`, `android`) //! - `run_ondemand` (available on `windows`, `unix`, `macos`, `android`)
//! - `pump_events` (available on `windows`, `unix`, `macos`, `android`) //! - `pump_events` (available on `windows`, `unix`, `macos`, `android`)
//! - `run_return` (available on `windows`, `unix`, `macos`, and `android`)
//! //!
//! However only the module corresponding to the platform you're compiling to will be available. //! However only the module corresponding to the platform you're compiling to will be available.
@ -54,15 +53,5 @@ pub mod run_ondemand;
))] ))]
pub mod pump_events; pub mod pump_events;
#[cfg(any(
windows_platform,
macos_platform,
android_platform,
x11_platform,
wayland_platform,
orbital_platform
))]
pub mod run_return;
pub mod modifier_supplement; pub mod modifier_supplement;
pub mod scancode; pub mod scancode;

View file

@ -1,53 +0,0 @@
use crate::{
event::Event,
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
};
/// Additional methods on [`EventLoop`] to return control flow to the caller.
pub trait EventLoopExtRunReturn {
/// A type provided by the user that can be passed through [`Event::UserEvent`].
type UserEvent;
/// Initializes the `winit` event loop.
///
/// Unlike [`EventLoop::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`].
///
/// # Caveats
///
/// Despite its appearance at first glance, this is *not* a perfect replacement for
/// `poll_events`. For example, this function will not return on Windows or macOS while a
/// window is getting resized, resulting in all application logic outside of the
/// `event_handler` closure not running until the resize operation ends. Other OS operations
/// may also result in such freezes. This behavior is caused by fundamental limitations in the
/// 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.
///
/// ## Platform-specific
///
/// - **X11 / Wayland:** This function returns `1` upon disconnection from
/// the display server.
fn run_return<F>(&mut self, event_handler: F) -> i32
where
F: FnMut(
Event<'_, Self::UserEvent>,
&EventLoopWindowTarget<Self::UserEvent>,
&mut ControlFlow,
);
}
impl<T> EventLoopExtRunReturn for EventLoop<T> {
type UserEvent = T;
fn run_return<F>(&mut self, event_handler: F) -> i32
where
F: FnMut(
Event<'_, Self::UserEvent>,
&EventLoopWindowTarget<Self::UserEvent>,
&mut ControlFlow,
),
{
self.event_loop.run_return(event_handler)
}
}

View file

@ -539,17 +539,6 @@ impl<T: 'static> EventLoop<T> {
::std::process::exit(exit_code); ::std::process::exit(exit_code);
} }
pub fn run_return<F>(&mut self, callback: F) -> i32
where
F: FnMut(event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
match self.run_ondemand(callback) {
Err(RunLoopError::ExitFailure(code)) => code,
Err(_err) => 1,
Ok(_) => 0,
}
}
pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), RunLoopError> pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), RunLoopError>
where where
F: FnMut(event::Event<'_, T>, &event_loop::EventLoopWindowTarget<T>, &mut ControlFlow), F: FnMut(event::Event<'_, T>, &event_loop::EventLoopWindowTarget<T>, &mut ControlFlow),

View file

@ -830,13 +830,6 @@ impl<T: 'static> EventLoop<T> {
x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy) x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy)
} }
pub fn run_return<F>(&mut self, callback: F) -> i32
where
F: FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
x11_or_wayland!(match self; EventLoop(evlp) => evlp.run_return(callback))
}
pub fn run<F>(self, callback: F) -> ! pub fn run<F>(self, callback: F) -> !
where where
F: 'static + FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow), F: 'static + FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),

View file

@ -157,17 +157,6 @@ impl<T: 'static> EventLoop<T> {
::std::process::exit(exit_code) ::std::process::exit(exit_code)
} }
pub fn run_return<F>(&mut self, callback: F) -> i32
where
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
{
match self.run_ondemand(callback) {
Err(RunLoopError::ExitFailure(code)) => code,
Err(_err) => 1,
Ok(_) => 0,
}
}
pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), RunLoopError> pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), RunLoopError>
where where
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow), F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),

View file

@ -444,17 +444,6 @@ impl<T: 'static> EventLoop<T> {
::std::process::exit(exit_code) ::std::process::exit(exit_code)
} }
pub fn run_return<F>(&mut self, callback: F) -> i32
where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
match self.run_ondemand(callback) {
Err(RunLoopError::ExitFailure(code)) => code,
Err(_err) => 1,
Ok(_) => 0,
}
}
pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), RunLoopError> pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), RunLoopError>
where where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow), F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),

View file

@ -204,17 +204,6 @@ impl<T> EventLoop<T> {
process::exit(exit_code); process::exit(exit_code);
} }
pub fn run_return<F>(&mut self, callback: F) -> i32
where
F: FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
{
match self.run_ondemand(callback) {
Err(RunLoopError::ExitFailure(code)) => code,
Err(_err) => 1,
Ok(_) => 0,
}
}
// NB: we don't base this on `pump_events` because for `MacOs` we can't support // NB: we don't base this on `pump_events` because for `MacOs` we can't support
// `pump_events` elegantly (we just ask to run the loop for a "short" amount of // `pump_events` elegantly (we just ask to run the loop for a "short" amount of
// time and so a layered implementation would end up using a lot of CPU due to // time and so a layered implementation would end up using a lot of CPU due to

View file

@ -301,15 +301,6 @@ impl<T: 'static> EventLoop<T> {
} }
} }
pub fn run<F>(mut self, event_handler: F) -> !
where
F: 'static
+ FnMut(event::Event<'_, T>, &event_loop::EventLoopWindowTarget<T>, &mut ControlFlow),
{
let exit_code = self.run_return(event_handler);
::std::process::exit(exit_code);
}
fn process_event<F>( fn process_event<F>(
window_id: WindowId, window_id: WindowId,
event_option: EventOption, event_option: EventOption,
@ -451,9 +442,10 @@ impl<T: 'static> EventLoop<T> {
} }
} }
pub fn run_return<F>(&mut self, mut event_handler_inner: F) -> i32 pub fn run<F>(mut self, event_handler: F) -> !
where where
F: FnMut(event::Event<'_, T>, &event_loop::EventLoopWindowTarget<T>, &mut ControlFlow), F: 'static
+ FnMut(event::Event<'_, T>, &event_loop::EventLoopWindowTarget<T>, &mut ControlFlow),
{ {
// Wrapper for event handler function that prevents ExitWithCode from being unset. // Wrapper for event handler function that prevents ExitWithCode from being unset.
let mut event_handler = let mut event_handler =
@ -696,7 +688,7 @@ impl<T: 'static> EventLoop<T> {
&mut control_flow, &mut control_flow,
); );
code ::std::process::exit(code);
} }
pub fn window_target(&self) -> &event_loop::EventLoopWindowTarget<T> { pub fn window_target(&self) -> &event_loop::EventLoopWindowTarget<T> {

View file

@ -255,17 +255,6 @@ impl<T: 'static> EventLoop<T> {
::std::process::exit(exit_code); ::std::process::exit(exit_code);
} }
pub fn run_return<F>(&mut self, event_handler: F) -> i32
where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
match self.run_ondemand(event_handler) {
Err(RunLoopError::ExitFailure(code)) => code,
Err(_err) => 1,
Ok(_) => 0,
}
}
pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), RunLoopError> pub fn run_ondemand<F>(&mut self, mut event_handler: F) -> Result<(), RunLoopError>
where where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow), F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),