Skip to content

Commit

Permalink
Replaced many instances of reinterpret_cast with transmute
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiez committed Apr 20, 2013
1 parent f2b0ef1 commit 51a68eb
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 109 deletions.
8 changes: 4 additions & 4 deletions src/libcore/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub mod rustrt {
pub fn capacity<T>(v: @[T]) -> uint {
unsafe {
let repr: **raw::VecRepr =
::cast::reinterpret_cast(&addr_of(&v));
::cast::transmute(addr_of(&v));
(**repr).unboxed.alloc / sys::size_of::<T>()
}
}
Expand Down Expand Up @@ -208,7 +208,7 @@ pub mod raw {
*/
#[inline(always)]
pub unsafe fn set_len<T>(v: @[T], new_len: uint) {
let repr: **mut VecRepr = ::cast::reinterpret_cast(&addr_of(&v));
let repr: **mut VecRepr = ::cast::transmute(addr_of(&v));
(**repr).unboxed.fill = new_len * sys::size_of::<T>();
}

Expand All @@ -226,7 +226,7 @@ pub mod raw {

#[inline(always)] // really pretty please
pub unsafe fn push_fast<T>(v: &mut @[T], initval: T) {
let repr: **mut VecRepr = ::cast::reinterpret_cast(&v);
let repr: **mut VecRepr = ::cast::transmute(v);
let fill = (**repr).unboxed.fill;
(**repr).unboxed.fill += sys::size_of::<T>();
let p = addr_of(&((**repr).unboxed.data));
Expand Down Expand Up @@ -322,4 +322,4 @@ mod test {
assert!(from_slice([@"abc", @"123"]) == @[@"abc", @"123"]);
assert!(from_slice([@[42]]) == @[@[42]]);
}
}
}
18 changes: 9 additions & 9 deletions src/libcore/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ pub mod rustrt {
}

unsafe fn bump<T, U>(ptr: *T, count: uint) -> *U {
return cast::reinterpret_cast(&ptr::offset(ptr, count));
return cast::transmute(ptr::offset(ptr, count));
}

unsafe fn align_to_pointer<T>(ptr: *T) -> *T {
let align = sys::min_align_of::<*T>();
let ptr: uint = cast::reinterpret_cast(&ptr);
let ptr: uint = cast::transmute(ptr);
let ptr = (ptr + (align - 1)) & -align;
return cast::reinterpret_cast(&ptr);
return cast::transmute(ptr);
}

unsafe fn get_safe_point_count() -> uint {
Expand Down Expand Up @@ -129,8 +129,8 @@ type Visitor<'self> = &'self fn(root: **Word, tydesc: *Word) -> bool;
// Walks the list of roots for the given safe point, and calls visitor
// on each root.
unsafe fn walk_safe_point(fp: *Word, sp: SafePoint, visitor: Visitor) {
let fp_bytes: *u8 = cast::reinterpret_cast(&fp);
let sp_meta: *u32 = cast::reinterpret_cast(&sp.sp_meta);
let fp_bytes: *u8 = cast::transmute(fp);
let sp_meta: *u32 = cast::transmute(sp.sp_meta);

let num_stack_roots = *sp_meta as uint;
let num_reg_roots = *ptr::offset(sp_meta, 1) as uint;
Expand Down Expand Up @@ -171,9 +171,9 @@ unsafe fn walk_safe_point(fp: *Word, sp: SafePoint, visitor: Visitor) {

// Is fp contained in segment?
unsafe fn is_frame_in_segment(fp: *Word, segment: *StackSegment) -> bool {
let begin: Word = cast::reinterpret_cast(&segment);
let end: Word = cast::reinterpret_cast(&(*segment).end);
let frame: Word = cast::reinterpret_cast(&fp);
let begin: Word = cast::transmute(segment);
let end: Word = cast::transmute((*segment).end);
let frame: Word = cast::transmute(fp);

return begin <= frame && frame <= end;
}
Expand Down Expand Up @@ -339,7 +339,7 @@ pub fn cleanup_stack_for_failure() {
// own stack roots on the stack anyway.
let sentinel_box = ~0;
let sentinel: **Word = if expect_sentinel() {
cast::reinterpret_cast(&ptr::addr_of(&sentinel_box))
cast::transmute(ptr::addr_of(&sentinel_box))
} else {
ptr::null()
};
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ pub fn getenv(n: &str) -> Option<~str> {
unsafe {
do with_env_lock {
let s = str::as_c_str(n, |s| libc::getenv(s));
if ptr::null::<u8>() == cast::reinterpret_cast(&s) {
if ptr::null::<u8>() == cast::transmute(s) {
option::None::<~str>
} else {
let s = cast::reinterpret_cast(&s);
let s = cast::transmute(s);
option::Some::<~str>(str::raw::from_buf(s))
}
}
Expand Down Expand Up @@ -644,7 +644,7 @@ pub fn make_dir(p: &Path, mode: c_int) -> bool {
// FIXME: turn mode into something useful? #2623
do as_utf16_p(p.to_str()) |buf| {
libc::CreateDirectoryW(buf, unsafe {
cast::reinterpret_cast(&0)
cast::transmute(0)
})
!= (0 as libc::BOOL)
}
Expand Down
34 changes: 17 additions & 17 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ pub unsafe fn position<T>(buf: *T, f: &fn(&T) -> bool) -> uint {

/// Create an unsafe null pointer
#[inline(always)]
pub fn null<T>() -> *T { unsafe { cast::reinterpret_cast(&0u) } }
pub fn null<T>() -> *T { unsafe { cast::transmute(0u) } }

/// Create an unsafe mutable null pointer
#[inline(always)]
pub fn mut_null<T>() -> *mut T { unsafe { cast::reinterpret_cast(&0u) } }
pub fn mut_null<T>() -> *mut T { unsafe { cast::transmute(0u) } }

/// Returns true if the pointer is equal to the null pointer.
#[inline(always)]
Expand Down Expand Up @@ -134,7 +134,7 @@ pub unsafe fn set_memory<T>(dst: *mut T, c: int, count: uint) {
*/
#[inline(always)]
pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
unsafe { cast::reinterpret_cast(&thing) }
unsafe { cast::transmute(thing) }
}

/**
Expand All @@ -144,7 +144,7 @@ pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
*/
#[inline(always)]
pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
unsafe { cast::reinterpret_cast(&thing) }
unsafe { cast::transmute(thing) }
}

/**
Expand All @@ -154,7 +154,7 @@ pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
*/
#[inline(always)]
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
unsafe { cast::reinterpret_cast(&thing) }
unsafe { cast::transmute(thing) }
}

/**
Expand All @@ -167,7 +167,7 @@ pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
#[inline(always)]
pub fn to_uint<T>(thing: &T) -> uint {
unsafe {
cast::reinterpret_cast(&thing)
cast::transmute(thing)
}
}

Expand Down Expand Up @@ -259,8 +259,8 @@ impl<T> Eq for *const T {
#[inline(always)]
fn eq(&self, other: &*const T) -> bool {
unsafe {
let a: uint = cast::reinterpret_cast(&(*self));
let b: uint = cast::reinterpret_cast(&(*other));
let a: uint = cast::transmute(*self);
let b: uint = cast::transmute(*other);
return a == b;
}
}
Expand All @@ -274,32 +274,32 @@ impl<T> Ord for *const T {
#[inline(always)]
fn lt(&self, other: &*const T) -> bool {
unsafe {
let a: uint = cast::reinterpret_cast(&(*self));
let b: uint = cast::reinterpret_cast(&(*other));
let a: uint = cast::transmute(*self);
let b: uint = cast::transmute(*other);
return a < b;
}
}
#[inline(always)]
fn le(&self, other: &*const T) -> bool {
unsafe {
let a: uint = cast::reinterpret_cast(&(*self));
let b: uint = cast::reinterpret_cast(&(*other));
let a: uint = cast::transmute(*self);
let b: uint = cast::transmute(*other);
return a <= b;
}
}
#[inline(always)]
fn ge(&self, other: &*const T) -> bool {
unsafe {
let a: uint = cast::reinterpret_cast(&(*self));
let b: uint = cast::reinterpret_cast(&(*other));
let a: uint = cast::transmute(*self);
let b: uint = cast::transmute(*other);
return a >= b;
}
}
#[inline(always)]
fn gt(&self, other: &*const T) -> bool {
unsafe {
let a: uint = cast::reinterpret_cast(&(*self));
let b: uint = cast::reinterpret_cast(&(*other));
let a: uint = cast::transmute(*self);
let b: uint = cast::transmute(*other);
return a > b;
}
}
Expand Down Expand Up @@ -350,7 +350,7 @@ pub mod ptr_tests {
struct Pair {mut fst: int, mut snd: int};
let mut p = Pair {fst: 10, snd: 20};
let pptr: *mut Pair = &mut p;
let iptr: *mut int = cast::reinterpret_cast(&pptr);
let iptr: *mut int = cast::transmute(pptr);
assert!((*iptr == 10));;
*iptr = 30;
assert!((*iptr == 30));
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn with_envp<T>(env: &Option<~[(~str,~str)]>,
}
ptrs.push(ptr::null());
vec::as_imm_buf(ptrs, |p, _len|
unsafe { cb(::cast::reinterpret_cast(&p)) }
unsafe { cb(::cast::transmute(p)) }
)
}
_ => cb(ptr::null())
Expand All @@ -167,12 +167,12 @@ fn with_envp<T>(env: &Option<~[(~str,~str)]>,
for vec::each(*es) |e| {
let (k,v) = copy *e;
let t = fmt!("%s=%s", k, v);
let mut v : ~[u8] = ::cast::reinterpret_cast(&t);
let mut v : ~[u8] = ::cast::transmute(t);
blk += v;
::cast::forget(v);
}
blk += ~[0_u8];
vec::as_imm_buf(blk, |p, _len| cb(::cast::reinterpret_cast(&p)))
vec::as_imm_buf(blk, |p, _len| cb(::cast::transmute(p)))
}
_ => cb(ptr::null())
}
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/stackwalk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#[doc(hidden)]; // FIXME #3538

use cast::reinterpret_cast;
use cast::transmute;

pub type Word = uint;

Expand All @@ -30,16 +30,16 @@ pub fn walk_stack(visit: &fn(Frame) -> bool) {

do frame_address |frame_pointer| {
let mut frame_address: *Word = unsafe {
reinterpret_cast(&frame_pointer)
transmute(frame_pointer)
};
loop {
let fr = Frame(frame_address);

debug!("frame: %x", unsafe { reinterpret_cast(&fr.fp) });
debug!("frame: %x", unsafe { transmute(fr.fp) });
visit(fr);

unsafe {
let next_fp: **Word = reinterpret_cast(&frame_address);
let next_fp: **Word = transmute(frame_address);
frame_address = *next_fp;
if *frame_address == 0u {
debug!("encountered task_start_wrapper. ending walk");
Expand Down
18 changes: 9 additions & 9 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn push_char(s: &mut ~str, ch: char) {
reserve_at_least(&mut *s, new_len);
let off = len;
do as_buf(*s) |buf, _len| {
let buf: *mut u8 = ::cast::reinterpret_cast(&buf);
let buf: *mut u8 = ::cast::transmute(buf);
if nb == 1u {
*ptr::mut_offset(buf, off) =
code as u8;
Expand Down Expand Up @@ -2023,9 +2023,9 @@ pub fn as_bytes<T>(s: &const ~str, f: &fn(&~[u8]) -> T) -> T {
*/
pub fn as_bytes_slice<'a>(s: &'a str) -> &'a [u8] {
unsafe {
let (ptr, len): (*u8, uint) = ::cast::reinterpret_cast(&s);
let (ptr, len): (*u8, uint) = ::cast::transmute(s);
let outgoing_tuple: (*u8, uint) = (ptr, len - 1);
return ::cast::reinterpret_cast(&outgoing_tuple);
return ::cast::transmute(outgoing_tuple);
}
}

Expand Down Expand Up @@ -2067,7 +2067,7 @@ pub fn as_c_str<T>(s: &str, f: &fn(*libc::c_char) -> T) -> T {
#[inline(always)]
pub fn as_buf<T>(s: &str, f: &fn(*u8, uint) -> T) -> T {
unsafe {
let v : *(*u8,uint) = ::cast::reinterpret_cast(&ptr::addr_of(&s));
let v : *(*u8,uint) = ::cast::transmute(ptr::addr_of(&s));
let (buf,len) = *v;
f(buf, len)
}
Expand Down Expand Up @@ -2217,12 +2217,12 @@ pub mod raw {

/// Create a Rust string from a null-terminated C string
pub unsafe fn from_c_str(c_str: *libc::c_char) -> ~str {
from_buf(::cast::reinterpret_cast(&c_str))
from_buf(::cast::transmute(c_str))
}

/// Create a Rust string from a `*c_char` buffer of the given length
pub unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> ~str {
from_buf_len(::cast::reinterpret_cast(&c_str), len)
from_buf_len(::cast::transmute(c_str), len)
}

/// Converts a vector of bytes to a new owned string.
Expand All @@ -2246,7 +2246,7 @@ pub mod raw {
pub unsafe fn buf_as_slice<T>(buf: *u8, len: uint,
f: &fn(v: &str) -> T) -> T {
let v = (buf, len + 1);
assert!(is_utf8(::cast::reinterpret_cast(&v)));
assert!(is_utf8(::cast::transmute(v)));
f(::cast::transmute(v))
}

Expand Down Expand Up @@ -2294,7 +2294,7 @@ pub mod raw {
assert!((end <= n));

let tuple = (ptr::offset(sbuf, begin), end - begin + 1);
::cast::reinterpret_cast(&tuple)
::cast::transmute(tuple)
}
}

Expand All @@ -2303,7 +2303,7 @@ pub mod raw {
let new_len = s.len() + 1;
reserve_at_least(&mut *s, new_len);
do as_buf(*s) |buf, len| {
let buf: *mut u8 = ::cast::reinterpret_cast(&buf);
let buf: *mut u8 = ::cast::transmute(buf);
*ptr::mut_offset(buf, len) = b;
}
set_len(&mut *s, new_len);
Expand Down
10 changes: 5 additions & 5 deletions src/libcore/task/local_data_priv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ impl<T:Durable> LocalData for @T { }
impl Eq for @LocalData {
fn eq(&self, other: &@LocalData) -> bool {
unsafe {
let ptr_a: (uint, uint) = cast::reinterpret_cast(&(*self));
let ptr_b: (uint, uint) = cast::reinterpret_cast(other);
let ptr_a: (uint, uint) = cast::transmute(*self);
let ptr_b: (uint, uint) = cast::transmute(*other);
return ptr_a == ptr_b;
}
}
Expand All @@ -44,7 +44,7 @@ extern fn cleanup_task_local_map(map_ptr: *libc::c_void) {
assert!(!map_ptr.is_null());
// Get and keep the single reference that was created at the
// beginning.
let _map: TaskLocalMap = cast::reinterpret_cast(&map_ptr);
let _map: TaskLocalMap = cast::transmute(map_ptr);
// All local_data will be destroyed along with the map.
}
}
Expand All @@ -61,7 +61,7 @@ unsafe fn get_task_local_map(task: *rust_task) -> TaskLocalMap {
let map: TaskLocalMap = @mut ~[];
// Use reinterpret_cast -- transmute would take map away from us also.
rt::rust_set_task_local_data(
task, cast::reinterpret_cast(&map));
task, cast::transmute(map));
rt::rust_task_local_data_atexit(task, cleanup_task_local_map);
// Also need to reference it an extra time to keep it for now.
let nonmut = cast::transmute::<TaskLocalMap,
Expand Down Expand Up @@ -152,7 +152,7 @@ pub unsafe fn local_set<T:Durable>(
// own on it can be dropped when the box is destroyed. The unsafe pointer
// does not have a reference associated with it, so it may become invalid
// when the box is destroyed.
let data_ptr = cast::reinterpret_cast(&data);
let data_ptr = cast::transmute(data);
let data_box = @data as @LocalData;
// Construct new entry to store in the map.
let new_entry = Some((keyval, data_ptr, data_box));
Expand Down
Loading

1 comment on commit 51a68eb

@catamorphism
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r+ (implementing my suggestion can be a separate patch, or maybe there's a reason why it wouldn't work that I don't know about). Thanks!

Please sign in to comment.