Skip to content

Commit

Permalink
Add as_singleton method allowing the inner singleton version to be …
Browse files Browse the repository at this point in the history
…accessed
  • Loading branch information
zanieb committed Jan 8, 2024
1 parent cb0eacc commit 5a9aa2e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,8 @@ impl<V: Ord + Clone> Range<V> {
V: 'v,
{
// Do not simplify singletons
if self.is_singleton() {
return self.clone();
if let Some(version) = self.as_singleton() {
return Self::singleton(version);
}

// Return the segment index in the range for each version in the range, None otherwise
Expand Down Expand Up @@ -505,10 +505,18 @@ impl<V: Ord + Clone> Range<V> {
Self { segments }.check_invariants()
}

pub fn is_singleton(&self) -> bool {
/// If the range includes a single version, return it.
/// Otherwise, returns [None].
pub fn as_singleton(&self) -> Option<V> {
match self.segments.as_slice() {
[(Included(v1), Included(v2))] => v1 == v2,
_ => false,
[(Included(v1), Included(v2))] => {
if v1 == v2 {
Some(v1.clone())
} else {
None
}
}
_ => None,
}
}
}
Expand Down

0 comments on commit 5a9aa2e

Please sign in to comment.