Skip to content

Commit 51a68eb

Browse files
committed
Replaced many instances of reinterpret_cast with transmute
1 parent f2b0ef1 commit 51a68eb

File tree

21 files changed

+107
-109
lines changed

21 files changed

+107
-109
lines changed

Diff for: src/libcore/at_vec.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub mod rustrt {
4141
pub fn capacity<T>(v: @[T]) -> uint {
4242
unsafe {
4343
let repr: **raw::VecRepr =
44-
::cast::reinterpret_cast(&addr_of(&v));
44+
::cast::transmute(addr_of(&v));
4545
(**repr).unboxed.alloc / sys::size_of::<T>()
4646
}
4747
}
@@ -208,7 +208,7 @@ pub mod raw {
208208
*/
209209
#[inline(always)]
210210
pub unsafe fn set_len<T>(v: @[T], new_len: uint) {
211-
let repr: **mut VecRepr = ::cast::reinterpret_cast(&addr_of(&v));
211+
let repr: **mut VecRepr = ::cast::transmute(addr_of(&v));
212212
(**repr).unboxed.fill = new_len * sys::size_of::<T>();
213213
}
214214

@@ -226,7 +226,7 @@ pub mod raw {
226226

227227
#[inline(always)] // really pretty please
228228
pub unsafe fn push_fast<T>(v: &mut @[T], initval: T) {
229-
let repr: **mut VecRepr = ::cast::reinterpret_cast(&v);
229+
let repr: **mut VecRepr = ::cast::transmute(v);
230230
let fill = (**repr).unboxed.fill;
231231
(**repr).unboxed.fill += sys::size_of::<T>();
232232
let p = addr_of(&((**repr).unboxed.data));
@@ -322,4 +322,4 @@ mod test {
322322
assert!(from_slice([@"abc", @"123"]) == @[@"abc", @"123"]);
323323
assert!(from_slice([@[42]]) == @[@[42]]);
324324
}
325-
}
325+
}

Diff for: src/libcore/gc.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ pub mod rustrt {
7777
}
7878

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

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

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

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

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

178178
return begin <= frame && frame <= end;
179179
}
@@ -339,7 +339,7 @@ pub fn cleanup_stack_for_failure() {
339339
// own stack roots on the stack anyway.
340340
let sentinel_box = ~0;
341341
let sentinel: **Word = if expect_sentinel() {
342-
cast::reinterpret_cast(&ptr::addr_of(&sentinel_box))
342+
cast::transmute(ptr::addr_of(&sentinel_box))
343343
} else {
344344
ptr::null()
345345
};

Diff for: src/libcore/os.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ pub fn getenv(n: &str) -> Option<~str> {
239239
unsafe {
240240
do with_env_lock {
241241
let s = str::as_c_str(n, |s| libc::getenv(s));
242-
if ptr::null::<u8>() == cast::reinterpret_cast(&s) {
242+
if ptr::null::<u8>() == cast::transmute(s) {
243243
option::None::<~str>
244244
} else {
245-
let s = cast::reinterpret_cast(&s);
245+
let s = cast::transmute(s);
246246
option::Some::<~str>(str::raw::from_buf(s))
247247
}
248248
}
@@ -644,7 +644,7 @@ pub fn make_dir(p: &Path, mode: c_int) -> bool {
644644
// FIXME: turn mode into something useful? #2623
645645
do as_utf16_p(p.to_str()) |buf| {
646646
libc::CreateDirectoryW(buf, unsafe {
647-
cast::reinterpret_cast(&0)
647+
cast::transmute(0)
648648
})
649649
!= (0 as libc::BOOL)
650650
}

Diff for: src/libcore/ptr.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ pub unsafe fn position<T>(buf: *T, f: &fn(&T) -> bool) -> uint {
8686

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

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

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

140140
/**
@@ -144,7 +144,7 @@ pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
144144
*/
145145
#[inline(always)]
146146
pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
147-
unsafe { cast::reinterpret_cast(&thing) }
147+
unsafe { cast::transmute(thing) }
148148
}
149149

150150
/**
@@ -154,7 +154,7 @@ pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
154154
*/
155155
#[inline(always)]
156156
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
157-
unsafe { cast::reinterpret_cast(&thing) }
157+
unsafe { cast::transmute(thing) }
158158
}
159159

160160
/**
@@ -167,7 +167,7 @@ pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
167167
#[inline(always)]
168168
pub fn to_uint<T>(thing: &T) -> uint {
169169
unsafe {
170-
cast::reinterpret_cast(&thing)
170+
cast::transmute(thing)
171171
}
172172
}
173173

@@ -259,8 +259,8 @@ impl<T> Eq for *const T {
259259
#[inline(always)]
260260
fn eq(&self, other: &*const T) -> bool {
261261
unsafe {
262-
let a: uint = cast::reinterpret_cast(&(*self));
263-
let b: uint = cast::reinterpret_cast(&(*other));
262+
let a: uint = cast::transmute(*self);
263+
let b: uint = cast::transmute(*other);
264264
return a == b;
265265
}
266266
}
@@ -274,32 +274,32 @@ impl<T> Ord for *const T {
274274
#[inline(always)]
275275
fn lt(&self, other: &*const T) -> bool {
276276
unsafe {
277-
let a: uint = cast::reinterpret_cast(&(*self));
278-
let b: uint = cast::reinterpret_cast(&(*other));
277+
let a: uint = cast::transmute(*self);
278+
let b: uint = cast::transmute(*other);
279279
return a < b;
280280
}
281281
}
282282
#[inline(always)]
283283
fn le(&self, other: &*const T) -> bool {
284284
unsafe {
285-
let a: uint = cast::reinterpret_cast(&(*self));
286-
let b: uint = cast::reinterpret_cast(&(*other));
285+
let a: uint = cast::transmute(*self);
286+
let b: uint = cast::transmute(*other);
287287
return a <= b;
288288
}
289289
}
290290
#[inline(always)]
291291
fn ge(&self, other: &*const T) -> bool {
292292
unsafe {
293-
let a: uint = cast::reinterpret_cast(&(*self));
294-
let b: uint = cast::reinterpret_cast(&(*other));
293+
let a: uint = cast::transmute(*self);
294+
let b: uint = cast::transmute(*other);
295295
return a >= b;
296296
}
297297
}
298298
#[inline(always)]
299299
fn gt(&self, other: &*const T) -> bool {
300300
unsafe {
301-
let a: uint = cast::reinterpret_cast(&(*self));
302-
let b: uint = cast::reinterpret_cast(&(*other));
301+
let a: uint = cast::transmute(*self);
302+
let b: uint = cast::transmute(*other);
303303
return a > b;
304304
}
305305
}
@@ -350,7 +350,7 @@ pub mod ptr_tests {
350350
struct Pair {mut fst: int, mut snd: int};
351351
let mut p = Pair {fst: 10, snd: 20};
352352
let pptr: *mut Pair = &mut p;
353-
let iptr: *mut int = cast::reinterpret_cast(&pptr);
353+
let iptr: *mut int = cast::transmute(pptr);
354354
assert!((*iptr == 10));;
355355
*iptr = 30;
356356
assert!((*iptr == 30));

Diff for: src/libcore/run.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn with_envp<T>(env: &Option<~[(~str,~str)]>,
147147
}
148148
ptrs.push(ptr::null());
149149
vec::as_imm_buf(ptrs, |p, _len|
150-
unsafe { cb(::cast::reinterpret_cast(&p)) }
150+
unsafe { cb(::cast::transmute(p)) }
151151
)
152152
}
153153
_ => cb(ptr::null())
@@ -167,12 +167,12 @@ fn with_envp<T>(env: &Option<~[(~str,~str)]>,
167167
for vec::each(*es) |e| {
168168
let (k,v) = copy *e;
169169
let t = fmt!("%s=%s", k, v);
170-
let mut v : ~[u8] = ::cast::reinterpret_cast(&t);
170+
let mut v : ~[u8] = ::cast::transmute(t);
171171
blk += v;
172172
::cast::forget(v);
173173
}
174174
blk += ~[0_u8];
175-
vec::as_imm_buf(blk, |p, _len| cb(::cast::reinterpret_cast(&p)))
175+
vec::as_imm_buf(blk, |p, _len| cb(::cast::transmute(p)))
176176
}
177177
_ => cb(ptr::null())
178178
}

Diff for: src/libcore/stackwalk.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

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

13-
use cast::reinterpret_cast;
13+
use cast::transmute;
1414

1515
pub type Word = uint;
1616

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

3131
do frame_address |frame_pointer| {
3232
let mut frame_address: *Word = unsafe {
33-
reinterpret_cast(&frame_pointer)
33+
transmute(frame_pointer)
3434
};
3535
loop {
3636
let fr = Frame(frame_address);
3737

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

4141
unsafe {
42-
let next_fp: **Word = reinterpret_cast(&frame_address);
42+
let next_fp: **Word = transmute(frame_address);
4343
frame_address = *next_fp;
4444
if *frame_address == 0u {
4545
debug!("encountered task_start_wrapper. ending walk");

Diff for: src/libcore/str.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub fn push_char(s: &mut ~str, ch: char) {
121121
reserve_at_least(&mut *s, new_len);
122122
let off = len;
123123
do as_buf(*s) |buf, _len| {
124-
let buf: *mut u8 = ::cast::reinterpret_cast(&buf);
124+
let buf: *mut u8 = ::cast::transmute(buf);
125125
if nb == 1u {
126126
*ptr::mut_offset(buf, off) =
127127
code as u8;
@@ -2023,9 +2023,9 @@ pub fn as_bytes<T>(s: &const ~str, f: &fn(&~[u8]) -> T) -> T {
20232023
*/
20242024
pub fn as_bytes_slice<'a>(s: &'a str) -> &'a [u8] {
20252025
unsafe {
2026-
let (ptr, len): (*u8, uint) = ::cast::reinterpret_cast(&s);
2026+
let (ptr, len): (*u8, uint) = ::cast::transmute(s);
20272027
let outgoing_tuple: (*u8, uint) = (ptr, len - 1);
2028-
return ::cast::reinterpret_cast(&outgoing_tuple);
2028+
return ::cast::transmute(outgoing_tuple);
20292029
}
20302030
}
20312031

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

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

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

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

@@ -2294,7 +2294,7 @@ pub mod raw {
22942294
assert!((end <= n));
22952295

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

@@ -2303,7 +2303,7 @@ pub mod raw {
23032303
let new_len = s.len() + 1;
23042304
reserve_at_least(&mut *s, new_len);
23052305
do as_buf(*s) |buf, len| {
2306-
let buf: *mut u8 = ::cast::reinterpret_cast(&buf);
2306+
let buf: *mut u8 = ::cast::transmute(buf);
23072307
*ptr::mut_offset(buf, len) = b;
23082308
}
23092309
set_len(&mut *s, new_len);

Diff for: src/libcore/task/local_data_priv.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ impl<T:Durable> LocalData for @T { }
2525
impl Eq for @LocalData {
2626
fn eq(&self, other: &@LocalData) -> bool {
2727
unsafe {
28-
let ptr_a: (uint, uint) = cast::reinterpret_cast(&(*self));
29-
let ptr_b: (uint, uint) = cast::reinterpret_cast(other);
28+
let ptr_a: (uint, uint) = cast::transmute(*self);
29+
let ptr_b: (uint, uint) = cast::transmute(*other);
3030
return ptr_a == ptr_b;
3131
}
3232
}
@@ -44,7 +44,7 @@ extern fn cleanup_task_local_map(map_ptr: *libc::c_void) {
4444
assert!(!map_ptr.is_null());
4545
// Get and keep the single reference that was created at the
4646
// beginning.
47-
let _map: TaskLocalMap = cast::reinterpret_cast(&map_ptr);
47+
let _map: TaskLocalMap = cast::transmute(map_ptr);
4848
// All local_data will be destroyed along with the map.
4949
}
5050
}
@@ -61,7 +61,7 @@ unsafe fn get_task_local_map(task: *rust_task) -> TaskLocalMap {
6161
let map: TaskLocalMap = @mut ~[];
6262
// Use reinterpret_cast -- transmute would take map away from us also.
6363
rt::rust_set_task_local_data(
64-
task, cast::reinterpret_cast(&map));
64+
task, cast::transmute(map));
6565
rt::rust_task_local_data_atexit(task, cleanup_task_local_map);
6666
// Also need to reference it an extra time to keep it for now.
6767
let nonmut = cast::transmute::<TaskLocalMap,
@@ -152,7 +152,7 @@ pub unsafe fn local_set<T:Durable>(
152152
// own on it can be dropped when the box is destroyed. The unsafe pointer
153153
// does not have a reference associated with it, so it may become invalid
154154
// when the box is destroyed.
155-
let data_ptr = cast::reinterpret_cast(&data);
155+
let data_ptr = cast::transmute(data);
156156
let data_box = @data as @LocalData;
157157
// Construct new entry to store in the map.
158158
let new_entry = Some((keyval, data_ptr, data_box));

0 commit comments

Comments
 (0)