Skip to content

Commit 2b10df7

Browse files
committedAug 26, 2016
Replace unnecessary uses of TraitObject with casts
1 parent eaf71f8 commit 2b10df7

File tree

4 files changed

+9
-38
lines changed

4 files changed

+9
-38
lines changed
 

‎src/liballoc/boxed.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ use core::mem;
6767
use core::ops::{CoerceUnsized, Deref, DerefMut};
6868
use core::ops::{BoxPlace, Boxed, InPlace, Place, Placer};
6969
use core::ptr::{self, Unique};
70-
use core::raw::TraitObject;
7170
use core::convert::From;
7271

7372
/// A value that represents the heap. This is the default place that the `box`
@@ -428,12 +427,8 @@ impl Box<Any> {
428427
pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> {
429428
if self.is::<T>() {
430429
unsafe {
431-
// Get the raw representation of the trait object
432-
let raw = Box::into_raw(self);
433-
let to: TraitObject = mem::transmute::<*mut Any, TraitObject>(raw);
434-
435-
// Extract the data pointer
436-
Ok(Box::from_raw(to.data as *mut T))
430+
let raw: *mut Any = Box::into_raw(self);
431+
Ok(Box::from_raw(raw as *mut T))
437432
}
438433
} else {
439434
Err(self)

‎src/liballoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
#![cfg_attr(stage0, feature(unsafe_no_drop_flag))]
9292
#![feature(unsize)]
9393

94-
#![cfg_attr(not(test), feature(fused, raw, fn_traits, placement_new_protocol))]
94+
#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol))]
9595
#![cfg_attr(test, feature(test, box_heap))]
9696

9797
// Allow testing this library

‎src/libcore/any.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@
7272
#![stable(feature = "rust1", since = "1.0.0")]
7373

7474
use fmt;
75-
use mem::transmute;
76-
use raw::TraitObject;
7775
use intrinsics;
7876
use marker::Reflect;
7977

@@ -199,11 +197,7 @@ impl Any {
199197
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
200198
if self.is::<T>() {
201199
unsafe {
202-
// Get the raw representation of the trait object
203-
let to: TraitObject = transmute(self);
204-
205-
// Extract the data pointer
206-
Some(&*(to.data as *const T))
200+
Some(&*(self as *const Any as *const T))
207201
}
208202
} else {
209203
None
@@ -240,11 +234,7 @@ impl Any {
240234
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
241235
if self.is::<T>() {
242236
unsafe {
243-
// Get the raw representation of the trait object
244-
let to: TraitObject = transmute(self);
245-
246-
// Extract the data pointer
247-
Some(&mut *(to.data as *const T as *mut T))
237+
Some(&mut *(self as *mut Any as *mut T))
248238
}
249239
} else {
250240
None

‎src/libstd/error.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ use fmt::{self, Debug, Display};
5454
use marker::Reflect;
5555
use mem::transmute;
5656
use num;
57-
use raw::TraitObject;
5857
use str;
5958
use string;
6059

@@ -326,11 +325,7 @@ impl Error + 'static {
326325
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
327326
if self.is::<T>() {
328327
unsafe {
329-
// Get the raw representation of the trait object
330-
let to: TraitObject = transmute(self);
331-
332-
// Extract the data pointer
333-
Some(&*(to.data as *const T))
328+
Some(&*(self as *const Error as *const T))
334329
}
335330
} else {
336331
None
@@ -344,11 +339,7 @@ impl Error + 'static {
344339
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
345340
if self.is::<T>() {
346341
unsafe {
347-
// Get the raw representation of the trait object
348-
let to: TraitObject = transmute(self);
349-
350-
// Extract the data pointer
351-
Some(&mut *(to.data as *const T as *mut T))
342+
Some(&mut *(self as *mut Error as *mut T))
352343
}
353344
} else {
354345
None
@@ -409,13 +400,8 @@ impl Error {
409400
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error>> {
410401
if self.is::<T>() {
411402
unsafe {
412-
// Get the raw representation of the trait object
413-
let raw = Box::into_raw(self);
414-
let to: TraitObject =
415-
transmute::<*mut Error, TraitObject>(raw);
416-
417-
// Extract the data pointer
418-
Ok(Box::from_raw(to.data as *mut T))
403+
let raw: *mut Error = Box::into_raw(self);
404+
Ok(Box::from_raw(raw as *mut T))
419405
}
420406
} else {
421407
Err(self)

0 commit comments

Comments
 (0)
Please sign in to comment.