From 49e11630fa84eefc27a34c39ad28a9afb515c5a1 Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Fri, 14 Feb 2014 18:42:01 -0500 Subject: [PATCH 1/2] std: clean up ptr a bit --- src/doc/guide-ffi.md | 10 +- src/libcollections/dlist.rs | 2 +- src/librustc/middle/ty.rs | 5 +- src/libstd/io/test.rs | 10 +- src/libstd/managed.rs | 5 +- src/libstd/ptr.rs | 110 ++++++------------ src/libstd/rc.rs | 10 +- src/libstd/repr.rs | 9 +- src/libstd/sync/deque.rs | 2 +- src/libstd/task.rs | 4 +- src/libstd/vec.rs | 76 ++++++------ src/libstd/vec_ng.rs | 12 +- src/test/compile-fail/issue-3096-2.rs | 4 +- .../borrowck-borrow-from-expr-block.rs | 4 +- .../borrowck-preserve-box-in-field.rs | 9 +- .../run-pass/borrowck-preserve-box-in-uniq.rs | 9 +- src/test/run-pass/borrowck-preserve-box.rs | 9 +- .../run-pass/borrowck-preserve-expl-deref.rs | 9 +- src/test/run-pass/cap-clause-move.rs | 23 ---- .../run-pass/const-region-ptrs-noncopy.rs | 4 +- src/test/run-pass/enum-alignment.rs | 4 +- src/test/run-pass/stable-addr-of.rs | 4 +- src/test/run-pass/swap-overlapping.rs | 2 +- src/test/run-pass/task-spawn-move-and-copy.rs | 5 +- src/test/run-pass/uniq-cc-generic.rs | 3 +- 25 files changed, 126 insertions(+), 218 deletions(-) delete mode 100644 src/test/run-pass/cap-clause-move.rs diff --git a/src/doc/guide-ffi.md b/src/doc/guide-ffi.md index 053a8612694ab..c279ff314b474 100644 --- a/src/doc/guide-ffi.md +++ b/src/doc/guide-ffi.md @@ -229,7 +229,7 @@ impl Drop for Unique { let x = mem::uninit(); // dummy value to swap in // We need to move the object out of the box, so that // the destructor is called (at the end of this scope.) - ptr::replace_ptr(self.ptr, x); + ptr::replace(self.ptr, x); free(self.ptr as *mut c_void) } } @@ -306,7 +306,7 @@ which would call back to `callback()` in Rust. The former example showed how a global function can be called from C code. However it is often desired that the callback is targetted to a special Rust object. This could be the object that represents the wrapper for the -respective C object. +respective C object. This can be achieved by passing an unsafe pointer to the object down to the C library. The C library can then include the pointer to the Rust object in @@ -335,7 +335,7 @@ extern { fn main() { // Create the object that will be referenced in the callback let rust_object = ~RustObject{a: 5, ...}; - + unsafe { // Gets a raw pointer to the object let target_addr:*RustObject = ptr::to_unsafe_ptr(rust_object); @@ -380,8 +380,8 @@ Rust is to use channels (in `std::comm`) to forward data from the C thread that invoked the callback into a Rust task. If an asychronous callback targets a special object in the Rust address space -it is also absolutely necessary that no more callbacks are performed by the -C library after the respective Rust object gets destroyed. +it is also absolutely necessary that no more callbacks are performed by the +C library after the respective Rust object gets destroyed. This can be achieved by unregistering the callback in the object's destructor and designing the library in a way that guarantees that no callback will be performed after unregistration. diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index 15e2303bd8502..28e7b9460dc7a 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -83,7 +83,7 @@ impl Rawlink { /// Like Option::Some for Rawlink fn some(n: &mut T) -> Rawlink { - Rawlink{p: ptr::to_mut_unsafe_ptr(n)} + Rawlink{p: n} } /// Convert the `Rawlink` into an Option value diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 22ed9b5010f83..374f49a36be5e 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -33,7 +33,6 @@ use std::cell::{Cell, RefCell}; use std::cmp; use std::hashmap::{HashMap, HashSet}; use std::ops; -use std::ptr::to_unsafe_ptr; use std::rc::Rc; use std::to_bytes; use std::to_str::ToStr; @@ -1137,7 +1136,7 @@ pub fn mk_t(cx: ctxt, st: sty) -> t { _ => {} }; - let key = intern_key { sty: to_unsafe_ptr(&st) }; + let key = intern_key { sty: &st }; { let mut interner = cx.interner.borrow_mut(); @@ -1234,7 +1233,7 @@ pub fn mk_t(cx: ctxt, st: sty) -> t { flags: flags, }; - let sty_ptr = to_unsafe_ptr(&t.sty); + let sty_ptr = &t.sty as *sty; let key = intern_key { sty: sty_ptr, diff --git a/src/libstd/io/test.rs b/src/libstd/io/test.rs index 6ac73e7f61e71..04ecb479060c4 100644 --- a/src/libstd/io/test.rs +++ b/src/libstd/io/test.rs @@ -155,7 +155,7 @@ mod darwin_fd_limit { pub unsafe fn raise_fd_limit() { // The strategy here is to fetch the current resource limits, read the kern.maxfilesperproc // sysctl value, and bump the soft resource limit for maxfiles up to the sysctl value. - use ptr::{to_unsafe_ptr, to_mut_unsafe_ptr, mut_null}; + use ptr::mut_null; use mem::size_of_val; use os::last_os_error; @@ -163,9 +163,7 @@ mod darwin_fd_limit { let mut mib: [libc::c_int, ..2] = [CTL_KERN, KERN_MAXFILESPERPROC]; let mut maxfiles: libc::c_int = 0; let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t; - if sysctl(to_mut_unsafe_ptr(&mut mib[0]), 2, - to_mut_unsafe_ptr(&mut maxfiles) as *mut libc::c_void, - to_mut_unsafe_ptr(&mut size), + if sysctl(&mut mib[0], 2, &mut maxfiles as *mut libc::c_int as *mut libc::c_void, &mut size, mut_null(), 0) != 0 { let err = last_os_error(); error!("raise_fd_limit: error calling sysctl: {}", err); @@ -174,7 +172,7 @@ mod darwin_fd_limit { // Fetch the current resource limits let mut rlim = rlimit{rlim_cur: 0, rlim_max: 0}; - if getrlimit(RLIMIT_NOFILE, to_mut_unsafe_ptr(&mut rlim)) != 0 { + if getrlimit(RLIMIT_NOFILE, &mut rlim) != 0 { let err = last_os_error(); error!("raise_fd_limit: error calling getrlimit: {}", err); return; @@ -184,7 +182,7 @@ mod darwin_fd_limit { rlim.rlim_cur = ::cmp::min(maxfiles as rlim_t, rlim.rlim_max); // Set our newly-increased resource limit - if setrlimit(RLIMIT_NOFILE, to_unsafe_ptr(&rlim)) != 0 { + if setrlimit(RLIMIT_NOFILE, &rlim) != 0 { let err = last_os_error(); error!("raise_fd_limit: error calling setrlimit: {}", err); return; diff --git a/src/libstd/managed.rs b/src/libstd/managed.rs index 914cc25250c7f..63196cd4f162e 100644 --- a/src/libstd/managed.rs +++ b/src/libstd/managed.rs @@ -10,8 +10,6 @@ //! Operations on managed box types -use ptr::to_unsafe_ptr; - #[cfg(not(test))] use cmp::*; /// Returns the refcount of a shared box (as just before calling this) @@ -24,8 +22,7 @@ pub fn refcount(t: @T) -> uint { /// Determine if two shared boxes point to the same object #[inline] pub fn ptr_eq(a: @T, b: @T) -> bool { - let (a_ptr, b_ptr): (*T, *T) = (to_unsafe_ptr(&*a), to_unsafe_ptr(&*b)); - a_ptr == b_ptr + &*a as *T == &*b as *T } #[cfg(not(test))] diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index 2ba6f7d4fd64d..037984d9e7fc7 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -102,10 +102,10 @@ pub unsafe fn zero_memory(dst: *mut T, count: uint) { /** * Swap the values at two mutable locations of the same type, without - * deinitialising or copying either one. + * deinitialising either. They may overlap. */ #[inline] -pub unsafe fn swap_ptr(x: *mut T, y: *mut T) { +pub unsafe fn swap(x: *mut T, y: *mut T) { // Give ourselves some scratch space to work with let mut tmp: T = mem::uninit(); let t: *mut T = &mut tmp; @@ -122,19 +122,19 @@ pub unsafe fn swap_ptr(x: *mut T, y: *mut T) { /** * Replace the value at a mutable location with a new one, returning the old - * value, without deinitialising or copying either one. + * value, without deinitialising either. */ #[inline] -pub unsafe fn replace_ptr(dest: *mut T, mut src: T) -> T { +pub unsafe fn replace(dest: *mut T, mut src: T) -> T { mem::swap(cast::transmute(dest), &mut src); // cannot overlap src } /** - * Reads the value from `*src` and returns it. Does not copy `*src`. + * Reads the value from `*src` and returns it. */ #[inline(always)] -pub unsafe fn read_ptr(src: *T) -> T { +pub unsafe fn read(src: *T) -> T { let mut tmp: T = mem::uninit(); copy_nonoverlapping_memory(&mut tmp, src, 1); tmp @@ -145,9 +145,9 @@ pub unsafe fn read_ptr(src: *T) -> T { * This currently prevents destructors from executing. */ #[inline(always)] -pub unsafe fn read_and_zero_ptr(dest: *mut T) -> T { +pub unsafe fn read_and_zero(dest: *mut T) -> T { // Copy the data out from `dest`: - let tmp = read_ptr(&*dest); + let tmp = read(&*dest); // Now zero out `dest`: zero_memory(dest, 1); @@ -155,18 +155,6 @@ pub unsafe fn read_and_zero_ptr(dest: *mut T) -> T { tmp } -/// Transform a region pointer - &T - to an unsafe pointer - *T. -#[inline] -pub fn to_unsafe_ptr(thing: &T) -> *T { - thing as *T -} - -/// Transform a mutable region pointer - &mut T - to a mutable unsafe pointer - *mut T. -#[inline] -pub fn to_mut_unsafe_ptr(thing: &mut T) -> *mut T { - thing as *mut T -} - /** Given a **T (pointer to an array of pointers), iterate through each *T, up to the provided `len`, @@ -176,7 +164,7 @@ pub fn to_mut_unsafe_ptr(thing: &mut T) -> *mut T { */ pub unsafe fn array_each_with_len(arr: **T, len: uint, cb: |*T|) { debug!("array_each_with_len: before iterate"); - if arr as uint == 0 { + if arr.is_null() { fail!("ptr::array_each_with_len failure: arr input is null pointer"); } //let start_ptr = *arr; @@ -197,7 +185,7 @@ pub unsafe fn array_each_with_len(arr: **T, len: uint, cb: |*T|) { Dragons be here. */ pub unsafe fn array_each(arr: **T, cb: |*T|) { - if arr as uint == 0 { + if arr.is_null() { fail!("ptr::array_each_with_len failure: arr input is null pointer"); } let len = buf_len(arr); @@ -205,100 +193,74 @@ pub unsafe fn array_each(arr: **T, cb: |*T|) { array_each_with_len(arr, len, cb); } -#[allow(missing_doc)] +/// Extension methods for raw pointers. pub trait RawPtr { + /// Returns the null pointer. fn null() -> Self; + /// Returns true if the pointer is equal to the null pointer. fn is_null(&self) -> bool; - fn is_not_null(&self) -> bool; + /// Returns true if the pointer is not equal to the null pointer. + fn is_not_null(&self) -> bool { !self.is_null() } + /// Returns the value of this pointer (ie, the address it points to) fn to_uint(&self) -> uint; + /// Returns `None` if the pointer is null, or else returns the value wrapped + /// in `Some`. + /// + /// # Safety Notes + /// + /// While this method is useful for null-safety, it is important to note + /// that this is still an unsafe operation because the returned value could + /// be pointing to invalid memory. unsafe fn to_option(&self) -> Option<&T>; + /// Calculates the offset from a pointer. The offset *must* be in-bounds of + /// the object, or one-byte-past-the-end. unsafe fn offset(self, count: int) -> Self; } -/// Extension methods for immutable pointers impl RawPtr for *T { - /// Returns the null pointer. #[inline] fn null() -> *T { null() } - /// Returns true if the pointer is equal to the null pointer. #[inline] fn is_null(&self) -> bool { *self == RawPtr::null() } - /// Returns true if the pointer is not equal to the null pointer. #[inline] - fn is_not_null(&self) -> bool { *self != RawPtr::null() } + fn to_uint(&self) -> uint { *self as uint } - /// Returns the address of this pointer. #[inline] - fn to_uint(&self) -> uint { *self as uint } + unsafe fn offset(self, count: int) -> *T { intrinsics::offset(self, count) } - /// - /// Returns `None` if the pointer is null, or else returns the value wrapped - /// in `Some`. - /// - /// # Safety Notes - /// - /// While this method is useful for null-safety, it is important to note - /// that this is still an unsafe operation because the returned value could - /// be pointing to invalid memory. - /// #[inline] unsafe fn to_option(&self) -> Option<&T> { - if self.is_null() { None } else { + if self.is_null() { + None + } else { Some(cast::transmute(*self)) } } - - /// Calculates the offset from a pointer. The offset *must* be in-bounds of - /// the object, or one-byte-past-the-end. - #[inline] - unsafe fn offset(self, count: int) -> *T { intrinsics::offset(self, count) } } -/// Extension methods for mutable pointers impl RawPtr for *mut T { - /// Returns the null pointer. #[inline] fn null() -> *mut T { mut_null() } - /// Returns true if the pointer is equal to the null pointer. #[inline] fn is_null(&self) -> bool { *self == RawPtr::null() } - /// Returns true if the pointer is not equal to the null pointer. #[inline] - fn is_not_null(&self) -> bool { *self != RawPtr::null() } + fn to_uint(&self) -> uint { *self as uint } - /// Returns the address of this pointer. #[inline] - fn to_uint(&self) -> uint { *self as uint } + unsafe fn offset(self, count: int) -> *mut T { intrinsics::offset(self as *T, count) as *mut T } - /// - /// Returns `None` if the pointer is null, or else returns the value wrapped - /// in `Some`. - /// - /// # Safety Notes - /// - /// While this method is useful for null-safety, it is important to note - /// that this is still an unsafe operation because the returned value could - /// be pointing to invalid memory. - /// #[inline] unsafe fn to_option(&self) -> Option<&T> { - if self.is_null() { None } else { + if self.is_null() { + None + } else { Some(cast::transmute(*self)) } } - - /// Calculates the offset from a pointer. The offset *must* be in-bounds of - /// the object, or one-byte-past-the-end. An arithmetic overflow is also - /// undefined behaviour. - /// - /// This method should be preferred over `offset` when the guarantee can be - /// satisfied, to enable better optimization. - #[inline] - unsafe fn offset(self, count: int) -> *mut T { intrinsics::offset(self as *T, count) as *mut T } } // Equality for pointers diff --git a/src/libstd/rc.rs b/src/libstd/rc.rs index a1565bc85dede..ea3d5e0edac01 100644 --- a/src/libstd/rc.rs +++ b/src/libstd/rc.rs @@ -24,13 +24,13 @@ pointers, and then storing the parent pointers as `Weak` pointers. */ use cast::transmute; -use ops::Drop; -use cmp::{Eq, Ord}; use clone::{Clone, DeepClone}; +use cmp::{Eq, Ord}; use kinds::marker; -use rt::global_heap::exchange_free; -use ptr::read_ptr; +use ops::Drop; use option::{Option, Some, None}; +use ptr; +use rt::global_heap::exchange_free; struct RcBox { value: T, @@ -85,7 +85,7 @@ impl Drop for Rc { if self.ptr != 0 as *mut RcBox { (*self.ptr).strong -= 1; if (*self.ptr).strong == 0 { - read_ptr(self.borrow()); // destroy the contained object + ptr::read(self.borrow()); // destroy the contained object // remove the implicit "strong weak" pointer now // that we've destroyed the contents. diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs index 58c00177b9016..fb2053ddaf615 100644 --- a/src/libstd/repr.rs +++ b/src/libstd/repr.rs @@ -22,7 +22,6 @@ use container::Container; use io; use iter::Iterator; use option::{Some, None, Option}; -use ptr; use ptr::RawPtr; use reflect; use reflect::{MovePtr, align}; @@ -230,7 +229,7 @@ impl<'a> ReprVisitor<'a> { } pub fn write_unboxed_vec_repr(&mut self, _: uint, v: &raw::Vec<()>, inner: *TyDesc) -> bool { - self.write_vec_range(ptr::to_unsafe_ptr(&v.data), v.fill, inner) + self.write_vec_range(&v.data, v.fill, inner) } fn write_escaped_char(&mut self, ch: char, is_str: bool) -> bool { @@ -319,7 +318,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> { if_ok!(self, self.writer.write(['@' as u8])); self.write_mut_qualifier(mtbl); self.get::<&raw::Box<()>>(|this, b| { - let p = ptr::to_unsafe_ptr(&b.data) as *u8; + let p = &b.data as *() as *u8; this.visit_ptr_inner(p, inner) }) } @@ -387,7 +386,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> { _: uint, inner: *TyDesc) -> bool { let assumed_size = if sz == 0 { n } else { sz }; self.get::<()>(|this, b| { - this.write_vec_range(ptr::to_unsafe_ptr(b), assumed_size, inner) + this.write_vec_range(b, assumed_size, inner) }) } @@ -606,7 +605,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> { pub fn write_repr(writer: &mut io::Writer, object: &T) -> io::IoResult<()> { unsafe { - let ptr = ptr::to_unsafe_ptr(object) as *u8; + let ptr = object as *T as *u8; let tydesc = get_tydesc::(); let u = ReprVisitor(ptr, writer); let mut v = reflect::MovePtrAdaptor(u); diff --git a/src/libstd/sync/deque.rs b/src/libstd/sync/deque.rs index 7feff127d691a..7ce760040e65e 100644 --- a/src/libstd/sync/deque.rs +++ b/src/libstd/sync/deque.rs @@ -363,7 +363,7 @@ impl Buffer { // very unsafe method which the caller needs to treat specially in case a // race is lost. unsafe fn get(&self, i: int) -> T { - ptr::read_ptr(self.storage.offset(i & self.mask())) + ptr::read(self.storage.offset(i & self.mask())) } // Unsafe because this unsafely overwrites possibly uninitialized or diff --git a/src/libstd/task.rs b/src/libstd/task.rs index 5d8c4a87b3935..1cdf5998e8b20 100644 --- a/src/libstd/task.rs +++ b/src/libstd/task.rs @@ -509,10 +509,10 @@ fn avoid_copying_the_body(spawnfn: |v: proc()|) { let (p, ch) = Chan::::new(); let x = ~1; - let x_in_parent = ptr::to_unsafe_ptr(&*x) as uint; + let x_in_parent = (&*x) as *int as uint; spawnfn(proc() { - let x_in_child = ptr::to_unsafe_ptr(&*x) as uint; + let x_in_child = (&*x) as *int as uint; ch.send(x_in_child); }); diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index cfe2ad5a08af4..f8f9f82b657a5 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -102,6 +102,7 @@ There are a number of free functions that create or take vectors, for example: #[warn(non_camel_case_types)]; use cast; +use cast::transmute; use ops::Drop; use clone::{Clone, DeepClone}; use container::{Container, Mutable}; @@ -112,7 +113,6 @@ use default::Default; use iter::*; use num::{Integer, CheckedAdd, Saturating, checked_next_power_of_two}; use option::{None, Option, Some}; -use ptr::to_unsafe_ptr; use ptr; use ptr::RawPtr; use rt::global_heap::{malloc_raw, realloc_raw, exchange_free}; @@ -188,7 +188,7 @@ pub fn with_capacity(capacity: uint) -> ~[T] { let ptr = malloc_raw(size) as *mut Vec<()>; (*ptr).alloc = alloc; (*ptr).fill = 0; - cast::transmute(ptr) + transmute(ptr) } } @@ -216,7 +216,7 @@ pub fn build(size: Option, builder: |push: |v: A||) -> ~[A] { */ pub fn ref_slice<'a, A>(s: &'a A) -> &'a [A] { unsafe { - cast::transmute(Slice { data: s, len: 1 }) + transmute(Slice { data: s, len: 1 }) } } @@ -225,8 +225,8 @@ pub fn ref_slice<'a, A>(s: &'a A) -> &'a [A] { */ pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] { unsafe { - let ptr: *A = cast::transmute(s); - cast::transmute(Slice { data: ptr, len: 1 }) + let ptr: *A = transmute(s); + transmute(Slice { data: ptr, len: 1 }) } } @@ -991,7 +991,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { assert!(start <= end); assert!(end <= self.len()); unsafe { - cast::transmute(Slice { + transmute(Slice { data: self.as_ptr().offset(start as int), len: (end - start) }) @@ -1109,7 +1109,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { #[inline] unsafe fn unsafe_ref(self, index: uint) -> &'a T { - cast::transmute(self.repr().data.offset(index as int)) + transmute(self.repr().data.offset(index as int)) } #[inline] @@ -1144,7 +1144,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { fn shift_ref(&mut self) -> Option<&'a T> { if self.len() == 0 { return None; } unsafe { - let s: &mut Slice = cast::transmute(self); + let s: &mut Slice = transmute(self); Some(&*raw::shift_ptr(s)) } } @@ -1152,7 +1152,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] { fn pop_ref(&mut self) -> Option<&'a T> { if self.len() == 0 { return None; } unsafe { - let s: &mut Slice = cast::transmute(self); + let s: &mut Slice = transmute(self); Some(&*raw::pop_ptr(s)) } } @@ -1417,8 +1417,8 @@ impl OwnedVector for ~[T] { #[inline] fn move_iter(self) -> MoveItems { unsafe { - let iter = cast::transmute(self.iter()); - let ptr = cast::transmute(self); + let iter = transmute(self.iter()); + let ptr = transmute(self); MoveItems { allocation: ptr, iter: iter } } } @@ -1432,7 +1432,7 @@ impl OwnedVector for ~[T] { // Only make the (slow) call into the runtime if we have to if self.capacity() < n { unsafe { - let ptr: *mut *mut Vec<()> = cast::transmute(self); + let ptr: *mut *mut Vec<()> = transmute(self); let alloc = n * mem::nonzero_size_of::(); let size = alloc + mem::size_of::>(); if alloc / mem::nonzero_size_of::() != n || size < alloc { @@ -1463,14 +1463,14 @@ impl OwnedVector for ~[T] { #[inline] fn capacity(&self) -> uint { unsafe { - let repr: **Vec<()> = cast::transmute(self); + let repr: **Vec<()> = transmute(self); (**repr).alloc / mem::nonzero_size_of::() } } fn shrink_to_fit(&mut self) { unsafe { - let ptr: *mut *mut Vec<()> = cast::transmute(self); + let ptr: *mut *mut Vec<()> = transmute(self); let alloc = (**ptr).fill; let size = alloc + mem::size_of::>(); *ptr = realloc_raw(*ptr as *mut u8, size) as *mut Vec<()>; @@ -1481,7 +1481,7 @@ impl OwnedVector for ~[T] { #[inline] fn push(&mut self, t: T) { unsafe { - let repr: **Vec<()> = cast::transmute(&mut *self); + let repr: **Vec<()> = transmute(&mut *self); let fill = (**repr).fill; if (**repr).alloc <= fill { self.reserve_additional(1); @@ -1493,10 +1493,10 @@ impl OwnedVector for ~[T] { // This doesn't bother to make sure we have space. #[inline] // really pretty please unsafe fn push_fast(this: &mut ~[T], t: T) { - let repr: **mut Vec = cast::transmute(this); + let repr: **mut Vec = transmute(this); let fill = (**repr).fill; (**repr).fill += mem::nonzero_size_of::(); - let p = to_unsafe_ptr(&((**repr).data)); + let p = &((**repr).data) as *u8; let p = p.offset(fill as int) as *mut T; mem::move_val_init(&mut(*p), t); } @@ -1521,10 +1521,10 @@ impl OwnedVector for ~[T] { match self.len() { 0 => None, ln => { - let valptr = ptr::to_mut_unsafe_ptr(&mut self[ln - 1u]); + let valptr = &mut self[ln - 1u] as *mut T; unsafe { self.set_len(ln - 1u); - Some(ptr::read_ptr(&*valptr)) + Some(ptr::read(&*valptr)) } } } @@ -1568,7 +1568,7 @@ impl OwnedVector for ~[T] { let ptr = self.as_mut_ptr().offset(i as int); // copy it out, unsafely having a copy of the value on // the stack and in the vector at the same time. - let ret = Some(ptr::read_ptr(ptr as *T)); + let ret = Some(ptr::read(ptr as *T)); // Shift everything down to fill in that spot. ptr::copy_memory(ptr, &*ptr.offset(1), len - i - 1); @@ -1598,7 +1598,7 @@ impl OwnedVector for ~[T] { let p = self.as_mut_ptr(); // This loop is optimized out for non-drop types. for i in range(newlen, oldlen) { - ptr::read_and_zero_ptr(p.offset(i as int)); + ptr::read_and_zero(p.offset(i as int)); } } unsafe { self.set_len(newlen); } @@ -1648,7 +1648,7 @@ impl OwnedVector for ~[T] { #[inline] unsafe fn set_len(&mut self, new_len: uint) { - let repr: **mut Vec<()> = cast::transmute(self); + let repr: **mut Vec<()> = transmute(self); (**repr).fill = new_len * mem::nonzero_size_of::(); } } @@ -1844,7 +1844,7 @@ fn insertion_sort(v: &mut [T], compare: |&T, &T| -> Ordering) { // `.offset(j)` is always in bounds. if i != j { - let tmp = ptr::read_ptr(read_ptr); + let tmp = ptr::read(read_ptr); ptr::copy_memory(buf_v.offset(j + 1), &*buf_v.offset(j), (i - j) as uint); @@ -2269,7 +2269,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { assert!(start <= end); assert!(end <= self.len()); unsafe { - cast::transmute(Slice { + transmute(Slice { data: self.as_mut_ptr().offset(start as int) as *T, len: (end - start) }) @@ -2338,7 +2338,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { fn mut_shift_ref(&mut self) -> Option<&'a mut T> { if self.len() == 0 { return None; } unsafe { - let s: &mut Slice = cast::transmute(self); + let s: &mut Slice = transmute(self); Some(cast::transmute_mut(&*raw::shift_ptr(s))) } } @@ -2346,7 +2346,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { fn mut_pop_ref(&mut self) -> Option<&'a mut T> { if self.len() == 0 { return None; } unsafe { - let s: &mut Slice = cast::transmute(self); + let s: &mut Slice = transmute(self); Some(cast::transmute_mut(&*raw::pop_ptr(s))) } } @@ -2357,7 +2357,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { // them to their raw pointers to do the swap let pa: *mut T = &mut self[a]; let pb: *mut T = &mut self[b]; - ptr::swap_ptr(pa, pb); + ptr::swap(pa, pb); } } @@ -2385,7 +2385,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] { #[inline] unsafe fn unsafe_mut_ref(self, index: uint) -> &'a mut T { - cast::transmute((self.repr().data as *mut T).offset(index as int)) + transmute((self.repr().data as *mut T).offset(index as int)) } #[inline] @@ -2484,7 +2484,7 @@ pub unsafe fn from_buf(ptr: *T, elts: uint) -> ~[T] { /// Unsafe operations pub mod raw { - use cast; + use cast::transmute; use ptr; use ptr::RawPtr; use vec::{with_capacity, MutableVector, OwnedVector}; @@ -2497,7 +2497,7 @@ pub mod raw { #[inline] pub unsafe fn buf_as_slice(p: *T, len: uint, f: |v: &[T]| -> U) -> U { - f(cast::transmute(Slice { + f(transmute(Slice { data: p, len: len })) @@ -2514,7 +2514,7 @@ pub mod raw { len: uint, f: |v: &mut [T]| -> U) -> U { - f(cast::transmute(Slice { + f(transmute(Slice { data: p as *T, len: len })) @@ -2698,12 +2698,12 @@ macro_rules! iterator { // purposefully don't use 'ptr.offset' because for // vectors with 0-size elements this would return the // same pointer. - cast::transmute(self.ptr as uint + 1) + transmute(self.ptr as uint + 1) } else { self.ptr.offset(1) }; - Some(cast::transmute(old)) + Some(transmute(old)) } } } @@ -2726,11 +2726,11 @@ macro_rules! iterator { } else { self.end = if mem::size_of::() == 0 { // See above for why 'ptr.offset' isn't used - cast::transmute(self.end as uint - 1) + transmute(self.end as uint - 1) } else { self.end.offset(-1) }; - Some(cast::transmute(self.end)) + Some(transmute(self.end)) } } } @@ -2749,7 +2749,7 @@ impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> { fn idx(&self, index: uint) -> Option<&'a T> { unsafe { if index < self.indexable() { - cast::transmute(self.ptr.offset(index as int)) + transmute(self.ptr.offset(index as int)) } else { None } @@ -2895,7 +2895,7 @@ impl Iterator for MoveItems { #[inline] fn next(&mut self) -> Option { unsafe { - self.iter.next().map(|x| ptr::read_ptr(x)) + self.iter.next().map(|x| ptr::read(x)) } } @@ -2909,7 +2909,7 @@ impl DoubleEndedIterator for MoveItems { #[inline] fn next_back(&mut self) -> Option { unsafe { - self.iter.next_back().map(|x| ptr::read_ptr(x)) + self.iter.next_back().map(|x| ptr::read(x)) } } } diff --git a/src/libstd/vec_ng.rs b/src/libstd/vec_ng.rs index 25ba45021b3fa..a450aab127bc4 100644 --- a/src/libstd/vec_ng.rs +++ b/src/libstd/vec_ng.rs @@ -22,7 +22,7 @@ use cast::{forget, transmute}; use rt::global_heap::{malloc_raw, realloc_raw}; use vec::{ImmutableVector, Items, MutableVector}; use unstable::raw::Slice; -use ptr::read_ptr; +use ptr; use ptr::RawPtr; use libc::{free, c_void}; @@ -117,7 +117,7 @@ impl Vec { } else { unsafe { self.len -= 1; - Some(read_ptr(self.as_slice().unsafe_ref(self.len()))) + Some(ptr::read(self.as_slice().unsafe_ref(self.len()))) } } } @@ -147,7 +147,7 @@ impl Vec { let mut i = len; // drop any extra elements while i < self.len { - read_ptr(self.as_slice().unsafe_ref(i)); + ptr::read(self.as_slice().unsafe_ref(i)); i += 1; } } @@ -188,7 +188,7 @@ impl Drop for Vec { fn drop(&mut self) { unsafe { for x in self.as_mut_slice().iter() { - read_ptr(x); + ptr::read(x); } free(self.ptr as *mut c_void) } @@ -204,7 +204,7 @@ impl Iterator for MoveItems { #[inline] fn next(&mut self) -> Option { unsafe { - self.iter.next().map(|x| read_ptr(x)) + self.iter.next().map(|x| ptr::read(x)) } } @@ -218,7 +218,7 @@ impl DoubleEndedIterator for MoveItems { #[inline] fn next_back(&mut self) -> Option { unsafe { - self.iter.next_back().map(|x| read_ptr(x)) + self.iter.next_back().map(|x| ptr::read(x)) } } } diff --git a/src/test/compile-fail/issue-3096-2.rs b/src/test/compile-fail/issue-3096-2.rs index 5f3af86545462..799f5b4d5320c 100644 --- a/src/test/compile-fail/issue-3096-2.rs +++ b/src/test/compile-fail/issue-3096-2.rs @@ -8,11 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::ptr; - enum bottom { } fn main() { - let x = ptr::to_unsafe_ptr(&()) as *bottom; + let x = &() as *() as *bottom; match x { } //~ ERROR non-exhaustive patterns } diff --git a/src/test/run-pass/borrowck-borrow-from-expr-block.rs b/src/test/run-pass/borrowck-borrow-from-expr-block.rs index fe1be6d06db83..4aedb4e96cfa3 100644 --- a/src/test/run-pass/borrowck-borrow-from-expr-block.rs +++ b/src/test/run-pass/borrowck-borrow-from-expr-block.rs @@ -10,15 +10,13 @@ #[feature(managed_boxes)]; -use std::ptr; - fn borrow(x: &int, f: |x: &int|) { f(x) } fn test1(x: @~int) { borrow(&*(*x).clone(), |p| { - let x_a = ptr::to_unsafe_ptr(&**x); + let x_a = &**x as *int; assert!((x_a as uint) != (p as *int as uint)); assert_eq!(unsafe{*x_a}, *p); }) diff --git a/src/test/run-pass/borrowck-preserve-box-in-field.rs b/src/test/run-pass/borrowck-preserve-box-in-field.rs index e789727c4fe29..5cdda81c43604 100644 --- a/src/test/run-pass/borrowck-preserve-box-in-field.rs +++ b/src/test/run-pass/borrowck-preserve-box-in-field.rs @@ -14,8 +14,6 @@ #[feature(managed_boxes)]; -use std::ptr; - fn borrow(x: &int, f: |x: &int|) { let before = *x; f(x); @@ -29,12 +27,11 @@ pub fn main() { let mut x = @F {f: ~3}; borrow(x.f, |b_x| { assert_eq!(*b_x, 3); - assert_eq!(ptr::to_unsafe_ptr(&(*x.f)), ptr::to_unsafe_ptr(&(*b_x))); + assert_eq!(&(*x.f) as *int, &(*b_x) as *int); x = @F {f: ~4}; - info!("ptr::to_unsafe_ptr(*b_x) = {:x}", - ptr::to_unsafe_ptr(&(*b_x)) as uint); + info!("&*b_x = {:p}", &(*b_x)); assert_eq!(*b_x, 3); - assert!(ptr::to_unsafe_ptr(&(*x.f)) != ptr::to_unsafe_ptr(&(*b_x))); + assert!(&(*x.f) as *int != &(*b_x) as *int); }) } diff --git a/src/test/run-pass/borrowck-preserve-box-in-uniq.rs b/src/test/run-pass/borrowck-preserve-box-in-uniq.rs index 8ac654ffbddba..3050d6fa01130 100644 --- a/src/test/run-pass/borrowck-preserve-box-in-uniq.rs +++ b/src/test/run-pass/borrowck-preserve-box-in-uniq.rs @@ -14,8 +14,6 @@ #[feature(managed_boxes)]; -use std::ptr; - fn borrow(x: &int, f: |x: &int|) { let before = *x; f(x); @@ -29,12 +27,11 @@ pub fn main() { let mut x = ~@F{f: ~3}; borrow(x.f, |b_x| { assert_eq!(*b_x, 3); - assert_eq!(ptr::to_unsafe_ptr(&(*x.f)), ptr::to_unsafe_ptr(&(*b_x))); + assert_eq!(&(*x.f) as *int, &(*b_x) as *int); *x = @F{f: ~4}; - info!("ptr::to_unsafe_ptr(*b_x) = {:x}", - ptr::to_unsafe_ptr(&(*b_x)) as uint); + info!("&*b_x = {:p}", &(*b_x)); assert_eq!(*b_x, 3); - assert!(ptr::to_unsafe_ptr(&(*x.f)) != ptr::to_unsafe_ptr(&(*b_x))); + assert!(&(*x.f) as *int != &(*b_x) as *int); }) } diff --git a/src/test/run-pass/borrowck-preserve-box.rs b/src/test/run-pass/borrowck-preserve-box.rs index 9ce8daa966cf6..76dfbffc09c06 100644 --- a/src/test/run-pass/borrowck-preserve-box.rs +++ b/src/test/run-pass/borrowck-preserve-box.rs @@ -14,8 +14,6 @@ #[feature(managed_boxes)]; -use std::ptr; - fn borrow(x: &int, f: |x: &int|) { let before = *x; f(x); @@ -27,12 +25,11 @@ pub fn main() { let mut x = @3; borrow(x, |b_x| { assert_eq!(*b_x, 3); - assert_eq!(ptr::to_unsafe_ptr(&(*x)), ptr::to_unsafe_ptr(&(*b_x))); + assert_eq!(&(*x) as *int, &(*b_x) as *int); x = @22; - info!("ptr::to_unsafe_ptr(*b_x) = {:x}", - ptr::to_unsafe_ptr(&(*b_x)) as uint); + info!("&*b_x = {:p}", &(*b_x)); assert_eq!(*b_x, 3); - assert!(ptr::to_unsafe_ptr(&(*x)) != ptr::to_unsafe_ptr(&(*b_x))); + assert!(&(*x) as *int != &(*b_x) as *int); }) } diff --git a/src/test/run-pass/borrowck-preserve-expl-deref.rs b/src/test/run-pass/borrowck-preserve-expl-deref.rs index 065ef1dd42379..00e59f5132db7 100644 --- a/src/test/run-pass/borrowck-preserve-expl-deref.rs +++ b/src/test/run-pass/borrowck-preserve-expl-deref.rs @@ -14,8 +14,6 @@ #[feature(managed_boxes)]; -use std::ptr; - fn borrow(x: &int, f: |x: &int|) { let before = *x; f(x); @@ -29,12 +27,11 @@ pub fn main() { let mut x = @F {f: ~3}; borrow((*x).f, |b_x| { assert_eq!(*b_x, 3); - assert_eq!(ptr::to_unsafe_ptr(&(*x.f)), ptr::to_unsafe_ptr(&(*b_x))); + assert_eq!(&(*x.f) as *int, &(*b_x) as *int); x = @F {f: ~4}; - info!("ptr::to_unsafe_ptr(*b_x) = {:x}", - ptr::to_unsafe_ptr(&(*b_x)) as uint); + info!("&*b_x = {:p}", &(*b_x)); assert_eq!(*b_x, 3); - assert!(ptr::to_unsafe_ptr(&(*x.f)) != ptr::to_unsafe_ptr(&(*b_x))); + assert!(&(*x.f) as *int != &(*b_x) as *int); }) } diff --git a/src/test/run-pass/cap-clause-move.rs b/src/test/run-pass/cap-clause-move.rs deleted file mode 100644 index 1fa78628d8ae2..0000000000000 --- a/src/test/run-pass/cap-clause-move.rs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use std::ptr; - -pub fn main() { - let x = ~3; - let y = ptr::to_unsafe_ptr(&(*x)) as uint; - let snd_move: proc() -> uint = proc() ptr::to_unsafe_ptr(&(*x)) as uint; - assert_eq!(snd_move(), y); - - let x = ~4; - let y = ptr::to_unsafe_ptr(&(*x)) as uint; - let lam_move: proc() -> uint = proc() ptr::to_unsafe_ptr(&(*x)) as uint; - assert_eq!(lam_move(), y); -} diff --git a/src/test/run-pass/const-region-ptrs-noncopy.rs b/src/test/run-pass/const-region-ptrs-noncopy.rs index 6dce262dd9a88..87363c0e55e86 100644 --- a/src/test/run-pass/const-region-ptrs-noncopy.rs +++ b/src/test/run-pass/const-region-ptrs-noncopy.rs @@ -8,13 +8,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::ptr; - type Big = [u64, ..8]; struct Pair<'a> { a: int, b: &'a Big } static x: &'static Big = &([13, 14, 10, 13, 11, 14, 14, 15]); static y: &'static Pair<'static> = &Pair {a: 15, b: x}; pub fn main() { - assert_eq!(ptr::to_unsafe_ptr(x), ptr::to_unsafe_ptr(y.b)); + assert_eq!(x as *Big, y.b as *Big); } diff --git a/src/test/run-pass/enum-alignment.rs b/src/test/run-pass/enum-alignment.rs index 3c8dd33ae9c35..5763c61379865 100644 --- a/src/test/run-pass/enum-alignment.rs +++ b/src/test/run-pass/enum-alignment.rs @@ -9,12 +9,10 @@ // except according to those terms. use std::cast; -use std::ptr; use std::mem; fn addr_of(ptr: &T) -> uint { - let ptr = ptr::to_unsafe_ptr(ptr); - ptr as uint + ptr as *T as uint } fn is_aligned(ptr: &T) -> bool { diff --git a/src/test/run-pass/stable-addr-of.rs b/src/test/run-pass/stable-addr-of.rs index dc68777e033b0..083d2e167a075 100644 --- a/src/test/run-pass/stable-addr-of.rs +++ b/src/test/run-pass/stable-addr-of.rs @@ -10,9 +10,7 @@ // Issue #2040 -use std::ptr; - pub fn main() { let foo = 1; - assert_eq!(ptr::to_unsafe_ptr(&foo), ptr::to_unsafe_ptr(&foo)); + assert_eq!(&foo as *int, &foo as *int); } diff --git a/src/test/run-pass/swap-overlapping.rs b/src/test/run-pass/swap-overlapping.rs index 42f3089a87a19..d9cf585ad6c0a 100644 --- a/src/test/run-pass/swap-overlapping.rs +++ b/src/test/run-pass/swap-overlapping.rs @@ -25,7 +25,7 @@ pub fn main() { fn do_swap(test: &mut TestDescAndFn) { unsafe { - ptr::swap_ptr(test, test); + ptr::swap(test, test); } } diff --git a/src/test/run-pass/task-spawn-move-and-copy.rs b/src/test/run-pass/task-spawn-move-and-copy.rs index 1ed0c23fd7a4c..944c498ede37f 100644 --- a/src/test/run-pass/task-spawn-move-and-copy.rs +++ b/src/test/run-pass/task-spawn-move-and-copy.rs @@ -8,17 +8,16 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::ptr; use std::task; pub fn main() { let (p, ch) = Chan::::new(); let x = ~1; - let x_in_parent = ptr::to_unsafe_ptr(&(*x)) as uint; + let x_in_parent = &(*x) as *int as uint; task::spawn(proc() { - let x_in_child = ptr::to_unsafe_ptr(&(*x)) as uint; + let x_in_child = &(*x) as *int as uint; ch.send(x_in_child); }); diff --git a/src/test/run-pass/uniq-cc-generic.rs b/src/test/run-pass/uniq-cc-generic.rs index 53836a6e17dfb..9efa6c1b9646a 100644 --- a/src/test/run-pass/uniq-cc-generic.rs +++ b/src/test/run-pass/uniq-cc-generic.rs @@ -11,7 +11,6 @@ #[feature(managed_boxes)]; use std::cell::RefCell; -use std::ptr; enum maybe_pointy { none, @@ -24,7 +23,7 @@ struct Pointy { } fn make_uniq_closure(a: A) -> proc() -> uint { - let result: proc() -> uint = proc() ptr::to_unsafe_ptr(&a) as uint; + let result: proc() -> uint = proc() &a as *A as uint; result } From 254c155fca415359d998e79e9989b4219136ccdc Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Sat, 15 Feb 2014 00:10:31 -0500 Subject: [PATCH 2/2] impl fmt::Pointer for &T and &mut T --- src/libstd/fmt/mod.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index c8b5d3b2d5a48..cfcb467d7aa64 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -1200,7 +1200,17 @@ impl Pointer for *T { } impl Pointer for *mut T { fn fmt(&self, f: &mut Formatter) -> Result { - secret_pointer(&(*self as *T), f) + secret_pointer::<*T>(&(*self as *T), f) + } +} +impl<'a, T> Pointer for &'a T { + fn fmt(&self, f: &mut Formatter) -> Result { + secret_pointer::<*T>(&(&**self as *T), f) + } +} +impl<'a, T> Pointer for &'a mut T { + fn fmt(&self, f: &mut Formatter) -> Result { + secret_pointer::<*T>(&(&**self as *T), f) } }