@@ -21,10 +21,11 @@ use core::prelude::*;
21
21
use core:: borrow:: BorrowFrom ;
22
22
use core:: cmp:: Ordering :: { Greater , Less , Equal } ;
23
23
use core:: iter:: Zip ;
24
+ use core:: marker:: PhantomData ;
24
25
use core:: ops:: { Deref , DerefMut , Index , IndexMut } ;
25
26
use core:: ptr:: Unique ;
26
27
use core:: { slice, mem, ptr, cmp, num, raw} ;
27
- use alloc:: heap;
28
+ use alloc:: heap:: { self , EMPTY } ;
28
29
29
30
/// Represents the result of an Insertion: either the item fit, or the node had to split
30
31
pub enum InsertionResult < K , V > {
@@ -57,8 +58,8 @@ pub struct Node<K, V> {
57
58
keys : Unique < K > ,
58
59
vals : Unique < V > ,
59
60
60
- // In leaf nodes, this will be null , and no space will be allocated for edges.
61
- edges : Unique < Node < K , V > > ,
61
+ // In leaf nodes, this will be None , and no space will be allocated for edges.
62
+ edges : Option < Unique < Node < K , V > > > ,
62
63
63
64
// At any given time, there will be `_len` keys, `_len` values, and (in an internal node)
64
65
// `_len + 1` edges. In a leaf node, there will never be any edges.
@@ -278,8 +279,11 @@ impl<T> Drop for RawItems<T> {
278
279
#[ unsafe_destructor]
279
280
impl < K , V > Drop for Node < K , V > {
280
281
fn drop ( & mut self ) {
281
- if self . keys . ptr . is_null ( ) {
282
- // We have already cleaned up this node.
282
+ if self . keys . is_null ( ) {
283
+ // Since we have #[unsafe_no_drop_flag], we have to watch
284
+ // out for a null value being stored in self.keys. (Using
285
+ // null is technically a violation of the `Unique`
286
+ // requirements, though.)
283
287
return ;
284
288
}
285
289
@@ -292,7 +296,7 @@ impl<K, V> Drop for Node<K, V> {
292
296
self . destroy ( ) ;
293
297
}
294
298
295
- self . keys . ptr = ptr :: null_mut ( ) ;
299
+ self . keys = unsafe { Unique :: new ( 0 as * mut K ) } ;
296
300
}
297
301
}
298
302
@@ -308,9 +312,9 @@ impl<K, V> Node<K, V> {
308
312
let ( vals_offset, edges_offset) = calculate_offsets_generic :: < K , V > ( capacity, false ) ;
309
313
310
314
Node {
311
- keys : Unique ( buffer as * mut K ) ,
312
- vals : Unique ( buffer. offset ( vals_offset as isize ) as * mut V ) ,
313
- edges : Unique ( buffer. offset ( edges_offset as isize ) as * mut Node < K , V > ) ,
315
+ keys : Unique :: new ( buffer as * mut K ) ,
316
+ vals : Unique :: new ( buffer. offset ( vals_offset as isize ) as * mut V ) ,
317
+ edges : Some ( Unique :: new ( buffer. offset ( edges_offset as isize ) as * mut Node < K , V > ) ) ,
314
318
_len : 0 ,
315
319
_capacity : capacity,
316
320
}
@@ -326,9 +330,9 @@ impl<K, V> Node<K, V> {
326
330
let ( vals_offset, _) = calculate_offsets_generic :: < K , V > ( capacity, true ) ;
327
331
328
332
Node {
329
- keys : Unique ( buffer as * mut K ) ,
330
- vals : Unique ( unsafe { buffer. offset ( vals_offset as isize ) as * mut V } ) ,
331
- edges : Unique ( ptr :: null_mut ( ) ) ,
333
+ keys : unsafe { Unique :: new ( buffer as * mut K ) } ,
334
+ vals : unsafe { Unique :: new ( buffer. offset ( vals_offset as isize ) as * mut V ) } ,
335
+ edges : None ,
332
336
_len : 0 ,
333
337
_capacity : capacity,
334
338
}
@@ -337,18 +341,18 @@ impl<K, V> Node<K, V> {
337
341
unsafe fn destroy ( & mut self ) {
338
342
let ( alignment, size) =
339
343
calculate_allocation_generic :: < K , V > ( self . capacity ( ) , self . is_leaf ( ) ) ;
340
- heap:: deallocate ( self . keys . ptr as * mut u8 , size, alignment) ;
344
+ heap:: deallocate ( * self . keys as * mut u8 , size, alignment) ;
341
345
}
342
346
343
347
#[ inline]
344
348
pub fn as_slices < ' a > ( & ' a self ) -> ( & ' a [ K ] , & ' a [ V ] ) {
345
349
unsafe { (
346
350
mem:: transmute ( raw:: Slice {
347
- data : self . keys . ptr ,
351
+ data : * self . keys as * const K ,
348
352
len : self . len ( )
349
353
} ) ,
350
354
mem:: transmute ( raw:: Slice {
351
- data : self . vals . ptr ,
355
+ data : * self . vals as * const V ,
352
356
len : self . len ( )
353
357
} )
354
358
) }
@@ -367,8 +371,12 @@ impl<K, V> Node<K, V> {
367
371
& [ ]
368
372
} else {
369
373
unsafe {
374
+ let data = match self . edges {
375
+ None => heap:: EMPTY as * const Node < K , V > ,
376
+ Some ( ref p) => * * p as * const Node < K , V > ,
377
+ } ;
370
378
mem:: transmute ( raw:: Slice {
371
- data : self . edges . ptr ,
379
+ data : data ,
372
380
len : self . len ( ) + 1
373
381
} )
374
382
}
@@ -524,7 +532,8 @@ impl<K: Clone, V: Clone> Clone for Node<K, V> {
524
532
#[ derive( Copy ) ]
525
533
pub struct Handle < NodeRef , Type , NodeType > {
526
534
node : NodeRef ,
527
- index : usize
535
+ index : usize ,
536
+ marker : PhantomData < ( Type , NodeType ) > ,
528
537
}
529
538
530
539
pub mod handle {
@@ -548,8 +557,8 @@ impl<K: Ord, V> Node<K, V> {
548
557
// For the B configured as of this writing (B = 6), binary search was *significantly*
549
558
// worse for usizes.
550
559
match node. as_slices_internal ( ) . search_linear ( key) {
551
- ( index, true ) => Found ( Handle { node : node, index : index } ) ,
552
- ( index, false ) => GoDown ( Handle { node : node, index : index } ) ,
560
+ ( index, true ) => Found ( Handle { node : node, index : index, marker : PhantomData } ) ,
561
+ ( index, false ) => GoDown ( Handle { node : node, index : index, marker : PhantomData } ) ,
553
562
}
554
563
}
555
564
}
@@ -586,7 +595,7 @@ impl <K, V> Node<K, V> {
586
595
587
596
/// If the node has any children
588
597
pub fn is_leaf ( & self ) -> bool {
589
- self . edges . ptr . is_null ( )
598
+ self . edges . is_none ( )
590
599
}
591
600
592
601
/// if the node has too few elements
@@ -618,7 +627,8 @@ impl<K, V, NodeRef, Type, NodeType> Handle<NodeRef, Type, NodeType> where
618
627
pub fn as_raw ( & mut self ) -> Handle < * mut Node < K , V > , Type , NodeType > {
619
628
Handle {
620
629
node : & mut * self . node as * mut _ ,
621
- index : self . index
630
+ index : self . index ,
631
+ marker : PhantomData ,
622
632
}
623
633
}
624
634
}
@@ -630,7 +640,8 @@ impl<K, V, Type, NodeType> Handle<*mut Node<K, V>, Type, NodeType> {
630
640
pub unsafe fn from_raw < ' a > ( & ' a self ) -> Handle < & ' a Node < K , V > , Type , NodeType > {
631
641
Handle {
632
642
node : & * self . node ,
633
- index : self . index
643
+ index : self . index ,
644
+ marker : PhantomData ,
634
645
}
635
646
}
636
647
@@ -640,7 +651,8 @@ impl<K, V, Type, NodeType> Handle<*mut Node<K, V>, Type, NodeType> {
640
651
pub unsafe fn from_raw_mut < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , Type , NodeType > {
641
652
Handle {
642
653
node : & mut * self . node ,
643
- index : self . index
654
+ index : self . index ,
655
+ marker : PhantomData ,
644
656
}
645
657
}
646
658
}
@@ -688,12 +700,14 @@ impl<K, V, NodeRef: Deref<Target=Node<K, V>>, Type> Handle<NodeRef, Type, handle
688
700
if self . node . is_leaf ( ) {
689
701
Leaf ( Handle {
690
702
node : self . node ,
691
- index : self . index
703
+ index : self . index ,
704
+ marker : PhantomData ,
692
705
} )
693
706
} else {
694
707
Internal ( Handle {
695
708
node : self . node ,
696
- index : self . index
709
+ index : self . index ,
710
+ marker : PhantomData ,
697
711
} )
698
712
}
699
713
}
@@ -826,7 +840,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
826
840
unsafe fn left_kv < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , handle:: KV , NodeType > {
827
841
Handle {
828
842
node : & mut * self . node ,
829
- index : self . index - 1
843
+ index : self . index - 1 ,
844
+ marker : PhantomData ,
830
845
}
831
846
}
832
847
@@ -836,7 +851,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
836
851
unsafe fn right_kv < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , handle:: KV , NodeType > {
837
852
Handle {
838
853
node : & mut * self . node ,
839
- index : self . index
854
+ index : self . index ,
855
+ marker : PhantomData ,
840
856
}
841
857
}
842
858
}
@@ -876,7 +892,8 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a mut Node<K, V>, handle::KV, NodeType
876
892
pub fn into_left_edge ( self ) -> Handle < & ' a mut Node < K , V > , handle:: Edge , NodeType > {
877
893
Handle {
878
894
node : & mut * self . node ,
879
- index : self . index
895
+ index : self . index ,
896
+ marker : PhantomData ,
880
897
}
881
898
}
882
899
}
@@ -926,7 +943,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
926
943
pub fn left_edge < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , handle:: Edge , NodeType > {
927
944
Handle {
928
945
node : & mut * self . node ,
929
- index : self . index
946
+ index : self . index ,
947
+ marker : PhantomData ,
930
948
}
931
949
}
932
950
@@ -935,7 +953,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
935
953
pub fn right_edge < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , handle:: Edge , NodeType > {
936
954
Handle {
937
955
node : & mut * self . node ,
938
- index : self . index + 1
956
+ index : self . index + 1 ,
957
+ marker : PhantomData ,
939
958
}
940
959
}
941
960
}
@@ -1044,7 +1063,8 @@ impl<K, V> Node<K, V> {
1044
1063
debug_assert ! ( index < self . len( ) , "kv_handle index out of bounds" ) ;
1045
1064
Handle {
1046
1065
node : self ,
1047
- index : index
1066
+ index : index,
1067
+ marker : PhantomData ,
1048
1068
}
1049
1069
}
1050
1070
@@ -1064,7 +1084,7 @@ impl<K, V> Node<K, V> {
1064
1084
vals : RawItems :: from_slice ( self . vals ( ) ) ,
1065
1085
edges : RawItems :: from_slice ( self . edges ( ) ) ,
1066
1086
1067
- ptr : self . keys . ptr as * mut u8 ,
1087
+ ptr : * self . keys as * mut u8 ,
1068
1088
capacity : self . capacity ( ) ,
1069
1089
is_leaf : self . is_leaf ( )
1070
1090
} ,
0 commit comments