@@ -44,34 +44,7 @@ const B: usize = 6;
44
44
pub const MIN_LEN : usize = B - 1 ;
45
45
pub const CAPACITY : usize = 2 * B - 1 ;
46
46
47
- /// The underlying representation of leaf nodes. Note that it is often unsafe to actually store
48
- /// these, since only the first `len` keys and values are assumed to be initialized. As such,
49
- /// these should always be put behind pointers, and specifically behind `BoxedNode` in the owned
50
- /// case.
51
- ///
52
- /// We have a separate type for the header and rely on it matching the prefix of `LeafNode`, in
53
- /// order to statically allocate a single dummy node to avoid allocations. This struct is
54
- /// `repr(C)` to prevent them from being reordered. `LeafNode` does not just contain a
55
- /// `NodeHeader` because we do not want unnecessary padding between `len` and the keys.
56
- /// Crucially, `NodeHeader` can be safely transmuted to different K and V. (This is exploited
57
- /// by `as_header`.)
58
- #[ repr( C ) ]
59
- struct NodeHeader < K , V > {
60
- /// We use `*const` as opposed to `*mut` so as to be covariant in `K` and `V`.
61
- /// This either points to an actual node or is null.
62
- parent : * const InternalNode < K , V > ,
63
-
64
- /// This node's index into the parent node's `edges` array.
65
- /// `*node.parent.edges[node.parent_idx]` should be the same thing as `node`.
66
- /// This is only guaranteed to be initialized when `parent` is non-null.
67
- parent_idx : MaybeUninit < u16 > ,
68
-
69
- /// The number of keys and values this node stores.
70
- ///
71
- /// This next to `parent_idx` to encourage the compiler to join `len` and
72
- /// `parent_idx` into the same 32-bit word, reducing space overhead.
73
- len : u16 ,
74
- }
47
+ /// The underlying representation of leaf nodes.
75
48
#[ repr( C ) ]
76
49
struct LeafNode < K , V > {
77
50
/// We use `*const` as opposed to `*mut` so as to be covariant in `K` and `V`.
@@ -141,10 +114,7 @@ impl<K, V> InternalNode<K, V> {
141
114
/// A managed, non-null pointer to a node. This is either an owned pointer to
142
115
/// `LeafNode<K, V>` or an owned pointer to `InternalNode<K, V>`.
143
116
///
144
- /// All of these types have a `NodeHeader<K, V>` prefix, meaning that they have at
145
- /// least the same size as `NodeHeader<K, V>` and store the same kinds of data at the same
146
- /// offsets; and they have a pointer alignment at least as large as `NodeHeader<K, V>`'s.
147
- /// However, `BoxedNode` contains no information as to which of the three types
117
+ /// However, `BoxedNode` contains no information as to which of the two types
148
118
/// of nodes it actually contains, and, partially due to this lack of information,
149
119
/// has no destructor.
150
120
struct BoxedNode < K , V > {
@@ -279,8 +249,6 @@ impl<K, V> Root<K, V> {
279
249
/// `Leaf`, the `NodeRef` points to a leaf node, when this is `Internal` the
280
250
/// `NodeRef` points to an internal node, and when this is `LeafOrInternal` the
281
251
/// `NodeRef` could be pointing to either type of node.
282
- ///
283
- /// Turning this into a `NodeHeader` reference is always safe.
284
252
pub struct NodeRef < BorrowType , K , V , Type > {
285
253
/// The number of levels below the node.
286
254
height : usize ,
@@ -322,7 +290,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
322
290
/// Note that, despite being safe, calling this function can have the side effect
323
291
/// of invalidating mutable references that unsafe code has created.
324
292
pub fn len ( & self ) -> usize {
325
- self . as_header ( ) . len as usize
293
+ self . as_leaf ( ) . len as usize
326
294
}
327
295
328
296
/// Returns the height of this node in the whole tree. Zero height denotes the
@@ -353,10 +321,6 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
353
321
unsafe { self . node . as_ref ( ) }
354
322
}
355
323
356
- fn as_header ( & self ) -> & NodeHeader < K , V > {
357
- unsafe { & * ( self . node . as_ptr ( ) as * const NodeHeader < K , V > ) }
358
- }
359
-
360
324
/// Borrows a view into the keys stored in the node.
361
325
pub fn keys ( & self ) -> & [ K ] {
362
326
self . reborrow ( ) . into_key_slice ( )
@@ -377,7 +341,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
377
341
pub fn ascend (
378
342
self ,
379
343
) -> Result < Handle < NodeRef < BorrowType , K , V , marker:: Internal > , marker:: Edge > , Self > {
380
- let parent_as_leaf = self . as_header ( ) . parent as * const LeafNode < K , V > ;
344
+ let parent_as_leaf = self . as_leaf ( ) . parent as * const LeafNode < K , V > ;
381
345
if let Some ( non_zero) = NonNull :: new ( parent_as_leaf as * mut _ ) {
382
346
Ok ( Handle {
383
347
node : NodeRef {
@@ -386,7 +350,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
386
350
root : self . root ,
387
351
_marker : PhantomData ,
388
352
} ,
389
- idx : unsafe { usize:: from ( * self . as_header ( ) . parent_idx . as_ptr ( ) ) } ,
353
+ idx : unsafe { usize:: from ( * self . as_leaf ( ) . parent_idx . as_ptr ( ) ) } ,
390
354
_marker : PhantomData ,
391
355
} )
392
356
} else {
0 commit comments