Skip to content

Commit d085f34

Browse files
committed
Add UnorderedKeyError
1 parent 166e348 commit d085f34

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

library/alloc/src/collections/btree/map.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::vec::Vec;
22
use core::borrow::Borrow;
33
use core::cmp::Ordering;
4+
use core::error::Error;
45
use core::fmt::{self, Debug};
56
use core::hash::{Hash, Hasher};
67
use core::iter::FusedIterator;
@@ -2750,7 +2751,7 @@ impl<K: Debug, V: Debug> Debug for Cursor<'_, K, V> {
27502751
/// references is tied to its own lifetime, instead of just the underlying map. This means
27512752
/// cursors cannot yield multiple elements at once.
27522753
///
2753-
/// Cursors always point to a gao between two elements in the map, and can
2754+
/// Cursors always point to a gap between two elements in the map, and can
27542755
/// operate on the two immediately adjacent elements.
27552756
///
27562757
/// A `CursorMut` is created with the [`BTreeMap::lower_bound_mut`] and [`BTreeMap::upper_bound_mut`]
@@ -2780,7 +2781,7 @@ impl<K: Debug, V: Debug, A> Debug for CursorMut<'_, K, V, A> {
27802781
/// references is tied to its own lifetime, instead of just the underlying map. This means
27812782
/// cursors cannot yield multiple elements at once.
27822783
///
2783-
/// Cursors always point to a gao between two elements in the map, and can
2784+
/// Cursors always point to a gap between two elements in the map, and can
27842785
/// operate on the two immediately adjacent elements.
27852786
///
27862787
/// A `CursorMutKey` is created from a [`CursorMut`] with the
@@ -3142,20 +3143,21 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMutKey<'a, K, V, A> {
31423143
/// - the given key compares greater than or equal to the next element (if
31433144
/// any).
31443145
#[unstable(feature = "btree_cursors", issue = "107540")]
3145-
pub fn insert_after(&mut self, key: K, value: V) {
3146+
pub fn insert_after(&mut self, key: K, value: V) -> Result<(), UnorderedKeyError> {
31463147
if let Some((prev, _)) = self.peek_prev() {
31473148
if &key <= prev {
3148-
panic!("key must be ordered above the previous element");
3149+
return Err(UnorderedKeyError {});
31493150
}
31503151
}
31513152
if let Some((next, _)) = self.peek_next() {
31523153
if &key >= next {
3153-
panic!("key must be ordered below the next element");
3154+
return Err(UnorderedKeyError {});
31543155
}
31553156
}
31563157
unsafe {
31573158
self.insert_after_unchecked(key, value);
31583159
}
3160+
Ok(())
31593161
}
31603162

31613163
/// Inserts a new element into the `BTreeMap` in the gap that the
@@ -3172,20 +3174,21 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMutKey<'a, K, V, A> {
31723174
/// - the given key compares less than or equal to the previous element (if
31733175
/// any).
31743176
#[unstable(feature = "btree_cursors", issue = "107540")]
3175-
pub fn insert_before(&mut self, key: K, value: V) {
3177+
pub fn insert_before(&mut self, key: K, value: V) -> Result<(), UnorderedKeyError> {
31763178
if let Some((prev, _)) = self.peek_prev() {
31773179
if &key <= prev {
3178-
panic!("key must be ordered above the previous element");
3180+
return Err(UnorderedKeyError {});
31793181
}
31803182
}
31813183
if let Some((next, _)) = self.peek_next() {
31823184
if &key >= next {
3183-
panic!("key must be ordered below the next element");
3185+
return Err(UnorderedKeyError {});
31843186
}
31853187
}
31863188
unsafe {
31873189
self.insert_before_unchecked(key, value);
31883190
}
3191+
Ok(())
31893192
}
31903193

31913194
/// Removes the next element from the `BTreeMap`.
@@ -3286,7 +3289,7 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMut<'a, K, V, A> {
32863289
/// - the given key compares greater than or equal to the next element (if
32873290
/// any).
32883291
#[unstable(feature = "btree_cursors", issue = "107540")]
3289-
pub fn insert_after(&mut self, key: K, value: V) {
3292+
pub fn insert_after(&mut self, key: K, value: V) -> Result<(), UnorderedKeyError> {
32903293
self.inner.insert_after(key, value)
32913294
}
32923295

@@ -3304,7 +3307,7 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMut<'a, K, V, A> {
33043307
/// - the given key compares less than or equal to the previous element (if
33053308
/// any).
33063309
#[unstable(feature = "btree_cursors", issue = "107540")]
3307-
pub fn insert_before(&mut self, key: K, value: V) {
3310+
pub fn insert_before(&mut self, key: K, value: V) -> Result<(), UnorderedKeyError> {
33083311
self.inner.insert_before(key, value)
33093312
}
33103313

@@ -3327,5 +3330,22 @@ impl<'a, K: Ord, V, A: Allocator + Clone> CursorMut<'a, K, V, A> {
33273330
}
33283331
}
33293332

3333+
/// Error type returned by [`CursorMut::insert_before`] and
3334+
/// [`CursorMut::insert_after`] if the key being inserted is not properly
3335+
/// ordered with regards to adjacent keys.
3336+
#[derive(Clone, PartialEq, Eq, Debug)]
3337+
#[unstable(feature = "btree_cursors", issue = "107540")]
3338+
pub struct UnorderedKeyError {}
3339+
3340+
#[unstable(feature = "btree_cursors", issue = "107540")]
3341+
impl fmt::Display for UnorderedKeyError {
3342+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3343+
write!(f, "key is not properly ordered relative to neighbors")
3344+
}
3345+
}
3346+
3347+
#[unstable(feature = "btree_cursors", issue = "107540")]
3348+
impl Error for UnorderedKeyError {}
3349+
33303350
#[cfg(test)]
33313351
mod tests;

library/alloc/src/collections/btree/map/tests.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -2378,14 +2378,14 @@ fn test_cursor_mut() {
23782378
assert_eq!(cur.peek_next(), Some((&5, &mut 'e')));
23792379
assert_eq!(cur.peek_prev(), Some((&3, &mut 'c')));
23802380

2381-
cur.insert_before(4, 'd');
2381+
cur.insert_before(4, 'd').unwrap();
23822382
assert_eq!(cur.peek_next(), Some((&5, &mut 'e')));
23832383
assert_eq!(cur.peek_prev(), Some((&4, &mut 'd')));
23842384

23852385
assert_eq!(cur.next(), Some((&5, &mut 'e')));
23862386
assert_eq!(cur.peek_next(), None);
23872387
assert_eq!(cur.peek_prev(), Some((&5, &mut 'e')));
2388-
cur.insert_before(6, 'f');
2388+
cur.insert_before(6, 'f').unwrap();
23892389
assert_eq!(cur.peek_next(), None);
23902390
assert_eq!(cur.peek_prev(), Some((&6, &mut 'f')));
23912391
assert_eq!(cur.remove_prev(), Some((6, 'f')));
@@ -2409,14 +2409,14 @@ fn test_cursor_mut_key() {
24092409
assert_eq!(cur.peek_next(), Some((&mut 5, &mut 'e')));
24102410
assert_eq!(cur.peek_prev(), Some((&mut 3, &mut 'c')));
24112411

2412-
cur.insert_before(4, 'd');
2412+
cur.insert_before(4, 'd').unwrap();
24132413
assert_eq!(cur.peek_next(), Some((&mut 5, &mut 'e')));
24142414
assert_eq!(cur.peek_prev(), Some((&mut 4, &mut 'd')));
24152415

24162416
assert_eq!(cur.next(), Some((&mut 5, &mut 'e')));
24172417
assert_eq!(cur.peek_next(), None);
24182418
assert_eq!(cur.peek_prev(), Some((&mut 5, &mut 'e')));
2419-
cur.insert_before(6, 'f');
2419+
cur.insert_before(6, 'f').unwrap();
24202420
assert_eq!(cur.peek_next(), None);
24212421
assert_eq!(cur.peek_prev(), Some((&mut 6, &mut 'f')));
24222422
assert_eq!(cur.remove_prev(), Some((6, 'f')));
@@ -2439,74 +2439,66 @@ fn test_cursor_empty() {
24392439
let mut cur = map.lower_bound_mut(Bound::Excluded(&3));
24402440
assert_eq!(cur.peek_next(), None);
24412441
assert_eq!(cur.peek_prev(), None);
2442-
cur.insert_after(0, 0);
2442+
cur.insert_after(0, 0).unwrap();
24432443
assert_eq!(cur.peek_next(), Some((&0, &mut 0)));
24442444
assert_eq!(cur.peek_prev(), None);
24452445
assert_eq!(map, BTreeMap::from([(0, 0)]));
24462446
}
24472447

2448-
#[should_panic(expected = "key must be ordered above the previous element")]
24492448
#[test]
24502449
fn test_cursor_mut_insert_before_1() {
24512450
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
24522451
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2453-
cur.insert_before(0, 'd');
2452+
cur.insert_before(0, 'd').unwrap_err();
24542453
}
24552454

2456-
#[should_panic(expected = "key must be ordered above the previous element")]
24572455
#[test]
24582456
fn test_cursor_mut_insert_before_2() {
24592457
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
24602458
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2461-
cur.insert_before(1, 'd');
2459+
cur.insert_before(1, 'd').unwrap_err();
24622460
}
24632461

2464-
#[should_panic(expected = "key must be ordered above the previous element")]
24652462
#[test]
24662463
fn test_cursor_mut_insert_before_3() {
24672464
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
24682465
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2469-
cur.insert_before(2, 'd');
2466+
cur.insert_before(2, 'd').unwrap_err();
24702467
}
24712468

2472-
#[should_panic(expected = "key must be ordered below the next element")]
24732469
#[test]
24742470
fn test_cursor_mut_insert_before_4() {
24752471
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
24762472
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2477-
cur.insert_before(3, 'd');
2473+
cur.insert_before(3, 'd').unwrap_err();
24782474
}
24792475

2480-
#[should_panic(expected = "key must be ordered above the previous element")]
24812476
#[test]
24822477
fn test_cursor_mut_insert_after_1() {
24832478
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
24842479
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2485-
cur.insert_after(1, 'd');
2480+
cur.insert_after(1, 'd').unwrap_err();
24862481
}
24872482

2488-
#[should_panic(expected = "key must be ordered above the previous element")]
24892483
#[test]
24902484
fn test_cursor_mut_insert_after_2() {
24912485
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
24922486
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2493-
cur.insert_after(2, 'd');
2487+
cur.insert_after(2, 'd').unwrap_err();
24942488
}
24952489

2496-
#[should_panic(expected = "key must be ordered below the next element")]
24972490
#[test]
24982491
fn test_cursor_mut_insert_after_3() {
24992492
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
25002493
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2501-
cur.insert_after(3, 'd');
2494+
cur.insert_after(3, 'd').unwrap_err();
25022495
}
25032496

2504-
#[should_panic(expected = "key must be ordered below the next element")]
25052497
#[test]
25062498
fn test_cursor_mut_insert_after_4() {
25072499
let mut map = BTreeMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);
25082500
let mut cur = map.upper_bound_mut(Bound::Included(&2));
2509-
cur.insert_after(4, 'd');
2501+
cur.insert_after(4, 'd').unwrap_err();
25102502
}
25112503

25122504
#[test]

0 commit comments

Comments
 (0)