Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing IntoIter #62

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading