Skip to content

Commit f4ad02f

Browse files
committed
collections: use Unique in btree::Node
1 parent 7112390 commit f4ad02f

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/libcollections/btree/node.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use core::prelude::*;
2121
use core::{slice, mem, ptr, cmp, num, raw};
2222
use core::iter::Zip;
2323
use core::borrow::BorrowFrom;
24+
use core::ptr::Unique;
2425
use alloc::heap;
2526

2627
/// Represents the result of an Insertion: either the item fit, or the node had to split
@@ -51,11 +52,11 @@ pub struct Node<K, V> {
5152
// These will never be null during normal usage of a `Node`. However, to avoid the need for a
5253
// drop flag, `Node::drop` zeroes `keys`, signaling that the `Node` has already been cleaned
5354
// up.
54-
keys: *mut K,
55-
vals: *mut V,
55+
keys: Unique<K>,
56+
vals: Unique<V>,
5657

5758
// In leaf nodes, this will be null, and no space will be allocated for edges.
58-
edges: *mut Node<K, V>,
59+
edges: Unique<Node<K, V>>,
5960

6061
// At any given time, there will be `_len` keys, `_len` values, and (in an internal node)
6162
// `_len + 1` edges. In a leaf node, there will never be any edges.
@@ -255,7 +256,7 @@ impl<T> Drop for RawItems<T> {
255256
#[unsafe_destructor]
256257
impl<K, V> Drop for Node<K, V> {
257258
fn drop(&mut self) {
258-
if self.keys.is_null() {
259+
if self.keys.0.is_null() {
259260
// We have already cleaned up this node.
260261
return;
261262
}
@@ -269,7 +270,7 @@ impl<K, V> Drop for Node<K, V> {
269270
self.destroy();
270271
}
271272

272-
self.keys = ptr::null_mut();
273+
self.keys.0 = ptr::null_mut();
273274
}
274275
}
275276

@@ -285,9 +286,9 @@ impl<K, V> Node<K, V> {
285286
let (vals_offset, edges_offset) = calculate_offsets_generic::<K, V>(capacity, false);
286287

287288
Node {
288-
keys: buffer as *mut K,
289-
vals: buffer.offset(vals_offset as int) as *mut V,
290-
edges: buffer.offset(edges_offset as int) as *mut Node<K, V>,
289+
keys: Unique(buffer as *mut K),
290+
vals: Unique(buffer.offset(vals_offset as int) as *mut V),
291+
edges: Unique(buffer.offset(edges_offset as int) as *mut Node<K, V>),
291292
_len: 0,
292293
_capacity: capacity,
293294
}
@@ -303,9 +304,9 @@ impl<K, V> Node<K, V> {
303304
let (vals_offset, _) = calculate_offsets_generic::<K, V>(capacity, true);
304305

305306
Node {
306-
keys: buffer as *mut K,
307-
vals: unsafe { buffer.offset(vals_offset as int) as *mut V },
308-
edges: ptr::null_mut(),
307+
keys: Unique(buffer as *mut K).
308+
vals: Unique(unsafe { buffer.offset(vals_offset as int) as *mut V }),
309+
edges: Unique(ptr::null_mut::<u8>()),
309310
_len: 0,
310311
_capacity: capacity,
311312
}
@@ -314,18 +315,18 @@ impl<K, V> Node<K, V> {
314315
unsafe fn destroy(&mut self) {
315316
let (alignment, size) =
316317
calculate_allocation_generic::<K, V>(self.capacity(), self.is_leaf());
317-
heap::deallocate(self.keys as *mut u8, size, alignment);
318+
heap::deallocate(self.keys.0 as *mut u8, size, alignment);
318319
}
319320

320321
#[inline]
321322
pub fn as_slices<'a>(&'a self) -> (&'a [K], &'a [V]) {
322323
unsafe {(
323324
mem::transmute(raw::Slice {
324-
data: self.keys as *const K,
325+
data: self.keys.0 as *const K,
325326
len: self.len()
326327
}),
327328
mem::transmute(raw::Slice {
328-
data: self.vals as *const V,
329+
data: self.vals.0 as *const V,
329330
len: self.len()
330331
})
331332
)}
@@ -344,7 +345,7 @@ impl<K, V> Node<K, V> {
344345
} else {
345346
unsafe {
346347
mem::transmute(raw::Slice {
347-
data: self.edges as *const Node<K, V>,
348+
data: self.edges.0 as *const Node<K, V>,
348349
len: self.len() + 1
349350
})
350351
}

0 commit comments

Comments
 (0)