@@ -20,10 +20,11 @@ use core::prelude::*;
2020
2121use core:: cmp:: Ordering :: { Greater , Less , Equal } ;
2222use core:: iter:: Zip ;
23+ use core:: marker:: PhantomData ;
2324use core:: ops:: { Deref , DerefMut , Index , IndexMut } ;
2425use core:: ptr:: Unique ;
2526use core:: { slice, mem, ptr, cmp, num, raw} ;
26- use alloc:: heap;
27+ use alloc:: heap:: { self , EMPTY } ;
2728
2829use borrow:: Borrow ;
2930
@@ -58,8 +59,8 @@ pub struct Node<K, V> {
5859 keys : Unique < K > ,
5960 vals : Unique < V > ,
6061
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 > > > ,
6364
6465 // At any given time, there will be `_len` keys, `_len` values, and (in an internal node)
6566 // `_len + 1` edges. In a leaf node, there will never be any edges.
@@ -279,8 +280,11 @@ impl<T> Drop for RawItems<T> {
279280#[ unsafe_destructor]
280281impl < K , V > Drop for Node < K , V > {
281282 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.)
284288 return ;
285289 }
286290
@@ -293,7 +297,7 @@ impl<K, V> Drop for Node<K, V> {
293297 self . destroy ( ) ;
294298 }
295299
296- self . keys . ptr = ptr :: null_mut ( ) ;
300+ self . keys = unsafe { Unique :: new ( 0 as * mut K ) } ;
297301 }
298302}
299303
@@ -309,9 +313,9 @@ impl<K, V> Node<K, V> {
309313 let ( vals_offset, edges_offset) = calculate_offsets_generic :: < K , V > ( capacity, false ) ;
310314
311315 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 > ) ) ,
315319 _len : 0 ,
316320 _capacity : capacity,
317321 }
@@ -327,9 +331,9 @@ impl<K, V> Node<K, V> {
327331 let ( vals_offset, _) = calculate_offsets_generic :: < K , V > ( capacity, true ) ;
328332
329333 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 ,
333337 _len : 0 ,
334338 _capacity : capacity,
335339 }
@@ -338,18 +342,18 @@ impl<K, V> Node<K, V> {
338342 unsafe fn destroy ( & mut self ) {
339343 let ( alignment, size) =
340344 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) ;
342346 }
343347
344348 #[ inline]
345349 pub fn as_slices < ' a > ( & ' a self ) -> ( & ' a [ K ] , & ' a [ V ] ) {
346350 unsafe { (
347351 mem:: transmute ( raw:: Slice {
348- data : self . keys . ptr ,
352+ data : * self . keys as * const K ,
349353 len : self . len ( )
350354 } ) ,
351355 mem:: transmute ( raw:: Slice {
352- data : self . vals . ptr ,
356+ data : * self . vals as * const V ,
353357 len : self . len ( )
354358 } )
355359 ) }
@@ -368,8 +372,12 @@ impl<K, V> Node<K, V> {
368372 & [ ]
369373 } else {
370374 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+ } ;
371379 mem:: transmute ( raw:: Slice {
372- data : self . edges . ptr ,
380+ data : data ,
373381 len : self . len ( ) + 1
374382 } )
375383 }
@@ -525,7 +533,8 @@ impl<K: Clone, V: Clone> Clone for Node<K, V> {
525533#[ derive( Copy ) ]
526534pub struct Handle < NodeRef , Type , NodeType > {
527535 node : NodeRef ,
528- index : usize
536+ index : usize ,
537+ marker : PhantomData < ( Type , NodeType ) > ,
529538}
530539
531540pub mod handle {
@@ -549,8 +558,8 @@ impl<K: Ord, V> Node<K, V> {
549558 // For the B configured as of this writing (B = 6), binary search was *significantly*
550559 // worse for usizes.
551560 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 } ) ,
554563 }
555564 }
556565}
@@ -587,7 +596,7 @@ impl <K, V> Node<K, V> {
587596
588597 /// If the node has any children
589598 pub fn is_leaf ( & self ) -> bool {
590- self . edges . ptr . is_null ( )
599+ self . edges . is_none ( )
591600 }
592601
593602 /// if the node has too few elements
@@ -619,7 +628,8 @@ impl<K, V, NodeRef, Type, NodeType> Handle<NodeRef, Type, NodeType> where
619628 pub fn as_raw ( & mut self ) -> Handle < * mut Node < K , V > , Type , NodeType > {
620629 Handle {
621630 node : & mut * self . node as * mut _ ,
622- index : self . index
631+ index : self . index ,
632+ marker : PhantomData ,
623633 }
624634 }
625635}
@@ -631,7 +641,8 @@ impl<K, V, Type, NodeType> Handle<*mut Node<K, V>, Type, NodeType> {
631641 pub unsafe fn from_raw < ' a > ( & ' a self ) -> Handle < & ' a Node < K , V > , Type , NodeType > {
632642 Handle {
633643 node : & * self . node ,
634- index : self . index
644+ index : self . index ,
645+ marker : PhantomData ,
635646 }
636647 }
637648
@@ -641,7 +652,8 @@ impl<K, V, Type, NodeType> Handle<*mut Node<K, V>, Type, NodeType> {
641652 pub unsafe fn from_raw_mut < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , Type , NodeType > {
642653 Handle {
643654 node : & mut * self . node ,
644- index : self . index
655+ index : self . index ,
656+ marker : PhantomData ,
645657 }
646658 }
647659}
@@ -689,12 +701,14 @@ impl<K, V, NodeRef: Deref<Target=Node<K, V>>, Type> Handle<NodeRef, Type, handle
689701 if self . node . is_leaf ( ) {
690702 Leaf ( Handle {
691703 node : self . node ,
692- index : self . index
704+ index : self . index ,
705+ marker : PhantomData ,
693706 } )
694707 } else {
695708 Internal ( Handle {
696709 node : self . node ,
697- index : self . index
710+ index : self . index ,
711+ marker : PhantomData ,
698712 } )
699713 }
700714 }
@@ -827,7 +841,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
827841 unsafe fn left_kv < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , handle:: KV , NodeType > {
828842 Handle {
829843 node : & mut * self . node ,
830- index : self . index - 1
844+ index : self . index - 1 ,
845+ marker : PhantomData ,
831846 }
832847 }
833848
@@ -837,7 +852,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
837852 unsafe fn right_kv < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , handle:: KV , NodeType > {
838853 Handle {
839854 node : & mut * self . node ,
840- index : self . index
855+ index : self . index ,
856+ marker : PhantomData ,
841857 }
842858 }
843859}
@@ -877,7 +893,8 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a mut Node<K, V>, handle::KV, NodeType
877893 pub fn into_left_edge ( self ) -> Handle < & ' a mut Node < K , V > , handle:: Edge , NodeType > {
878894 Handle {
879895 node : & mut * self . node ,
880- index : self . index
896+ index : self . index ,
897+ marker : PhantomData ,
881898 }
882899 }
883900}
@@ -927,7 +944,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
927944 pub fn left_edge < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , handle:: Edge , NodeType > {
928945 Handle {
929946 node : & mut * self . node ,
930- index : self . index
947+ index : self . index ,
948+ marker : PhantomData ,
931949 }
932950 }
933951
@@ -936,7 +954,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
936954 pub fn right_edge < ' a > ( & ' a mut self ) -> Handle < & ' a mut Node < K , V > , handle:: Edge , NodeType > {
937955 Handle {
938956 node : & mut * self . node ,
939- index : self . index + 1
957+ index : self . index + 1 ,
958+ marker : PhantomData ,
940959 }
941960 }
942961}
@@ -1045,7 +1064,8 @@ impl<K, V> Node<K, V> {
10451064 debug_assert ! ( index < self . len( ) , "kv_handle index out of bounds" ) ;
10461065 Handle {
10471066 node : self ,
1048- index : index
1067+ index : index,
1068+ marker : PhantomData ,
10491069 }
10501070 }
10511071
@@ -1065,7 +1085,7 @@ impl<K, V> Node<K, V> {
10651085 vals : RawItems :: from_slice ( self . vals ( ) ) ,
10661086 edges : RawItems :: from_slice ( self . edges ( ) ) ,
10671087
1068- ptr : self . keys . ptr as * mut u8 ,
1088+ ptr : * self . keys as * mut u8 ,
10691089 capacity : self . capacity ( ) ,
10701090 is_leaf : self . is_leaf ( )
10711091 } ,
0 commit comments