gstreamer/audio/video: Correctly implement ExactSizeIterator

len() is optional but size_hint() must return the correct values. Also
this shouldn't return the overall length but the remaining length.
This commit is contained in:
Sebastian Dröge 2020-10-09 11:30:44 +03:00
parent 6e404f1831
commit 0b70e52032
2 changed files with 20 additions and 8 deletions

View file

@ -263,14 +263,20 @@ impl Iterator for AudioFormatIterator {
Some(fmt)
}
}
}
impl ExactSizeIterator for AudioFormatIterator {
fn len(&self) -> usize {
self.len
fn size_hint(&self) -> (usize, Option<usize>) {
if self.idx == self.len {
return (0, Some(0));
}
let remaining = (self.len - self.idx) as usize;
(remaining, Some(remaining))
}
}
impl ExactSizeIterator for AudioFormatIterator {}
impl DoubleEndedIterator for AudioFormatIterator {
fn next_back(&mut self) -> Option<Self::Item> {
if self.idx >= self.len {

View file

@ -402,14 +402,20 @@ impl Iterator for VideoFormatIterator {
Some(fmt)
}
}
}
impl ExactSizeIterator for VideoFormatIterator {
fn len(&self) -> usize {
self.len
fn size_hint(&self) -> (usize, Option<usize>) {
if self.idx == self.len {
return (0, Some(0));
}
let remaining = (self.len - self.idx) as usize;
(remaining, Some(remaining))
}
}
impl ExactSizeIterator for VideoFormatIterator {}
impl DoubleEndedIterator for VideoFormatIterator {
fn next_back(&mut self) -> Option<Self::Item> {
if self.idx >= self.len {