From 1b393cd612cca9ffe0d6c9b9a129a6ca11ea2999 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Sat, 11 Jun 2022 20:40:12 +0100 Subject: [PATCH] Fix new clippy lints in tiled --- .../display/tiled/infinite_scrolled_map.rs | 5 +-- agb/src/display/tiled/map.rs | 3 +- agb/src/display/tiled/mod.rs | 32 +++++++++---------- agb/src/display/tiled/vram_manager.rs | 17 +++++++--- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/agb/src/display/tiled/infinite_scrolled_map.rs b/agb/src/display/tiled/infinite_scrolled_map.rs index 41dc67d..98c2d6e 100644 --- a/agb/src/display/tiled/infinite_scrolled_map.rs +++ b/agb/src/display/tiled/infinite_scrolled_map.rs @@ -24,6 +24,7 @@ pub enum PartialUpdateStatus { } impl<'a> InfiniteScrolledMap<'a> { + #[must_use] pub fn new( map: MapLoan<'a, RegularMap>, tile: Box) -> (&'a TileSet<'a>, TileSetting) + 'a>, @@ -190,8 +191,8 @@ impl<'a> InfiniteScrolledMap<'a> { let current_scroll = self.map.scroll_pos(); let new_scroll = ( - size.px_offset_x(current_scroll.x as i32 + difference.x), - size.px_offset_y(current_scroll.y as i32 + difference.y), + size.px_offset_x(i32::from(current_scroll.x) + difference.x), + size.px_offset_y(i32::from(current_scroll.y) + difference.y), ) .into(); diff --git a/agb/src/display/tiled/map.rs b/agb/src/display/tiled/map.rs index 34cdc77..fbe5b69 100644 --- a/agb/src/display/tiled/map.rs +++ b/agb/src/display/tiled/map.rs @@ -105,7 +105,7 @@ impl RegularMap { pub fn commit(&mut self, vram: &mut VRamManager) { let new_bg_control_value = (self.priority as u16) - | ((self.screenblock as u16) << 8) + | (u16::from(self.screenblock) << 8) | (self.size.size_flag() << 14); self.bg_control_register().set(new_bg_control_value); @@ -136,6 +136,7 @@ impl RegularMap { self.y_scroll = pos.y; } + #[must_use] pub fn scroll_pos(&self) -> Vector2D { (self.x_scroll, self.y_scroll).into() } diff --git a/agb/src/display/tiled/mod.rs b/agb/src/display/tiled/mod.rs index 1590779..37e2f44 100644 --- a/agb/src/display/tiled/mod.rs +++ b/agb/src/display/tiled/mod.rs @@ -18,25 +18,23 @@ pub enum RegularBackgroundSize { } impl RegularBackgroundSize { + #[must_use] pub fn width(&self) -> u32 { match self { - RegularBackgroundSize::Background32x32 => 32, - RegularBackgroundSize::Background64x32 => 64, - RegularBackgroundSize::Background32x64 => 32, - RegularBackgroundSize::Background64x64 => 64, + RegularBackgroundSize::Background32x64 | RegularBackgroundSize::Background32x32 => 32, + RegularBackgroundSize::Background64x64 | RegularBackgroundSize::Background64x32 => 64, } } + #[must_use] pub fn height(&self) -> u32 { match self { - RegularBackgroundSize::Background32x32 => 32, - RegularBackgroundSize::Background64x32 => 32, - RegularBackgroundSize::Background32x64 => 64, - RegularBackgroundSize::Background64x64 => 64, + RegularBackgroundSize::Background32x32 | RegularBackgroundSize::Background64x32 => 32, + RegularBackgroundSize::Background32x64 | RegularBackgroundSize::Background64x64 => 64, } } - pub(crate) fn size_flag(&self) -> u16 { + pub(crate) fn size_flag(self) -> u16 { match self { RegularBackgroundSize::Background32x32 => 0, RegularBackgroundSize::Background64x32 => 1, @@ -45,17 +43,17 @@ impl RegularBackgroundSize { } } - pub(crate) fn num_tiles(&self) -> usize { + pub(crate) fn num_tiles(self) -> usize { (self.width() * self.height()) as usize } - pub(crate) fn num_screen_blocks(&self) -> usize { + pub(crate) fn num_screen_blocks(self) -> usize { self.num_tiles() / (32 * 32) } // This is hilariously complicated due to how the GBA stores the background screenblocks. // See https://www.coranac.com/tonc/text/regbg.htm#sec-map for an explanation - pub(crate) fn gba_offset(&self, pos: Vector2D) -> usize { + pub(crate) fn gba_offset(self, pos: Vector2D) -> usize { let x_mod = pos.x & (self.width() as u16 - 1); let y_mod = pos.y & (self.height() as u16 - 1); @@ -66,19 +64,19 @@ impl RegularBackgroundSize { pos as usize } - pub(crate) fn tile_pos_x(&self, x: i32) -> u16 { + pub(crate) fn tile_pos_x(self, x: i32) -> u16 { ((x as u32) & (self.width() - 1)) as u16 } - pub(crate) fn tile_pos_y(&self, y: i32) -> u16 { + pub(crate) fn tile_pos_y(self, y: i32) -> u16 { ((y as u32) & (self.height() - 1)) as u16 } - pub(crate) fn px_offset_x(&self, x: i32) -> u16 { + pub(crate) fn px_offset_x(self, x: i32) -> u16 { ((x as u32) & (self.width() * 8 - 1)) as u16 } - pub(crate) fn px_offset_y(&self, y: i32) -> u16 { + pub(crate) fn px_offset_y(self, y: i32) -> u16 { ((y as u32) & (self.height() * 8 - 1)) as u16 } } @@ -101,6 +99,7 @@ impl Tile { pub struct TileSetting(u16); impl TileSetting { + #[must_use] pub const fn new(tile_id: u16, hflip: bool, vflip: bool, palette_id: u8) -> Self { Self( (tile_id & ((1 << 10) - 1)) @@ -110,6 +109,7 @@ impl TileSetting { ) } + #[must_use] pub const fn from_raw(raw: u16) -> Self { Self(raw) } diff --git a/agb/src/display/tiled/vram_manager.rs b/agb/src/display/tiled/vram_manager.rs index bedbe80..a2a2ce1 100644 --- a/agb/src/display/tiled/vram_manager.rs +++ b/agb/src/display/tiled/vram_manager.rs @@ -44,6 +44,7 @@ pub struct TileSet<'a> { } impl<'a> TileSet<'a> { + #[must_use] pub fn new(tiles: &'a [u8], format: TileFormat) -> Self { Self { tiles, format } } @@ -53,7 +54,7 @@ impl<'a> TileSet<'a> { } } -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub struct TileIndex(u16); impl TileIndex { @@ -61,7 +62,7 @@ impl TileIndex { Self(index as u16) } - pub(crate) const fn index(&self) -> u16 { + pub(crate) const fn index(self) -> u16 { self.0 } } @@ -128,8 +129,9 @@ pub struct DynamicTile<'a> { } impl DynamicTile<'_> { + #[must_use] pub fn fill_with(self, colour_index: u8) -> Self { - let colour_index = colour_index as u32; + let colour_index = u32::from(colour_index); let mut value = 0; for i in 0..8 { @@ -142,6 +144,7 @@ impl DynamicTile<'_> { } impl DynamicTile<'_> { + #[must_use] pub fn tile_set(&self) -> TileSet<'_> { let tiles = unsafe { slice::from_raw_parts_mut( @@ -153,6 +156,7 @@ impl DynamicTile<'_> { TileSet::new(tiles, TileFormat::FourBpp) } + #[must_use] pub fn tile_index(&self) -> u16 { let difference = self.tile_data.as_ptr() as usize - TILE_RAM_START; (difference / (8 * 8 / 2)) as u16 @@ -188,6 +192,7 @@ impl VRamManager { TileReference(NonNull::new(ptr as *mut _).unwrap()) } + #[must_use] pub fn new_dynamic_tile<'a>(&mut self) -> DynamicTile<'a> { let tile_format = TileFormat::FourBpp; let new_reference: NonNull = @@ -227,6 +232,8 @@ impl VRamManager { } } + // This needs to take ownership of the dynamic tile because it will no longer be valid after this call + #[allow(clippy::needless_pass_by_value)] pub fn remove_dynamic_tile(&mut self, dynamic_tile: DynamicTile<'_>) { let pointer = NonNull::new(dynamic_tile.tile_data.as_mut_ptr() as *mut _).unwrap(); let tile_reference = TileReference(pointer); @@ -336,7 +343,7 @@ impl VRamManager { tile_slice.as_ptr() as *const u16, target_location, tile_size_in_half_words, - ) + ); }; } @@ -356,7 +363,7 @@ impl VRamManager { /// Copies palettes to the background palettes without any checks. pub fn set_background_palettes(&mut self, palettes: &[palette16::Palette16]) { for (palette_index, entry) in palettes.iter().enumerate() { - self.set_background_palette(palette_index as u8, entry) + self.set_background_palette(palette_index as u8, entry); } } }