Skip to content

Commit f041854

Browse files
konstinzanieb
andauthored
Avoid over-simplifying version ranges (#228)
Co-authored-by: Zanie Blue <[email protected]>
1 parent 5c45048 commit f041854

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/range.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -610,21 +610,26 @@ impl<V: Ord + Clone> Range<V> {
610610
true
611611
}
612612

613-
/// Returns a simpler Range that contains the same versions
613+
/// Returns a simpler Range that contains the same versions.
614614
///
615-
/// For every one of the Versions provided in versions the existing range and
616-
/// the simplified range will agree on whether it is contained.
615+
/// For every one of the Versions provided in versions the existing range and the simplified range will agree on whether it is contained.
617616
/// The simplified version may include or exclude versions that are not in versions as the implementation wishes.
618-
/// For example:
619-
/// - If all the versions are contained in the original than the range will be simplified to `full`.
620-
/// - If none of the versions are contained in the original than the range will be simplified to `empty`.
621617
///
622-
/// If versions are not sorted the correctness of this function is not guaranteed.
618+
/// If none of the versions are contained in the original than the range will be returned unmodified.
619+
/// If the range includes a single version, it will be returned unmodified.
620+
/// If all the versions are contained in the original than the range will be simplified to `full`.
621+
///
622+
/// If the given versions are not sorted the correctness of this function is not guaranteed.
623623
pub fn simplify<'s, I, BV>(&self, versions: I) -> Self
624624
where
625625
I: Iterator<Item = BV> + 's,
626626
BV: Borrow<V> + 's,
627627
{
628+
// Do not simplify singletons
629+
if self.as_singleton().is_some() {
630+
return self.clone();
631+
}
632+
628633
#[cfg(debug_assertions)]
629634
let mut last: Option<BV> = None;
630635
// Return the segment index in the range for each version in the range, None otherwise
@@ -651,7 +656,13 @@ impl<V: Ord + Clone> Range<V> {
651656
}
652657
Some(None)
653658
});
654-
let kept_segments = group_adjacent_locations(version_locations);
659+
let mut kept_segments = group_adjacent_locations(version_locations).peekable();
660+
661+
// Do not return null sets
662+
if kept_segments.peek().is_none() {
663+
return self.clone();
664+
}
665+
655666
self.keep_segments(kept_segments)
656667
}
657668

0 commit comments

Comments
 (0)