Skip to content

Commit fb569fd

Browse files
committed
auto merge of #14069 : alexcrichton/rust/cast-module, r=brson
This commit revisits the `cast` module in libcore and libstd, and scrutinizes all functions inside of it. The result was to remove the `cast` module entirely, folding all functionality into the `mem` module. Specifically, this is the fate of each function in the `cast` module. * transmute - This function was moved to `mem`, but it is now marked as #[unstable]. This is due to planned changes to the `transmute` function and how it can be invoked (see the #[unstable] comment). For more information, see RFC 5 and #12898 * transmute_copy - This function was moved to `mem`, with clarification that is is not an error to invoke it with T/U that are different sizes, but rather that it is strongly discouraged. This function is now #[stable] * forget - This function was moved to `mem` and marked #[stable] * bump_box_refcount - This function was removed due to the deprecation of managed boxes as well as its questionable utility. * transmute_mut - This function was previously deprecated, and removed as part of this commit. * transmute_mut_unsafe - This function doesn't serve much of a purpose when it can be achieved with an `as` in safe code, so it was removed. * transmute_lifetime - This function was removed because it is likely a strong indication that code is incorrect in the first place. * transmute_mut_lifetime - This function was removed for the same reasons as `transmute_lifetime` * copy_lifetime - This function was moved to `mem`, but it is marked `#[unstable]` now due to the likelihood of being removed in the future if it is found to not be very useful. * copy_mut_lifetime - This function was also moved to `mem`, but had the same treatment as `copy_lifetime`. * copy_lifetime_vec - This function was removed because it is not used today, and its existence is not necessary with DST (copy_lifetime will suffice). In summary, the cast module was stripped down to these functions, and then the functions were moved to the `mem` module. transmute - #[unstable] transmute_copy - #[stable] forget - #[stable] copy_lifetime - #[unstable] copy_mut_lifetime - #[unstable]
2 parents adb8b0b + f94d671 commit fb569fd

File tree

137 files changed

+723
-766
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+723
-766
lines changed

src/doc/guide-unsafe.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ restrictions is undefined behaviour. For example, the following
6666
creates two aliasing `&mut` pointers, and is invalid.
6767

