Skip to content

Any: use plain transmute instead of transmute_copy for downcasting. #19225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use core::clone::Clone;
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;
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

#![stable]

use mem::{transmute, transmute_copy};
use mem::{transmute};
use option::{Option, Some, None};
use raw::TraitObject;
use intrinsics::TypeId;
Expand Down Expand Up @@ -134,7 +134,7 @@ impl<'a> AnyRefExt<'a> for &'a Any {
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))
Expand Down Expand Up @@ -162,7 +162,7 @@ impl<'a> AnyMutRefExt<'a> for &'a mut Any {
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))
Expand Down