Skip to content

Commit 2a37e13

Browse files
authored
Add IntoIter on Ranges (#276)
* Add `IntoIter` on `Ranges` * impl ExactSizeIterator and DoubleEndedIterator on ranges
1 parent 36c2e41 commit 2a37e13

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

version-ranges/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,39 @@ impl<V: Ord + Clone> Ranges<V> {
846846
}
847847
}
848848

849+
// Newtype to avoid leaking our internal representation.
850+
pub struct RangesIter<V>(smallvec::IntoIter<[Interval<V>; 1]>);
851+
852+
impl<V> Iterator for RangesIter<V> {
853+
type Item = Interval<V>;
854+
855+
fn next(&mut self) -> Option<Self::Item> {
856+
self.0.next()
857+
}
858+
859+
fn size_hint(&self) -> (usize, Option<usize>) {
860+
(self.0.len(), Some(self.0.len()))
861+
}
862+
}
863+
864+
impl<V> ExactSizeIterator for RangesIter<V> {}
865+
866+
impl<V> DoubleEndedIterator for RangesIter<V> {
867+
fn next_back(&mut self) -> Option<Self::Item> {
868+
self.0.next_back()
869+
}
870+
}
871+
872+
impl<V> IntoIterator for Ranges<V> {
873+
type Item = (Bound<V>, Bound<V>);
874+
// Newtype to avoid leaking our internal representation.
875+
type IntoIter = RangesIter<V>;
876+
877+
fn into_iter(self) -> Self::IntoIter {
878+
RangesIter(self.segments.into_iter())
879+
}
880+
}
881+
849882
// REPORT ######################################################################
850883

851884
impl<V: Display + Eq> Display for Ranges<V> {

0 commit comments

Comments
 (0)