Skip to content

Commit 1232492

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 11571cd + b39baeb commit 1232492

File tree

137 files changed

+679
-735
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

+679
-735
lines changed

src/doc/guide-unsafe.md

+2-2
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

+23-35
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};
@@ -136,7 +134,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
136134
let fill = chunk.fill.get();
137135

138136
while idx < fill {
139-
let tydesc_data: *uint = transmute(buf.offset(idx as int));
137+
let tydesc_data: *uint = mem::transmute(buf.offset(idx as int));
140138
let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
141139
let (size, align) = ((*tydesc).size, (*tydesc).align);
142140

@@ -186,28 +184,28 @@ impl Arena {
186184
#[inline]
187185
fn alloc_copy_inner(&mut self, n_bytes: uint, align: uint) -> *u8 {
188186
unsafe {
189-
let this = transmute_mut_lifetime(self);
190-
let start = round_up(this.copy_head.fill.get(), align);
187+
let start = round_up(self.copy_head.fill.get(), align);
191188
let end = start + n_bytes;
192189
if end > self.chunk_size() {
193-
return this.alloc_copy_grow(n_bytes, align);
190+
return self.alloc_copy_grow(n_bytes, align);
194191
}
195-
this.copy_head.fill.set(end);
192+
self.copy_head.fill.set(end);
196193

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

200-
this.copy_head.as_ptr().offset(start as int)
197+
self.copy_head.as_ptr().offset(start as int)
201198
}
202199
}
203200

204201
#[inline]
205202
fn alloc_copy<'a, T>(&'a mut self, op: || -> T) -> &'a T {
206203
unsafe {
207-
let ptr = self.alloc_copy_inner(mem::size_of::<T>(), mem::min_align_of::<T>());
208-
let ptr: *mut T = transmute(ptr);
204+
let ptr = self.alloc_copy_inner(mem::size_of::<T>(),
205+
mem::min_align_of::<T>());
206+
let ptr = ptr as *mut T;
209207
mem::move_val_init(&mut (*ptr), op());
210-
return transmute(ptr);
208+
return &*ptr;
211209
}
212210
}
213211

@@ -227,26 +225,16 @@ impl Arena {
227225
fn alloc_noncopy_inner(&mut self, n_bytes: uint, align: uint)
228226
-> (*u8, *u8) {
229227
unsafe {
230-
let start;
231-
let end;
232-
let tydesc_start;
233-
let after_tydesc;
234-
235-
{
236-
let head = transmute_mut_lifetime(&mut self.head);
237-
238-
tydesc_start = head.fill.get();
239-
after_tydesc = head.fill.get() + mem::size_of::<*TyDesc>();
240-
start = round_up(after_tydesc, align);
241-
end = start + n_bytes;
242-
}
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;
243232

244233
if end > self.head.capacity() {
245234
return self.alloc_noncopy_grow(n_bytes, align);
246235
}
247236

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

251239
//debug!("idx = {}, size = {}, align = {}, fill = {}",
252240
// start, n_bytes, align, head.fill);
@@ -262,18 +250,18 @@ impl Arena {
262250
let tydesc = get_tydesc::<T>();
263251
let (ty_ptr, ptr) =
264252
self.alloc_noncopy_inner(mem::size_of::<T>(), mem::min_align_of::<T>());
265-
let ty_ptr: *mut uint = transmute(ty_ptr);
266-
let ptr: *mut T = transmute(ptr);
253+
let ty_ptr = ty_ptr as *mut uint;
254+
let ptr = ptr as *mut T;
267255
// Write in our tydesc along with a bit indicating that it
268256
// has *not* been initialized yet.
269-
*ty_ptr = transmute(tydesc);
257+
*ty_ptr = mem::transmute(tydesc);
270258
// Actually initialize it
271259
mem::move_val_init(&mut(*ptr), op());
272260
// Now that we are done, update the tydesc to indicate that
273261
// the object is there.
274262
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
275263

276-
return transmute(ptr);
264+
return &*ptr;
277265
}
278266
}
279267

@@ -282,7 +270,7 @@ impl Arena {
282270
pub fn alloc<'a, T>(&'a self, op: || -> T) -> &'a T {
283271
unsafe {
284272
// FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes
285-
let this: &mut Arena = transmute::<&_, &mut _>(self);
273+
let this: &mut Arena = mem::transmute::<&_, &mut _>(self);
286274
if intrinsics::needs_drop::<T>() {
287275
this.alloc_noncopy(op)
288276
} else {
@@ -364,7 +352,7 @@ impl<T> TypedArenaChunk<T> {
364352

365353
let mut chunk = unsafe {
366354
let chunk = global_heap::exchange_malloc(size);
367-
let mut chunk: Box<TypedArenaChunk<T>> = cast::transmute(chunk);
355+
let mut chunk: Box<TypedArenaChunk<T>> = mem::transmute(chunk);
368356
mem::move_val_init(&mut chunk.next, next);
369357
chunk
370358
};
@@ -402,7 +390,7 @@ impl<T> TypedArenaChunk<T> {
402390
fn start(&self) -> *u8 {
403391
let this: *TypedArenaChunk<T> = self;
404392
unsafe {
405-
cast::transmute(round_up(this.offset(1) as uint, mem::min_align_of::<T>()))
393+
mem::transmute(round_up(this.offset(1) as uint, mem::min_align_of::<T>()))
406394
}
407395
}
408396

@@ -440,12 +428,12 @@ impl<T> TypedArena<T> {
440428
pub fn alloc<'a>(&'a self, object: T) -> &'a T {
441429
unsafe {
442430
// FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes
443-
let this: &mut TypedArena<T> = cast::transmute::<&_, &mut _>(self);
431+
let this: &mut TypedArena<T> = mem::transmute::<&_, &mut _>(self);
444432
if this.ptr == this.end {
445433
this.grow()
446434
}
447435

448-
let ptr: &'a mut T = cast::transmute(this.ptr);
436+
let ptr: &'a mut T = mem::transmute(this.ptr);
449437
mem::move_val_init(ptr, object);
450438
this.ptr = this.ptr.offset(1);
451439
let ptr: &'a T = ptr;

src/libcollections/dlist.rs

+5-6
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

+2-2
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

+4-5
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

+1-1
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;

src/libcore/cast.rs

-127
This file was deleted.

0 commit comments

Comments
 (0)