Removed usages of removed Option::expect_none api.

Added more sanity checks to `btree_set_modify`.
This commit is contained in:
Filipe Rodrigues 2021-04-19 23:06:40 +01:00
parent 2b9cf4a790
commit ba799baf85
2 changed files with 14 additions and 4 deletions

View File

@ -224,10 +224,21 @@ fn range_intersect<T: Ord>(lhs: Range<T>, rhs: Range<T>) -> bool {
/// Removes, modifies and re-inserts a value back into a set
///
/// Panics if `element` doesn't exist.
fn btree_set_modify<T: Ord + Borrow<Q> + std::fmt::Debug, Q: Ord, U>(set: &mut BTreeSet<T>, element: &Q, f: impl FnOnce(&mut T) -> U) -> U {
/// It is a logical error to modify an element's order.
/// This function *might* panic if the order is changed
fn btree_set_modify<T: Ord + Borrow<Q>, Q: Ord, U>(set: &mut BTreeSet<T>, element: &Q, f: impl FnOnce(&mut T) -> U) -> U {
// Take the element from the set
let mut node = set.take(element).expect("Element didn't exist");
// Run the function on it and then reinsert it.
let res = f(&mut node);
set.replace(node).expect_none("Just removed it");
// Then re-insert it
match set.replace(node) {
Some(_) => panic!("Order of element changed during mutation"),
// Sanity check to make sure the element hasn't changed order
None => assert!(set.contains(element), "Order of element changed during mutation"),
}
res
}

View File

@ -17,7 +17,6 @@
array_chunks,
ordering_helpers,
const_btree_new,
option_expect_none,
)]
// Lints
#![warn(clippy::restriction, clippy::pedantic, clippy::nursery)]