about summary refs log tree commit diff
path: root/src/libcore/ptr.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-05-09 10:34:51 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-05-11 01:13:02 -0700
commitf94d671bfae5d8e9a4a4add310b1c40af0ab62a6 (patch)
tree97bea161eb7fff71a0e9a484aa9f190dbe037f58 /src/libcore/ptr.rs
parentadb8b0b230d5e5c79b4f873825b3d3cff8d1bc8f (diff)
downloadrust-f94d671bfae5d8e9a4a4add310b1c40af0ab62a6.tar.gz
rust-f94d671bfae5d8e9a4a4add310b1c40af0ab62a6.zip
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]
Diffstat (limited to 'src/libcore/ptr.rs')
-rw-r--r--src/libcore/ptr.rs38
1 files changed, 18 insertions, 20 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index bb587c0e42d..438e18d999b 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -52,18 +52,18 @@
 //! though unsafely, transformed from one type to the other.
 //!
 //! ```
-//! use std::cast;
+//! use std::mem;
 //!
 //! unsafe {
 //!     let my_num: Box<int> = box 10;
-//!     let my_num: *int = cast::transmute(my_num);
+//!     let my_num: *int = mem::transmute(my_num);
 //!     let my_speed: Box<int> = box 88;
-//!     let my_speed: *mut int = cast::transmute(my_speed);
+//!     let my_speed: *mut int = mem::transmute(my_speed);
 //!
 //!     // By taking ownership of the original `Box<T>` though
 //!     // we are obligated to transmute it back later to be destroyed.
-//!     drop(cast::transmute::<_, Box<int>>(my_speed));
-//!     drop(cast::transmute::<_, Box<int>>(my_num));
+//!     drop(mem::transmute::<_, Box<int>>(my_speed));
+//!     drop(mem::transmute::<_, Box<int>>(my_num));
 //! }
 //! ```
 //!
@@ -92,11 +92,10 @@
 //! but C APIs hand out a lot of pointers generally, so are a common source
 //! of unsafe pointers in Rust.
 
-use cast;
+use mem;
 use clone::Clone;
 use intrinsics;
 use iter::{range, Iterator};
-use mem;
 use option::{Some, None, Option};
 
 #[cfg(not(test))] use cmp::{Eq, TotalEq, Ord, Equiv};
@@ -196,7 +195,6 @@ pub unsafe fn copy_memory<T>(dst: *mut T, src: *T, count: uint) {
 /// A safe swap function:
 ///
 /// ```
-/// use std::cast;
 /// use std::mem;
 /// use std::ptr;
 ///
@@ -212,7 +210,7 @@ pub unsafe fn copy_memory<T>(dst: *mut T, src: *T, count: uint) {
 ///
 ///         // y and t now point to the same thing, but we need to completely forget `tmp`
 ///         // because it's no longer relevant.
-///         cast::forget(t);
+///         mem::forget(t);
 ///     }
 /// }
 /// ```
@@ -256,14 +254,14 @@ pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
 
     // y and t now point to the same thing, but we need to completely forget `tmp`
     // because it's no longer relevant.
-    cast::forget(tmp);
+    mem::forget(tmp);
 }
 
 /// Replace the value at a mutable location with a new one, returning the old
 /// value, without deinitialising either.
 #[inline]
 pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
-    mem::swap(cast::transmute(dest), &mut src); // cannot overlap
+    mem::swap(mem::transmute(dest), &mut src); // cannot overlap
     src
 }
 
@@ -361,7 +359,7 @@ impl<T> RawPtr<T> for *T {
         if self.is_null() {
             None
         } else {
-            Some(cast::transmute(*self))
+            Some(mem::transmute(*self))
         }
     }
 }
@@ -386,7 +384,7 @@ impl<T> RawPtr<T> for *mut T {
         if self.is_null() {
             None
         } else {
-            Some(cast::transmute(*self))
+            Some(mem::transmute(*self))
         }
     }
 }
@@ -436,14 +434,14 @@ impl<T> Equiv<*T> for *mut T {
 // Equality for extern "C" fn pointers
 #[cfg(not(test))]
 mod externfnpointers {
-    use cast;
+    use mem;
     use cmp::Eq;
 
     impl<_R> Eq for extern "C" fn() -> _R {
         #[inline]
         fn eq(&self, other: &extern "C" fn() -> _R) -> bool {
-            let self_: *() = unsafe { cast::transmute(*self) };
-            let other_: *() = unsafe { cast::transmute(*other) };
+            let self_: *() = unsafe { mem::transmute(*self) };
+            let other_: *() = unsafe { mem::transmute(*other) };
             self_ == other_
         }
     }
@@ -452,8 +450,8 @@ mod externfnpointers {
             impl<_R,$($p),*> Eq for extern "C" fn($($p),*) -> _R {
                 #[inline]
                 fn eq(&self, other: &extern "C" fn($($p),*) -> _R) -> bool {
-                    let self_: *() = unsafe { cast::transmute(*self) };
-                    let other_: *() = unsafe { cast::transmute(*other) };
+                    let self_: *() = unsafe { mem::transmute(*self) };
+                    let other_: *() = unsafe { mem::transmute(*other) };
                     self_ == other_
                 }
             }
@@ -485,7 +483,7 @@ pub mod ptr_tests {
     use realstd::prelude::*;
 
     use realstd::c_str::ToCStr;
-    use cast;
+    use mem;
     use libc;
     use realstd::str;
     use slice::{ImmutableVector, MutableVector};
@@ -499,7 +497,7 @@ pub mod ptr_tests {
             };
             let mut p = Pair {fst: 10, snd: 20};
             let pptr: *mut Pair = &mut p;
-            let iptr: *mut int = cast::transmute(pptr);
+            let iptr: *mut int = mem::transmute(pptr);
             assert_eq!(*iptr, 10);
             *iptr = 30;
             assert_eq!(*iptr, 30);