6868
```
69-
use std::cast;
69+
use std::mem;
7070
let mut x: u8 = 1;
7171
7272
let ref_1: &mut u8 = &mut x;
73-
let ref_2: &mut u8 = unsafe { cast::transmute_mut_lifetime(ref_1) };
73+
let ref_2: &mut u8 = unsafe { mem::transmute(&mut *ref_1) };
7474
7575
// oops, ref_1 and ref_2 point to the same piece of data (x) and are
7676
// both usable

src/libarena/lib.rs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
extern crate collections;
2828

29-
use std::cast::{transmute, transmute_mut_lifetime};
30-
use std::cast;
3129
use std::cell::{Cell, RefCell};
3230
use std::cmp;
3331
use std::intrinsics::{TyDesc, get_tydesc};
@@ -137,7 +135,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
137135
let fill = chunk.fill.get();
138136

139137
while idx < fill {
140-
let tydesc_data: *uint = transmute(buf.offset(idx as int));
138+
let tydesc_data: *uint = mem::transmute(buf.offset(idx as int));
141139
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
142140
let (size, align) = ((*tydesc).size, (*tydesc).align);
143141

@@ -187,28 +185,27 @@ impl Arena {
187185
#[inline]
188186
fn alloc_copy_inner(&mut self, n_bytes: uint, align: uint) -> *u8 {
189187
unsafe {
190-
let this = transmute_mut_lifetime(self);
191-
let start = round_up(this.copy_head.fill.get(), align);
188+
let start = round_up(self.copy_head.fill.get(), align);
192189
let end = start + n_bytes;
193190
if end > self.chunk_size() {
194-
return this.alloc_copy_grow(n_bytes, align);
191+
return self.alloc_copy_grow(n_bytes, align);
195192
}
196-
this.copy_head.fill.set(end);
193+
self.copy_head.fill.set(end);
197194

198195
//debug!("idx = {}, size = {}, align = {}, fill = {}",
199196
// start, n_bytes, align, head.fill.get());
200197

201-
this.copy_head.as_ptr().offset(start as int)
198+
self.copy_head.as_ptr().offset(start as int)
202199
}
203200
}
204201

205202
#[inline]
206203
fn alloc_copy<'a, T>(&'a mut self, op: || -> T) -> &'a T {
207204
unsafe {
208205
let ptr = self.alloc_copy_inner(mem::size_of::<T>(), min_align_of::<T>());
209-
let ptr: *mut T = transmute(ptr);
206+
let ptr = ptr as *mut T;
210207
mem::move_val_init(&mut (*ptr), op());
211-
return transmute(ptr);
208+
return &*ptr;
212209
}
213210
}
214211

@@ -228,26 +225,16 @@ impl Arena {
228225
fn alloc_noncopy_inner(&mut self, n_bytes: uint, align: uint)
229226
-> (*u8, *u8) {
230227
unsafe {
231-
let start;
232-
let end;
233-
let tydesc_start;
234-
let after_tydesc;
235-
236-
{
237-
let head = transmute_mut_lifetime(&mut self.head);
238-
239-
tydesc_start = head.fill.get();
240-
after_tydesc = head.fill.get() + mem::size_of::<*TyDesc>();
241-
start = round_up(after_tydesc, align);
242-
end = start + n_bytes;
243-
}
228+
let tydesc_start = self.head.fill.get();
229+
let after_tydesc = self.head.fill.get() + mem::size_of::<*TyDesc>();
230+
let start = round_up(after_tydesc, align);
231+
let end = start + n_bytes;
244232

245233
if end > self.head.capacity() {
246234
return self.alloc_noncopy_grow(n_bytes, align);
247235
}
248236

249-
let head = transmute_mut_lifetime(&mut self.head);
250-
head.fill.set(round_up(end, mem::pref_align_of::<*TyDesc>()));
237+
self.head.fill.set(round_up(end, mem::pref_align_of::<*TyDesc>()));
251238

252239
//debug!("idx = {}, size = {}, align = {}, fill = {}",
253240
// start, n_bytes, align, head.fill);
@@ -263,18 +250,18 @@ impl Arena {
263250
let tydesc = get_tydesc::<T>();
264251
let (ty_ptr, ptr) =
265252
self.alloc_noncopy_inner(mem::size_of::<T>(), min_align_of::<T>());
266-
let ty_ptr: *mut uint = transmute(ty_ptr);
267-
let ptr: *mut T = transmute(ptr);
253+
let ty_ptr = ty_ptr as *mut uint;
254+
let ptr = ptr as *mut T;
268255
// Write in our tydesc along with a bit indicating that it
269256
// has *not* been initialized yet.
270-
*ty_ptr = transmute(tydesc);
257+
*ty_ptr = mem::transmute(tydesc);
271258
// Actually initialize it
272259
mem::move_val_init(&mut(*ptr), op());
273260
// Now that we are done, update the tydesc to indicate that
274261
// the object is there.
275262
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
276263

277-
return transmute(ptr);
264+
return &*ptr;
278265
}
279266
}
280267

@@ -283,7 +270,7 @@ impl Arena {
283270
pub fn alloc<'a, T>(&'a self, op: || -> T) -> &'a T {
284271
unsafe {
285272
// FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes
286-
let this: &mut Arena = transmute::<&_, &mut _>(self);
273+
let this: &mut Arena = mem::transmute::<&_, &mut _>(self);
287274
if intrinsics::needs_drop::<T>() {
288275
this.alloc_noncopy(op)
289276
} else {
@@ -366,7 +353,7 @@ impl<T> TypedArenaChunk<T> {
366353

367354
let mut chunk = unsafe {
368355
let chunk = exchange_malloc(size);
369-
let mut chunk: Box<TypedArenaChunk<T>> = cast::transmute(chunk);
356+
let mut chunk: Box<TypedArenaChunk<T>> = mem::transmute(chunk);
370357
mem::move_val_init(&mut chunk.next, next);
371358
chunk
372359
};
@@ -387,7 +374,7 @@ impl<T> TypedArenaChunk<T> {
387374

388375
let mut chunk = unsafe {
389376
let chunk = exchange_malloc(size, min_align_of::<TypedArenaChunk<T>>());
390-
let mut chunk: Box<TypedArenaChunk<T>> = cast::transmute(chunk);
377+
let mut chunk: Box<TypedArenaChunk<T>> = mem::transmute(chunk);
391378
mem::move_val_init(&mut chunk.next, next);
392379
chunk
393380
};
@@ -425,7 +412,7 @@ impl<T> TypedArenaChunk<T> {
425412
fn start(&self) -> *u8 {
426413
let this: *TypedArenaChunk<T> = self;
427414
unsafe {
428-
cast::transmute(round_up(this.offset(1) as uint, min_align_of::<T>()))
415+
mem::transmute(round_up(this.offset(1) as uint, min_align_of::<T>()))
429416
}
430417
}
431418

@@ -463,12 +450,12 @@ impl<T> TypedArena<T> {
463450
pub fn alloc<'a>(&'a self, object: T) -> &'a T {
464451
unsafe {
465452
// FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes
466-
let this: &mut TypedArena<T> = cast::transmute::<&_, &mut _>(self);
453+
let this: &mut TypedArena<T> = mem::transmute::<&_, &mut _>(self);
467454
if this.ptr == this.end {
468455
this.grow()
469456
}
470457

471-
let ptr: &'a mut T = cast::transmute(this.ptr);
458+
let ptr: &'a mut T = mem::transmute(this.ptr);
472459
mem::move_val_init(ptr, object);
473460
this.ptr = this.ptr.offset(1);
474461
let ptr: &'a T = ptr;

src/libcollections/dlist.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
// Backlinks over DList::prev are raw pointers that form a full chain in
2222
// the reverse direction.
2323

24-
use std::cast;
2524
use std::iter::Rev;
2625
use std::iter;
27-
use std::mem::{replace, swap};
26+
use std::mem;
2827
use std::ptr;
2928

3029
use deque::Deque;
@@ -93,13 +92,13 @@ impl<T> Rawlink<T> {
9392
if self.p.is_null() {
9493
None
9594
} else {
96-
Some(unsafe { cast::transmute(self.p) })
95+
Some(unsafe { mem::transmute(self.p) })
9796
}
9897
}
9998

10099
/// Return the `Rawlink` and replace with `Rawlink::none()`
101100
fn take(&mut self) -> Rawlink<T> {
102-
replace(self, Rawlink::none())
101+
mem::replace(self, Rawlink::none())
103102
}
104103
}
105104

@@ -159,7 +158,7 @@ impl<T> DList<T> {
159158
Some(ref mut head) => {
160159
new_head.prev = Rawlink::none();
161160
head.prev = Rawlink::some(new_head);
162-
swap(head, &mut new_head);
161+
mem::swap(head, &mut new_head);
163162
head.next = Some(new_head);
164163
}
165164
}
@@ -317,7 +316,7 @@ impl<T> DList<T> {
317316
/// O(1)
318317
#[inline]
319318
pub fn prepend(&mut self, mut other: DList<T>) {
320-
swap(self, &mut other);
319+
mem::swap(self, &mut other);
321320
self.append(other);
322321
}
323322

src/libcollections/enum_set.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<E:CLike> Iterator<E> for Items<E> {
137137
#[cfg(test)]
138138
mod test {
139139

140-
use std::cast;
140+
use std::mem;
141141

142142
use enum_set::{EnumSet, CLike};
143143

@@ -153,7 +153,7 @@ mod test {
153153
}
154154

155155
fn from_uint(v: uint) -> Foo {
156-
unsafe { cast::transmute(v) }
156+
unsafe { mem::transmute(v) }
157157
}
158158
}
159159

src/libcollections/lru_cache.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
//! assert!(cache.get(&2).is_none());
3838
//! ```
3939
40-
use std::cast;
4140
use std::container::Container;
4241
use std::hash::Hash;
4342
use std::fmt;
@@ -93,7 +92,7 @@ impl<K: Hash + TotalEq, V> LruCache<K, V> {
9392
let cache = LruCache {
9493
map: HashMap::new(),
9594
max_size: capacity,
96-
head: unsafe{ cast::transmute(box mem::uninit::<LruEntry<K, V>>()) },
95+
head: unsafe{ mem::transmute(box mem::uninit::<LruEntry<K, V>>()) },
9796
};
9897
unsafe {
9998
(*cache.head).next = cache.head;
@@ -241,11 +240,11 @@ impl<K: Hash + TotalEq, V> Mutable for LruCache<K, V> {
241240
impl<K, V> Drop for LruCache<K, V> {
242241
fn drop(&mut self) {
243242
unsafe {
244-
let node: Box<LruEntry<K, V>> = cast::transmute(self.head);
243+
let node: Box<LruEntry<K, V>> = mem::transmute(self.head);
245244
// Prevent compiler from trying to drop the un-initialized field in the sigil node.
246245
let box LruEntry { key: k, value: v, .. } = node;
247-
cast::forget(k);
248-
cast::forget(v);
246+
mem::forget(k);
247+
mem::forget(v);
249248
}
250249
}
251250
}

src/libcore/any.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! value. `Box<Any>` adds the `move` method, which will unwrap a `Box<T>` from the object. See
2121
//! the extension traits (`*Ext`) for the full details.
2222
23-
use cast::{transmute, transmute_copy};
23+
use mem::{transmute, transmute_copy};
2424
use option::{Option, Some, None};
2525
use owned::Box;
2626
use raw::TraitObject;

0 commit comments

Comments
 (0)