From c73ab25c9dbe40284cfd38beaf31e2a20a9810de Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 22 Sep 2026 18:22:46 +0200 Subject: refactor(engine): fix portion of clippy lints --- engine/src/windowing/dpi.rs | 24 ++++++++++++++++++++ engine/src/windowing/keyboard.rs | 48 +++++++++++++++++++++------------------- engine/src/windowing/monitor.rs | 4 ++++ engine/src/windowing/mouse.rs | 7 ++++-- engine/src/windowing/window.rs | 20 ++++++++--------- 5 files changed, 68 insertions(+), 35 deletions(-) (limited to 'engine/src/windowing') diff --git a/engine/src/windowing/dpi.rs b/engine/src/windowing/dpi.rs index e3b1be1..9bc42e1 100644 --- a/engine/src/windowing/dpi.rs +++ b/engine/src/windowing/dpi.rs @@ -75,6 +75,12 @@ impl PhysicalPosition } } + /// Attempts to convert the `x` and `y` values in `source` from the type `SourcePixel` + /// to the type `Pixel`. + /// + /// # Errors + /// Returns `Err` if conversion of either `x` or `y` by their respective `TryFrom` + /// implementations fails. pub fn try_convert_from( source: PhysicalPosition, ) -> Result @@ -100,6 +106,12 @@ pub struct LogicalPosition impl LogicalPosition { + /// Attempts to convert the `x` and `y` values in `source` from the type `SourcePixel` + /// to the type `Pixel`. + /// + /// # Errors + /// Returns `Err` if conversion of either `x` or `y` by their respective `TryFrom` + /// implementations fails. pub fn try_convert_from( source: LogicalPosition, ) -> Result @@ -159,6 +171,12 @@ impl PhysicalSize impl PhysicalSize { + /// Attempts to convert the `width` and `height` values in `source` from the type + /// `SourcePixel` to the type `Pixel`. + /// + /// # Errors + /// Returns `Err` if conversion of either `width` or `height` by their respective + /// `TryFrom` implementations fails. pub fn try_convert_from( source: PhysicalSize, ) -> Result @@ -184,6 +202,12 @@ pub struct LogicalSize impl LogicalSize { + /// Attempts to convert the `width` and `height` values in `source` from the type + /// `SourcePixel` to the type `Pixel`. + /// + /// # Errors + /// Returns `Err` if conversion of either `width` or `height` by their respective + /// `TryFrom` implementations fails. pub fn try_convert_from( source: LogicalSize, ) -> Result diff --git a/engine/src/windowing/keyboard.rs b/engine/src/windowing/keyboard.rs index dffa95e..a8ec6df 100644 --- a/engine/src/windowing/keyboard.rs +++ b/engine/src/windowing/keyboard.rs @@ -14,6 +14,7 @@ impl Keyboard { /// Returns whether the given key was just pressed this frame. This function will /// return `false` if the key was also pressed the previous frame. + #[must_use] pub fn just_pressed(&self, key: Key) -> bool { self.get_key_state(key) == KeyState::Pressed @@ -22,6 +23,7 @@ impl Keyboard /// Returns whether the given key was just released this frame. This function will /// return `false` if the key was also released the previous frame. + #[must_use] pub fn just_released(&self, key: Key) -> bool { self.get_key_state(key) == KeyState::Released @@ -29,12 +31,14 @@ impl Keyboard } /// Returns whether the given key is currently pressed. + #[must_use] pub fn pressed(&self, key: Key) -> bool { self.get_key_state(key) == KeyState::Pressed } /// Returns whether the given key is currently released. + #[must_use] pub fn released(&self, key: Key) -> bool { self.get_key_state(key) == KeyState::Released @@ -66,13 +70,11 @@ impl Keyboard { let bits = self.keys.get(key as usize); - let state = match bits & KEY_CURR_PRESSED_BITS { + match bits & KEY_CURR_PRESSED_BITS { KEY_CURR_PRESSED_BITS => KeyState::Pressed, 0 => KeyState::Released, _ => unreachable!(), - }; - - state + } } #[must_use] @@ -80,15 +82,14 @@ impl Keyboard { let bits = self.keys.get(key as usize); - let state = match bits & KEY_PREV_PRESSED_BITS { + match bits & KEY_PREV_PRESSED_BITS { KEY_PREV_PRESSED_BITS => KeyState::Pressed, 0 => KeyState::Released, _ => unreachable!(), - }; - - state + } } + #[must_use] pub fn text_keys(&self) -> &str { &self.text_keys @@ -109,7 +110,7 @@ impl Keyboard pub fn make_key_states_previous(&mut self) { for byte in self.keys.bytes_mut() { - *byte = (*byte >> 1) & 0b01010101 | *byte & 0b10101010; + *byte = (*byte >> 1) & 0b0101_0101 | *byte & 0b1010_1010; } } @@ -125,7 +126,7 @@ impl Keyboard #[non_exhaustive] pub enum Key { - /// ` on a US keyboard. This is also called a backtick or grave. + #[doc = "` on a US keyboard. This is also called a backtick or grave."] /// This is the 半角/全角/漢字 /// (hankaku/zenkaku/kanji) key on Japanese keyboards Backquote, @@ -243,12 +244,12 @@ pub enum Key /// Alt, Option, or . AltLeft, /// Alt, Option, or . - /// This is labeled AltGr on many keyboard layouts. + /// This is labeled `AltGr` on many keyboard layouts. AltRight, /// Backspace or . /// Labeled Delete on Apple keyboards. Backspace, - /// CapsLock or + /// `CapsLock` or CapsLock, /// The application context menu key, which is typically found between the right /// Super key and the right Control key. @@ -276,7 +277,7 @@ pub enum Key /// Japanese: カタカナ/ひらがな/ローマ字 /// (katakana/hiragana/romaji) KanaMode, - /// Korean: HangulMode 한/영 (han/yeong) + /// Korean: `HangulMode` 한/영 (han/yeong) /// /// Japanese (Mac keyboard): (kana) Lang1, @@ -306,9 +307,9 @@ pub enum Key Home, /// Insert or Ins. Not present on Apple keyboards. Insert, - /// Page Down, PgDn, or + /// Page Down, `PgDn`, or PageDown, - /// Page Up, PgUp, or + /// Page Up, `PgUp`, or PageUp, /// ArrowDown, @@ -327,7 +328,7 @@ pub enum Key Numpad1, /// 2 ↓ on a keyboard. 2 ABC on a phone or remote control Numpad2, - /// 3 PgDn on a keyboard. 3 DEF on a phone or remote control + /// 3 `PgDn` on a keyboard. 3 DEF on a phone or remote control Numpad3, /// 4 ← on a keyboard. 4 GHI on a phone or remote control Numpad4, @@ -340,15 +341,15 @@ pub enum Key Numpad7, /// 8 ↑ on a keyboard. 8 TUV on a phone or remote control Numpad8, - /// 9 PgUp on a keyboard. 9 WXYZ or 9 WXY on a phone - /// or remote control + /// 9 `PgUp` on a keyboard. 9 WXYZ or 9 WXY on a + /// phone or remote control Numpad9, /// + NumpadAdd, /// Found on the Microsoft Natural Keyboard. NumpadBackspace, /// C or A (All Clear). Also for use with numpads that have a - /// Clear key that is separate from the NumLock key. On the + /// Clear key that is separate from the `NumLock` key. On the /// Mac, the numpad Clear key is encoded as [`NumLock`]. /// /// [`NumLock`]: Self::NumLock @@ -393,7 +394,7 @@ pub enum Key /// This key is typically found below the 7 key and to the left of /// the 0 key. /// - /// Use "NumpadMultiply" for the * key on + /// Use "`NumpadMultiply`" for the * key on /// numeric keypads. NumpadStar, /// - @@ -403,10 +404,10 @@ pub enum Key /// Fn This is typically a hardware key that does not generate a separate /// code. Fn, - /// FLock or FnLock. Function Lock key. Found on the Microsoft - /// Natural Keyboard. + /// `FLock` or `FnLock`. Function Lock key. Found on the + /// Microsoft Natural Keyboard. FnLock, - /// PrtScr SysRq or Print Screen + /// `PrtScr` `SysRq` or Print Screen PrintScreen, /// Scroll Lock ScrollLock, @@ -577,6 +578,7 @@ impl TryFrom for Key { type Error = UnknownKeyCodeError; + #[allow(clippy::too_many_lines)] fn try_from(key_code: winit::keyboard::KeyCode) -> Result { match key_code { diff --git a/engine/src/windowing/monitor.rs b/engine/src/windowing/monitor.rs index 894448a..392489d 100644 --- a/engine/src/windowing/monitor.rs +++ b/engine/src/windowing/monitor.rs @@ -11,6 +11,7 @@ impl Handle { /// Returns a human-readable name of the monitor. #[inline] + #[must_use] pub fn name(&self) -> Option { self.inner.name() @@ -18,6 +19,7 @@ impl Handle /// Returns the monitor's resolution. #[inline] + #[must_use] pub fn size(&self) -> PhysicalSize { self.inner.size().into() @@ -26,6 +28,7 @@ impl Handle /// Returns the top-left corner position of the monitor relative to the larger full /// screen area. #[inline] + #[must_use] pub fn position(&self) -> PhysicalPosition { self.inner.position().into() @@ -33,6 +36,7 @@ impl Handle /// Returns the scale factor of the underlying monitor. #[inline] + #[must_use] pub fn scale_factor(&self) -> f64 { self.inner.scale_factor() diff --git a/engine/src/windowing/mouse.rs b/engine/src/windowing/mouse.rs index 3a43e79..a5e4cc8 100644 --- a/engine/src/windowing/mouse.rs +++ b/engine/src/windowing/mouse.rs @@ -31,6 +31,7 @@ pub struct ScrollDelta impl ScrollDelta { + #[must_use] pub fn is_zero(&self) -> bool { self.vert_lines == 0.0 && self.hor_lines == 0.0 @@ -46,6 +47,7 @@ pub struct Buttons impl Buttons { + #[must_use] pub fn get(&self, button: Button) -> ButtonState { let Some(button_data) = self.map.get(&button) else { @@ -55,6 +57,7 @@ impl Buttons button_data.current_state } + #[must_use] pub fn get_previous(&self, button: Button) -> ButtonState { let Some(button_data) = self.map.get(&button) else { @@ -70,7 +73,7 @@ impl Buttons { self.map .iter() - .map(|(button, button_data)| (button.clone(), button_data.current_state)) + .map(|(button, button_data)| (*button, button_data.current_state)) } pub fn set(&mut self, button: Button, button_state: ButtonState) @@ -120,7 +123,7 @@ impl intmap::IntKey for Button Self::Middle => 2, Self::Back => 3, Self::Forward => 4, - Self::Other(other) => 5 + *other as u32, + Self::Other(other) => 5 + u32::from(*other), } } } diff --git a/engine/src/windowing/window.rs b/engine/src/windowing/window.rs index 6d7a464..ae4a36b 100644 --- a/engine/src/windowing/window.rs +++ b/engine/src/windowing/window.rs @@ -59,6 +59,7 @@ macro_rules! gen_creation_attrs_with_fn { paste::paste! { impl CreationAttributes { + #[must_use] pub fn [](mut self, new: impl Into<$field_type>) -> Self { self.$field = new.into(); @@ -93,12 +94,9 @@ impl CreationAttributes .with_title(self.title.into_owned()) .with_transparent(self.transparent) .with_maximized(self.maximized) - .with_fullscreen(match self.fullscreen { - Some(Fullscreen::Borderless) => { - Some(winit::window::Fullscreen::Borderless(None)) - } - None => None, - }) + .with_fullscreen(self.fullscreen.map(|Fullscreen::Borderless| { + winit::window::Fullscreen::Borderless(None) + })) .with_visible(self.visible) .with_resizable(self.resizable) .with_window_icon(match self.icon { @@ -212,11 +210,13 @@ pub struct Window impl Window { + #[must_use] pub fn wid(&self) -> Id { self.wid } + #[must_use] pub fn scale_factor(&self) -> f64 { self.scale_factor @@ -243,11 +243,11 @@ impl Window winit_window.set_title(&self.title); winit_window.set_cursor_visible(self.cursor_visible); - let curr_inner_size = winit_window.inner_size().clone().into(); + let curr_inner_size = winit_window.inner_size().into(); - let inner_size_request_result = match winit_window.request_inner_size( - winit::dpi::Size::Physical(self.inner_size.clone().into()), - ) { + let inner_size_request_result = match winit_window + .request_inner_size(winit::dpi::Size::Physical(self.inner_size.into())) + { // The comparison of curr_inner_size is in case the user's windowing system // lies about using the requested inner size None if curr_inner_size == self.inner_size => Ok(()), -- cgit v1.2.3-18-g5258