diff --git a/src/lib.rs b/src/lib.rs index 48b8799..3b6d9a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -636,7 +636,6 @@ impl Grid { /// assert_eq!(iter.next(), Some(&4)); /// assert_eq!(iter.next(), None); /// ``` - #[allow(clippy::iter_without_into_iter)] pub fn iter(&self) -> Iter { self.data.iter() } @@ -653,7 +652,6 @@ impl Grid { /// assert_eq!(next, Some(&mut 1)); /// *next.unwrap() = 10; /// ``` - #[allow(clippy::iter_without_into_iter)] pub fn iter_mut(&mut self) -> IterMut { self.data.iter_mut() } @@ -1643,6 +1641,24 @@ impl IndexMut<(usize, usize)> for Grid { } } +impl<'a, T> IntoIterator for &'a Grid { + type IntoIter = core::slice::Iter<'a, T>; + type Item = &'a T; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + +impl<'a, T> IntoIterator for &'a mut Grid { + type IntoIter = core::slice::IterMut<'a, T>; + type Item = &'a mut T; + + fn into_iter(self) -> Self::IntoIter { + self.iter_mut() + } +} + impl fmt::Debug for Grid { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "[")?; @@ -2710,6 +2726,23 @@ mod test { assert_eq!(iter.next(), None); } + #[test] + fn into_iter() { + let grid: Grid = grid![[1,1][1,1]]; + for val in &grid { + assert_eq!(val, &1); + } + } + + #[test] + fn into_iter_mut() { + let mut grid: Grid = grid![[1,1][1,1]]; + for val in &mut grid { + *val = 2; + } + assert_eq!(grid, grid![[2, 2][2, 2]]); + } + #[test] fn indexed_iter() { let grid: Grid = grid![[1,2][3,4]]; @@ -2926,7 +2959,7 @@ mod test { impl Person { fn new(name: &str, precise_age: f32) -> Self { - Person { + Self { _name: name.into(), _precise_age: precise_age, }