From f94d671bfae5d8e9a4a4add310b1c40af0ab62a6 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 9 May 2014 10:34:51 -0700 Subject: core: Remove the cast module This commit revisits the `cast` module in libcore and libstd, and scrutinizes all functions inside of it. The result was to remove the `cast` module entirely, folding all functionality into the `mem` module. Specifically, this is the fate of each function in the `cast` module. * transmute - This function was moved to `mem`, but it is now marked as #[unstable]. This is due to planned changes to the `transmute` function and how it can be invoked (see the #[unstable] comment). For more information, see RFC 5 and #12898 * transmute_copy - This function was moved to `mem`, with clarification that is is not an error to invoke it with T/U that are different sizes, but rather that it is strongly discouraged. This function is now #[stable] * forget - This function was moved to `mem` and marked #[stable] * bump_box_refcount - This function was removed due to the deprecation of managed boxes as well as its questionable utility. * transmute_mut - This function was previously deprecated, and removed as part of this commit. * transmute_mut_unsafe - This function doesn't serve much of a purpose when it can be achieved with an `as` in safe code, so it was removed. * transmute_lifetime - This function was removed because it is likely a strong indication that code is incorrect in the first place. * transmute_mut_lifetime - This function was removed for the same reasons as `transmute_lifetime` * copy_lifetime - This function was moved to `mem`, but it is marked `#[unstable]` now due to the likelihood of being removed in the future if it is found to not be very useful. * copy_mut_lifetime - This function was also moved to `mem`, but had the same treatment as `copy_lifetime`. * copy_lifetime_vec - This function was removed because it is not used today, and its existence is not necessary with DST (copy_lifetime will suffice). In summary, the cast module was stripped down to these functions, and then the functions were moved to the `mem` module. transmute - #[unstable] transmute_copy - #[stable] forget - #[stable] copy_lifetime - #[unstable] copy_mut_lifetime - #[unstable] [breaking-change] --- src/libnative/io/addrinfo.rs | 8 ++++---- src/libnative/io/file_win32.rs | 3 +-- src/libnative/io/net.rs | 9 ++++----- src/libnative/io/pipe_unix.rs | 3 +-- src/libnative/io/process.rs | 6 +++--- src/libnative/io/timer_helper.rs | 6 +++--- src/libnative/lib.rs | 4 ++-- src/libnative/task.rs | 12 ++++++------ 8 files changed, 24 insertions(+), 27 deletions(-) (limited to 'src/libnative') diff --git a/src/libnative/io/addrinfo.rs b/src/libnative/io/addrinfo.rs index 57b87f21521..8ebae70f73c 100644 --- a/src/libnative/io/addrinfo.rs +++ b/src/libnative/io/addrinfo.rs @@ -9,11 +9,11 @@ // except according to those terms. use ai = std::io::net::addrinfo; +use libc::{c_char, c_int}; +use libc; use std::c_str::CString; -use std::cast; use std::io::IoError; -use libc; -use libc::{c_char, c_int}; +use std::mem; use std::ptr::{null, mut_null}; use super::net::sockaddr_to_addr; @@ -61,7 +61,7 @@ impl GetAddrInfoRequest { let mut rp = res; while rp.is_not_null() { unsafe { - let addr = match sockaddr_to_addr(cast::transmute((*rp).ai_addr), + let addr = match sockaddr_to_addr(mem::transmute((*rp).ai_addr), (*rp).ai_addrlen as uint) { Ok(a) => a, Err(e) => return Err(e) diff --git a/src/libnative/io/file_win32.rs b/src/libnative/io/file_win32.rs index 5fc9e506cf2..fc08a7f128f 100644 --- a/src/libnative/io/file_win32.rs +++ b/src/libnative/io/file_win32.rs @@ -11,7 +11,6 @@ //! Blocking win32-based file I/O use std::c_str::CString; -use std::cast; use std::io::IoError; use std::io; use libc::{c_int, c_void}; @@ -175,7 +174,7 @@ impl rtio::RtioFileStream for FileDesc { // This transmute is fine because our seek implementation doesn't // actually use the mutable self at all. // FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes - unsafe { cast::transmute::<&_, &mut FileDesc>(self).seek(0, io::SeekCur) } + unsafe { mem::transmute::<&_, &mut FileDesc>(self).seek(0, io::SeekCur) } } fn fsync(&mut self) -> Result<(), IoError> { diff --git a/src/libnative/io/net.rs b/src/libnative/io/net.rs index 218f8a4ef49..40b66cc526f 100644 --- a/src/libnative/io/net.rs +++ b/src/libnative/io/net.rs @@ -9,7 +9,6 @@ // except according to those terms. use libc; -use std::cast; use std::io::net::ip; use std::io; use std::mem; @@ -72,14 +71,14 @@ fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) { let storage: libc::sockaddr_storage = mem::init(); let len = match ip_to_inaddr(addr.ip) { InAddr(inaddr) => { - let storage: *mut libc::sockaddr_in = cast::transmute(&storage); + let storage: *mut libc::sockaddr_in = mem::transmute(&storage); (*storage).sin_family = libc::AF_INET as libc::sa_family_t; (*storage).sin_port = htons(addr.port); (*storage).sin_addr = inaddr; mem::size_of::() } In6Addr(inaddr) => { - let storage: *mut libc::sockaddr_in6 = cast::transmute(&storage); + let storage: *mut libc::sockaddr_in6 = mem::transmute(&storage); (*storage).sin6_family = libc::AF_INET6 as libc::sa_family_t; (*storage).sin6_port = htons(addr.port); (*storage).sin6_addr = inaddr; @@ -173,7 +172,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage, libc::AF_INET => { assert!(len as uint >= mem::size_of::()); let storage: &libc::sockaddr_in = unsafe { - cast::transmute(storage) + mem::transmute(storage) }; let addr = storage.sin_addr.s_addr as u32; let a = (addr >> 0) as u8; @@ -188,7 +187,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage, libc::AF_INET6 => { assert!(len as uint >= mem::size_of::()); let storage: &libc::sockaddr_in6 = unsafe { - cast::transmute(storage) + mem::transmute(storage) }; let a = ntohs(storage.sin6_addr.s6_addr[0]); let b = ntohs(storage.sin6_addr.s6_addr[1]); diff --git a/src/libnative/io/pipe_unix.rs b/src/libnative/io/pipe_unix.rs index 36ae2ba06d5..d66075f4419 100644 --- a/src/libnative/io/pipe_unix.rs +++ b/src/libnative/io/pipe_unix.rs @@ -10,7 +10,6 @@ use libc; use std::c_str::CString; -use std::cast; use std::intrinsics; use std::io; use std::mem; @@ -36,7 +35,7 @@ fn addr_to_sockaddr_un(addr: &CString) -> IoResult<(libc::sockaddr_storage, uint assert!(mem::size_of::() >= mem::size_of::()); let mut storage: libc::sockaddr_storage = unsafe { intrinsics::init() }; - let s: &mut libc::sockaddr_un = unsafe { cast::transmute(&mut storage) }; + let s: &mut libc::sockaddr_un = unsafe { mem::transmute(&mut storage) }; let len = addr.len(); if len > s.sun_path.len() - 1 { diff --git a/src/libnative/io/process.rs b/src/libnative/io/process.rs index c83af20d1d8..81c76bba7a0 100644 --- a/src/libnative/io/process.rs +++ b/src/libnative/io/process.rs @@ -19,7 +19,7 @@ use p = std::io::process; use super::IoResult; use super::file; -#[cfg(windows)] use std::cast; +#[cfg(windows)] use std::mem; #[cfg(windows)] use std::strbuf::StrBuf; #[cfg(not(windows))] use super::retry; @@ -326,7 +326,7 @@ fn spawn_process_os(config: p::ProcessConfig, with_envp(env, |envp| { with_dirp(dir, |dirp| { cmd.with_c_str(|cmdp| { - let created = CreateProcessA(ptr::null(), cast::transmute(cmdp), + let created = CreateProcessA(ptr::null(), mem::transmute(cmdp), ptr::mut_null(), ptr::mut_null(), TRUE, flags, envp, dirp, &mut si, &mut pi); @@ -714,7 +714,7 @@ fn with_dirp(d: Option<&Path>, cb: |*libc::c_char| -> T) -> T { #[cfg(windows)] fn free_handle(handle: *()) { assert!(unsafe { - libc::CloseHandle(cast::transmute(handle)) != 0 + libc::CloseHandle(mem::transmute(handle)) != 0 }) } diff --git a/src/libnative/io/timer_helper.rs b/src/libnative/io/timer_helper.rs index 3bf967cb426..95b2620f3c7 100644 --- a/src/libnative/io/timer_helper.rs +++ b/src/libnative/io/timer_helper.rs @@ -20,7 +20,7 @@ //! can be created in the future and there must be no active timers at that //! time. -use std::cast; +use std::mem; use std::rt::bookkeeping; use std::rt; use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; @@ -48,7 +48,7 @@ pub fn boot(helper: fn(imp::signal, Receiver)) { let (tx, rx) = channel(); // promote this to a shared channel drop(tx.clone()); - HELPER_CHAN = cast::transmute(box tx); + HELPER_CHAN = mem::transmute(box tx); let (receive, send) = imp::new(); HELPER_SIGNAL = send; @@ -86,7 +86,7 @@ fn shutdown() { // Clean up after ther helper thread unsafe { imp::close(HELPER_SIGNAL); - let _chan: Box> = cast::transmute(HELPER_CHAN); + let _chan: Box> = mem::transmute(HELPER_CHAN); HELPER_CHAN = 0 as *mut Sender; HELPER_SIGNAL = 0 as imp::signal; } diff --git a/src/libnative/lib.rs b/src/libnative/lib.rs index 4c0c4dcc18e..0df45f7d5a0 100644 --- a/src/libnative/lib.rs +++ b/src/libnative/lib.rs @@ -73,9 +73,9 @@ static OS_DEFAULT_STACK_ESTIMATE: uint = 2 * (1 << 20); #[lang = "start"] #[cfg(not(test))] pub fn lang_start(main: *u8, argc: int, argv: **u8) -> int { - use std::cast; + use std::mem; start(argc, argv, proc() { - let main: extern "Rust" fn() = unsafe { cast::transmute(main) }; + let main: extern "Rust" fn() = unsafe { mem::transmute(main) }; main(); }) } diff --git a/src/libnative/task.rs b/src/libnative/task.rs index d5b02dc007b..4183dec19d0 100644 --- a/src/libnative/task.rs +++ b/src/libnative/task.rs @@ -15,7 +15,7 @@ //! in order to spawn new tasks and deschedule the current task. use std::any::Any; -use std::cast; +use std::mem; use std::rt::bookkeeping; use std::rt::env; use std::rt::local::Local; @@ -169,7 +169,7 @@ impl rt::Runtime for Ops { // for both tasks because these operations are all done inside of a mutex. // // You'll also find that if blocking fails (the `f` function hands the - // BlockedTask back to us), we will `cast::forget` the handles. The + // BlockedTask back to us), we will `mem::forget` the handles. The // reasoning for this is the same logic as above in that the task silently // transfers ownership via the `uint`, not through normal compiler // semantics. @@ -198,7 +198,7 @@ impl rt::Runtime for Ops { guard.wait(); } } - Err(task) => { cast::forget(task.wake()); } + Err(task) => { mem::forget(task.wake()); } } } else { let iter = task.make_selectable(times); @@ -217,7 +217,7 @@ impl rt::Runtime for Ops { Some(task) => { match task.wake() { Some(task) => { - cast::forget(task); + mem::forget(task); (*me).awoken = true; } None => {} @@ -229,7 +229,7 @@ impl rt::Runtime for Ops { } } // re-acquire ownership of the task - cur_task = cast::transmute(cur_task_dupe); + cur_task = mem::transmute(cur_task_dupe); } // put the task back in TLS, and everything is as it once was. @@ -242,7 +242,7 @@ impl rt::Runtime for Ops { unsafe { let me = &mut *self as *mut Ops; to_wake.put_runtime(self); - cast::forget(to_wake); + mem::forget(to_wake); let guard = (*me).lock.lock(); (*me).awoken = true; guard.signal(); -- cgit 1.4.1-3-g733a5