]> git.lizzy.rs Git - rust.git/commitdiff
Any: use plain transmute instead of transmute_copy for downcasting.
authorJonathan Reem <jonathan.reem@gmail.com>
Sun, 23 Nov 2014 00:06:21 +0000 (16:06 -0800)
committerJonathan Reem <jonathan.reem@gmail.com>
Sun, 23 Nov 2014 00:06:21 +0000 (16:06 -0800)
transmute_copy is no longer needed and is just slow.

src/liballoc/boxed.rs
src/libcore/any.rs

index 26f8522e1c18a9b6dd5f4aa9e47ad87048d69224..000dda59e3ddaeb3e3e38d095abdda7c8b9794e3 100644 (file)
@@ -15,7 +15,6 @@
 use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
 use core::default::Default;
 use core::fmt;
-use core::intrinsics;
 use core::kinds::Sized;
 use core::mem;
 use core::option::Option;
@@ -104,17 +103,14 @@ pub trait BoxAny {
 }
 
 #[stable]
-impl BoxAny for Box<Any+'static> {
+impl BoxAny for Box<Any> {
     #[inline]
-    fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any+'static>> {
+    fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any>> {
         if self.is::<T>() {
             unsafe {
                 // Get the raw representation of the trait object
                 let to: TraitObject =
-                    *mem::transmute::<&Box<Any>, &TraitObject>(&self);
-
-                // Prevent destructor on self being run
-                intrinsics::forget(self);
+                    mem::transmute::<Box<Any>, TraitObject>(self);
 
                 // Extract the data pointer
                 Ok(mem::transmute(to.data))
index 5511266b4cd2276600c3b8fdf9232fa903c3f37a..ebd6fab34e9b519e1a1799e311c9c53a17ca1b81 100644 (file)
@@ -71,7 +71,7 @@
 
 #![stable]
 
-use mem::{transmute, transmute_copy};
+use mem::{transmute};
 use option::{Option, Some, None};
 use raw::TraitObject;
 use intrinsics::TypeId;
@@ -134,7 +134,7 @@ fn downcast_ref<T: 'static>(self) -> Option<&'a T> {
         if self.is::<T>() {
             unsafe {
                 // Get the raw representation of the trait object
-                let to: TraitObject = transmute_copy(&self);
+                let to: TraitObject = transmute(self);
 
                 // Extract the data pointer
                 Some(transmute(to.data))
@@ -162,7 +162,7 @@ fn downcast_mut<T: 'static>(self) -> Option<&'a mut T> {
         if self.is::<T>() {
             unsafe {
                 // Get the raw representation of the trait object
-                let to: TraitObject = transmute_copy(&self);
+                let to: TraitObject = transmute(self);
 
                 // Extract the data pointer
                 Some(transmute(to.data))