Skip to content

Commit 7cec62f

Browse files
committed
Add IntoIter on Ranges
1 parent 216f3fd commit 7cec62f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

version-ranges/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ impl<V: Ord> Ranges<V> {
283283
}
284284
}
285285

286+
/// See `Ranges` docstring for the invariants.
286287
fn check_invariants(self) -> Self {
287288
if cfg!(debug_assertions) {
288289
for p in self.segments.as_slice().windows(2) {
@@ -826,6 +827,27 @@ impl<V: Ord + Clone> Ranges<V> {
826827
}
827828
}
828829

830+
// Newtype to avoid leaking our internal representation.
831+
pub struct RangesIter<V>(smallvec::IntoIter<[Interval<V>; 1]>);
832+
833+
impl<V> Iterator for RangesIter<V> {
834+
type Item = Interval<V>;
835+
836+
fn next(&mut self) -> Option<Self::Item> {
837+
self.0.next()
838+
}
839+
}
840+
841+
impl<V> IntoIterator for Ranges<V> {
842+
type Item = (Bound<V>, Bound<V>);
843+
// Newtype to avoid leaking our internal representation.
844+
type IntoIter = RangesIter<V>;
845+
846+
fn into_iter(self) -> Self::IntoIter {
847+
RangesIter(self.segments.into_iter())
848+
}
849+
}
850+
829851
// REPORT ######################################################################
830852

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

0 commit comments

Comments
 (0)