Skip to content

Commit 25fd310

Browse files
henryiiibecheran
authored andcommitted
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 25fd310

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/lib.rs

+36-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,24 @@ 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+
1648+
fn into_iter(self) -> Self::IntoIter {
1649+
self.iter()
1650+
}
1651+
}
1652+
1653+
impl<'a, T> IntoIterator for &'a mut Grid<T> {
1654+
type IntoIter = core::slice::IterMut<'a, T>;
1655+
type Item = &'a mut T;
1656+
1657+
fn into_iter(self) -> Self::IntoIter {
1658+
self.iter_mut()
1659+
}
1660+
}
1661+
16461662
impl<T: fmt::Debug> fmt::Debug for Grid<T> {
16471663
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16481664
write!(f, "[")?;
@@ -2710,6 +2726,23 @@ mod test {
27102726
assert_eq!(iter.next(), None);
27112727
}
27122728

2729+
#[test]
2730+
fn into_iter() {
2731+
let grid: Grid<u8> = grid![[1,1][1,1]];
2732+
for val in &grid {
2733+
assert_eq!(val, &1);
2734+
}
2735+
}
2736+
2737+
#[test]
2738+
fn into_iter_mut() {
2739+
let mut grid: Grid<u8> = grid![[1,1][1,1]];
2740+
for val in &mut grid {
2741+
*val = 2;
2742+
}
2743+
assert_eq!(grid, grid![[2, 2][2, 2]]);
2744+
}
2745+
27132746
#[test]
27142747
fn indexed_iter() {
27152748
let grid: Grid<u8> = grid![[1,2][3,4]];
@@ -2926,7 +2959,7 @@ mod test {
29262959

29272960
impl Person {
29282961
fn new(name: &str, precise_age: f32) -> Self {
2929-
Person {
2962+
Self {
29302963
_name: name.into(),
29312964
_precise_age: precise_age,
29322965
}

0 commit comments

Comments
 (0)