Skip to content
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

Replace unnecessary uses of TraitObject with casts #36005

Merged
merged 1 commit into from
Aug 27, 2016
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
9 changes: 2 additions & 7 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ use core::mem;
use core::ops::{CoerceUnsized, Deref, DerefMut};
use core::ops::{BoxPlace, Boxed, InPlace, Place, Placer};
use core::ptr::{self, Unique};
use core::raw::TraitObject;
use core::convert::From;

/// A value that represents the heap. This is the default place that the `box`
Expand Down Expand Up @@ -428,12 +427,8 @@ impl Box<Any> {
pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let raw = Box::into_raw(self);
let to: TraitObject = mem::transmute::<*mut Any, TraitObject>(raw);

// Extract the data pointer
Ok(Box::from_raw(to.data as *mut T))
let raw: *mut Any = Box::into_raw(self);
Ok(Box::from_raw(raw as *mut T))
}
} else {
Err(self)
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
#![cfg_attr(stage0, feature(unsafe_no_drop_flag))]
#![feature(unsize)]

#![cfg_attr(not(test), feature(fused, raw, fn_traits, placement_new_protocol))]
#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol))]
#![cfg_attr(test, feature(test, box_heap))]

// Allow testing this library
Expand Down
14 changes: 2 additions & 12 deletions src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@
#![stable(feature = "rust1", since = "1.0.0")]

use fmt;
use mem::transmute;
use raw::TraitObject;
use intrinsics;
use marker::Reflect;

Expand Down Expand Up @@ -199,11 +197,7 @@ impl Any {
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let to: TraitObject = transmute(self);

// Extract the data pointer
Some(&*(to.data as *const T))
Some(&*(self as *const Any as *const T))
}
} else {
None
Expand Down Expand Up @@ -240,11 +234,7 @@ impl Any {
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let to: TraitObject = transmute(self);

// Extract the data pointer
Some(&mut *(to.data as *const T as *mut T))
Some(&mut *(self as *mut Any as *mut T))
}
} else {
None
Expand Down
22 changes: 4 additions & 18 deletions src/libstd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ use fmt::{self, Debug, Display};
use marker::Reflect;
use mem::transmute;
use num;
use raw::TraitObject;
use str;
use string;

Expand Down Expand Up @@ -326,11 +325,7 @@ impl Error + 'static {
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let to: TraitObject = transmute(self);

// Extract the data pointer
Some(&*(to.data as *const T))
Some(&*(self as *const Error as *const T))
}
} else {
None
Expand All @@ -344,11 +339,7 @@ impl Error + 'static {
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let to: TraitObject = transmute(self);

// Extract the data pointer
Some(&mut *(to.data as *const T as *mut T))
Some(&mut *(self as *mut Error as *mut T))
}
} else {
None
Expand Down Expand Up @@ -409,13 +400,8 @@ impl Error {
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error>> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let raw = Box::into_raw(self);
let to: TraitObject =
transmute::<*mut Error, TraitObject>(raw);

// Extract the data pointer
Ok(Box::from_raw(to.data as *mut T))
let raw: *mut Error = Box::into_raw(self);
Ok(Box::from_raw(raw as *mut T))
}
} else {
Err(self)
Expand Down