Skip to content

Commit 339c152

Browse files
authored
Rollup merge of rust-lang#105641 - Amanieu:btree_cursor, r=m-ou-se
Implement cursors for BTreeMap See the ACP for an overview of the API: rust-lang/libs-team#141 The implementation is split into 2 commits: - The first changes the internal insertion functions to return a handle to the newly inserted element. The lifetimes involved are a bit hairy since we need a mutable handle to both the `BTreeMap` itself (which holds the root) and the nodes allocated in memory. I have tested that this passes the standard library testsuite under miri. - The second commit implements the cursor API itself. This is more straightforward to follow but still involves some unsafe code to deal with simultaneous mutable borrows of the tree root and the node that is currently being iterated.
2 parents 6eb9f2d + 36831b3 commit 339c152

File tree

6 files changed

+957
-41
lines changed

6 files changed

+957
-41
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ impl<'a, T> DormantMutRef<'a, T> {
4141
// SAFETY: our own safety conditions imply this reference is again unique.
4242
unsafe { &mut *self.ptr.as_ptr() }
4343
}
44+
45+
/// Borrows a new mutable reference from the unique borrow initially captured.
46+
///
47+
/// # Safety
48+
///
49+
/// The reborrow must have ended, i.e., the reference returned by `new` and
50+
/// all pointers and references derived from it, must not be used anymore.
51+
pub unsafe fn reborrow(&mut self) -> &'a mut T {
52+
// SAFETY: our own safety conditions imply this reference is again unique.
53+
unsafe { &mut *self.ptr.as_ptr() }
54+
}
55+
56+
/// Borrows a new shared reference from the unique borrow initially captured.
57+
///
58+
/// # Safety
59+
///
60+
/// The reborrow must have ended, i.e., the reference returned by `new` and
61+
/// all pointers and references derived from it, must not be used anymore.
62+
pub unsafe fn reborrow_shared(&self) -> &'a T {
63+
// SAFETY: our own safety conditions imply this reference is again unique.
64+
unsafe { &*self.ptr.as_ptr() }
65+
}
4466
}
4567

4668
#[cfg(test)]

0 commit comments

Comments
 (0)