Skip to content

Commit aa29df4

Browse files
committed
---
yaml --- r: 184155 b: refs/heads/beta c: 68ebe64 h: refs/heads/master i: 184153: cfb86a0 184151: e5655cc v: v3
1 parent a991dae commit aa29df4

File tree

2 files changed

+53
-33
lines changed

2 files changed

+53
-33
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ refs/heads/automation-fail: 1bf06495443584539b958873e04cc2f864ab10e4
3131
refs/heads/issue-18208-method-dispatch-3-quick-reject: 2009f85b9f99dedcec4404418eda9ddba90258a2
3232
refs/heads/batch: b7fd822592a4fb577552d93010c4a4e14f314346
3333
refs/heads/building: 126db549b038c84269a1e4fe46f051b2c15d6970
34-
refs/heads/beta: b3c00a69f23b68cba2901eb98b3de7dfc6990396
34+
refs/heads/beta: 68ebe640b6c99f53fee53671e09c673c8c17726a
3535
refs/heads/windistfix: 7608dbad651f02e837ed05eef3d74a6662a6e928
3636
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
3737
refs/heads/tmp: c65fb1a81e5dc58cf171cc47f65de9e6e2119247

branches/beta/src/libcollections/btree/node.rs

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ use core::prelude::*;
2121
use core::borrow::BorrowFrom;
2222
use core::cmp::Ordering::{Greater, Less, Equal};
2323
use core::iter::Zip;
24+
use core::marker::PhantomData;
2425
use core::ops::{Deref, DerefMut, Index, IndexMut};
2526
use core::ptr::Unique;
2627
use core::{slice, mem, ptr, cmp, num, raw};
27-
use alloc::heap;
28+
use alloc::heap::{self, EMPTY};
2829

2930
/// Represents the result of an Insertion: either the item fit, or the node had to split
3031
pub enum InsertionResult<K, V> {
@@ -57,8 +58,8 @@ pub struct Node<K, V> {
5758
keys: Unique<K>,
5859
vals: Unique<V>,
5960

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>>>,
6263

6364
// At any given time, there will be `_len` keys, `_len` values, and (in an internal node)
6465
// `_len + 1` edges. In a leaf node, there will never be any edges.
@@ -278,8 +279,11 @@ impl<T> Drop for RawItems<T> {
278279
#[unsafe_destructor]
279280
impl<K, V> Drop for Node<K, V> {
280281
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.)
283287
return;
284288
}
285289

@@ -292,7 +296,7 @@ impl<K, V> Drop for Node<K, V> {
292296
self.destroy();
293297
}
294298

295-
self.keys.ptr = ptr::null_mut();
299+
self.keys = unsafe { Unique::new(0 as *mut K) };
296300
}
297301
}
298302

@@ -308,9 +312,9 @@ impl<K, V> Node<K, V> {
308312
let (vals_offset, edges_offset) = calculate_offsets_generic::<K, V>(capacity, false);
309313

310314
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>)),
314318
_len: 0,
315319
_capacity: capacity,
316320
}
@@ -326,9 +330,9 @@ impl<K, V> Node<K, V> {
326330
let (vals_offset, _) = calculate_offsets_generic::<K, V>(capacity, true);
327331

328332
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,
332336
_len: 0,
333337
_capacity: capacity,
334338
}
@@ -337,18 +341,18 @@ impl<K, V> Node<K, V> {
337341
unsafe fn destroy(&mut self) {
338342
let (alignment, size) =
339343
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);
341345
}
342346

