Skip to content

Commit 2aa3133

Browse files
committed
Add and fix BTreeMap comments
1 parent 9d09331 commit 2aa3133

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

src/liballoc/collections/btree/map.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,8 @@ where
16971697
pred: F,
16981698
inner: DrainFilterInner<'a, K, V>,
16991699
}
1700+
/// Most of the implementation of DrainFilter, independent of the type
1701+
/// of the predicate, thus also serving for BTreeSet::DrainFilter.
17001702
pub(super) struct DrainFilterInner<'a, K: 'a, V: 'a> {
17011703
length: &'a mut usize,
17021704
cur_leaf_edge: Option<Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>>,

src/liballoc/collections/btree/navigate.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,16 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
161161
impl<K, V> Handle<NodeRef<marker::Owned, K, V, marker::Leaf>, marker::Edge> {
162162
/// Moves the leaf edge handle to the next leaf edge and returns the key and value
163163
/// in between, while deallocating any node left behind.
164-
/// Unsafe for three reasons:
164+
/// Unsafe for two reasons:
165165
/// - The caller must ensure that the leaf edge is not the last one in the tree
166166
/// and is not a handle previously resulting from counterpart `next_back_unchecked`.
167-
/// - If the leaf edge is the last edge of a node, that node and possibly ancestors
167+
/// - Further use of the updated leaf edge handle is very dangerous. In particular,
168+
/// if the leaf edge is the last edge of a node, that node and possibly ancestors
168169
/// will be deallocated, while the reference to those nodes in the surviving ancestor
169-
/// is left dangling; thus further use of the leaf edge handle is dangerous.
170-
/// It is, however, safe to call this method again on the updated handle.
171-
/// if the two preconditions above hold.
172-
/// - Using the updated handle may well invalidate the returned references.
170+
/// is left dangling.
171+
/// The only safe way to proceed with the updated handle is to compare it, drop it,
172+
/// call this method again subject to both preconditions listed in the first point,
173+
/// or call counterpart `next_back_unchecked` subject to its preconditions.
173174
pub unsafe fn next_unchecked(&mut self) -> (K, V) {
174175
unsafe {
175176
replace(self, |leaf_edge| {
@@ -183,15 +184,16 @@ impl<K, V> Handle<NodeRef<marker::Owned, K, V, marker::Leaf>, marker::Edge> {
183184

184185
/// Moves the leaf edge handle to the previous leaf edge and returns the key
185186
/// and value in between, while deallocating any node left behind.
186-
/// Unsafe for three reasons:
187+
/// Unsafe for two reasons:
187188
/// - The caller must ensure that the leaf edge is not the first one in the tree
188189
/// and is not a handle previously resulting from counterpart `next_unchecked`.
189-
/// - If the lead edge is the first edge of a node, that node and possibly ancestors
190+
/// - Further use of the updated leaf edge handle is very dangerous. In particular,
191+
/// if the leaf edge is the first edge of a node, that node and possibly ancestors
190192
/// will be deallocated, while the reference to those nodes in the surviving ancestor
191-
/// is left dangling; thus further use of the leaf edge handle is dangerous.
192-
/// It is, however, safe to call this method again on the updated handle.
193-
/// if the two preconditions above hold.
194-
/// - Using the updated handle may well invalidate the returned references.
193+
/// is left dangling.
194+
/// The only safe way to proceed with the updated handle is to compare it, drop it,
195+
/// call this method again subject to both preconditions listed in the first point,
196+
/// or call counterpart `next_unchecked` subject to its preconditions.
195197
pub unsafe fn next_back_unchecked(&mut self) -> (K, V) {
196198
unsafe {
197199
replace(self, |leaf_edge| {

src/liballoc/collections/btree/node.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ struct InternalNode<K, V> {
9494
data: LeafNode<K, V>,
9595

9696
/// The pointers to the children of this node. `len + 1` of these are considered
97-
/// initialized and valid.
97+
/// initialized and valid. Although during the process of `into_iter` or `drop`,
98+
/// some pointers are dangling while others still need to be traversed.
9899
edges: [MaybeUninit<BoxedNode<K, V>>; 2 * B],
99100
}
100101

@@ -408,7 +409,7 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
408409

409410
impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
410411
/// Unsafely asserts to the compiler some static information about whether this
411-
/// node is a `Leaf`.
412+
/// node is a `Leaf` or an `Internal`.
412413
unsafe fn cast_unchecked<NewType>(&mut self) -> NodeRef<marker::Mut<'_>, K, V, NewType> {
413414
NodeRef { height: self.height, node: self.node, root: self.root, _marker: PhantomData }
414415
}
@@ -515,7 +516,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
515516
}
516517

517518
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
518-
/// Adds a key/value pair the end of the node.
519+
/// Adds a key/value pair to the end of the node.
519520
pub fn push(&mut self, key: K, val: V) {
520521
assert!(self.len() < CAPACITY);
521522

@@ -602,8 +603,10 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
602603
}
603604

604605
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
605-
/// Removes a key/value pair from the end of this node. If this is an internal node,
606-
/// also removes the edge that was to the right of that pair.
606+
/// Removes a key/value pair from the end of this node and returns the pair.
607+
/// If this is an internal node, also removes the edge that was to the right
608+
/// of that pair and returns the orphaned node that this edge owned with its
609+
/// parent erased.
607610
pub fn pop(&mut self) -> (K, V, Option<Root<K, V>>) {
608611
assert!(self.len() > 0);
609612

@@ -883,7 +886,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
883886
}
884887

885888
/// Unsafely asserts to the compiler some static information about whether the underlying
886-
/// node of this handle is a `Leaf`.
889+
/// node of this handle is a `Leaf` or an `Internal`.
887890
unsafe fn cast_unchecked<NewType>(
888891
&mut self,
889892
) -> Handle<NodeRef<marker::Mut<'_>, K, V, NewType>, marker::Edge> {

0 commit comments

Comments
 (0)