Skip to content

Commit c99ab29

Browse files
authored
Rollup merge of rust-lang#111238 - workingjubilee:fix-btree-cursormut-peek-prev, r=Amanieu
btree_map: `Cursor{,Mut}::peek_prev` must agree Our `Cursor::peek_prev` and `CursorMut::peek_prev` must agree on how to behave when they are called on the "null element". This will fix rust-lang#111228. r? `@Amanieu`
2 parents ded0a9e + 00cb59b commit c99ab29

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -3079,8 +3079,8 @@ impl<'a, K, V, A> CursorMut<'a, K, V, A> {
30793079
unsafe { self.root.reborrow() }
30803080
.as_mut()?
30813081
.borrow_mut()
3082-
.first_leaf_edge()
3083-
.next_kv()
3082+
.last_leaf_edge()
3083+
.next_back_kv()
30843084
.ok()?
30853085
.into_kv_valmut()
30863086
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::testing::crash_test::{CrashTestDummy, Panic};
88
use crate::testing::ord_chaos::{Cyclic3, Governed, Governor};
99
use crate::testing::rng::DeterministicRng;
1010
use crate::vec::Vec;
11+
use core::assert_matches::assert_matches;
1112
use std::cmp::Ordering;
1213
use std::iter;
1314
use std::mem;
@@ -2448,3 +2449,21 @@ fn test_cursor_mut_insert_after_4() {
24482449
let mut cur = map.upper_bound_mut(Bound::Included(&2));
24492450
cur.insert_after(4, 'd');
24502451
}
2452+
2453+
#[test]
2454+
fn cursor_peek_prev_agrees_with_cursor_mut() {
2455+
let mut map = BTreeMap::from([(1, 1), (2, 2), (3, 3)]);
2456+
2457+
let cursor = map.lower_bound(Bound::Excluded(&3));
2458+
assert!(cursor.key().is_none());
2459+
2460+
let prev = cursor.peek_prev();
2461+
assert_matches!(prev, Some((&3, _)));
2462+
2463+
// Shadow names so the two parts of this test match.
2464+
let mut cursor = map.lower_bound_mut(Bound::Excluded(&3));
2465+
assert!(cursor.key().is_none());
2466+
2467+
let prev = cursor.peek_prev();
2468+
assert_matches!(prev, Some((&3, _)));
2469+
}

0 commit comments

Comments
 (0)