@@ -419,21 +419,26 @@ impl<V: Ord + Clone> Range<V> {
419
419
Self { segments : output } . check_invariants ( )
420
420
}
421
421
422
- /// Returns a simpler Range that contains the same versions
422
+ /// Returns a simpler Range that contains the same versions.
423
423
///
424
- /// For every one of the Versions provided in versions the existing range and
425
- /// the simplified range will agree on whether it is contained.
424
+ /// For every one of the Versions provided in versions the existing range and the simplified range will agree on whether it is contained.
426
425
/// The simplified version may include or exclude versions that are not in versions as the implementation wishes.
427
- /// For example:
428
- /// - If all the versions are contained in the original than the range will be simplified to `full`.
429
- /// - If none of the versions are contained in the original than the range will be simplified to `empty`.
430
426
///
431
- /// If versions are not sorted the correctness of this function is not guaranteed.
427
+ /// If none of the versions are contained in the original than the range will be returned unmodified.
428
+ /// If the range includes a single version, it will be returned unmodified.
429
+ /// If all the versions are contained in the original than the range will be simplified to `full`.
430
+ ///
431
+ /// If the given versions are not sorted the correctness of this function is not guaranteed.
432
432
pub fn simplify < ' v , I > ( & self , versions : I ) -> Self
433
433
where
434
434
I : Iterator < Item = & ' v V > + ' v ,
435
435
V : ' v ,
436
436
{
437
+ // Do not simplify singletons
438
+ if self . is_singleton ( ) {
439
+ return self . clone ( ) ;
440
+ }
441
+
437
442
// Return the segment index in the range for each version in the range, None otherwise
438
443
let version_locations = versions. scan ( 0 , move |i, v| {
439
444
while let Some ( segment) = self . segments . get ( * i) {
@@ -445,7 +450,13 @@ impl<V: Ord + Clone> Range<V> {
445
450
}
446
451
Some ( None )
447
452
} ) ;
448
- let kept_segments = group_adjacent_locations ( version_locations) ;
453
+ let mut kept_segments = group_adjacent_locations ( version_locations) . peekable ( ) ;
454
+
455
+ // Do not return null sets
456
+ if kept_segments. peek ( ) . is_none ( ) {
457
+ return self . clone ( ) ;
458
+ }
459
+
449
460
self . keep_segments ( kept_segments)
450
461
}
451
462
@@ -466,6 +477,13 @@ impl<V: Ord + Clone> Range<V> {
466
477
}
467
478
Self { segments } . check_invariants ( )
468
479
}
480
+
481
+ pub fn is_singleton ( & self ) -> bool {
482
+ match self . segments . as_slice ( ) {
483
+ [ ( Included ( v1) , Included ( v2) ) ] => v1 == v2,
484
+ _ => false ,
485
+ }
486
+ }
469
487
}
470
488
471
489
impl < T : Debug + Display + Clone + Eq + Ord > VersionSet for Range < T > {
0 commit comments