From 26d9f0ab1aeb3d7b440911104cf17741f83aa0f5 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Mon, 23 Feb 2015 02:58:22 +0300 Subject: Use boxed functions instead of transmute ... to convert between Box and raw pointers. E. g. use ``` let b: Box = Box::from_raw(p); ``` instead of ``` let b: Box = 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. --- src/libstd/rt/at_exit_imp.rs | 6 +++--- src/libstd/rt/unwind.rs | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'src/libstd/rt') diff --git a/src/libstd/rt/at_exit_imp.rs b/src/libstd/rt/at_exit_imp.rs index 72486fc55d4..08755ba829f 100644 --- a/src/libstd/rt/at_exit_imp.rs +++ b/src/libstd/rt/at_exit_imp.rs @@ -14,9 +14,9 @@ use core::prelude::*; +use boxed; use boxed::Box; use vec::Vec; -use mem; use thunk::Thunk; use sys_common::mutex::{Mutex, MUTEX_INIT}; @@ -32,7 +32,7 @@ static mut QUEUE: *mut Queue = 0 as *mut Queue; unsafe fn init() { if QUEUE.is_null() { let state: Box = box Vec::new(); - QUEUE = mem::transmute(state); + QUEUE = boxed::into_raw(state); } else { // can't re-init after a cleanup rtassert!(QUEUE as uint != 1); @@ -57,7 +57,7 @@ pub fn cleanup() { // If we never called init, not need to cleanup! if queue as uint != 0 { - let queue: Box = mem::transmute(queue); + let queue: Box = Box::from_raw(queue); for to_run in *queue { to_run.invoke(()); } diff --git a/src/libstd/rt/unwind.rs b/src/libstd/rt/unwind.rs index 1f5eb3af695..4dda3ea8c99 100644 --- a/src/libstd/rt/unwind.rs +++ b/src/libstd/rt/unwind.rs @@ -60,6 +60,7 @@ use prelude::v1::*; use any::Any; +use boxed; use cell::Cell; use cmp; use panicking; @@ -173,7 +174,8 @@ fn rust_panic(cause: Box) -> ! { }, cause: Some(cause), }; - let error = uw::_Unwind_RaiseException(mem::transmute(exception)); + let exception_param = boxed::into_raw(exception) as *mut uw::_Unwind_Exception; + let error = uw::_Unwind_RaiseException(exception_param); rtabort!("Could not unwind stack, error = {}", error as int) } @@ -181,7 +183,7 @@ fn rust_panic(cause: Box) -> ! { exception: *mut uw::_Unwind_Exception) { rtdebug!("exception_cleanup()"); unsafe { - let _: Box = mem::transmute(exception); + let _: Box = Box::from_raw(exception as *mut Exception); } } } -- cgit 1.4.1-3-g733a5