343347
#[inline]
344348
pub fn as_slices<'a>(&'a self) -> (&'a [K], &'a [V]) {
345349
unsafe {(
346350
mem::transmute(raw::Slice {
347-
data: self.keys.ptr,
351+
data: *self.keys as *const K,
348352
len: self.len()
349353
}),
350354
mem::transmute(raw::Slice {
351-
data: self.vals.ptr,
355+
data: *self.vals as *const V,
352356
len: self.len()
353357
})
354358
)}
@@ -367,8 +371,12 @@ impl<K, V> Node<K, V> {
367371
&[]
368372
} else {
369373
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+
};
370378
mem::transmute(raw::Slice {
371-
data: self.edges.ptr,
379+
data: data,
372380
len: self.len() + 1
373381
})
374382
}
@@ -524,7 +532,8 @@ impl<K: Clone, V: Clone> Clone for Node<K, V> {
524532
#[derive(Copy)]
525533
pub struct Handle<NodeRef, Type, NodeType> {
526534
node: NodeRef,
527-
index: usize
535+
index: usize,
536+
marker: PhantomData<(Type, NodeType)>,
528537
}
529538

530539
pub mod handle {
@@ -548,8 +557,8 @@ impl<K: Ord, V> Node<K, V> {
548557
// For the B configured as of this writing (B = 6), binary search was *significantly*
549558
// worse for usizes.
550559
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 }),
553562
}
554563
}
555564
}
@@ -586,7 +595,7 @@ impl <K, V> Node<K, V> {
586595

587596
/// If the node has any children
588597
pub fn is_leaf(&self) -> bool {
589-
self.edges.ptr.is_null()
598+
self.edges.is_none()
590599
}
591600

592601
/// if the node has too few elements
@@ -618,7 +627,8 @@ impl<K, V, NodeRef, Type, NodeType> Handle<NodeRef, Type, NodeType> where
618627
pub fn as_raw(&mut self) -> Handle<*mut Node<K, V>, Type, NodeType> {
619628
Handle {
620629
node: &mut *self.node as *mut _,
621-
index: self.index
630+
index: self.index,
631+
marker: PhantomData,
622632
}
623633
}
624634
}
@@ -630,7 +640,8 @@ impl<K, V, Type, NodeType> Handle<*mut Node<K, V>, Type, NodeType> {
630640
pub unsafe fn from_raw<'a>(&'a self) -> Handle<&'a Node<K, V>, Type, NodeType> {
631641
Handle {
632642
node: &*self.node,
633-
index: self.index
643+
index: self.index,
644+
marker: PhantomData,
634645
}
635646
}
636647

@@ -640,7 +651,8 @@ impl<K, V, Type, NodeType> Handle<*mut Node<K, V>, Type, NodeType> {
640651
pub unsafe fn from_raw_mut<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, Type, NodeType> {
641652
Handle {
642653
node: &mut *self.node,
643-
index: self.index
654+
index: self.index,
655+
marker: PhantomData,
644656
}
645657
}
646658
}
@@ -688,12 +700,14 @@ impl<K, V, NodeRef: Deref<Target=Node<K, V>>, Type> Handle<NodeRef, Type, handle
688700
if self.node.is_leaf() {
689701
Leaf(Handle {
690702
node: self.node,
691-
index: self.index
703+
index: self.index,
704+
marker: PhantomData,
692705
})
693706
} else {
694707
Internal(Handle {
695708
node: self.node,
696-
index: self.index
709+
index: self.index,
710+
marker: PhantomData,
697711
})
698712
}
699713
}
@@ -826,7 +840,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
826840
unsafe fn left_kv<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::KV, NodeType> {
827841
Handle {
828842
node: &mut *self.node,
829-
index: self.index - 1
843+
index: self.index - 1,
844+
marker: PhantomData,
830845
}
831846
}
832847

@@ -836,7 +851,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
836851
unsafe fn right_kv<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::KV, NodeType> {
837852
Handle {
838853
node: &mut *self.node,
839-
index: self.index
854+
index: self.index,
855+
marker: PhantomData,
840856
}
841857
}
842858
}
@@ -876,7 +892,8 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a mut Node<K, V>, handle::KV, NodeType
876892
pub fn into_left_edge(self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
877893
Handle {
878894
node: &mut *self.node,
879-
index: self.index
895+
index: self.index,
896+
marker: PhantomData,
880897
}
881898
}
882899
}
@@ -926,7 +943,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
926943
pub fn left_edge<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
927944
Handle {
928945
node: &mut *self.node,
929-
index: self.index
946+
index: self.index,
947+
marker: PhantomData,
930948
}
931949
}
932950

@@ -935,7 +953,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
935953
pub fn right_edge<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
936954
Handle {
937955
node: &mut *self.node,
938-
index: self.index + 1
956+
index: self.index + 1,
957+
marker: PhantomData,
939958
}
940959
}
941960
}
@@ -1044,7 +1063,8 @@ impl<K, V> Node<K, V> {
10441063
debug_assert!(index < self.len(), "kv_handle index out of bounds");
10451064
Handle {
10461065
node: self,
1047-
index: index
1066+
index: index,
1067+
marker: PhantomData,
10481068
}
10491069
}
10501070

@@ -1064,7 +1084,7 @@ impl<K, V> Node<K, V> {
10641084
vals: RawItems::from_slice(self.vals()),
10651085
edges: RawItems::from_slice(self.edges()),
10661086

1067-
ptr: self.keys.ptr as *mut u8,
1087+
ptr: *self.keys as *mut u8,
10681088
capacity: self.capacity(),
10691089
is_leaf: self.is_leaf()
10701090
},

0 commit comments

Comments
 (0)