From 2a37e132307d9cd8b9648767bdff90418cb3623f Mon Sep 17 00:00:00 2001 From: konsti Date: Tue, 12 Nov 2024 14:10:21 +0100 Subject: [PATCH] Add `IntoIter` on `Ranges` (#276) * Add `IntoIter` on `Ranges` * impl ExactSizeIterator and DoubleEndedIterator on ranges --- version-ranges/src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/version-ranges/src/lib.rs b/version-ranges/src/lib.rs index 9d3b9959..4fe8f134 100644 --- a/version-ranges/src/lib.rs +++ b/version-ranges/src/lib.rs @@ -846,6 +846,39 @@ impl Ranges { } } +// Newtype to avoid leaking our internal representation. +pub struct RangesIter(smallvec::IntoIter<[Interval; 1]>); + +impl Iterator for RangesIter { + type Item = Interval; + + fn next(&mut self) -> Option { + self.0.next() + } + + fn size_hint(&self) -> (usize, Option) { + (self.0.len(), Some(self.0.len())) + } +} + +impl ExactSizeIterator for RangesIter {} + +impl DoubleEndedIterator for RangesIter { + fn next_back(&mut self) -> Option { + self.0.next_back() + } +} + +impl IntoIterator for Ranges { + type Item = (Bound, Bound); + // Newtype to avoid leaking our internal representation. + type IntoIter = RangesIter; + + fn into_iter(self) -> Self::IntoIter { + RangesIter(self.segments.into_iter()) + } +} + // REPORT ###################################################################### impl Display for Ranges {