diff options
| author | Stepan Koltsov <stepan.koltsov@gmail.com> | 2015-02-23 02:58:22 +0300 |
|---|---|---|
| committer | Stepan Koltsov <stepan.koltsov@gmail.com> | 2015-02-23 02:59:17 +0300 |
| commit | 26d9f0ab1aeb3d7b440911104cf17741f83aa0f5 (patch) | |
| tree | eea0a5891102880a9ce3bb76a8d3098f263aa7a9 /src/liballoc | |
| parent | 554022e58392b173092e707b4c553713d07ed44d (diff) | |
| download | rust-26d9f0ab1aeb3d7b440911104cf17741f83aa0f5.tar.gz rust-26d9f0ab1aeb3d7b440911104cf17741f83aa0f5.zip | |
Use boxed functions instead of transmute
... to convert between Box and raw pointers. E. g. use ``` let b: Box<Foo> = Box::from_raw(p); ``` instead of ``` let b: Box<Foo> = mem::transmute(p); ``` Patch also changes closure release code in `src/libstd/sys/unix/thread.rs` when `pthread_create` failed. Raw pointer was transmuted to box of `FnOnce()` instead of `Thunk`. This code was probably never executed, because `pthread_create` rarely fails in practice.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 5 | ||||
| -rw-r--r-- | src/liballoc/rc.rs | 5 |
2 files changed, 6 insertions, 4 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index befdf995701..a93872dfe36 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -250,11 +250,12 @@ impl BoxAny for Box<Any> { if self.is::<T>() { unsafe { // Get the raw representation of the trait object + let raw = into_raw(self); let to: TraitObject = - mem::transmute::<Box<Any>, TraitObject>(self); + mem::transmute::<*mut Any, TraitObject>(raw); // Extract the data pointer - Ok(mem::transmute(to.data)) + Ok(Box::from_raw(to.data as *mut T)) } } else { Err(self) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index ed7d34de7a6..5b3d7c9ea90 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -144,6 +144,7 @@ #![stable(feature = "rust1", since = "1.0.0")] +use boxed; use core::cell::Cell; use core::clone::Clone; use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering}; @@ -151,7 +152,7 @@ use core::default::Default; use core::fmt; use core::hash::{Hasher, Hash}; use core::marker; -use core::mem::{transmute, min_align_of, size_of, forget}; +use core::mem::{min_align_of, size_of, forget}; use core::nonzero::NonZero; use core::ops::{Deref, Drop}; use core::option::Option; @@ -201,7 +202,7 @@ impl<T> Rc<T> { // there is an implicit weak pointer owned by all the strong pointers, which // ensures that the weak destructor never frees the allocation while the strong // destructor is running, even if the weak pointer is stored inside the strong one. - _ptr: NonZero::new(transmute(box RcBox { + _ptr: NonZero::new(boxed::into_raw(box RcBox { value: value, strong: Cell::new(1), weak: Cell::new(1) |
