From 866c0f2a87fee1e8abe804d40a2ee934de0973d7 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Mon, 8 Jan 2024 12:12:26 -0600 Subject: [PATCH] Add `as_singleton` method allowing the inner singleton version to be accessed (#17) --- src/range.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/range.rs b/src/range.rs index 2444a6e4..f674452d 100644 --- a/src/range.rs +++ b/src/range.rs @@ -453,8 +453,8 @@ impl Range { 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 @@ -496,10 +496,18 @@ impl Range { 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 { 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, } } }