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: implement size_hint for ImageBuffer iterators #1790

Merged
merged 1 commit into from Sep 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/buffer.rs
Expand Up @@ -33,6 +33,12 @@ where
fn next(&mut self) -> Option<&'a P> {
self.chunks.next().map(|v| <P as Pixel>::from_slice(v))
}

#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}

impl<'a, P: Pixel + 'a> ExactSizeIterator for Pixels<'a, P>
Expand Down Expand Up @@ -91,6 +97,12 @@ where
fn next(&mut self) -> Option<&'a mut P> {
self.chunks.next().map(|v| <P as Pixel>::from_slice_mut(v))
}

#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}

impl<'a, P: Pixel + 'a> ExactSizeIterator for PixelsMut<'a, P>
Expand Down Expand Up @@ -173,6 +185,12 @@ where
chunks: row.chunks_exact(<P as Pixel>::CHANNEL_COUNT as usize),
})
}

#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}

impl<'a, P: Pixel + 'a> ExactSizeIterator for Rows<'a, P>
Expand Down Expand Up @@ -265,6 +283,12 @@ where
chunks: row.chunks_exact_mut(<P as Pixel>::CHANNEL_COUNT as usize),
})
}

#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}

impl<'a, P: Pixel + 'a> ExactSizeIterator for RowsMut<'a, P>
Expand Down Expand Up @@ -328,6 +352,12 @@ where
self.x += 1;
self.pixels.next().map(|p| (x, y, p))
}

#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}

impl<'a, P: Pixel + 'a> ExactSizeIterator for EnumeratePixels<'a, P>
Expand Down Expand Up @@ -394,6 +424,12 @@ where
)
})
}

#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}

impl<'a, P: Pixel + 'a> ExactSizeIterator for EnumerateRows<'a, P>
Expand Down Expand Up @@ -454,6 +490,12 @@ where
self.x += 1;
self.pixels.next().map(|p| (x, y, p))
}

#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}

impl<'a, P: Pixel + 'a> ExactSizeIterator for EnumeratePixelsMut<'a, P>
Expand Down Expand Up @@ -511,6 +553,12 @@ where
)
})
}

#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}

impl<'a, P: Pixel + 'a> ExactSizeIterator for EnumerateRowsMut<'a, P>
Expand Down Expand Up @@ -1567,6 +1615,49 @@ mod test {
let mut buffer = std::io::Cursor::new(vec![]);
assert!(img.write_to(&mut buffer, ImageOutputFormat::Png).is_ok());
}

#[test]
fn exact_size_iter_size_hint() {
// The docs for `std::iter::ExactSizeIterator` requires that the implementation of
// `size_hint` on the iterator returns the same value as the `len` implementation.

// This test should work for any size image.
const N: u32 = 10;

let mut image = RgbImage::from_raw(N, N, vec![0; (N * N * 3) as usize]).unwrap();

let iter = image.pixels();
let exact_len = ExactSizeIterator::len(&iter);
assert_eq!(iter.size_hint(), (exact_len, Some(exact_len)));

let iter = image.pixels_mut();
let exact_len = ExactSizeIterator::len(&iter);
assert_eq!(iter.size_hint(), (exact_len, Some(exact_len)));

let iter = image.rows();
let exact_len = ExactSizeIterator::len(&iter);
assert_eq!(iter.size_hint(), (exact_len, Some(exact_len)));

let iter = image.rows_mut();
let exact_len = ExactSizeIterator::len(&iter);
assert_eq!(iter.size_hint(), (exact_len, Some(exact_len)));

let iter = image.enumerate_pixels();
let exact_len = ExactSizeIterator::len(&iter);
assert_eq!(iter.size_hint(), (exact_len, Some(exact_len)));

let iter = image.enumerate_rows();
let exact_len = ExactSizeIterator::len(&iter);
assert_eq!(iter.size_hint(), (exact_len, Some(exact_len)));

let iter = image.enumerate_pixels_mut();
let exact_len = ExactSizeIterator::len(&iter);
assert_eq!(iter.size_hint(), (exact_len, Some(exact_len)));

let iter = image.enumerate_rows_mut();
let exact_len = ExactSizeIterator::len(&iter);
assert_eq!(iter.size_hint(), (exact_len, Some(exact_len)));
}
}

#[cfg(test)]
Expand Down