about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorJeremy Fitzhardinge <jsgf@fb.com>2018-05-17 08:25:48 -0700
committerJeremy Fitzhardinge <jsgf@fb.com>2018-05-31 13:27:08 -0700
commit0c7bf56d805d981bb003a20a4c6b1f6b29790881 (patch)
tree68edaeac99d19d5a058cb5f036027de5deec5cb2 /src/liballoc
parent37f5cf563c2c039503e8e50e252f2c1b31d69268 (diff)
downloadrust-0c7bf56d805d981bb003a20a4c6b1f6b29790881.tar.gz
rust-0c7bf56d805d981bb003a20a4c6b1f6b29790881.zip
Update `Arc` and `Rc` to use `NonNull::cast` to cast the inner pointers
This avoids an `unsafe` block in each case.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/arc.rs11
-rw-r--r--src/liballoc/rc.rs12
2 files changed, 6 insertions, 17 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs
index 0795498f87f..4026b3ababa 100644
--- a/src/liballoc/arc.rs
+++ b/src/liballoc/arc.rs
@@ -1001,14 +1001,9 @@ impl Arc<Any + Send + Sync> {
         T: Any + Send + Sync + 'static,
     {
         if (*self).is::<T>() {
-            unsafe {
-                let raw: *const ArcInner<Any + Send + Sync> = self.ptr.as_ptr();
-                mem::forget(self);
-                Ok(Arc {
-                    ptr: NonNull::new_unchecked(raw as *const ArcInner<T> as *mut _),
-                    phantom: PhantomData,
-                })
-            }
+            let ptr = self.ptr.cast::<ArcInner<T>>();
+            mem::forget(self);
+            Ok(Arc { ptr, phantom: PhantomData })
         } else {
             Err(self)
         }
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs
index 1648fc6b7ef..553c8b5ca32 100644
--- a/src/liballoc/rc.rs
+++ b/src/liballoc/rc.rs
@@ -644,15 +644,9 @@ impl Rc<Any> {
     /// ```
     pub fn downcast<T: Any>(self) -> Result<Rc<T>, Rc<Any>> {
         if (*self).is::<T>() {
-            // avoid the pointer arithmetic in from_raw
-            unsafe {
-                let raw: *const RcBox<Any> = self.ptr.as_ptr();
-                forget(self);
-                Ok(Rc {
-                    ptr: NonNull::new_unchecked(raw as *const RcBox<T> as *mut _),
-                    phantom: PhantomData,
-                })
-            }
+            let ptr = self.ptr.cast::<RcBox<T>>();
+            forget(self);
+            Ok(Rc { ptr, phantom: PhantomData })
         } else {
             Err(self)
         }