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 (#17)
  • Loading branch information
zanieb authored Jan 8, 2024
1 parent 78b8add commit 866c0f2
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 @@ -453,8 +453,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 @@ -496,10 +496,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 866c0f2

Please sign in to comment.