Skip to content

Commit edc4429

Browse files
zaniebkonstin
authored andcommitted
Avoid loss of data in Range.simplify (#13)
* Add `Range.is_singleton` * Do not simplify singletons * Do not return null sets * Tweak docs
1 parent 35b9cf8 commit edc4429

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/range.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -609,21 +609,26 @@ impl<V: Ord + Clone> Range<V> {
609609
true
610610
}
611611

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

0 commit comments

Comments
 (0)