提交 7762baa8 编写于 作者: B bors

auto merge of #12282 : cmr/rust/cleanup-ptr, r=huonw

......@@ -229,7 +229,7 @@ impl<T: Send> Drop for Unique<T> {
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.
......
......@@ -83,7 +83,7 @@ fn none() -> Rawlink<T> {
/// Like Option::Some for Rawlink
fn some(n: &mut T) -> Rawlink<T> {
Rawlink{p: ptr::to_mut_unsafe_ptr(n)}
Rawlink{p: n}
}
/// Convert the `Rawlink` into an Option value
......
......@@ -33,7 +33,6 @@
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 @@ fn sflags(substs: &substs) -> uint {
flags: flags,
};
let sty_ptr = to_unsafe_ptr(&t.sty);
let sty_ptr = &t.sty as *sty;
let key = intern_key {
sty: sty_ptr,
......
......@@ -1200,7 +1200,17 @@ fn fmt(&self, f: &mut Formatter) -> Result {
}
impl<T> 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)
}
}
......
......@@ -155,7 +155,7 @@ fn sysctl(name: *mut libc::c_int, namelen: libc::c_uint,
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 @@ pub unsafe fn raise_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 @@ pub unsafe fn raise_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 @@ pub unsafe fn raise_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;
......
......@@ -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: @T) -> uint {
/// Determine if two shared boxes point to the same object
#[inline]
pub fn ptr_eq<T>(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))]
......
......@@ -102,10 +102,10 @@ pub unsafe fn zero_memory<T>(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<T>(x: *mut T, y: *mut T) {
pub unsafe fn swap<T>(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<T>(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<T>(dest: *mut T, mut src: T) -> T {
pub unsafe fn replace<T>(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<T>(src: *T) -> T {
pub unsafe fn read<T>(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<T>(src: *T) -> T {
* This currently prevents destructors from executing.
*/
#[inline(always)]
pub unsafe fn read_and_zero_ptr<T>(dest: *mut T) -> T {
pub unsafe fn read_and_zero<T>(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<T>(dest: *mut T) -> T {
tmp
}
/// Transform a region pointer - &T - to an unsafe pointer - *T.
#[inline]
pub fn to_unsafe_ptr<T>(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<T>(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<T>(thing: &mut T) -> *mut T {
*/
pub unsafe fn array_each_with_len<T>(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<T>(arr: **T, len: uint, cb: |*T|) {
Dragons be here.
*/
pub unsafe fn array_each<T>(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<T>(arr: **T, cb: |*T|) {
array_each_with_len(arr, len, cb);
}
#[allow(missing_doc)]
/// Extension methods for raw pointers.
pub trait RawPtr<T> {
/// 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<T> RawPtr<T> 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<T> RawPtr<T> 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
......
......@@ -24,13 +24,13 @@
*/
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<T> {
value: T,
......@@ -85,7 +85,7 @@ fn drop(&mut self) {
if self.ptr != 0 as *mut RcBox<T> {
(*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.
......
......@@ -22,7 +22,6 @@
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 @@ pub fn write_vec_range(&mut self, ptr: *(), len: uint, inner: *TyDesc) -> bool {
}
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 @@ fn visit_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
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 @@ fn visit_evec_fixed(&mut self, n: uint, sz: uint, _align: uint,
_: 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 @@ fn visit_self(&mut self) -> bool { true }
pub fn write_repr<T>(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::<T>();
let u = ReprVisitor(ptr, writer);
let mut v = reflect::MovePtrAdaptor(u);
......
......@@ -363,7 +363,7 @@ fn mask(&self) -> int { (1 << self.log_size) - 1 }
// 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
......
......@@ -509,10 +509,10 @@ fn avoid_copying_the_body(spawnfn: |v: proc()|) {
let (p, ch) = Chan::<uint>::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);
});
......
......@@ -102,6 +102,7 @@
#[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 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<T>(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<A>(size: Option<uint>, 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 @@ fn slice(&self, start: uint, end: uint) -> &'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 @@ fn flat_map<U>(&self, f: |t: &T| -> ~[U]) -> ~[U] {
#[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 @@ fn map<U>(&self, f: |t: &T| -> U) -> ~[U] {
fn shift_ref(&mut self) -> Option<&'a T> {
if self.len() == 0 { return None; }
unsafe {
let s: &mut Slice<T> = cast::transmute(self);
let s: &mut Slice<T> = transmute(self);
Some(&*raw::shift_ptr(s))
}
}
......@@ -1152,7 +1152,7 @@ fn shift_ref(&mut self) -> Option<&'a T> {
fn pop_ref(&mut self) -> Option<&'a T> {
if self.len() == 0 { return None; }
unsafe {
let s: &mut Slice<T> = cast::transmute(self);
let s: &mut Slice<T> = transmute(self);
Some(&*raw::pop_ptr(s))
}
}
......@@ -1417,8 +1417,8 @@ impl<T> OwnedVector<T> for ~[T] {
#[inline]
fn move_iter(self) -> MoveItems<T> {
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 @@ fn reserve_exact(&mut self, n: uint) {
// 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::<T>();
let size = alloc + mem::size_of::<Vec<()>>();
if alloc / mem::nonzero_size_of::<T>() != n || size < alloc {
......@@ -1463,14 +1463,14 @@ fn reserve_additional(&mut self, n: uint) {
#[inline]
fn capacity(&self) -> uint {
unsafe {
let repr: **Vec<()> = cast::transmute(self);
let repr: **Vec<()> = transmute(self);
(**repr).alloc / mem::nonzero_size_of::<T>()
}
}
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::<Vec<()>>();
*ptr = realloc_raw(*ptr as *mut u8, size) as *mut Vec<()>;
......@@ -1481,7 +1481,7 @@ fn shrink_to_fit(&mut self) {
#[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 @@ fn push(&mut self, t: T) {
// This doesn't bother to make sure we have space.
#[inline] // really pretty please
unsafe fn push_fast<T>(this: &mut ~[T], t: T) {
let repr: **mut Vec<u8> = cast::transmute(this);
let repr: **mut Vec<u8> = transmute(this);
let fill = (**repr).fill;
(**repr).fill += mem::nonzero_size_of::<T>();
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 @@ fn pop(&mut self) -> Option<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 @@ fn remove(&mut self, i: uint) -> Option<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 @@ fn truncate(&mut self, newlen: uint) {
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 @@ fn grow_fn(&mut self, n: uint, op: |uint| -> 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::<T>();
}
}
......@@ -1844,7 +1844,7 @@ fn insertion_sort<T>(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 @@ fn mut_slice(self, start: uint, end: uint) -> &'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 @@ fn mut_chunks(self, chunk_size: uint) -> MutChunks<'a, T> {
fn mut_shift_ref(&mut self) -> Option<&'a mut T> {
if self.len() == 0 { return None; }
unsafe {
let s: &mut Slice<T> = cast::transmute(self);
let s: &mut Slice<T> = transmute(self);
Some(cast::transmute_mut(&*raw::shift_ptr(s)))
}
}
......@@ -2346,7 +2346,7 @@ fn mut_shift_ref(&mut self) -> Option<&'a mut T> {
fn mut_pop_ref(&mut self) -> Option<&'a mut T> {
if self.len() == 0 { return None; }
unsafe {
let s: &mut Slice<T> = cast::transmute(self);
let s: &mut Slice<T> = transmute(self);
Some(cast::transmute_mut(&*raw::pop_ptr(s)))
}
}
......@@ -2357,7 +2357,7 @@ fn swap(self, a: uint, b: uint) {
// 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 @@ fn move_from(self, mut src: ~[T], start: uint, end: uint) -> uint {
#[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<T>(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<T,U>(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 unsafe fn mut_buf_as_slice<T,
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 @@ fn next(&mut self) -> Option<$elem> {
// 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 @@ fn next_back(&mut self) -> Option<$elem> {
} else {
self.end = if mem::size_of::<T>() == 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 @@ fn indexable(&self) -> uint {
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<T> Iterator<T> for MoveItems<T> {
#[inline]
fn next(&mut self) -> Option<T> {
unsafe {
self.iter.next().map(|x| ptr::read_ptr(x))
self.iter.next().map(|x| ptr::read(x))
}
}
......@@ -2909,7 +2909,7 @@ impl<T> DoubleEndedIterator<T> for MoveItems<T> {
#[inline]
fn next_back(&mut self) -> Option<T> {
unsafe {
self.iter.next_back().map(|x| ptr::read_ptr(x))
self.iter.next_back().map(|x| ptr::read(x))
}
}
}
......
......@@ -22,7 +22,7 @@
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 @@ pub fn pop(&mut self) -> Option<T> {
} 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 @@ pub fn truncate(&mut self, len: uint) {
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<T> Drop for Vec<T> {
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<T> Iterator<T> for MoveItems<T> {
#[inline]
fn next(&mut self) -> Option<T> {
unsafe {
self.iter.next().map(|x| read_ptr(x))
self.iter.next().map(|x| ptr::read(x))
}
}
......@@ -218,7 +218,7 @@ impl<T> DoubleEndedIterator<T> for MoveItems<T> {
#[inline]
fn next_back(&mut self) -> Option<T> {
unsafe {
self.iter.next_back().map(|x| read_ptr(x))
self.iter.next_back().map(|x| ptr::read(x))
}
}
}
......
......@@ -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
}
......@@ -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);
})
......
......@@ -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);
})
}
......@@ -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);
})
}
......@@ -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);
})
}
......@@ -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);
})
}
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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);
}
......@@ -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);
}
......@@ -9,12 +9,10 @@
// except according to those terms.
use std::cast;
use std::ptr;
use std::mem;
fn addr_of<T>(ptr: &T) -> uint {
let ptr = ptr::to_unsafe_ptr(ptr);
ptr as uint
ptr as *T as uint
}
fn is_aligned<T>(ptr: &T) -> bool {
......
......@@ -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);
}
......@@ -25,7 +25,7 @@ pub fn main() {
fn do_swap(test: &mut TestDescAndFn) {
unsafe {
ptr::swap_ptr(test, test);
ptr::swap(test, test);
}
}
......
......@@ -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::<uint>::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);
});
......
......@@ -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:Send>(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
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册