Skip to content

Commit 6f16f50

Browse files
committed
fix: add missing intoiter
This removes a clippy lint, which states anything with `.iter()` should implement IntoIter (and likewise for mut). Signed-off-by: Henry Schreiner <[email protected]>
1 parent 8c09cca commit 6f16f50

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/lib.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,6 @@ impl<T> Grid<T> {
636636
/// assert_eq!(iter.next(), Some(&4));
637637
/// assert_eq!(iter.next(), None);
638638
/// ```
639-
#[allow(clippy::iter_without_into_iter)]
640639
pub fn iter(&self) -> Iter<T> {
641640
self.data.iter()
642641
}
@@ -653,7 +652,6 @@ impl<T> Grid<T> {
653652
/// assert_eq!(next, Some(&mut 1));
654653
/// *next.unwrap() = 10;
655654
/// ```
656-
#[allow(clippy::iter_without_into_iter)]
657655
pub fn iter_mut(&mut self) -> IterMut<T> {
658656
self.data.iter_mut()
659657
}
@@ -1643,6 +1641,22 @@ impl<T> IndexMut<(usize, usize)> for Grid<T> {
16431641
}
16441642
}
16451643

1644+
impl<'a, T> IntoIterator for &'a Grid<T> {
1645+
type IntoIter = core::slice::Iter<'a, T>;
1646+
type Item = &'a T;
1647+
fn into_iter(self) -> Self::IntoIter {
1648+
self.iter()
1649+
}
1650+
}
1651+
1652+
impl<'a, T> IntoIterator for &'a mut Grid<T> {
1653+
type IntoIter = core::slice::IterMut<'a, T>;
1654+
type Item = &'a mut T;
1655+
fn into_iter(self) -> Self::IntoIter {
1656+
self.iter_mut()
1657+
}
1658+
}
1659+
16461660
impl<T: fmt::Debug> fmt::Debug for Grid<T> {
16471661
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16481662
write!(f, "[")?;
@@ -2710,6 +2724,23 @@ mod test {
27102724
assert_eq!(iter.next(), None);
27112725
}
27122726

2727+
#[test]
2728+
fn into_iter() {
2729+
let grid: Grid<u8> = grid![[1,1][1,1]];
2730+
for val in &grid {
2731+
assert_eq!(val, &1);
2732+
}
2733+
}
2734+
2735+
#[test]
2736+
fn into_iter_mut() {
2737+
let mut grid: Grid<u8> = grid![[1,1][1,1]];
2738+
for val in &mut grid {
2739+
*val = 2;
2740+
}
2741+
assert_eq!(grid, grid![[2, 2][2, 2]]);
2742+
}
2743+
27132744
#[test]
27142745
fn indexed_iter() {
27152746
let grid: Grid<u8> = grid![[1,2][3,4]];
@@ -2926,7 +2957,7 @@ mod test {
29262957

29272958
impl Person {
29282959
fn new(name: &str, precise_age: f32) -> Self {
2929-
Person {
2960+
Self {
29302961
_name: name.into(),
29312962
_precise_age: precise_age,
29322963
}

0 commit comments

Comments
 (0)