Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

range: make 'as_singleton' return a borrow #23

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ impl<V: Clone> Range<V> {
}

impl<V: Ord> Range<V> {
/// 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))] => {
if v1 == v2 {
Some(v1)
} else {
None
}
}
_ => None,
}
}

/// Convert to something that can be used with
/// [BTreeMap::range](std::collections::BTreeMap::range).
/// All versions contained in self, will be in the output,
Expand Down Expand Up @@ -454,7 +469,7 @@ impl<V: Ord + Clone> Range<V> {
{
// Do not simplify singletons
if let Some(version) = self.as_singleton() {
return Self::singleton(version);
return Self::singleton(version.clone());
}

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

/// 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))] => {
if v1 == v2 {
Some(v1.clone())
} else {
None
}
}
_ => None,
}
}

/// Iterate over the parts of the range.
pub fn iter(&self) -> impl Iterator<Item = &(Bound<V>, Bound<V>)> {
self.segments.iter()
Expand Down