From dcc68ffc00a3d7404b5ac2ae39986b5d524a3f8d Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Sun, 8 Dec 2024 19:45:43 +0200 Subject: [PATCH] chore(lint): fix clippy warnings --- Cargo.lock | 10 ++++---- Cargo.toml | 8 +++++- src/builder.rs | 48 ++++++++++++++++++++---------------- src/generator.rs | 11 +++++---- src/{maze.rs => hex_maze.rs} | 8 ++++-- src/{tile.rs => hex_tile.rs} | 11 +++++++-- src/lib.rs | 8 +++--- src/walls.rs | 24 ++++++++++++------ 8 files changed, 81 insertions(+), 47 deletions(-) rename src/{maze.rs => hex_maze.rs} (97%) rename src/{tile.rs => hex_tile.rs} (96%) diff --git a/Cargo.lock b/Cargo.lock index 66c5011..d8de1f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2063,7 +2063,7 @@ checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df" [[package]] name = "hexlab" -version = "0.1.4" +version = "0.1.5" dependencies = [ "bevy", "hexx", @@ -2251,9 +2251,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.161" +version = "0.2.167" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" [[package]] name = "libloading" @@ -3548,9 +3548,9 @@ dependencies = [ [[package]] name = "ttf-parser" -version = "0.25.0" +version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5902c5d130972a0000f60860bfbf46f7ca3db5391eddfedd1b8728bd9dc96c0e" +checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31" [[package]] name = "twox-hash" diff --git a/Cargo.toml b/Cargo.toml index 8fe2f67..31334b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "hexlab" authors = ["Kristofers Solo "] -version = "0.1.4" +version = "0.1.5" edition = "2021" description = "A hexagonal maze generation and manipulation library" repository = "https://github.com/kristoferssolo/hexlab" @@ -46,3 +46,9 @@ panic = "abort" # Smaller binary size [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] + +[lints.clippy] +pedantic = "warn" +nursery = "warn" +unwrap_used = "warn" +expect_used = "warn" diff --git a/src/builder.rs b/src/builder.rs index 11917ae..f62204c 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -76,6 +76,7 @@ pub enum MazeBuilderError { /// .build() /// .expect("Failed to create maze"); /// ``` +#[allow(clippy::module_name_repetitions)] #[derive(Default)] pub struct MazeBuilder { radius: Option, @@ -87,6 +88,7 @@ pub struct MazeBuilder { impl MazeBuilder { /// Creates a new [`MazeBuilder`] instance. #[inline] + #[must_use] pub fn new() -> Self { Self::default() } @@ -97,7 +99,8 @@ impl MazeBuilder { /// /// * `radius` - The size of the maze (number of tiles along one edge). #[inline] - pub fn with_radius(mut self, radius: u32) -> Self { + #[must_use] + pub const fn with_radius(mut self, radius: u32) -> Self { self.radius = Some(radius); self } @@ -108,7 +111,8 @@ impl MazeBuilder { /// /// * `seed` - The random seed value. #[inline] - pub fn with_seed(mut self, seed: u64) -> Self { + #[must_use] + pub const fn with_seed(mut self, seed: u64) -> Self { self.seed = Some(seed); self } @@ -121,13 +125,15 @@ impl MazeBuilder { /// /// * `generator_type` - The maze generation algorithm to use. #[inline] - pub fn with_generator(mut self, generator_type: GeneratorType) -> Self { + #[must_use] + pub const fn with_generator(mut self, generator_type: GeneratorType) -> Self { self.generator_type = generator_type; self } #[inline] - pub fn with_start_position(mut self, pos: Hex) -> Self { + #[must_use] + pub const fn with_start_position(mut self, pos: Hex) -> Self { self.start_position = Some(pos); self } @@ -159,7 +165,7 @@ impl MazeBuilder { /// ``` pub fn build(self) -> Result { let radius = self.radius.ok_or(MazeBuilderError::NoRadius)?; - let mut maze = self.create_hex_maze(radius); + let mut maze = create_hex_maze(radius); if let Some(start_pos) = self.start_position { if maze.get_tile(&start_pos).is_none() { @@ -174,29 +180,29 @@ impl MazeBuilder { Ok(maze) } - fn create_hex_maze(&self, radius: u32) -> HexMaze { - let mut maze = HexMaze::new(); - let radius = radius as i32; - for q in -radius..=radius { - let r1 = (-radius).max(-q - radius); - let r2 = radius.min(-q + radius); - for r in r1..=r2 { - let pos = Hex::new(q, r); - maze.add_tile(pos); - } - } - - maze - } - fn generate_maze(&self, maze: &mut HexMaze) { match self.generator_type { GeneratorType::RecursiveBacktracking => { - generate_backtracking(maze, self.start_position, self.seed) + generate_backtracking(maze, self.start_position, self.seed); } } } } +fn create_hex_maze(radius: u32) -> HexMaze { + let mut maze = HexMaze::new(); + let radius = i32::try_from(radius).unwrap_or(5); + + for q in -radius..=radius { + let r1 = (-radius).max(-q - radius); + let r2 = radius.min(-q + radius); + for r in r1..=r2 { + let pos = Hex::new(q, r); + maze.add_tile(pos); + } + } + + maze +} #[cfg(test)] mod test { diff --git a/src/generator.rs b/src/generator.rs index 6b7b1a8..527911a 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -6,13 +6,14 @@ use rand_chacha::ChaCha8Rng; use crate::HexMaze; +#[allow(clippy::module_name_repetitions)] #[derive(Debug, Clone, Copy, Default)] pub enum GeneratorType { #[default] RecursiveBacktracking, } -pub(crate) fn generate_backtracking(maze: &mut HexMaze, start_pos: Option, seed: Option) { +pub fn generate_backtracking(maze: &mut HexMaze, start_pos: Option, seed: Option) { if maze.is_empty() { return; } @@ -21,10 +22,10 @@ pub(crate) fn generate_backtracking(maze: &mut HexMaze, start_pos: Option, let mut visited = HashSet::new(); - let mut rng: Box = match seed { - Some(seed) => Box::new(ChaCha8Rng::seed_from_u64(seed)), - None => Box::new(thread_rng()), - }; + let mut rng: Box = seed.map_or_else( + || Box::new(thread_rng()) as Box, + |seed| Box::new(ChaCha8Rng::seed_from_u64(seed)) as Box, + ); recursive_backtrack(maze, start, &mut visited, &mut rng); } diff --git a/src/maze.rs b/src/hex_maze.rs similarity index 97% rename from src/maze.rs rename to src/hex_maze.rs index 2335240..db2fa60 100644 --- a/src/maze.rs +++ b/src/hex_maze.rs @@ -15,6 +15,7 @@ pub struct HexMaze(HashMap); impl HexMaze { /// Creates a new empty maze #[inline] + #[must_use] pub fn new() -> Self { Self::default() } @@ -28,29 +29,32 @@ impl HexMaze { /// Adds a wall in the specified direction at the given coordinates pub fn add_wall(&mut self, coord: Hex, direction: EdgeDirection) { if let Some(tile) = self.0.get_mut(&coord) { - tile.walls.add(direction) + tile.walls.add(direction); } } /// Returns a reference to the tile at the specified coordinates #[inline] + #[must_use] pub fn get_tile(&self, coord: &Hex) -> Option<&HexTile> { self.0.get(coord) } /// Returns a reference to the walls at the specified coordinates pub fn get_walls(&self, coord: &Hex) -> Option<&Walls> { - self.0.get(coord).map(|tile| tile.walls()) + self.0.get(coord).map(HexTile::walls) } /// Returns the number of tiles in the maze #[inline] + #[must_use] pub fn len(&self) -> usize { self.0.len() } /// Returns true if the maze is empty #[inline] + #[must_use] pub fn is_empty(&self) -> bool { self.0.is_empty() } diff --git a/src/tile.rs b/src/hex_tile.rs similarity index 96% rename from src/tile.rs rename to src/hex_tile.rs index dc6e4f8..23f499c 100644 --- a/src/tile.rs +++ b/src/hex_tile.rs @@ -21,6 +21,7 @@ pub struct HexTile { impl HexTile { /// Creates a new tile with pos and default walls + #[must_use] pub fn new(pos: Hex) -> Self { Self { pos, @@ -30,22 +31,28 @@ impl HexTile { /// Returns a reference to the tile's walls #[inline] - pub fn walls(&self) -> &Walls { + #[must_use] + pub const fn walls(&self) -> &Walls { &self.walls } /// Returns position of the tile #[inline] - pub fn pos(&self) -> Hex { + #[must_use] + pub const fn pos(&self) -> Hex { self.pos } #[cfg(feature = "bevy")] + #[inline] + #[must_use] pub fn to_vec2(&self, layout: &HexLayout) -> Vec2 { layout.hex_to_world_pos(self.pos) } #[cfg(feature = "bevy")] + #[inline] + #[must_use] pub fn to_vec3(&self, layout: &HexLayout) -> Vec3 { let pos = self.to_vec2(layout); Vec3::new(pos.x, 0., pos.y) diff --git a/src/lib.rs b/src/lib.rs index 0e8e955..fd59fad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,13 @@ mod builder; mod generator; -mod maze; -mod tile; +mod hex_maze; +mod hex_tile; mod walls; pub use builder::{MazeBuilder, MazeBuilderError}; pub use generator::GeneratorType; -pub use maze::HexMaze; -pub use tile::HexTile; +pub use hex_maze::HexMaze; +pub use hex_tile::HexTile; pub use walls::Walls; pub mod prelude { diff --git a/src/walls.rs b/src/walls.rs index 25da5bf..74ce546 100644 --- a/src/walls.rs +++ b/src/walls.rs @@ -62,6 +62,8 @@ impl Walls { /// assert!(walls.is_closed()); /// assert_eq!(walls.count(), 6); /// ``` + #[inline] + #[must_use] pub fn new() -> Self { Self::default() } @@ -77,7 +79,9 @@ impl Walls { /// assert!(walls.is_empty()); /// assert_eq!(walls.count(), 0); /// ``` - pub fn empty() -> Self { + #[inline] + #[must_use] + pub const fn empty() -> Self { Self(0) } @@ -95,7 +99,9 @@ impl Walls { /// let walls = Walls::new(); /// assert!(!walls.is_empty()); /// ``` - pub fn is_empty(&self) -> bool { + #[inline] + #[must_use] + pub const fn is_empty(&self) -> bool { self.0 == 0 } @@ -193,7 +199,8 @@ impl Walls { /// assert_eq!(walls.as_bits(), 0b111111); /// ``` #[inline] - pub fn as_bits(&self) -> u8 { + #[must_use] + pub const fn as_bits(&self) -> u8 { self.0 } @@ -215,8 +222,9 @@ impl Walls { /// assert_eq!(walls.count(), 2); /// ``` #[inline] + #[must_use] pub fn count(&self) -> u8 { - self.0.count_ones() as u8 + u8::try_from(self.0.count_ones()).unwrap_or_default() } /// Returns all possible directions as a `Walls` value @@ -234,8 +242,9 @@ impl Walls { /// assert!(all_walls.is_closed()); /// ``` #[inline] - pub fn all_directions() -> Self { - Self(0b111111) + #[must_use] + pub const fn all_directions() -> Self { + Self(0b11_1111) } /// Toggles a wall in the specified direction. @@ -293,6 +302,7 @@ impl Walls { /// assert!(walls.is_closed()); /// ``` #[inline] + #[must_use] pub fn is_closed(&self) -> bool { self.count() == 6 } @@ -354,7 +364,7 @@ impl From<[EdgeDirection; N]> for Walls { impl Default for Walls { fn default() -> Self { - Self(0b111111) + Self(0b11_1111) } }