From a0f06f0e492c2eddeb9b9bd3d60e426992b04b02 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Fri, 27 Dec 2024 17:03:51 +0100 Subject: [PATCH] fix(style): make some functions `const` --- benches/algos-fill.rs | 4 ++-- benches/algos.rs | 4 ++-- examples/sliding-puzzle.rs | 4 ++-- src/grid.rs | 6 +++--- src/matrix.rs | 2 +- tests/pathfinding.rs | 2 +- tests/r299.rs | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/benches/algos-fill.rs b/benches/algos-fill.rs index 704bd415..73e36383 100644 --- a/benches/algos-fill.rs +++ b/benches/algos-fill.rs @@ -12,7 +12,7 @@ struct Pt { } impl Pt { - fn new(x: u16, y: u16) -> Pt { + const fn new(x: u16, y: u16) -> Pt { Pt { x, y, @@ -21,7 +21,7 @@ impl Pt { } #[inline] - fn heuristic(p: &Pt) -> usize { + const fn heuristic(p: &Pt) -> usize { (64 - p.x - p.y) as usize } } diff --git a/benches/algos.rs b/benches/algos.rs index 58e0b058..c4cc0c8c 100644 --- a/benches/algos.rs +++ b/benches/algos.rs @@ -15,12 +15,12 @@ struct Pt { } impl Pt { - fn new(x: u16, y: u16) -> Pt { + const fn new(x: u16, y: u16) -> Pt { Pt { x, y } } #[inline] - fn heuristic(p: &Pt) -> usize { + const fn heuristic(p: &Pt) -> usize { (128 - p.x - p.y) as usize } } diff --git a/examples/sliding-puzzle.rs b/examples/sliding-puzzle.rs index 79ee9921..1c91cb88 100644 --- a/examples/sliding-puzzle.rs +++ b/examples/sliding-puzzle.rs @@ -68,12 +68,12 @@ impl Game { } #[inline] - fn x(pos: u8) -> u8 { + const fn x(pos: u8) -> u8 { pos % SIDE } #[inline] - fn y(pos: u8) -> u8 { + const fn y(pos: u8) -> u8 { pos / SIDE } diff --git a/src/grid.rs b/src/grid.rs index 316d59b3..289f4050 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -118,7 +118,7 @@ impl Grid { /// is located outside the grid. #[inline] #[must_use] - pub fn is_inside(&self, vertex: (usize, usize)) -> bool { + pub const fn is_inside(&self, vertex: (usize, usize)) -> bool { vertex.0 < self.width && vertex.1 < self.height } @@ -167,7 +167,7 @@ impl Grid { /// Return the number of positions in this grid. #[must_use] - pub fn size(&self) -> usize { + pub const fn size(&self) -> usize { self.width * self.height } @@ -333,7 +333,7 @@ impl Grid { /// Iterate over edges. #[must_use] - pub fn edges(&self) -> EdgesIterator { + pub const fn edges(&self) -> EdgesIterator { EdgesIterator { grid: self, x: 0, diff --git a/src/matrix.rs b/src/matrix.rs index 0b71c1ef..d9f4a27a 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -613,7 +613,7 @@ impl Matrix { /// requires building vectors of column data which are not stored /// consecutively in memory. #[must_use] - pub fn column_iter(&self) -> ColumnIterator<'_, C> { + pub const fn column_iter(&self) -> ColumnIterator<'_, C> { ColumnIterator { matrix: self, column: 0, diff --git a/tests/pathfinding.rs b/tests/pathfinding.rs index 2248454a..1daff697 100644 --- a/tests/pathfinding.rs +++ b/tests/pathfinding.rs @@ -186,7 +186,7 @@ mod ex2 { .collect() } - fn distance(&(x1, y1): &(usize, usize), &(x2, y2): &(usize, usize)) -> usize { + const fn distance(&(x1, y1): &(usize, usize), &(x2, y2): &(usize, usize)) -> usize { x1.abs_diff(x2) + y1.abs_diff(y2) } diff --git a/tests/r299.rs b/tests/r299.rs index 2eb3a0bb..48c22c82 100644 --- a/tests/r299.rs +++ b/tests/r299.rs @@ -52,7 +52,7 @@ fn parse(input: &str) -> (Vec, HashMap) { (nodes, successors) } -fn distance(a: &Point, b: &Point) -> usize { +const fn distance(a: &Point, b: &Point) -> usize { a.row.abs_diff(b.row) + a.col.abs_diff(b.col) }