From 0b70e52032b6496e5578346b614294bfc9faae8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 9 Oct 2020 11:30:44 +0300 Subject: [PATCH] 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. --- gstreamer-audio/src/audio_format.rs | 14 ++++++++++---- gstreamer-video/src/video_format.rs | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/gstreamer-audio/src/audio_format.rs b/gstreamer-audio/src/audio_format.rs index 2d3ffa457..10db2259a 100644 --- a/gstreamer-audio/src/audio_format.rs +++ b/gstreamer-audio/src/audio_format.rs @@ -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) { + 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 { if self.idx >= self.len { diff --git a/gstreamer-video/src/video_format.rs b/gstreamer-video/src/video_format.rs index 6b90ace76..55aa36271 100644 --- a/gstreamer-video/src/video_format.rs +++ b/gstreamer-video/src/video_format.rs @@ -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) { + 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 { if self.idx >= self.len {