Skip to content

Commit cb3e3e8

Browse files
committed
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`.
1 parent 7c4af4b commit cb3e3e8

File tree

7 files changed

+115
-107
lines changed

7 files changed

+115
-107
lines changed

examples/animation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use winit::event::{Event, KeyEvent, WindowEvent};
77
use winit::event_loop::{ControlFlow, EventLoop};
88
use winit::keyboard::{Key, NamedKey};
99

10-
include!("./utils/winit_app.rs");
10+
#[path = "utils/winit_app.rs"]
11+
mod winit_app;
1112

1213
fn main() {
1314
let event_loop = EventLoop::new().unwrap();

examples/fruit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use winit::event::{Event, KeyEvent, WindowEvent};
44
use winit::event_loop::{ControlFlow, EventLoop};
55
use winit::keyboard::{Key, NamedKey};
66

7-
include!("utils/winit_app.rs");
7+
#[path = "utils/winit_app.rs"]
8+
mod winit_app;
89

910
fn main() {
1011
//see fruit.jpg.license for the license of fruit.jpg

examples/rectangle.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
33
use winit::event_loop::{ControlFlow, EventLoop};
44
use winit::keyboard::{Key, NamedKey};
55

6-
include!("utils/winit_app.rs");
6+
#[path = "utils/winit_app.rs"]
7+
mod winit_app;
78

89
fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
910
for y in 0..height {

examples/utils/winit_app.rs

Lines changed: 100 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,121 @@
11
/// Common boilerplate for setting up a winit application.
2-
mod winit_app {
3-
use std::marker::PhantomData;
4-
use std::rc::Rc;
5-
6-
use winit::application::ApplicationHandler;
7-
use winit::event::{Event, WindowEvent};
8-
use winit::event_loop::{EventLoop, ActiveEventLoop};
9-
use winit::window::{WindowAttributes, WindowId, Window};
10-
11-
/// Run a Winit application.
12-
#[allow(unused_mut)]
13-
pub(crate) fn run_app(event_loop: EventLoop<()>, mut app: impl ApplicationHandler<()> + 'static) {
14-
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
15-
event_loop.run_app(&mut app).unwrap();
16-
17-
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
18-
winit::platform::web::EventLoopExtWebSys::spawn_app(event_loop, app);
19-
}
20-
21-
/// Create a window from a set of window attributes.
22-
#[allow(dead_code)]
23-
pub(crate) fn make_window(elwt: &ActiveEventLoop, f: impl FnOnce(WindowAttributes) -> WindowAttributes) -> Rc<Window> {
24-
let attributes = f(WindowAttributes::default());
25-
#[cfg(target_arch = "wasm32")]
26-
let attributes = winit::platform::web::WindowAttributesExtWebSys::with_append(
27-
attributes,
28-
true
29-
);
30-
let window = elwt.create_window(attributes);
31-
Rc::new(window.unwrap())
32-
}
2+
use std::marker::PhantomData;
3+
use std::rc::Rc;
4+
5+
use winit::application::ApplicationHandler;
6+
use winit::event::{Event, WindowEvent};
7+
use winit::event_loop::{ActiveEventLoop, EventLoop};
8+
use winit::window::{Window, WindowAttributes, WindowId};
9+
10+
/// Run a Winit application.
11+
#[allow(unused_mut)]
12+
pub(crate) fn run_app(event_loop: EventLoop<()>, mut app: impl ApplicationHandler<()> + 'static) {
13+
#[cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))]
14+
event_loop.run_app(&mut app).unwrap();
15+
16+
#[cfg(any(target_arch = "wasm32", target_arch = "wasm64"))]
17+
winit::platform::web::EventLoopExtWebSys::spawn_app(event_loop, app);
18+
}
3319

34-
/// Easily constructable winit application.
35-
pub(crate) struct WinitApp<T, Init, Handler> {
36-
/// Closure to initialize state.
37-
init: Init,
20+
/// Create a window from a set of window attributes.
21+
#[allow(dead_code)]
22+
pub(crate) fn make_window(
23+
elwt: &ActiveEventLoop,
24+
f: impl FnOnce(WindowAttributes) -> WindowAttributes,
25+
) -> Rc<Window> {
26+
let attributes = f(WindowAttributes::default());
27+
#[cfg(target_arch = "wasm32")]
28+
let attributes = winit::platform::web::WindowAttributesExtWebSys::with_append(attributes, true);
29+
let window = elwt.create_window(attributes);
30+
Rc::new(window.unwrap())
31+
}
3832

39-
/// Closure to run on window events.
40-
event: Handler,
33+
/// Easily constructable winit application.
34+
pub(crate) struct WinitApp<T, Init, Handler> {
35+
/// Closure to initialize state.
36+
init: Init,
4137

42-
/// Contained state.
43-
state: Option<T>
44-
}
38+
/// Closure to run on window events.
39+
event: Handler,
4540

46-
/// Builder that makes it so we don't have to name `T`.
47-
pub(crate) struct WinitAppBuilder<T, Init> {
48-
/// Closure to initialize state.
49-
init: Init,
41+
/// Contained state.
42+
state: Option<T>,
43+
}
5044

51-
/// Eat the type parameter.
52-
_marker: PhantomData<Option<T>>,
53-
}
45+
/// Builder that makes it so we don't have to name `T`.
46+
pub(crate) struct WinitAppBuilder<T, Init> {
47+
/// Closure to initialize state.
48+
init: Init,
5449

55-
impl<T, Init> WinitAppBuilder<T, Init>
56-
where Init: FnMut(&ActiveEventLoop) -> T,
57-
{
58-
/// Create with an "init" closure.
59-
pub(crate) fn with_init(init: Init) -> Self {
60-
Self {
61-
init,
62-
_marker: PhantomData
63-
}
64-
}
50+
/// Eat the type parameter.
51+
_marker: PhantomData<Option<T>>,
52+
}
6553

66-
/// Build a new application.
67-
pub(crate) fn with_event_handler<F>(self, handler: F) -> WinitApp<T, Init, F>
68-
where F: FnMut(&mut T, Event<()>, &ActiveEventLoop)
69-
{
70-
WinitApp::new(self.init, handler)
54+
impl<T, Init> WinitAppBuilder<T, Init>
55+
where
56+
Init: FnMut(&ActiveEventLoop) -> T,
57+
{
58+
/// Create with an "init" closure.
59+
pub(crate) fn with_init(init: Init) -> Self {
60+
Self {
61+
init,
62+
_marker: PhantomData,
7163
}
7264
}
7365

74-
impl<T, Init, Handler> WinitApp<T, Init, Handler>
75-
where Init: FnMut(&ActiveEventLoop) -> T,
76-
Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop)
66+
/// Build a new application.
67+
pub(crate) fn with_event_handler<F>(self, handler: F) -> WinitApp<T, Init, F>
68+
where
69+
F: FnMut(&mut T, Event<()>, &ActiveEventLoop),
7770
{
78-
/// Create a new application.
79-
pub(crate) fn new(init: Init, event: Handler) -> Self {
80-
Self {
81-
init,
82-
event,
83-
state: None
84-
}
85-
}
71+
WinitApp::new(self.init, handler)
8672
}
73+
}
8774

88-
impl<T, Init, Handler> ApplicationHandler for WinitApp<T, Init, Handler>
89-
where Init: FnMut(&ActiveEventLoop) -> T,
90-
Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop) {
91-
92-
fn resumed(&mut self, el: &ActiveEventLoop) {
93-
debug_assert!(self.state.is_none());
94-
self.state = Some((self.init)(el));
75+
impl<T, Init, Handler> WinitApp<T, Init, Handler>
76+
where
77+
Init: FnMut(&ActiveEventLoop) -> T,
78+
Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop),
79+
{
80+
/// Create a new application.
81+
pub(crate) fn new(init: Init, event: Handler) -> Self {
82+
Self {
83+
init,
84+
event,
85+
state: None,
9586
}
87+
}
88+
}
9689

97-
fn suspended(&mut self, _event_loop: &ActiveEventLoop) {
98-
let state = self.state.take();
99-
debug_assert!(state.is_some());
100-
drop(state);
101-
}
90+
impl<T, Init, Handler> ApplicationHandler for WinitApp<T, Init, Handler>
91+
where
92+
Init: FnMut(&ActiveEventLoop) -> T,
93+
Handler: FnMut(&mut T, Event<()>, &ActiveEventLoop),
94+
{
95+
fn resumed(&mut self, el: &ActiveEventLoop) {
96+
debug_assert!(self.state.is_none());
97+
self.state = Some((self.init)(el));
98+
}
10299

103-
fn window_event(
104-
&mut self,
105-
event_loop: &ActiveEventLoop,
106-
window_id: WindowId,
107-
event: WindowEvent,
108-
) {
109-
let state = self.state.as_mut().unwrap();
110-
(self.event)(state, Event::WindowEvent {
111-
window_id,
112-
event
113-
}, event_loop);
114-
}
100+
fn suspended(&mut self, _event_loop: &ActiveEventLoop) {
101+
let state = self.state.take();
102+
debug_assert!(state.is_some());
103+
drop(state);
104+
}
105+
106+
fn window_event(
107+
&mut self,
108+
event_loop: &ActiveEventLoop,
109+
window_id: WindowId,
110+
event: WindowEvent,
111+
) {
112+
let state = self.state.as_mut().unwrap();
113+
(self.event)(state, Event::WindowEvent { window_id, event }, event_loop);
114+
}
115115

116-
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
117-
if let Some(state) = self.state.as_mut() {
118-
(self.event)(state, Event::AboutToWait, event_loop);
119-
}
116+
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
117+
if let Some(state) = self.state.as_mut() {
118+
(self.event)(state, Event::AboutToWait, event_loop);
120119
}
121120
}
122121
}

examples/winit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use winit::event::{Event, KeyEvent, WindowEvent};
33
use winit::event_loop::{ControlFlow, EventLoop};
44
use winit::keyboard::{Key, NamedKey};
55

6-
include!("utils/winit_app.rs");
6+
#[path = "utils/winit_app.rs"]
7+
mod winit_app;
78

89
fn main() {
910
let event_loop = EventLoop::new().unwrap();

examples/winit_multithread.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//! `Surface` implements `Send`. This makes sure that multithreading can work here.
22
3+
#[cfg(not(target_family = "wasm"))]
4+
#[path = "utils/winit_app.rs"]
5+
mod winit_app;
6+
37
#[cfg(not(target_family = "wasm"))]
48
mod ex {
59
use std::num::NonZeroU32;
@@ -9,7 +13,7 @@ mod ex {
913
use winit::keyboard::{Key, NamedKey};
1014
use winit::window::Window;
1115

12-
include!("utils/winit_app.rs");
16+
use super::winit_app;
1317

1418
type Surface = softbuffer::Surface<Arc<Window>, Arc<Window>>;
1519

examples/winit_wrong_sized_buffer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use winit::event::{Event, KeyEvent, WindowEvent};
33
use winit::event_loop::{ControlFlow, EventLoop};
44
use winit::keyboard::{Key, NamedKey};
55

6-
include!("utils/winit_app.rs");
6+
#[path = "utils/winit_app.rs"]
7+
mod winit_app;
78

89
const BUFFER_WIDTH: usize = 256;
910
const BUFFER_HEIGHT: usize = 128;

0 commit comments

Comments
 (0)