From 0264cba07b5325cdc0fa3745d87ed1ca5f32404f Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Tue, 25 Jun 2024 20:57:18 +0200 Subject: [PATCH] examples: Make `utils/winit_app.rs` a `path`-referenced module again to fix autoformatting Via `include!()` `rustfmt` doesn't know that this file belongs to the current project and doesn't autoformat it. Furthermore, jump-to-symbol likely doesn't play that well with `include!()`. Without creating a `lib.rs` file in `utils/`, use `#[path]` to reference the existing file without following a `mod`ule hierarchy. Note that the file remains in the `utils/` folder as that doesn't get picked up by `autoexamples`. --- README.md | 3 +- examples/animation.rs | 3 +- examples/fruit.rs | 3 +- examples/rectangle.rs | 3 +- examples/utils/winit_app.rs | 201 +++++++++++++-------------- examples/winit.rs | 3 +- examples/winit_multithread.rs | 6 +- examples/winit_wrong_sized_buffer.rs | 3 +- 8 files changed, 117 insertions(+), 108 deletions(-) diff --git a/README.md b/README.md index 3e39db6d..b19d4ac5 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,8 @@ use winit::event::{Event, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::window::Window; -include!("../examples/utils/winit_app.rs"); +#[path = "../examples/utils/winit_app.rs"] +mod winit_app; fn main() { let event_loop = EventLoop::new().unwrap(); diff --git a/examples/animation.rs b/examples/animation.rs index 7c2e22c6..72b387b0 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -7,7 +7,8 @@ use winit::event::{Event, KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; -include!("./utils/winit_app.rs"); +#[path = "utils/winit_app.rs"] +mod winit_app; fn main() { let event_loop = EventLoop::new().unwrap(); diff --git a/examples/fruit.rs b/examples/fruit.rs index 6109216b..f276ee9d 100644 --- a/examples/fruit.rs +++ b/examples/fruit.rs @@ -4,7 +4,8 @@ use winit::event::{Event, KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; -include!("utils/winit_app.rs"); +#[path = "utils/winit_app.rs"] +mod winit_app; fn main() { //see fruit.jpg.license for the license of fruit.jpg diff --git a/examples/rectangle.rs b/examples/rectangle.rs index 76d15569..7ab59175 100644 --- a/examples/rectangle.rs +++ b/examples/rectangle.rs @@ -3,7 +3,8 @@ use winit::event::{ElementState, Event, KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; -include!("utils/winit_app.rs"); +#[path = "utils/winit_app.rs"] +mod winit_app; fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) { for y in 0..height { diff --git a/examples/utils/winit_app.rs b/examples/utils/winit_app.rs index b8618bf9..f4278947 100644 --- a/examples/utils/winit_app.rs +++ b/examples/utils/winit_app.rs @@ -1,122 +1,121 @@ /// Common boilerplate for setting up a winit application. -mod winit_app { - use std::marker::PhantomData; - use std::rc::Rc; - - use winit::application::ApplicationHandler; - use winit::event::{Event, WindowEvent}; - use winit::event_loop::{EventLoop, ActiveEventLoop}; - use winit::window::{WindowAttributes, WindowId, Window}; - - /// Run a Winit application. - #[allow(unused_mut)] - pub(crate) fn run_app(event_loop: EventLoop<()>, mut app: impl ApplicationHandler<()> + 'static) { - #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] - event_loop.run_app(&mut app).unwrap(); - - #[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] - winit::platform::web::EventLoopExtWebSys::spawn_app(event_loop, app); - } - - /// Create a window from a set of window attributes. - #[allow(dead_code)] - pub(crate) fn make_window(elwt: &ActiveEventLoop, f: impl FnOnce(WindowAttributes) -> WindowAttributes) -> Rc { - let attributes = f(WindowAttributes::default()); - #[cfg(target_arch = "wasm32")] - let attributes = winit::platform::web::WindowAttributesExtWebSys::with_append( - attributes, - true - ); - let window = elwt.create_window(attributes); - Rc::new(window.unwrap()) - } +use std::marker::PhantomData; +use std::rc::Rc; + +use winit::application::ApplicationHandler; +use winit::event::{Event, WindowEvent}; +use winit::event_loop::{ActiveEventLoop, EventLoop}; +use winit::window::{Window, WindowAttributes, WindowId}; + +/// Run a Winit application. +#[allow(unused_mut)] +pub(crate) fn run_app(event_loop: EventLoop<()>, mut app: impl ApplicationHandler<()> + 'static) { + #[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))] + event_loop.run_app(&mut app).unwrap(); + + #[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))] + winit::platform::web::EventLoopExtWebSys::spawn_app(event_loop, app); +} - /// Easily constructable winit application. - pub(crate) struct WinitApp { - /// Closure to initialize state. - init: Init, +/// Create a window from a set of window attributes. +#[allow(dead_code)] +pub(crate) fn make_window( + elwt: &ActiveEventLoop, + f: impl FnOnce(WindowAttributes) -> WindowAttributes, +) -> Rc { + let attributes = f(WindowAttributes::default()); + #[cfg(target_arch = "wasm32")] + let attributes = winit::platform::web::WindowAttributesExtWebSys::with_append(attributes, true); + let window = elwt.create_window(attributes); + Rc::new(window.unwrap()) +} - /// Closure to run on window events. - event: Handler, +/// Easily constructable winit application. +pub(crate) struct WinitApp { + /// Closure to initialize state. + init: Init, - /// Contained state. - state: Option - } + /// Closure to run on window events. + event: Handler, - /// Builder that makes it so we don't have to name `T`. - pub(crate) struct WinitAppBuilder { - /// Closure to initialize state. - init: Init, + /// Contained state. + state: Option, +} - /// Eat the type parameter. - _marker: PhantomData>, - } +/// Builder that makes it so we don't have to name `T`. +pub(crate) struct WinitAppBuilder { + /// Closure to initialize state. + init: Init, - impl WinitAppBuilder - where Init: FnMut(&ActiveEventLoop) -> T, - { - /// Create with an "init" closure. - pub(crate) fn with_init(init: Init) -> Self { - Self { - init, - _marker: PhantomData - } - } + /// Eat the type parameter. + _marker: PhantomData>, +} - /// Build a new application. - pub(crate) fn with_event_handler(self, handler: F) -> WinitApp - where F: FnMut(&mut T, Event<()>, &ActiveEventLoop) - { - WinitApp::new(self.init, handler) +impl WinitAppBuilder +where + Init: FnMut(&ActiveEventLoop) -> T, +{ + /// Create with an "init" closure. + pub(crate) fn with_init(init: Init) -> Self { + Self { + init, + _marker: PhantomData, } } - impl WinitApp - where Init: FnMut(&ActiveEventLoop) -> T, - Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop) + /// Build a new application. + pub(crate) fn with_event_handler(self, handler: F) -> WinitApp + where + F: FnMut(&mut T, Event<()>, &ActiveEventLoop), { - /// Create a new application. - pub(crate) fn new(init: Init, event: Handler) -> Self { - Self { - init, - event, - state: None - } - } + WinitApp::new(self.init, handler) } +} - impl ApplicationHandler for WinitApp - where Init: FnMut(&ActiveEventLoop) -> T, - Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop) { - - fn resumed(&mut self, el: &ActiveEventLoop) { - debug_assert!(self.state.is_none()); - self.state = Some((self.init)(el)); +impl WinitApp +where + Init: FnMut(&ActiveEventLoop) -> T, + Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop), +{ + /// Create a new application. + pub(crate) fn new(init: Init, event: Handler) -> Self { + Self { + init, + event, + state: None, } + } +} - fn suspended(&mut self, _event_loop: &ActiveEventLoop) { - let state = self.state.take(); - debug_assert!(state.is_some()); - drop(state); - } +impl ApplicationHandler for WinitApp +where + Init: FnMut(&ActiveEventLoop) -> T, + Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop), +{ + fn resumed(&mut self, el: &ActiveEventLoop) { + debug_assert!(self.state.is_none()); + self.state = Some((self.init)(el)); + } - fn window_event( - &mut self, - event_loop: &ActiveEventLoop, - window_id: WindowId, - event: WindowEvent, - ) { - let state = self.state.as_mut().unwrap(); - (self.event)(state, Event::WindowEvent { - window_id, - event - }, event_loop); - } + fn suspended(&mut self, _event_loop: &ActiveEventLoop) { + let state = self.state.take(); + debug_assert!(state.is_some()); + drop(state); + } + + fn window_event( + &mut self, + event_loop: &ActiveEventLoop, + window_id: WindowId, + event: WindowEvent, + ) { + let state = self.state.as_mut().unwrap(); + (self.event)(state, Event::WindowEvent { window_id, event }, event_loop); + } - fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { - if let Some(state) = self.state.as_mut() { - (self.event)(state, Event::AboutToWait, event_loop); - } + fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { + if let Some(state) = self.state.as_mut() { + (self.event)(state, Event::AboutToWait, event_loop); } } } diff --git a/examples/winit.rs b/examples/winit.rs index 4bff0b3a..7678fa5e 100644 --- a/examples/winit.rs +++ b/examples/winit.rs @@ -3,7 +3,8 @@ use winit::event::{Event, KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; -include!("utils/winit_app.rs"); +#[path = "utils/winit_app.rs"] +mod winit_app; fn main() { let event_loop = EventLoop::new().unwrap(); diff --git a/examples/winit_multithread.rs b/examples/winit_multithread.rs index 52731c52..4c2be4d2 100644 --- a/examples/winit_multithread.rs +++ b/examples/winit_multithread.rs @@ -1,5 +1,9 @@ //! `Surface` implements `Send`. This makes sure that multithreading can work here. +#[cfg(not(target_family = "wasm"))] +#[path = "utils/winit_app.rs"] +mod winit_app; + #[cfg(not(target_family = "wasm"))] mod ex { use std::num::NonZeroU32; @@ -9,7 +13,7 @@ mod ex { use winit::keyboard::{Key, NamedKey}; use winit::window::Window; - include!("utils/winit_app.rs"); + use super::winit_app; type Surface = softbuffer::Surface, Arc>; diff --git a/examples/winit_wrong_sized_buffer.rs b/examples/winit_wrong_sized_buffer.rs index 3aa222d9..c0396d3a 100644 --- a/examples/winit_wrong_sized_buffer.rs +++ b/examples/winit_wrong_sized_buffer.rs @@ -3,7 +3,8 @@ use winit::event::{Event, KeyEvent, WindowEvent}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::keyboard::{Key, NamedKey}; -include!("utils/winit_app.rs"); +#[path = "utils/winit_app.rs"] +mod winit_app; const BUFFER_WIDTH: usize = 256; const BUFFER_HEIGHT: usize = 128;