Skip to content

Commit 1b17eef

Browse files
committed
Any: use plain transmute instead of transmute_copy for downcasting.
transmute_copy is no longer needed and is just slow.
1 parent 0d0a290 commit 1b17eef

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/liballoc/boxed.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use core::clone::Clone;
1515
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
1616
use core::default::Default;
1717
use core::fmt;
18-
use core::intrinsics;
1918
use core::kinds::Sized;
2019
use core::mem;
2120
use core::option::Option;
@@ -104,17 +103,14 @@ pub trait BoxAny {
104103
}
105104

106105
#[stable]
107-
impl BoxAny for Box<Any+'static> {
106+
impl BoxAny for Box<Any> {
108107
#[inline]
109-
fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any+'static>> {
108+
fn downcast<T: 'static>(self) -> Result<Box<T>, Box<Any>> {
110109
if self.is::<T>() {
111110
unsafe {
112111
// Get the raw representation of the trait object
113112
let to: TraitObject =
114-
*mem::transmute::<&Box<Any>, &TraitObject>(&self);
115-
116-
// Prevent destructor on self being run
117-
intrinsics::forget(self);
113+
mem::transmute::<Box<Any>, TraitObject>(self);
118114

119115
// Extract the data pointer
120116
Ok(mem::transmute(to.data))

src/libcore/any.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
7272
#![stable]
7373

74-
use mem::{transmute, transmute_copy};
74+
use mem::{transmute};
7575
use option::{Option, Some, None};
7676
use raw::TraitObject;
7777
use intrinsics::TypeId;
@@ -134,7 +134,7 @@ impl<'a> AnyRefExt<'a> for &'a Any {
134134
if self.is::<T>() {
135135
unsafe {
136136
// Get the raw representation of the trait object
137-
let to: TraitObject = transmute_copy(&self);
137+
let to: TraitObject = transmute(self);
138138

139139
// Extract the data pointer
140140
Some(transmute(to.data))
@@ -162,7 +162,7 @@ impl<'a> AnyMutRefExt<'a> for &'a mut Any {
162162
if self.is::<T>() {
163163
unsafe {
164164
// Get the raw representation of the trait object
165-
let to: TraitObject = transmute_copy(&self);
165+
let to: TraitObject = transmute(self);
166166

167167
// Extract the data pointer
168168
Some(transmute(to.data))

0 commit comments

Comments
 (0)