@@ -77,9 +77,8 @@ impl<K, V> LeafNode<K, V> {
77
77
}
78
78
}
79
79
80
- /// Creates a new boxed `LeafNode`. Unsafe because all nodes should really be hidden behind
81
- /// `BoxedNode`, preventing accidental dropping of uninitialized keys and values.
82
- unsafe fn new ( ) -> Box < Self > {
80
+ /// Creates a new boxed `LeafNode`.
81
+ fn new ( ) -> Box < Self > {
83
82
unsafe {
84
83
let mut leaf = Box :: new_uninit ( ) ;
85
84
LeafNode :: init ( leaf. as_mut_ptr ( ) ) ;
@@ -107,10 +106,9 @@ struct InternalNode<K, V> {
107
106
impl < K , V > InternalNode < K , V > {
108
107
/// Creates a new boxed `InternalNode`.
109
108
///
110
- /// This is unsafe for two reasons. First, it returns an owned `InternalNode` in a box, risking
111
- /// dropping of uninitialized fields. Second, an invariant of internal nodes is that `len + 1`
112
- /// edges are initialized and valid, meaning that even when the node is empty (having a
113
- /// `len` of 0), there must be one initialized and valid edge. This function does not set up
109
+ /// # Safety
110
+ /// An invariant of internal nodes is that they have at least one
111
+ /// initialized and valid edge. This function does not set up
114
112
/// such an edge.
115
113
unsafe fn new ( ) -> Box < Self > {
116
114
unsafe {
@@ -144,7 +142,7 @@ impl<K, V> Root<K, V> {
144
142
145
143
impl < K , V > NodeRef < marker:: Owned , K , V , marker:: Leaf > {
146
144
fn new_leaf ( ) -> Self {
147
- Self :: from_new_leaf ( unsafe { LeafNode :: new ( ) } )
145
+ Self :: from_new_leaf ( LeafNode :: new ( ) )
148
146
}
149
147
150
148
fn from_new_leaf ( leaf : Box < LeafNode < K , V > > ) -> Self {
@@ -156,10 +154,13 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::Internal> {
156
154
fn new_internal ( child : Root < K , V > ) -> Self {
157
155
let mut new_node = unsafe { InternalNode :: new ( ) } ;
158
156
new_node. edges [ 0 ] . write ( child. node ) ;
159
- NodeRef :: from_new_internal ( new_node, child. height + 1 )
157
+ unsafe { NodeRef :: from_new_internal ( new_node, child. height + 1 ) }
160
158
}
161
159
162
- fn from_new_internal ( internal : Box < InternalNode < K , V > > , height : usize ) -> Self {
160
+ /// # Safety
161
+ /// `height` must not be zero.
162
+ unsafe fn from_new_internal ( internal : Box < InternalNode < K , V > > , height : usize ) -> Self {
163
+ debug_assert ! ( height > 0 ) ;
163
164
let node = NonNull :: from ( Box :: leak ( internal) ) . cast ( ) ;
164
165
let mut this = NodeRef { height, node, _marker : PhantomData } ;
165
166
this. borrow_mut ( ) . correct_all_childrens_parent_links ( ) ;
@@ -1080,14 +1081,12 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
1080
1081
/// - All the key-value pairs to the right of this handle are put into a newly
1081
1082
/// allocated node.
1082
1083
pub fn split ( mut self ) -> SplitResult < ' a , K , V , marker:: Leaf > {
1083
- unsafe {
1084
- let mut new_node = LeafNode :: new ( ) ;
1084
+ let mut new_node = LeafNode :: new ( ) ;
1085
1085
1086
- let kv = self . split_leaf_data ( & mut new_node) ;
1086
+ let kv = self . split_leaf_data ( & mut new_node) ;
1087
1087
1088
- let right = NodeRef :: from_new_leaf ( new_node) ;
1089
- SplitResult { left : self . node , kv, right }
1090
- }
1088
+ let right = NodeRef :: from_new_leaf ( new_node) ;
1089
+ SplitResult { left : self . node , kv, right }
1091
1090
}
1092
1091
1093
1092
/// Removes the key-value pair pointed to by this handle and returns it, along with the edge
0 commit comments