From 08a7a8f962c195eac997100c9089fe555e84446b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Lescaudey=20de=20Maneville?= Date: Tue, 20 Feb 2024 11:03:00 +0100 Subject: [PATCH] Less allocation --- src/components/cell/hexagon_2d_cell.rs | 10 +++++----- src/components/cell/mod.rs | 2 +- src/components/cell/moore_2d_cell.rs | 10 +++++----- src/components/cell/moore_3d_cell.rs | 4 ++-- src/components/cell/neumann_2d_cell.rs | 10 +++++----- src/components/cell/neumann_3d_cell.rs | 4 ++-- src/systems/cells.rs | 2 +- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/components/cell/hexagon_2d_cell.rs b/src/components/cell/hexagon_2d_cell.rs index 52053e8..f178221 100644 --- a/src/components/cell/hexagon_2d_cell.rs +++ b/src/components/cell/hexagon_2d_cell.rs @@ -57,8 +57,8 @@ impl Cell for HexagonCell2d { } #[inline] - fn neighbor_coordinates(&self) -> Vec { - NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec() + fn neighbor_coordinates(&self) -> impl IntoIterator { + NEIGHBOR_COORDINATES.map(|c| c + *self.coords()) } } @@ -80,7 +80,7 @@ mod tests { let cell = HexagonCell2d { coords: IVec3::new(10, 10, 10), }; - let neighbors = cell.neighbor_coordinates(); + let neighbors = cell.neighbor_coordinates().into_iter().collect::>(); assert_eq!( neighbors, vec![ @@ -99,7 +99,7 @@ mod tests { let cell = HexagonCell2d { coords: IVec3::new(-10, 8, 5), }; - let neighbors = cell.neighbor_coordinates(); + let neighbors = cell.neighbor_coordinates().into_iter().collect::>(); assert_eq!( neighbors, vec![ @@ -118,7 +118,7 @@ mod tests { let cell = HexagonCell2d { coords: IVec3::new(0, 0, 0), }; - let neighbors = cell.neighbor_coordinates(); + let neighbors = cell.neighbor_coordinates().into_iter().collect::>(); assert_eq!( neighbors, vec![ diff --git a/src/components/cell/mod.rs b/src/components/cell/mod.rs index 5130546..918448a 100644 --- a/src/components/cell/mod.rs +++ b/src/components/cell/mod.rs @@ -28,5 +28,5 @@ pub trait Cell: Clone + Component { /// Retrieves the coordinates of the neighbor cells #[must_use] - fn neighbor_coordinates(&self) -> Vec; + fn neighbor_coordinates(&self) -> impl IntoIterator; } diff --git a/src/components/cell/moore_2d_cell.rs b/src/components/cell/moore_2d_cell.rs index a221ab7..4febfcf 100644 --- a/src/components/cell/moore_2d_cell.rs +++ b/src/components/cell/moore_2d_cell.rs @@ -63,8 +63,8 @@ impl Cell for MooreCell2d { } #[inline] - fn neighbor_coordinates(&self) -> Vec { - NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec() + fn neighbor_coordinates(&self) -> impl IntoIterator { + NEIGHBOR_COORDINATES.map(|c| c + *self.coords()) } } @@ -86,7 +86,7 @@ mod tests { let cell = MooreCell2d { coords: IVec2::new(10, 10), }; - let neighbors = cell.neighbor_coordinates(); + let neighbors = cell.neighbor_coordinates().into_iter().collect::>(); assert_eq!( neighbors, vec![ @@ -115,7 +115,7 @@ mod tests { let cell = MooreCell2d { coords: IVec2::new(-10, 10), }; - let neighbors = cell.neighbor_coordinates(); + let neighbors = cell.neighbor_coordinates().into_iter().collect::>(); assert_eq!( neighbors, vec![ @@ -144,7 +144,7 @@ mod tests { let cell = MooreCell2d { coords: IVec2::new(0, 0), }; - let neighbors = cell.neighbor_coordinates(); + let neighbors = cell.neighbor_coordinates().into_iter().collect::>(); assert_eq!( neighbors, vec![ diff --git a/src/components/cell/moore_3d_cell.rs b/src/components/cell/moore_3d_cell.rs index b9a7a48..217af94 100644 --- a/src/components/cell/moore_3d_cell.rs +++ b/src/components/cell/moore_3d_cell.rs @@ -89,8 +89,8 @@ impl Cell for MooreCell3d { } #[inline] - fn neighbor_coordinates(&self) -> Vec { - NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec() + fn neighbor_coordinates(&self) -> impl IntoIterator { + NEIGHBOR_COORDINATES.map(|c| c + *self.coords()) } } diff --git a/src/components/cell/neumann_2d_cell.rs b/src/components/cell/neumann_2d_cell.rs index 9715e94..6cac38a 100644 --- a/src/components/cell/neumann_2d_cell.rs +++ b/src/components/cell/neumann_2d_cell.rs @@ -54,8 +54,8 @@ impl Cell for NeumannCell2d { } #[inline] - fn neighbor_coordinates(&self) -> Vec { - NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec() + fn neighbor_coordinates(&self) -> impl IntoIterator { + NEIGHBOR_COORDINATES.map(|c| c + *self.coords()) } } @@ -77,7 +77,7 @@ mod tests { let cell = NeumannCell2d { coords: IVec2::new(10, 10), }; - let neighbors = cell.neighbor_coordinates(); + let neighbors = cell.neighbor_coordinates().into_iter().collect::>(); assert_eq!( neighbors, vec![ @@ -98,7 +98,7 @@ mod tests { let cell = NeumannCell2d { coords: IVec2::new(-10, 10), }; - let neighbors = cell.neighbor_coordinates(); + let neighbors = cell.neighbor_coordinates().into_iter().collect::>(); assert_eq!( neighbors, vec![ @@ -119,7 +119,7 @@ mod tests { let cell = NeumannCell2d { coords: IVec2::new(0, 0), }; - let neighbors = cell.neighbor_coordinates(); + let neighbors = cell.neighbor_coordinates().into_iter().collect::>(); assert_eq!( neighbors, vec![ diff --git a/src/components/cell/neumann_3d_cell.rs b/src/components/cell/neumann_3d_cell.rs index f587cfc..054fdc5 100644 --- a/src/components/cell/neumann_3d_cell.rs +++ b/src/components/cell/neumann_3d_cell.rs @@ -50,8 +50,8 @@ impl Cell for NeumannCell3d { } #[inline] - fn neighbor_coordinates(&self) -> Vec { - NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec() + fn neighbor_coordinates(&self) -> impl IntoIterator { + NEIGHBOR_COORDINATES.map(|c| c + *self.coords()) } } diff --git a/src/systems/cells.rs b/src/systems/cells.rs index 51217dd..a0de9bb 100644 --- a/src/systems/cells.rs +++ b/src/systems/cells.rs @@ -11,7 +11,7 @@ where S: CellState, { let neighbor_coords = cell.neighbor_coordinates(); - let neighbor_states = neighbor_coords.iter().filter_map(|c| map.get(c)); + let neighbor_states = neighbor_coords.into_iter().filter_map(|c| map.get(&c)); let new_state = state.new_cell_state(neighbor_states); if &new_state == state { None