]> git.lizzy.rs Git - rust.git/blobdiff - src/libcollections/btree/node.rs
Auto merge of #31052 - bluss:split-at-mut-str, r=alexcrichton
[rust.git] / src / libcollections / btree / node.rs
index 62e85cb64a1f043c49c25d29535580fd9b058698..f07962811fdabda17bd1c4cea7f503a51eaf1bf9 100644 (file)
@@ -97,13 +97,13 @@ fn from_internal(node: Box<InternalNode<K, V>>) -> Self {
         }
     }
 
-    unsafe fn from_ptr(ptr: NonZero<*mut LeafNode<K, V>>) -> Self {
-        BoxedNode { ptr: Unique::new(*ptr) }
+    unsafe fn from_ptr(ptr: NonZero<*const LeafNode<K, V>>) -> Self {
+        BoxedNode { ptr: Unique::new(*ptr as *mut LeafNode<K, V>) }
     }
 
-    fn as_ptr(&self) -> NonZero<*mut LeafNode<K, V>> {
+    fn as_ptr(&self) -> NonZero<*const LeafNode<K, V>> {
         unsafe {
-            NonZero::new(*self.ptr)
+            NonZero::new(*self.ptr as *const LeafNode<K, V>)
         }
     }
 }
@@ -209,6 +209,11 @@ pub fn pop_level(&mut self) {
     }
 }
 
+// N.B. `NodeRef` is always covariant in `K` and `V`, even when the `BorrowType`
+// is `Mut`. This is technically wrong, but cannot result in any unsafety due to
+// internal use of `NodeRef` because we stay completely generic over `K` and `V`.
+// However, whenever a public type wraps `NodeRef`, make sure that it has the
+// correct variance.
 /// A reference to a node.
 ///
 /// This type has a number of paramaters that controls how it acts:
@@ -223,8 +228,8 @@ pub fn pop_level(&mut self) {
 ///   `NodeRef` could be pointing to either type of node.
 pub struct NodeRef<BorrowType, K, V, Type> {
     height: usize,
-    node: NonZero<*mut LeafNode<K, V>>,
-    root: *mut Root<K, V>,
+    node: NonZero<*const LeafNode<K, V>>,
+    root: *const Root<K, V>,
     _marker: PhantomData<(BorrowType, Type)>
 }
 
@@ -401,7 +406,7 @@ unsafe fn reborrow_mut(&mut self) -> NodeRef<marker::Mut, K, V, Type> {
 
     fn as_leaf_mut(&mut self) -> &mut LeafNode<K, V> {
         unsafe {
-            &mut **self.node
+            &mut *(*self.node as *mut LeafNode<K, V>)
         }
     }
 
@@ -434,7 +439,7 @@ pub fn into_slices(self) -> (&'a [K], &'a [V]) {
 impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
     pub fn into_root_mut(self) -> &'a mut Root<K, V> {
         unsafe {
-            &mut *self.root
+            &mut *(self.root as *mut Root<K, V>)
         }
     }