Make ptr::addr_of return an immutable vec, add mut_addr_of

This commit is contained in:
Marijn Haverbeke 2011-11-02 11:42:51 +01:00
parent eaf9e05611
commit 0a20eed2db
8 changed files with 22 additions and 11 deletions

View File

@ -162,9 +162,10 @@ This program uses the Posix function `gettimeofday` to get a
microsecond-resolution timer.
use std;
type timeval = {tv_sec: u32, tv_usec: u32};
type timeval = {mutable tv_sec: u32,
mutable tv_usec: u32};
native "cdecl" mod libc = "" {
fn gettimeofday(tv: *mutable timeval, tz: *()) -> i32;
fn gettimeofday(tv: *timeval, tz: *()) -> i32;
}
fn unix_time_in_microseconds() -> u64 unsafe {
let x = {tv_sec: 0u32, tv_usec: 0u32};

View File

@ -64,7 +64,7 @@ fn dylib_filename(base: str) -> str { ret "lib" + base + ".so"; }
fn pipe() -> {in: int, out: int} {
let fds = {mutable in: 0, mutable out: 0};
assert (os::libc::pipe(ptr::addr_of(fds.in)) == 0);
assert (os::libc::pipe(ptr::mut_addr_of(fds.in)) == 0);
ret {in: fds.in, out: fds.out};
}

View File

@ -57,7 +57,7 @@ fn dylib_filename(base: str) -> str { ret "lib" + base + ".dylib"; }
fn pipe() -> {in: int, out: int} {
let fds = {mutable in: 0, mutable out: 0};
assert (os::libc::pipe(ptr::addr_of(fds.in)) == 0);
assert (os::libc::pipe(ptr::mut_addr_of(fds.in)) == 0);
ret {in: fds.in, out: fds.out};
}
@ -82,7 +82,8 @@ fn get_exe_path() -> option::t<fs::path> {
let bufsize = 1023u32;
let path = str::unsafe_from_bytes(vec::init_elt(0u8, bufsize as uint));
ret str::as_buf(path, { |path_buf|
if libc::_NSGetExecutablePath(path_buf, ptr::addr_of(bufsize)) == 0 {
if libc::_NSGetExecutablePath(path_buf,
ptr::mut_addr_of(bufsize)) == 0 {
option::some(fs::dirname(path) + fs::path_sep())
} else {
option::none

View File

@ -4,7 +4,7 @@ Module: ptr
Unsafe pointer utility functions
*/
native "rust-intrinsic" mod rusti {
fn addr_of<T>(val: T) -> *mutable T;
fn addr_of<T>(val: T) -> *T;
fn ptr_offset<T>(ptr: *T, count: uint) -> *T;
}
@ -13,7 +13,16 @@ Function: addr_of
Get an unsafe pointer to a value
*/
fn addr_of<T>(val: T) -> *mutable T { ret rusti::addr_of(val); }
fn addr_of<T>(val: T) -> *T { ret rusti::addr_of(val); }
/*
Function: mut_addr_of
Get an unsafe mutable pointer to a value
*/
fn mut_addr_of<T>(val: T) -> *mutable T unsafe {
ret unsafe::reinterpret_cast(rusti::addr_of(val));
}
/*
Function: offset

View File

@ -66,7 +66,7 @@ fn pipe() -> {in: int, out: int} {
// first, as in rust_run_program.
let fds = {mutable in: 0, mutable out: 0};
let res =
os::libc::_pipe(ptr::addr_of(fds.in), 1024u,
os::libc::_pipe(ptr::mut_addr_of(fds.in), 1024u,
libc_constants::O_BINARY() |
libc_constants::O_NOINHERIT());
assert (res == 0);

View File

@ -10,7 +10,7 @@ fn main() {
unsafe {
let a = 0;
let v = std::ptr::addr_of(a);
let v = std::ptr::mut_addr_of(a);
f(v);
}
}

View File

@ -4,7 +4,7 @@ use std;
fn main() {
let a = [0];
let v: *mutable [int] = std::ptr::addr_of(a);
let v: *mutable [int] = std::ptr::mut_addr_of(a);
fn f(&&v: *mutable [mutable? int]) {
unsafe {

View File

@ -7,7 +7,7 @@ type pair = {mutable fst: int, mutable snd: int};
#[test]
fn test() unsafe {
let p = {mutable fst: 10, mutable snd: 20};
let pptr: *mutable pair = ptr::addr_of(p);
let pptr: *mutable pair = ptr::mut_addr_of(p);
let iptr: *mutable int = unsafe::reinterpret_cast(pptr);
assert (*iptr == 10);;
*iptr = 30;