Skip to content

Commit

Permalink
Simplify BTreeMap RangeSet min/max getters for Rust 1.66
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Apr 6, 2024
1 parent 10155c1 commit 8fbcf08
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions quinn-proto/src/range_set/btree_range_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ impl RangeSet {
}

pub fn min(&self) -> Option<u64> {
self.iter().next().map(|x| x.start)
self.0.first_key_value().map(|(&start, _)| start)
}

pub fn max(&self) -> Option<u64> {
self.iter().next_back().map(|x| x.end - 1)
self.0.last_key_value().map(|(_, &end)| end - 1)
}

pub fn len(&self) -> usize {
Expand Down
10 changes: 10 additions & 0 deletions quinn-proto/src/range_set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ macro_rules! common_set_tests {
}
}

#[test]
fn min_max() {
let mut set = $set_type::new();
set.insert(1..3);
set.insert(4..5);
set.insert(6..10);
assert_eq!(set.min(), Some(1));
assert_eq!(set.max(), Some(9));
}

fn create_initial_sets(max_range: u64) -> ($set_type, RefRangeSet) {
let mut set = $set_type::new();
let mut reference = RefRangeSet::new(max_range as usize);
Expand Down

0 comments on commit 8fbcf08

Please sign in to comment.