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 and konstin committed Mar 6, 2024
1 parent e16f980 commit ec30428
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ impl<V: Ord + Clone> Range<V> {
BV: Borrow<V> + 's,
{
// Do not simplify singletons
if self.is_singleton() {
return self.clone();
if let Some(version) = self.as_singleton() {
return Self::singleton(version);
}

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -528,11 +528,18 @@ impl<V: Ord + Clone> Range<V> {
Self { segments }.check_invariants()
}

/// Whether the range is a single version
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 ec30428

Please sign in to comment.