Skip to content

Commit 2d13ddb

Browse files
committed
Use NonNull<_> instead of NonZero<*const _> in btree internals
1 parent f40877f commit 2d13ddb

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/liballoc/btree/node.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343

4444
use core::marker::PhantomData;
4545
use core::mem;
46-
use core::nonzero::NonZero;
47-
use core::ptr::{self, Unique};
46+
use core::ptr::{self, Unique, NonNull};
4847
use core::slice;
4948

5049
use boxed::Box;
@@ -149,14 +148,12 @@ impl<K, V> BoxedNode<K, V> {
149148
}
150149
}
151150

152-
unsafe fn from_ptr(ptr: NonZero<*const LeafNode<K, V>>) -> Self {
153-
BoxedNode { ptr: Unique::new_unchecked(ptr.get() as *mut LeafNode<K, V>) }
151+
unsafe fn from_ptr(ptr: NonNull<LeafNode<K, V>>) -> Self {
152+
BoxedNode { ptr: Unique::from(ptr) }
154153
}
155154

156-
fn as_ptr(&self) -> NonZero<*const LeafNode<K, V>> {
157-
unsafe {
158-
NonZero::from(self.ptr.as_ref())
159-
}
155+
fn as_ptr(&self) -> NonNull<LeafNode<K, V>> {
156+
NonNull::from(self.ptr)
160157
}
161158
}
162159

@@ -276,7 +273,7 @@ impl<K, V> Root<K, V> {
276273
/// `NodeRef` could be pointing to either type of node.
277274
pub struct NodeRef<BorrowType, K, V, Type> {
278275
height: usize,
279-
node: NonZero<*const LeafNode<K, V>>,
276+
node: NonNull<LeafNode<K, V>>,
280277
// This is null unless the borrow type is `Mut`
281278
root: *const Root<K, V>,
282279
_marker: PhantomData<(BorrowType, Type)>
@@ -302,15 +299,15 @@ unsafe impl<K: Send, V: Send, Type> Send
302299
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
303300
fn as_internal(&self) -> &InternalNode<K, V> {
304301
unsafe {
305-
&*(self.node.get() as *const InternalNode<K, V>)
302+
&*(self.node.as_ptr() as *mut InternalNode<K, V>)
306303
}
307304
}
308305
}
309306

310307
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
311308
fn as_internal_mut(&mut self) -> &mut InternalNode<K, V> {
312309
unsafe {
313-
&mut *(self.node.get() as *mut InternalNode<K, V>)
310+
&mut *(self.node.as_ptr() as *mut InternalNode<K, V>)
314311
}
315312
}
316313
}
@@ -352,7 +349,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
352349

353350
fn as_leaf(&self) -> &LeafNode<K, V> {
354351
unsafe {
355-
&*self.node.get()
352+
self.node.as_ref()
356353
}
357354
}
358355

@@ -382,7 +379,8 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
382379
>,
383380
Self
384381
> {
385-
if let Some(non_zero) = NonZero::new(self.as_leaf().parent as *const LeafNode<K, V>) {
382+
let parent_as_leaf = self.as_leaf().parent as *const LeafNode<K, V>;
383+
if let Some(non_zero) = NonNull::new(parent_as_leaf as *mut _) {
386384
Ok(Handle {
387385
node: NodeRef {
388386
height: self.height + 1,
@@ -498,7 +496,7 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
498496

499497
fn as_leaf_mut(&mut self) -> &mut LeafNode<K, V> {
500498
unsafe {
501-
&mut *(self.node.get() as *mut LeafNode<K, V>)
499+
self.node.as_mut()
502500
}
503501
}
504502

@@ -1241,12 +1239,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12411239
}
12421240

12431241
Heap.dealloc(
1244-
right_node.node.get() as *mut u8,
1242+
right_node.node.as_ptr() as *mut u8,
12451243
Layout::new::<InternalNode<K, V>>(),
12461244
);
12471245
} else {
12481246
Heap.dealloc(
1249-
right_node.node.get() as *mut u8,
1247+
right_node.node.as_ptr() as *mut u8,
12501248
Layout::new::<LeafNode<K, V>>(),
12511249
);
12521250
}

0 commit comments

Comments
 (0)