mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
std: clean up ptr a bit
This commit is contained in:
parent
3496e93d13
commit
49e11630fa
@ -229,7 +229,7 @@ impl<T: Send> Drop for Unique<T> {
|
|||||||
let x = mem::uninit(); // dummy value to swap in
|
let x = mem::uninit(); // dummy value to swap in
|
||||||
// We need to move the object out of the box, so that
|
// We need to move the object out of the box, so that
|
||||||
// the destructor is called (at the end of this scope.)
|
// 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)
|
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.
|
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
|
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
|
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
|
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
|
C library. The C library can then include the pointer to the Rust object in
|
||||||
@ -335,7 +335,7 @@ extern {
|
|||||||
fn main() {
|
fn main() {
|
||||||
// Create the object that will be referenced in the callback
|
// Create the object that will be referenced in the callback
|
||||||
let rust_object = ~RustObject{a: 5, ...};
|
let rust_object = ~RustObject{a: 5, ...};
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
// Gets a raw pointer to the object
|
// Gets a raw pointer to the object
|
||||||
let target_addr:*RustObject = ptr::to_unsafe_ptr(rust_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.
|
that invoked the callback into a Rust task.
|
||||||
|
|
||||||
If an asychronous callback targets a special object in the Rust address space
|
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
|
it is also absolutely necessary that no more callbacks are performed by the
|
||||||
C library after the respective Rust object gets destroyed.
|
C library after the respective Rust object gets destroyed.
|
||||||
This can be achieved by unregistering the callback in the object's
|
This can be achieved by unregistering the callback in the object's
|
||||||
destructor and designing the library in a way that guarantees that no
|
destructor and designing the library in a way that guarantees that no
|
||||||
callback will be performed after unregistration.
|
callback will be performed after unregistration.
|
||||||
|
@ -83,7 +83,7 @@ impl<T> Rawlink<T> {
|
|||||||
|
|
||||||
/// Like Option::Some for Rawlink
|
/// Like Option::Some for Rawlink
|
||||||
fn some(n: &mut T) -> Rawlink<T> {
|
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
|
/// Convert the `Rawlink` into an Option value
|
||||||
|
@ -33,7 +33,6 @@ use std::cell::{Cell, RefCell};
|
|||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::hashmap::{HashMap, HashSet};
|
use std::hashmap::{HashMap, HashSet};
|
||||||
use std::ops;
|
use std::ops;
|
||||||
use std::ptr::to_unsafe_ptr;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::to_bytes;
|
use std::to_bytes;
|
||||||
use std::to_str::ToStr;
|
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();
|
let mut interner = cx.interner.borrow_mut();
|
||||||
@ -1234,7 +1233,7 @@ pub fn mk_t(cx: ctxt, st: sty) -> t {
|
|||||||
flags: flags,
|
flags: flags,
|
||||||
};
|
};
|
||||||
|
|
||||||
let sty_ptr = to_unsafe_ptr(&t.sty);
|
let sty_ptr = &t.sty as *sty;
|
||||||
|
|
||||||
let key = intern_key {
|
let key = intern_key {
|
||||||
sty: sty_ptr,
|
sty: sty_ptr,
|
||||||
|
@ -155,7 +155,7 @@ mod darwin_fd_limit {
|
|||||||
pub unsafe fn raise_fd_limit() {
|
pub unsafe fn raise_fd_limit() {
|
||||||
// The strategy here is to fetch the current resource limits, read the kern.maxfilesperproc
|
// 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.
|
// 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 mem::size_of_val;
|
||||||
use os::last_os_error;
|
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 mib: [libc::c_int, ..2] = [CTL_KERN, KERN_MAXFILESPERPROC];
|
||||||
let mut maxfiles: libc::c_int = 0;
|
let mut maxfiles: libc::c_int = 0;
|
||||||
let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
|
let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
|
||||||
if sysctl(to_mut_unsafe_ptr(&mut mib[0]), 2,
|
if sysctl(&mut mib[0], 2, &mut maxfiles as *mut libc::c_int as *mut libc::c_void, &mut size,
|
||||||
to_mut_unsafe_ptr(&mut maxfiles) as *mut libc::c_void,
|
|
||||||
to_mut_unsafe_ptr(&mut size),
|
|
||||||
mut_null(), 0) != 0 {
|
mut_null(), 0) != 0 {
|
||||||
let err = last_os_error();
|
let err = last_os_error();
|
||||||
error!("raise_fd_limit: error calling sysctl: {}", err);
|
error!("raise_fd_limit: error calling sysctl: {}", err);
|
||||||
@ -174,7 +172,7 @@ mod darwin_fd_limit {
|
|||||||
|
|
||||||
// Fetch the current resource limits
|
// Fetch the current resource limits
|
||||||
let mut rlim = rlimit{rlim_cur: 0, rlim_max: 0};
|
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();
|
let err = last_os_error();
|
||||||
error!("raise_fd_limit: error calling getrlimit: {}", err);
|
error!("raise_fd_limit: error calling getrlimit: {}", err);
|
||||||
return;
|
return;
|
||||||
@ -184,7 +182,7 @@ mod darwin_fd_limit {
|
|||||||
rlim.rlim_cur = ::cmp::min(maxfiles as rlim_t, rlim.rlim_max);
|
rlim.rlim_cur = ::cmp::min(maxfiles as rlim_t, rlim.rlim_max);
|
||||||
|
|
||||||
// Set our newly-increased resource limit
|
// 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();
|
let err = last_os_error();
|
||||||
error!("raise_fd_limit: error calling setrlimit: {}", err);
|
error!("raise_fd_limit: error calling setrlimit: {}", err);
|
||||||
return;
|
return;
|
||||||
|
@ -10,8 +10,6 @@
|
|||||||
|
|
||||||
//! Operations on managed box types
|
//! Operations on managed box types
|
||||||
|
|
||||||
use ptr::to_unsafe_ptr;
|
|
||||||
|
|
||||||
#[cfg(not(test))] use cmp::*;
|
#[cfg(not(test))] use cmp::*;
|
||||||
|
|
||||||
/// Returns the refcount of a shared box (as just before calling this)
|
/// 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
|
/// Determine if two shared boxes point to the same object
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
|
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 as *T == &*b as *T
|
||||||
a_ptr == b_ptr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[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
|
* Swap the values at two mutable locations of the same type, without
|
||||||
* deinitialising or copying either one.
|
* deinitialising either. They may overlap.
|
||||||
*/
|
*/
|
||||||
#[inline]
|
#[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
|
// Give ourselves some scratch space to work with
|
||||||
let mut tmp: T = mem::uninit();
|
let mut tmp: T = mem::uninit();
|
||||||
let t: *mut T = &mut tmp;
|
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
|
* 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]
|
#[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
|
mem::swap(cast::transmute(dest), &mut src); // cannot overlap
|
||||||
src
|
src
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the value from `*src` and returns it. Does not copy `*src`.
|
* Reads the value from `*src` and returns it.
|
||||||
*/
|
*/
|
||||||
#[inline(always)]
|
#[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();
|
let mut tmp: T = mem::uninit();
|
||||||
copy_nonoverlapping_memory(&mut tmp, src, 1);
|
copy_nonoverlapping_memory(&mut tmp, src, 1);
|
||||||
tmp
|
tmp
|
||||||
@ -145,9 +145,9 @@ pub unsafe fn read_ptr<T>(src: *T) -> T {
|
|||||||
* This currently prevents destructors from executing.
|
* This currently prevents destructors from executing.
|
||||||
*/
|
*/
|
||||||
#[inline(always)]
|
#[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`:
|
// Copy the data out from `dest`:
|
||||||
let tmp = read_ptr(&*dest);
|
let tmp = read(&*dest);
|
||||||
|
|
||||||
// Now zero out `dest`:
|
// Now zero out `dest`:
|
||||||
zero_memory(dest, 1);
|
zero_memory(dest, 1);
|
||||||
@ -155,18 +155,6 @@ pub unsafe fn read_and_zero_ptr<T>(dest: *mut T) -> T {
|
|||||||
tmp
|
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),
|
Given a **T (pointer to an array of pointers),
|
||||||
iterate through each *T, up to the provided `len`,
|
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|) {
|
pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) {
|
||||||
debug!("array_each_with_len: before iterate");
|
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");
|
fail!("ptr::array_each_with_len failure: arr input is null pointer");
|
||||||
}
|
}
|
||||||
//let start_ptr = *arr;
|
//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.
|
Dragons be here.
|
||||||
*/
|
*/
|
||||||
pub unsafe fn array_each<T>(arr: **T, cb: |*T|) {
|
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");
|
fail!("ptr::array_each_with_len failure: arr input is null pointer");
|
||||||
}
|
}
|
||||||
let len = buf_len(arr);
|
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);
|
array_each_with_len(arr, len, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(missing_doc)]
|
/// Extension methods for raw pointers.
|
||||||
pub trait RawPtr<T> {
|
pub trait RawPtr<T> {
|
||||||
|
/// Returns the null pointer.
|
||||||
fn null() -> Self;
|
fn null() -> Self;
|
||||||
|
/// Returns true if the pointer is equal to the null pointer.
|
||||||
fn is_null(&self) -> bool;
|
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;
|
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>;
|
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;
|
unsafe fn offset(self, count: int) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extension methods for immutable pointers
|
|
||||||
impl<T> RawPtr<T> for *T {
|
impl<T> RawPtr<T> for *T {
|
||||||
/// Returns the null pointer.
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn null() -> *T { null() }
|
fn null() -> *T { null() }
|
||||||
|
|
||||||
/// Returns true if the pointer is equal to the null pointer.
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_null(&self) -> bool { *self == RawPtr::null() }
|
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() }
|
|
||||||
|
|
||||||
/// Returns the address of this pointer.
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_uint(&self) -> uint { *self as uint }
|
fn to_uint(&self) -> uint { *self as uint }
|
||||||
|
|
||||||
///
|
#[inline]
|
||||||
/// Returns `None` if the pointer is null, or else returns the value wrapped
|
unsafe fn offset(self, count: int) -> *T { intrinsics::offset(self, count) }
|
||||||
/// 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]
|
#[inline]
|
||||||
unsafe fn to_option(&self) -> Option<&T> {
|
unsafe fn to_option(&self) -> Option<&T> {
|
||||||
if self.is_null() { None } else {
|
if self.is_null() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
Some(cast::transmute(*self))
|
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 {
|
impl<T> RawPtr<T> for *mut T {
|
||||||
/// Returns the null pointer.
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn null() -> *mut T { mut_null() }
|
fn null() -> *mut T { mut_null() }
|
||||||
|
|
||||||
/// Returns true if the pointer is equal to the null pointer.
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_null(&self) -> bool { *self == RawPtr::null() }
|
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() }
|
|
||||||
|
|
||||||
/// Returns the address of this pointer.
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_uint(&self) -> uint { *self as uint }
|
fn to_uint(&self) -> uint { *self as uint }
|
||||||
|
|
||||||
///
|
#[inline]
|
||||||
/// Returns `None` if the pointer is null, or else returns the value wrapped
|
unsafe fn offset(self, count: int) -> *mut T { intrinsics::offset(self as *T, count) as *mut T }
|
||||||
/// 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]
|
#[inline]
|
||||||
unsafe fn to_option(&self) -> Option<&T> {
|
unsafe fn to_option(&self) -> Option<&T> {
|
||||||
if self.is_null() { None } else {
|
if self.is_null() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
Some(cast::transmute(*self))
|
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
|
// Equality for pointers
|
||||||
|
@ -24,13 +24,13 @@ pointers, and then storing the parent pointers as `Weak` pointers.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
use cast::transmute;
|
use cast::transmute;
|
||||||
use ops::Drop;
|
|
||||||
use cmp::{Eq, Ord};
|
|
||||||
use clone::{Clone, DeepClone};
|
use clone::{Clone, DeepClone};
|
||||||
|
use cmp::{Eq, Ord};
|
||||||
use kinds::marker;
|
use kinds::marker;
|
||||||
use rt::global_heap::exchange_free;
|
use ops::Drop;
|
||||||
use ptr::read_ptr;
|
|
||||||
use option::{Option, Some, None};
|
use option::{Option, Some, None};
|
||||||
|
use ptr;
|
||||||
|
use rt::global_heap::exchange_free;
|
||||||
|
|
||||||
struct RcBox<T> {
|
struct RcBox<T> {
|
||||||
value: T,
|
value: T,
|
||||||
@ -85,7 +85,7 @@ impl<T> Drop for Rc<T> {
|
|||||||
if self.ptr != 0 as *mut RcBox<T> {
|
if self.ptr != 0 as *mut RcBox<T> {
|
||||||
(*self.ptr).strong -= 1;
|
(*self.ptr).strong -= 1;
|
||||||
if (*self.ptr).strong == 0 {
|
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
|
// remove the implicit "strong weak" pointer now
|
||||||
// that we've destroyed the contents.
|
// that we've destroyed the contents.
|
||||||
|
@ -22,7 +22,6 @@ use container::Container;
|
|||||||
use io;
|
use io;
|
||||||
use iter::Iterator;
|
use iter::Iterator;
|
||||||
use option::{Some, None, Option};
|
use option::{Some, None, Option};
|
||||||
use ptr;
|
|
||||||
use ptr::RawPtr;
|
use ptr::RawPtr;
|
||||||
use reflect;
|
use reflect;
|
||||||
use reflect::{MovePtr, align};
|
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 {
|
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 {
|
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]));
|
if_ok!(self, self.writer.write(['@' as u8]));
|
||||||
self.write_mut_qualifier(mtbl);
|
self.write_mut_qualifier(mtbl);
|
||||||
self.get::<&raw::Box<()>>(|this, b| {
|
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)
|
this.visit_ptr_inner(p, inner)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -387,7 +386,7 @@ impl<'a> TyVisitor for ReprVisitor<'a> {
|
|||||||
_: uint, inner: *TyDesc) -> bool {
|
_: uint, inner: *TyDesc) -> bool {
|
||||||
let assumed_size = if sz == 0 { n } else { sz };
|
let assumed_size = if sz == 0 { n } else { sz };
|
||||||
self.get::<()>(|this, b| {
|
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<T>(writer: &mut io::Writer, object: &T) -> io::IoResult<()> {
|
pub fn write_repr<T>(writer: &mut io::Writer, object: &T) -> io::IoResult<()> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr = ptr::to_unsafe_ptr(object) as *u8;
|
let ptr = object as *T as *u8;
|
||||||
let tydesc = get_tydesc::<T>();
|
let tydesc = get_tydesc::<T>();
|
||||||
let u = ReprVisitor(ptr, writer);
|
let u = ReprVisitor(ptr, writer);
|
||||||
let mut v = reflect::MovePtrAdaptor(u);
|
let mut v = reflect::MovePtrAdaptor(u);
|
||||||
|
@ -363,7 +363,7 @@ impl<T: Send> Buffer<T> {
|
|||||||
// very unsafe method which the caller needs to treat specially in case a
|
// very unsafe method which the caller needs to treat specially in case a
|
||||||
// race is lost.
|
// race is lost.
|
||||||
unsafe fn get(&self, i: int) -> T {
|
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
|
// 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 (p, ch) = Chan::<uint>::new();
|
||||||
|
|
||||||
let x = ~1;
|
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() {
|
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);
|
ch.send(x_in_child);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -102,6 +102,7 @@ There are a number of free functions that create or take vectors, for example:
|
|||||||
#[warn(non_camel_case_types)];
|
#[warn(non_camel_case_types)];
|
||||||
|
|
||||||
use cast;
|
use cast;
|
||||||
|
use cast::transmute;
|
||||||
use ops::Drop;
|
use ops::Drop;
|
||||||
use clone::{Clone, DeepClone};
|
use clone::{Clone, DeepClone};
|
||||||
use container::{Container, Mutable};
|
use container::{Container, Mutable};
|
||||||
@ -112,7 +113,6 @@ use default::Default;
|
|||||||
use iter::*;
|
use iter::*;
|
||||||
use num::{Integer, CheckedAdd, Saturating, checked_next_power_of_two};
|
use num::{Integer, CheckedAdd, Saturating, checked_next_power_of_two};
|
||||||
use option::{None, Option, Some};
|
use option::{None, Option, Some};
|
||||||
use ptr::to_unsafe_ptr;
|
|
||||||
use ptr;
|
use ptr;
|
||||||
use ptr::RawPtr;
|
use ptr::RawPtr;
|
||||||
use rt::global_heap::{malloc_raw, realloc_raw, exchange_free};
|
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<()>;
|
let ptr = malloc_raw(size) as *mut Vec<()>;
|
||||||
(*ptr).alloc = alloc;
|
(*ptr).alloc = alloc;
|
||||||
(*ptr).fill = 0;
|
(*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] {
|
pub fn ref_slice<'a, A>(s: &'a A) -> &'a [A] {
|
||||||
unsafe {
|
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] {
|
pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr: *A = cast::transmute(s);
|
let ptr: *A = transmute(s);
|
||||||
cast::transmute(Slice { data: ptr, len: 1 })
|
transmute(Slice { data: ptr, len: 1 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -991,7 +991,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
|
|||||||
assert!(start <= end);
|
assert!(start <= end);
|
||||||
assert!(end <= self.len());
|
assert!(end <= self.len());
|
||||||
unsafe {
|
unsafe {
|
||||||
cast::transmute(Slice {
|
transmute(Slice {
|
||||||
data: self.as_ptr().offset(start as int),
|
data: self.as_ptr().offset(start as int),
|
||||||
len: (end - start)
|
len: (end - start)
|
||||||
})
|
})
|
||||||
@ -1109,7 +1109,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn unsafe_ref(self, index: uint) -> &'a T {
|
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]
|
#[inline]
|
||||||
@ -1144,7 +1144,7 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
|
|||||||
fn shift_ref(&mut self) -> Option<&'a T> {
|
fn shift_ref(&mut self) -> Option<&'a T> {
|
||||||
if self.len() == 0 { return None; }
|
if self.len() == 0 { return None; }
|
||||||
unsafe {
|
unsafe {
|
||||||
let s: &mut Slice<T> = cast::transmute(self);
|
let s: &mut Slice<T> = transmute(self);
|
||||||
Some(&*raw::shift_ptr(s))
|
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> {
|
fn pop_ref(&mut self) -> Option<&'a T> {
|
||||||
if self.len() == 0 { return None; }
|
if self.len() == 0 { return None; }
|
||||||
unsafe {
|
unsafe {
|
||||||
let s: &mut Slice<T> = cast::transmute(self);
|
let s: &mut Slice<T> = transmute(self);
|
||||||
Some(&*raw::pop_ptr(s))
|
Some(&*raw::pop_ptr(s))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1417,8 +1417,8 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn move_iter(self) -> MoveItems<T> {
|
fn move_iter(self) -> MoveItems<T> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let iter = cast::transmute(self.iter());
|
let iter = transmute(self.iter());
|
||||||
let ptr = cast::transmute(self);
|
let ptr = transmute(self);
|
||||||
MoveItems { allocation: ptr, iter: iter }
|
MoveItems { allocation: ptr, iter: iter }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1432,7 +1432,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
// Only make the (slow) call into the runtime if we have to
|
// Only make the (slow) call into the runtime if we have to
|
||||||
if self.capacity() < n {
|
if self.capacity() < n {
|
||||||
unsafe {
|
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 alloc = n * mem::nonzero_size_of::<T>();
|
||||||
let size = alloc + mem::size_of::<Vec<()>>();
|
let size = alloc + mem::size_of::<Vec<()>>();
|
||||||
if alloc / mem::nonzero_size_of::<T>() != n || size < alloc {
|
if alloc / mem::nonzero_size_of::<T>() != n || size < alloc {
|
||||||
@ -1463,14 +1463,14 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn capacity(&self) -> uint {
|
fn capacity(&self) -> uint {
|
||||||
unsafe {
|
unsafe {
|
||||||
let repr: **Vec<()> = cast::transmute(self);
|
let repr: **Vec<()> = transmute(self);
|
||||||
(**repr).alloc / mem::nonzero_size_of::<T>()
|
(**repr).alloc / mem::nonzero_size_of::<T>()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shrink_to_fit(&mut self) {
|
fn shrink_to_fit(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr: *mut *mut Vec<()> = cast::transmute(self);
|
let ptr: *mut *mut Vec<()> = transmute(self);
|
||||||
let alloc = (**ptr).fill;
|
let alloc = (**ptr).fill;
|
||||||
let size = alloc + mem::size_of::<Vec<()>>();
|
let size = alloc + mem::size_of::<Vec<()>>();
|
||||||
*ptr = realloc_raw(*ptr as *mut u8, size) as *mut Vec<()>;
|
*ptr = realloc_raw(*ptr as *mut u8, size) as *mut Vec<()>;
|
||||||
@ -1481,7 +1481,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn push(&mut self, t: T) {
|
fn push(&mut self, t: T) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let repr: **Vec<()> = cast::transmute(&mut *self);
|
let repr: **Vec<()> = transmute(&mut *self);
|
||||||
let fill = (**repr).fill;
|
let fill = (**repr).fill;
|
||||||
if (**repr).alloc <= fill {
|
if (**repr).alloc <= fill {
|
||||||
self.reserve_additional(1);
|
self.reserve_additional(1);
|
||||||
@ -1493,10 +1493,10 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
// This doesn't bother to make sure we have space.
|
// This doesn't bother to make sure we have space.
|
||||||
#[inline] // really pretty please
|
#[inline] // really pretty please
|
||||||
unsafe fn push_fast<T>(this: &mut ~[T], t: T) {
|
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;
|
let fill = (**repr).fill;
|
||||||
(**repr).fill += mem::nonzero_size_of::<T>();
|
(**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;
|
let p = p.offset(fill as int) as *mut T;
|
||||||
mem::move_val_init(&mut(*p), t);
|
mem::move_val_init(&mut(*p), t);
|
||||||
}
|
}
|
||||||
@ -1521,10 +1521,10 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
match self.len() {
|
match self.len() {
|
||||||
0 => None,
|
0 => None,
|
||||||
ln => {
|
ln => {
|
||||||
let valptr = ptr::to_mut_unsafe_ptr(&mut self[ln - 1u]);
|
let valptr = &mut self[ln - 1u] as *mut T;
|
||||||
unsafe {
|
unsafe {
|
||||||
self.set_len(ln - 1u);
|
self.set_len(ln - 1u);
|
||||||
Some(ptr::read_ptr(&*valptr))
|
Some(ptr::read(&*valptr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1568,7 +1568,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
let ptr = self.as_mut_ptr().offset(i as int);
|
let ptr = self.as_mut_ptr().offset(i as int);
|
||||||
// copy it out, unsafely having a copy of the value on
|
// copy it out, unsafely having a copy of the value on
|
||||||
// the stack and in the vector at the same time.
|
// 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.
|
// Shift everything down to fill in that spot.
|
||||||
ptr::copy_memory(ptr, &*ptr.offset(1), len - i - 1);
|
ptr::copy_memory(ptr, &*ptr.offset(1), len - i - 1);
|
||||||
@ -1598,7 +1598,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
let p = self.as_mut_ptr();
|
let p = self.as_mut_ptr();
|
||||||
// This loop is optimized out for non-drop types.
|
// This loop is optimized out for non-drop types.
|
||||||
for i in range(newlen, oldlen) {
|
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); }
|
unsafe { self.set_len(newlen); }
|
||||||
@ -1648,7 +1648,7 @@ impl<T> OwnedVector<T> for ~[T] {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn set_len(&mut self, new_len: uint) {
|
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>();
|
(**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.
|
// `.offset(j)` is always in bounds.
|
||||||
|
|
||||||
if i != j {
|
if i != j {
|
||||||
let tmp = ptr::read_ptr(read_ptr);
|
let tmp = ptr::read(read_ptr);
|
||||||
ptr::copy_memory(buf_v.offset(j + 1),
|
ptr::copy_memory(buf_v.offset(j + 1),
|
||||||
&*buf_v.offset(j),
|
&*buf_v.offset(j),
|
||||||
(i - j) as uint);
|
(i - j) as uint);
|
||||||
@ -2269,7 +2269,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
|
|||||||
assert!(start <= end);
|
assert!(start <= end);
|
||||||
assert!(end <= self.len());
|
assert!(end <= self.len());
|
||||||
unsafe {
|
unsafe {
|
||||||
cast::transmute(Slice {
|
transmute(Slice {
|
||||||
data: self.as_mut_ptr().offset(start as int) as *T,
|
data: self.as_mut_ptr().offset(start as int) as *T,
|
||||||
len: (end - start)
|
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> {
|
fn mut_shift_ref(&mut self) -> Option<&'a mut T> {
|
||||||
if self.len() == 0 { return None; }
|
if self.len() == 0 { return None; }
|
||||||
unsafe {
|
unsafe {
|
||||||
let s: &mut Slice<T> = cast::transmute(self);
|
let s: &mut Slice<T> = transmute(self);
|
||||||
Some(cast::transmute_mut(&*raw::shift_ptr(s)))
|
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> {
|
fn mut_pop_ref(&mut self) -> Option<&'a mut T> {
|
||||||
if self.len() == 0 { return None; }
|
if self.len() == 0 { return None; }
|
||||||
unsafe {
|
unsafe {
|
||||||
let s: &mut Slice<T> = cast::transmute(self);
|
let s: &mut Slice<T> = transmute(self);
|
||||||
Some(cast::transmute_mut(&*raw::pop_ptr(s)))
|
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
|
// them to their raw pointers to do the swap
|
||||||
let pa: *mut T = &mut self[a];
|
let pa: *mut T = &mut self[a];
|
||||||
let pb: *mut T = &mut self[b];
|
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]
|
#[inline]
|
||||||
unsafe fn unsafe_mut_ref(self, index: uint) -> &'a mut T {
|
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]
|
#[inline]
|
||||||
@ -2484,7 +2484,7 @@ pub unsafe fn from_buf<T>(ptr: *T, elts: uint) -> ~[T] {
|
|||||||
|
|
||||||
/// Unsafe operations
|
/// Unsafe operations
|
||||||
pub mod raw {
|
pub mod raw {
|
||||||
use cast;
|
use cast::transmute;
|
||||||
use ptr;
|
use ptr;
|
||||||
use ptr::RawPtr;
|
use ptr::RawPtr;
|
||||||
use vec::{with_capacity, MutableVector, OwnedVector};
|
use vec::{with_capacity, MutableVector, OwnedVector};
|
||||||
@ -2497,7 +2497,7 @@ pub mod raw {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn buf_as_slice<T,U>(p: *T, len: uint, f: |v: &[T]| -> U)
|
pub unsafe fn buf_as_slice<T,U>(p: *T, len: uint, f: |v: &[T]| -> U)
|
||||||
-> U {
|
-> U {
|
||||||
f(cast::transmute(Slice {
|
f(transmute(Slice {
|
||||||
data: p,
|
data: p,
|
||||||
len: len
|
len: len
|
||||||
}))
|
}))
|
||||||
@ -2514,7 +2514,7 @@ pub mod raw {
|
|||||||
len: uint,
|
len: uint,
|
||||||
f: |v: &mut [T]| -> U)
|
f: |v: &mut [T]| -> U)
|
||||||
-> U {
|
-> U {
|
||||||
f(cast::transmute(Slice {
|
f(transmute(Slice {
|
||||||
data: p as *T,
|
data: p as *T,
|
||||||
len: len
|
len: len
|
||||||
}))
|
}))
|
||||||
@ -2698,12 +2698,12 @@ macro_rules! iterator {
|
|||||||
// purposefully don't use 'ptr.offset' because for
|
// purposefully don't use 'ptr.offset' because for
|
||||||
// vectors with 0-size elements this would return the
|
// vectors with 0-size elements this would return the
|
||||||
// same pointer.
|
// same pointer.
|
||||||
cast::transmute(self.ptr as uint + 1)
|
transmute(self.ptr as uint + 1)
|
||||||
} else {
|
} else {
|
||||||
self.ptr.offset(1)
|
self.ptr.offset(1)
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(cast::transmute(old))
|
Some(transmute(old))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2726,11 +2726,11 @@ macro_rules! iterator {
|
|||||||
} else {
|
} else {
|
||||||
self.end = if mem::size_of::<T>() == 0 {
|
self.end = if mem::size_of::<T>() == 0 {
|
||||||
// See above for why 'ptr.offset' isn't used
|
// See above for why 'ptr.offset' isn't used
|
||||||
cast::transmute(self.end as uint - 1)
|
transmute(self.end as uint - 1)
|
||||||
} else {
|
} else {
|
||||||
self.end.offset(-1)
|
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> {
|
fn idx(&self, index: uint) -> Option<&'a T> {
|
||||||
unsafe {
|
unsafe {
|
||||||
if index < self.indexable() {
|
if index < self.indexable() {
|
||||||
cast::transmute(self.ptr.offset(index as int))
|
transmute(self.ptr.offset(index as int))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -2895,7 +2895,7 @@ impl<T> Iterator<T> for MoveItems<T> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn next(&mut self) -> Option<T> {
|
fn next(&mut self) -> Option<T> {
|
||||||
unsafe {
|
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]
|
#[inline]
|
||||||
fn next_back(&mut self) -> Option<T> {
|
fn next_back(&mut self) -> Option<T> {
|
||||||
unsafe {
|
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 cast::{forget, transmute};
|
|||||||
use rt::global_heap::{malloc_raw, realloc_raw};
|
use rt::global_heap::{malloc_raw, realloc_raw};
|
||||||
use vec::{ImmutableVector, Items, MutableVector};
|
use vec::{ImmutableVector, Items, MutableVector};
|
||||||
use unstable::raw::Slice;
|
use unstable::raw::Slice;
|
||||||
use ptr::read_ptr;
|
use ptr;
|
||||||
use ptr::RawPtr;
|
use ptr::RawPtr;
|
||||||
use libc::{free, c_void};
|
use libc::{free, c_void};
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ impl<T> Vec<T> {
|
|||||||
} else {
|
} else {
|
||||||
unsafe {
|
unsafe {
|
||||||
self.len -= 1;
|
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<T> Vec<T> {
|
|||||||
let mut i = len;
|
let mut i = len;
|
||||||
// drop any extra elements
|
// drop any extra elements
|
||||||
while i < self.len {
|
while i < self.len {
|
||||||
read_ptr(self.as_slice().unsafe_ref(i));
|
ptr::read(self.as_slice().unsafe_ref(i));
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -188,7 +188,7 @@ impl<T> Drop for Vec<T> {
|
|||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
for x in self.as_mut_slice().iter() {
|
for x in self.as_mut_slice().iter() {
|
||||||
read_ptr(x);
|
ptr::read(x);
|
||||||
}
|
}
|
||||||
free(self.ptr as *mut c_void)
|
free(self.ptr as *mut c_void)
|
||||||
}
|
}
|
||||||
@ -204,7 +204,7 @@ impl<T> Iterator<T> for MoveItems<T> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn next(&mut self) -> Option<T> {
|
fn next(&mut self) -> Option<T> {
|
||||||
unsafe {
|
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]
|
#[inline]
|
||||||
fn next_back(&mut self) -> Option<T> {
|
fn next_back(&mut self) -> Option<T> {
|
||||||
unsafe {
|
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
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
enum bottom { }
|
enum bottom { }
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = ptr::to_unsafe_ptr(&()) as *bottom;
|
let x = &() as *() as *bottom;
|
||||||
match x { } //~ ERROR non-exhaustive patterns
|
match x { } //~ ERROR non-exhaustive patterns
|
||||||
}
|
}
|
||||||
|
@ -10,15 +10,13 @@
|
|||||||
|
|
||||||
#[feature(managed_boxes)];
|
#[feature(managed_boxes)];
|
||||||
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
fn borrow(x: &int, f: |x: &int|) {
|
fn borrow(x: &int, f: |x: &int|) {
|
||||||
f(x)
|
f(x)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test1(x: @~int) {
|
fn test1(x: @~int) {
|
||||||
borrow(&*(*x).clone(), |p| {
|
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!((x_a as uint) != (p as *int as uint));
|
||||||
assert_eq!(unsafe{*x_a}, *p);
|
assert_eq!(unsafe{*x_a}, *p);
|
||||||
})
|
})
|
||||||
|
@ -14,8 +14,6 @@
|
|||||||
|
|
||||||
#[feature(managed_boxes)];
|
#[feature(managed_boxes)];
|
||||||
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
fn borrow(x: &int, f: |x: &int|) {
|
fn borrow(x: &int, f: |x: &int|) {
|
||||||
let before = *x;
|
let before = *x;
|
||||||
f(x);
|
f(x);
|
||||||
@ -29,12 +27,11 @@ pub fn main() {
|
|||||||
let mut x = @F {f: ~3};
|
let mut x = @F {f: ~3};
|
||||||
borrow(x.f, |b_x| {
|
borrow(x.f, |b_x| {
|
||||||
assert_eq!(*b_x, 3);
|
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};
|
x = @F {f: ~4};
|
||||||
|
|
||||||
info!("ptr::to_unsafe_ptr(*b_x) = {:x}",
|
info!("&*b_x = {:p}", &(*b_x));
|
||||||
ptr::to_unsafe_ptr(&(*b_x)) as uint);
|
|
||||||
assert_eq!(*b_x, 3);
|
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)];
|
#[feature(managed_boxes)];
|
||||||
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
fn borrow(x: &int, f: |x: &int|) {
|
fn borrow(x: &int, f: |x: &int|) {
|
||||||
let before = *x;
|
let before = *x;
|
||||||
f(x);
|
f(x);
|
||||||
@ -29,12 +27,11 @@ pub fn main() {
|
|||||||
let mut x = ~@F{f: ~3};
|
let mut x = ~@F{f: ~3};
|
||||||
borrow(x.f, |b_x| {
|
borrow(x.f, |b_x| {
|
||||||
assert_eq!(*b_x, 3);
|
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};
|
*x = @F{f: ~4};
|
||||||
|
|
||||||
info!("ptr::to_unsafe_ptr(*b_x) = {:x}",
|
info!("&*b_x = {:p}", &(*b_x));
|
||||||
ptr::to_unsafe_ptr(&(*b_x)) as uint);
|
|
||||||
assert_eq!(*b_x, 3);
|
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)];
|
#[feature(managed_boxes)];
|
||||||
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
fn borrow(x: &int, f: |x: &int|) {
|
fn borrow(x: &int, f: |x: &int|) {
|
||||||
let before = *x;
|
let before = *x;
|
||||||
f(x);
|
f(x);
|
||||||
@ -27,12 +25,11 @@ pub fn main() {
|
|||||||
let mut x = @3;
|
let mut x = @3;
|
||||||
borrow(x, |b_x| {
|
borrow(x, |b_x| {
|
||||||
assert_eq!(*b_x, 3);
|
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;
|
x = @22;
|
||||||
|
|
||||||
info!("ptr::to_unsafe_ptr(*b_x) = {:x}",
|
info!("&*b_x = {:p}", &(*b_x));
|
||||||
ptr::to_unsafe_ptr(&(*b_x)) as uint);
|
|
||||||
assert_eq!(*b_x, 3);
|
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)];
|
#[feature(managed_boxes)];
|
||||||
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
fn borrow(x: &int, f: |x: &int|) {
|
fn borrow(x: &int, f: |x: &int|) {
|
||||||
let before = *x;
|
let before = *x;
|
||||||
f(x);
|
f(x);
|
||||||
@ -29,12 +27,11 @@ pub fn main() {
|
|||||||
let mut x = @F {f: ~3};
|
let mut x = @F {f: ~3};
|
||||||
borrow((*x).f, |b_x| {
|
borrow((*x).f, |b_x| {
|
||||||
assert_eq!(*b_x, 3);
|
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};
|
x = @F {f: ~4};
|
||||||
|
|
||||||
info!("ptr::to_unsafe_ptr(*b_x) = {:x}",
|
info!("&*b_x = {:p}", &(*b_x));
|
||||||
ptr::to_unsafe_ptr(&(*b_x)) as uint);
|
|
||||||
assert_eq!(*b_x, 3);
|
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);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -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 <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
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
type Big = [u64, ..8];
|
type Big = [u64, ..8];
|
||||||
struct Pair<'a> { a: int, b: &'a Big }
|
struct Pair<'a> { a: int, b: &'a Big }
|
||||||
static x: &'static Big = &([13, 14, 10, 13, 11, 14, 14, 15]);
|
static x: &'static Big = &([13, 14, 10, 13, 11, 14, 14, 15]);
|
||||||
static y: &'static Pair<'static> = &Pair {a: 15, b: x};
|
static y: &'static Pair<'static> = &Pair {a: 15, b: x};
|
||||||
|
|
||||||
pub fn main() {
|
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.
|
// except according to those terms.
|
||||||
|
|
||||||
use std::cast;
|
use std::cast;
|
||||||
use std::ptr;
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
fn addr_of<T>(ptr: &T) -> uint {
|
fn addr_of<T>(ptr: &T) -> uint {
|
||||||
let ptr = ptr::to_unsafe_ptr(ptr);
|
ptr as *T as uint
|
||||||
ptr as uint
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_aligned<T>(ptr: &T) -> bool {
|
fn is_aligned<T>(ptr: &T) -> bool {
|
||||||
|
@ -10,9 +10,7 @@
|
|||||||
|
|
||||||
// Issue #2040
|
// Issue #2040
|
||||||
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let foo = 1;
|
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) {
|
fn do_swap(test: &mut TestDescAndFn) {
|
||||||
unsafe {
|
unsafe {
|
||||||
ptr::swap_ptr(test, test);
|
ptr::swap(test, test);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,17 +8,16 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use std::ptr;
|
|
||||||
use std::task;
|
use std::task;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let (p, ch) = Chan::<uint>::new();
|
let (p, ch) = Chan::<uint>::new();
|
||||||
|
|
||||||
let x = ~1;
|
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() {
|
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);
|
ch.send(x_in_child);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@
|
|||||||
#[feature(managed_boxes)];
|
#[feature(managed_boxes)];
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
enum maybe_pointy {
|
enum maybe_pointy {
|
||||||
none,
|
none,
|
||||||
@ -24,7 +23,7 @@ struct Pointy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn make_uniq_closure<A:Send>(a: A) -> proc() -> uint {
|
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
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user