@@ -14,32 +14,6 @@ use mem;
1414use intrinsics;
1515use ptr:: copy_nonoverlapping_memory;
1616
17- /// Casts the value at `src` to U. The two types must have the same length.
18- #[ inline]
19- pub unsafe fn transmute_copy < T , U > ( src : & T ) -> U {
20- let mut dest: U = mem:: uninit ( ) ;
21- let dest_ptr: * mut u8 = transmute ( & mut dest) ;
22- let src_ptr: * u8 = transmute ( src) ;
23- copy_nonoverlapping_memory ( dest_ptr, src_ptr, mem:: size_of :: < U > ( ) ) ;
24- dest
25- }
26-
27- /**
28- * Move a thing into the void
29- *
30- * The forget function will take ownership of the provided value but neglect
31- * to run any required cleanup or memory-management operations on it.
32- */
33- #[ inline]
34- pub unsafe fn forget < T > ( thing : T ) { intrinsics:: forget ( thing) ; }
35-
36- /**
37- * Force-increment the reference count on a shared box. If used
38- * carelessly, this can leak the box.
39- */
40- #[ inline]
41- pub unsafe fn bump_box_refcount < T > ( t : @T ) { forget ( t) ; }
42-
4317/**
4418 * Transform a value of one type into a value of another type.
4519 * Both types must have the same size and alignment.
@@ -54,10 +28,29 @@ pub unsafe fn bump_box_refcount<T>(t: @T) { forget(t); }
5428 * ```
5529 */
5630#[ inline]
57- pub unsafe fn transmute < L , G > ( thing : L ) -> G {
31+ pub unsafe fn transmute < T , U > ( thing : T ) -> U {
5832 intrinsics:: transmute ( thing)
5933}
6034
35+ /**
36+ * Move a thing into the void
37+ *
38+ * The forget function will take ownership of the provided value but neglect
39+ * to run any required cleanup or memory-management operations on it.
40+ */
41+ #[ inline]
42+ pub unsafe fn forget < T > ( thing : T ) { intrinsics:: forget ( thing) ; }
43+
44+ /// Casts the value at `src` to U. The two types must have the same length.
45+ #[ inline]
46+ pub unsafe fn transmute_copy < T , U > ( src : & T ) -> U {
47+ let mut dest: U = mem:: uninit ( ) ;
48+ let dest_ptr: * mut u8 = transmute ( & mut dest) ;
49+ let src_ptr: * u8 = transmute ( src) ;
50+ copy_nonoverlapping_memory ( dest_ptr, src_ptr, mem:: size_of :: < U > ( ) ) ;
51+ dest
52+ }
53+
6154/// Coerce an immutable reference to be mutable.
6255#[ inline]
6356#[ deprecated="casting &T to &mut T is undefined behaviour: use Cell<T>, RefCell<T> or Unsafe<T>" ]
@@ -106,7 +99,7 @@ pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
10699
107100#[ cfg( test) ]
108101mod tests {
109- use cast:: { bump_box_refcount , transmute} ;
102+ use cast:: transmute;
110103 use raw;
111104 use realstd:: str:: StrAllocating ;
112105
@@ -115,21 +108,6 @@ mod tests {
115108 assert_eq ! ( 1 u, unsafe { :: cast:: transmute_copy( & 1 ) } ) ;
116109 }
117110
118- #[ test]
119- fn test_bump_managed_refcount ( ) {
120- unsafe {
121- let managed = @"box box box". to_owned ( ) ; // refcount 1
122- bump_box_refcount ( managed) ; // refcount 2
123- let ptr: * int = transmute ( managed) ; // refcount 2
124- let _box1: @~str = :: cast:: transmute_copy ( & ptr) ;
125- let _box2: @~str = :: cast:: transmute_copy ( & ptr) ;
126- assert ! ( * _box1 == "box box box" . to_owned( ) ) ;
127- assert ! ( * _box2 == "box box box" . to_owned( ) ) ;
128- // Will destroy _box1 and _box2. Without the bump, this would
129- // use-after-free. With too many bumps, it would leak.
130- }
131- }
132-
133111 #[ test]
134112 fn test_transmute ( ) {
135113 unsafe {
0 commit comments