Skip to content

Commit

Permalink
fix: add missing intoiter
Browse files Browse the repository at this point in the history
This removes a clippy lint, which states anything with `.iter()` should implement IntoIter (and likewise for mut).

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii authored and becheran committed Dec 22, 2024
1 parent 8c09cca commit 25fd310
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ impl<T> Grid<T> {
/// assert_eq!(iter.next(), Some(&4));
/// assert_eq!(iter.next(), None);
/// ```
#[allow(clippy::iter_without_into_iter)]
pub fn iter(&self) -> Iter<T> {
self.data.iter()
}
Expand All @@ -653,7 +652,6 @@ impl<T> Grid<T> {
/// assert_eq!(next, Some(&mut 1));
/// *next.unwrap() = 10;
/// ```
#[allow(clippy::iter_without_into_iter)]
pub fn iter_mut(&mut self) -> IterMut<T> {
self.data.iter_mut()
}
Expand Down Expand Up @@ -1643,6 +1641,24 @@ impl<T> IndexMut<(usize, usize)> for Grid<T> {
}
}

impl<'a, T> IntoIterator for &'a Grid<T> {
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<T> {
type IntoIter = core::slice::IterMut<'a, T>;
type Item = &'a mut T;

fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}

impl<T: fmt::Debug> fmt::Debug for Grid<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[")?;
Expand Down Expand Up @@ -2710,6 +2726,23 @@ mod test {
assert_eq!(iter.next(), None);
}

#[test]
fn into_iter() {
let grid: Grid<u8> = grid![[1,1][1,1]];
for val in &grid {
assert_eq!(val, &1);
}
}

#[test]
fn into_iter_mut() {
let mut grid: Grid<u8> = 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<u8> = grid![[1,2][3,4]];
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 25fd310

Please sign in to comment.