diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2015-02-23 14:48:54 +0530 |
|---|---|---|
| committer | Manish Goregaokar <manishsmail@gmail.com> | 2015-02-23 23:28:49 +0530 |
| commit | db04229d23119e76f52408b61d960e1bcc52af7a (patch) | |
| tree | 0ea536aed48e5e6f97b607725575309102f60eca /src/libstd/sys | |
| parent | b2302a50ed534ed671d87835f435af4034705cbe (diff) | |
| parent | 26d9f0ab1aeb3d7b440911104cf17741f83aa0f5 (diff) | |
| download | rust-db04229d23119e76f52408b61d960e1bcc52af7a.tar.gz rust-db04229d23119e76f52408b61d960e1bcc52af7a.zip | |
Rollup merge of #22696 - stepancheg:use-box, r=alexcrichton
e. g. ``` 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. (And there are two more patches in PR: fix typo in doc and mark `from_raw` and `into_raw` functions inline.)
Diffstat (limited to 'src/libstd/sys')
| -rw-r--r-- | src/libstd/sys/common/helper_thread.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys/common/thread.rs | 2 | ||||
| -rw-r--r-- | src/libstd/sys/unix/thread.rs | 7 | ||||
| -rw-r--r-- | src/libstd/sys/windows/thread.rs | 7 | ||||
| -rw-r--r-- | src/libstd/sys/windows/thread_local.rs | 4 |
5 files changed, 16 insertions, 10 deletions
diff --git a/src/libstd/sys/common/helper_thread.rs b/src/libstd/sys/common/helper_thread.rs index dc1ae85efe0..5faaa928ee9 100644 --- a/src/libstd/sys/common/helper_thread.rs +++ b/src/libstd/sys/common/helper_thread.rs @@ -22,8 +22,8 @@ use prelude::v1::*; +use boxed; use cell::UnsafeCell; -use mem; use ptr; use rt; use sync::{StaticMutex, StaticCondvar}; @@ -88,7 +88,7 @@ impl<M: Send> Helper<M> { let _guard = self.lock.lock().unwrap(); if !*self.initialized.get() { let (tx, rx) = channel(); - *self.chan.get() = mem::transmute(box tx); + *self.chan.get() = boxed::into_raw(box tx); let (receive, send) = helper_signal::new(); *self.signal.get() = send as uint; @@ -132,7 +132,7 @@ impl<M: Send> Helper<M> { let mut guard = self.lock.lock().unwrap(); // Close the channel by destroying it - let chan: Box<Sender<M>> = mem::transmute(*self.chan.get()); + let chan: Box<Sender<M>> = Box::from_raw(*self.chan.get()); *self.chan.get() = ptr::null_mut(); drop(chan); helper_signal::signal(*self.signal.get() as helper_signal::signal); diff --git a/src/libstd/sys/common/thread.rs b/src/libstd/sys/common/thread.rs index b725b6c7e6e..731617858e9 100644 --- a/src/libstd/sys/common/thread.rs +++ b/src/libstd/sys/common/thread.rs @@ -27,7 +27,7 @@ pub fn start_thread(main: *mut libc::c_void) -> thread::rust_thread_return { unsafe { stack::record_os_managed_stack_bounds(0, usize::MAX); let handler = stack_overflow::Handler::new(); - let f: Box<Thunk> = mem::transmute(main); + let f: Box<Thunk> = Box::from_raw(main as *mut Thunk); f.invoke(()); drop(handler); mem::transmute(0 as thread::rust_thread_return) diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index c42d6d0e641..f4791d39da1 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -11,6 +11,7 @@ use core::prelude::*; use io; +use boxed; use boxed::Box; use cmp; use mem; @@ -241,13 +242,15 @@ pub unsafe fn create(stack: uint, p: Thunk) -> io::Result<rust_thread> { }, }; - let arg: *mut libc::c_void = mem::transmute(box p); // must box since sizeof(p)=2*uint + // must box since sizeof(p)=2*uint + let raw_p = boxed::into_raw(box p); + let arg = raw_p as *mut libc::c_void; let ret = pthread_create(&mut native, &attr, thread_start, arg); assert_eq!(pthread_attr_destroy(&mut attr), 0); if ret != 0 { // be sure to not leak the closure - let _p: Box<Box<FnOnce()+Send>> = mem::transmute(arg); + let _p: Box<Thunk> = Box::from_raw(raw_p); Err(io::Error::from_os_error(ret)) } else { Ok(native) diff --git a/src/libstd/sys/windows/thread.rs b/src/libstd/sys/windows/thread.rs index f3a27877e5c..7fe8dce1a3e 100644 --- a/src/libstd/sys/windows/thread.rs +++ b/src/libstd/sys/windows/thread.rs @@ -10,6 +10,8 @@ use prelude::v1::*; +use boxed; +use boxed::Box; use cmp; use io; use mem; @@ -45,7 +47,8 @@ pub mod guard { } pub unsafe fn create(stack: uint, p: Thunk) -> io::Result<rust_thread> { - let arg: *mut libc::c_void = mem::transmute(box p); + let raw_p = boxed::into_raw(box p); + let arg = raw_p as *mut libc::c_void; // FIXME On UNIX, we guard against stack sizes that are too small but // that's because pthreads enforces that stacks are at least // PTHREAD_STACK_MIN bytes big. Windows has no such lower limit, it's @@ -61,7 +64,7 @@ pub unsafe fn create(stack: uint, p: Thunk) -> io::Result<rust_thread> { if ret as uint == 0 { // be sure to not leak the closure - let _p: Box<Thunk> = mem::transmute(arg); + let _p: Box<Thunk> = Box::from_raw(raw_p); Err(io::Error::last_os_error()) } else { Ok(ret) diff --git a/src/libstd/sys/windows/thread_local.rs b/src/libstd/sys/windows/thread_local.rs index 0c24ab1fa09..cf942b5d025 100644 --- a/src/libstd/sys/windows/thread_local.rs +++ b/src/libstd/sys/windows/thread_local.rs @@ -133,13 +133,13 @@ unsafe fn init_dtors() { if !DTORS.is_null() { return } let dtors = box Vec::<(Key, Dtor)>::new(); - DTORS = mem::transmute(dtors); + DTORS = boxed::into_raw(dtors); rt::at_exit(move|| { DTOR_LOCK.lock(); let dtors = DTORS; DTORS = ptr::null_mut(); - mem::transmute::<_, Box<Vec<(Key, Dtor)>>>(dtors); + Boxed::from_raw(dtors); assert!(DTORS.is_null()); // can't re-init after destructing DTOR_LOCK.unlock(); }); |
