11use core:: cmp:: Ordering ;
2+ #[ cfg( feature = "slab-friendly" ) ]
3+ use core:: convert:: TryInto ;
24use core:: ops:: { Deref , DerefMut } ;
35
46use crate :: entry:: { ItemEntry , XEntry } ;
@@ -103,7 +105,10 @@ where
103105 offset_in_parent : u8 ,
104106 /// The slots storing `XEntry`s, which point to user-given items for leaf nodes and other
105107 /// `XNode`s for interior nodes.
108+ #[ cfg( not( feature = "slab-friendly" ) ) ]
106109 slots : [ XEntry < I > ; SLOT_SIZE ] ,
110+ #[ cfg( feature = "slab-friendly" ) ]
111+ slots : alloc:: boxed:: Box < [ XEntry < I > ; SLOT_SIZE ] > ,
107112 /// The marks representing whether each slot is marked or not.
108113 ///
109114 /// Users can set mark or unset mark on user-given items, and a leaf node or an interior node
@@ -112,6 +117,16 @@ where
112117}
113118
114119impl < I : ItemEntry > XNode < I > {
120+ #[ cfg( feature = "slab-friendly" ) ]
121+ fn empty_slots ( ) -> alloc:: boxed:: Box < [ XEntry < I > ; SLOT_SIZE ] > {
122+ let mut slots = alloc:: vec:: Vec :: with_capacity ( SLOT_SIZE ) ;
123+ slots. resize_with ( SLOT_SIZE , || XEntry :: EMPTY ) ;
124+ match slots. into_boxed_slice ( ) . try_into ( ) {
125+ Ok ( slots) => slots,
126+ Err ( _) => panic ! ( "xarray: invalid slab-friendly slot length" ) ,
127+ }
128+ }
129+
115130 pub fn new_root ( height : Height ) -> Self {
116131 Self :: new ( height, 0 )
117132 }
@@ -120,7 +135,10 @@ impl<I: ItemEntry> XNode<I> {
120135 Self {
121136 height,
122137 offset_in_parent : offset,
138+ #[ cfg( not( feature = "slab-friendly" ) ) ]
123139 slots : [ XEntry :: EMPTY ; SLOT_SIZE ] ,
140+ #[ cfg( feature = "slab-friendly" ) ]
141+ slots : Self :: empty_slots ( ) ,
124142 marks : [ Mark :: EMPTY ; NUM_MARKS ] ,
125143 }
126144 }
@@ -147,7 +165,10 @@ impl<I: ItemEntry> XNode<I> {
147165 }
148166
149167 pub fn entries_mut ( & mut self ) -> & mut [ XEntry < I > ] {
150- & mut self . slots
168+ #[ cfg( not( feature = "slab-friendly" ) ) ]
169+ return & mut self . slots ;
170+ #[ cfg( feature = "slab-friendly" ) ]
171+ return self . slots . as_mut ( ) ;
151172 }
152173
153174 pub fn is_marked ( & self , offset : u8 , mark : usize ) -> bool {
0 commit comments