Skip to content

Commit

Permalink
Add IntoIter on Ranges (pubgrub-rs#276)
Browse files Browse the repository at this point in the history
* Add `IntoIter` on `Ranges`

* impl ExactSizeIterator and DoubleEndedIterator on ranges
  • Loading branch information
konstin authored Nov 12, 2024
1 parent 36c2e41 commit 2a37e13
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions version-ranges/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,39 @@ impl<V: Ord + Clone> Ranges<V> {
}
}

// Newtype to avoid leaking our internal representation.
pub struct RangesIter<V>(smallvec::IntoIter<[Interval<V>; 1]>);

impl<V> Iterator for RangesIter<V> {
type Item = Interval<V>;

fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}

fn size_hint(&self) -> (usize, Option<usize>) {
(self.0.len(), Some(self.0.len()))
}
}

impl<V> ExactSizeIterator for RangesIter<V> {}

impl<V> DoubleEndedIterator for RangesIter<V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
}

impl<V> IntoIterator for Ranges<V> {
type Item = (Bound<V>, Bound<V>);
// Newtype to avoid leaking our internal representation.
type IntoIter = RangesIter<V>;

fn into_iter(self) -> Self::IntoIter {
RangesIter(self.segments.into_iter())
}
}

// REPORT ######################################################################

impl<V: Display + Eq> Display for Ranges<V> {
Expand Down

0 comments on commit 2a37e13

Please sign in to comment.