Skip to content

Commit

Permalink
feat: simplify works with iter of V not just &V
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Feb 2, 2024
1 parent 2d8d8ce commit d5ae939
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,30 +441,35 @@ impl<V: Ord + Clone> Range<V> {
/// - If none of the versions are contained in the original than the range will be simplified to `empty`.
///
/// If versions are not sorted the correctness of this function is not guaranteed.
pub fn simplify<'v, I>(&self, versions: I) -> Self
pub fn simplify<'s, I, BV>(&self, versions: I) -> Self
where
I: Iterator<Item = &'v V> + 'v,
V: 'v,
I: Iterator<Item = BV> + 's,
BV: Borrow<V> + 's,
{
#[cfg(debug_assertions)]
let mut last: Option<&V> = None;
let mut last: Option<BV> = None;
// Return the segment index in the range for each version in the range, None otherwise
let version_locations = versions.scan(0, move |i, v| {
#[cfg(debug_assertions)]
{
assert!(
last <= Some(v),
"`simplify` `versions` argument incorrectly sorted"
);
last = Some(v);
if let Some(l) = last.as_ref() {
assert!(
l.borrow() <= v.borrow(),
"`simplify` `versions` argument incorrectly sorted"
);
}
}
while let Some(segment) = self.segments.get(*i) {
match within_bounds(v, segment) {
match within_bounds(v.borrow(), segment) {
Ordering::Less => return Some(None),
Ordering::Equal => return Some(Some(*i)),
Ordering::Greater => *i += 1,
}
}
#[cfg(debug_assertions)]
{
last = Some(v);
}
Some(None)
});
let kept_segments = group_adjacent_locations(version_locations);
Expand Down Expand Up @@ -813,4 +818,20 @@ pub mod tests {
range.contains_many(versions.into_iter()).count()
);
}

#[test]
fn simplify_can_take_owned() {
let range: Range<u8> = Range::singleton(1);
let versions = vec![1, 2, 3];
// Check that iter can be a Cow
assert_eq!(
range.simplify(versions.iter()),
range.simplify(versions.iter().map(std::borrow::Cow::Borrowed))
);
// Check that iter can be a V
assert_eq!(
range.simplify(versions.iter()),
range.simplify(versions.into_iter())
);
}
}

0 comments on commit d5ae939

Please sign in to comment.