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