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)
}
}
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
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);
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.
/// 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
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;
_ => {}
};
- let key = intern_key { sty: to_unsafe_ptr(&st) };
+ let key = intern_key { sty: &st };
{
let mut interner = cx.interner.borrow_mut();
flags: flags,
};
- let sty_ptr = to_unsafe_ptr(&t.sty);
+ let sty_ptr = &t.sty as *sty;
let key = intern_key {
sty: sty_ptr,
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;
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);
// 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;
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;
//! 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)
/// 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))]
/**
* 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;
/**
* 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
* 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);
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`,
*/
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;
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);
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
*/
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,
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.
use io;
use iter::Iterator;
use option::{Some, None, Option};
-use ptr;
use ptr::RawPtr;
use reflect;
use reflect::{MovePtr, align};
}
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 {
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)
})
}
_: 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)
})
}
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);
// 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
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);
});
#[warn(non_camel_case_types)];
use cast;
+use cast::transmute;
use ops::Drop;
use clone::{Clone, DeepClone};
use container::{Container, Mutable};
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};
let ptr = malloc_raw(size) as *mut Vec<()>;
(*ptr).alloc = alloc;
(*ptr).fill = 0;
- cast::transmute(ptr)
+ transmute(ptr)
}
}
*/
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 })
}
}
*/
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 })
}
}
assert!(start <= end);
assert!(end <= self.len());
unsafe {
- cast::transmute(Slice {
+ transmute(Slice {
data: self.as_ptr().offset(start as int),
len: (end - start)
})
#[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]
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))
}
}
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))
}
}
#[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 }
}
}
// 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 {
#[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<()>;
#[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);
// 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);
}
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))
}
}
}
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);
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); }
#[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>();
}
}
// `.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);
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)
})
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)))
}
}
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)))
}
}
// 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);
}
}
#[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]
/// Unsafe operations
pub mod raw {
- use cast;
+ use cast::transmute;
use ptr;
use ptr::RawPtr;
use vec::{with_capacity, MutableVector, OwnedVector};
#[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
}))
len: uint,
f: |v: &mut [T]| -> U)
-> U {
- f(cast::transmute(Slice {
+ f(transmute(Slice {
data: p as *T,
len: len
}))
// 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))
}
}
}
} 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))
}
}
}
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
}
#[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))
}
}
#[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))
}
}
}
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};
} 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())))
}
}
}
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;
}
}
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)
}
#[inline]
fn next(&mut self) -> Option<T> {
unsafe {
- self.iter.next().map(|x| read_ptr(x))
+ self.iter.next().map(|x| ptr::read(x))
}
}
#[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))
}
}
}
// 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
}
#[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);
})
#[feature(managed_boxes)];
-use std::ptr;
-
fn borrow(x: &int, f: |x: &int|) {
let before = *x;
f(x);
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);
})
}
#[feature(managed_boxes)];
-use std::ptr;
-
fn borrow(x: &int, f: |x: &int|) {
let before = *x;
f(x);
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);
})
}
#[feature(managed_boxes)];
-use std::ptr;
-
fn borrow(x: &int, f: |x: &int|) {
let before = *x;
f(x);
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);
})
}
#[feature(managed_boxes)];
-use std::ptr;
-
fn borrow(x: &int, f: |x: &int|) {
let before = *x;
f(x);
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);
})
}
+++ /dev/null
-// 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);
-}
// 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);
}
// 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 {
// 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);
}
fn do_swap(test: &mut TestDescAndFn) {
unsafe {
- ptr::swap_ptr(test, test);
+ ptr::swap(test, test);
}
}
// 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);
});
#[feature(managed_boxes)];
use std::cell::RefCell;
-use std::ptr;
enum maybe_pointy {
none,
}
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
}