@@ -14,32 +14,6 @@ use mem;
14
14
use intrinsics;
15
15
use ptr:: copy_nonoverlapping_memory;
16
16
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
-
43
17
/**
44
18
* Transform a value of one type into a value of another type.
45
19
* Both types must have the same size and alignment.
@@ -54,10 +28,29 @@ pub unsafe fn bump_box_refcount<T>(t: @T) { forget(t); }
54
28
* ```
55
29
*/
56
30
#[ inline]
57
- pub unsafe fn transmute < L , G > ( thing : L ) -> G {
31
+ pub unsafe fn transmute < T , U > ( thing : T ) -> U {
58
32
intrinsics:: transmute ( thing)
59
33
}
60
34
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
+
61
54
/// Coerce an immutable reference to be mutable.
62
55
#[ inline]
63
56
#[ 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 {
106
99
107
100
#[ cfg( test) ]
108
101
mod tests {
109
- use cast:: { bump_box_refcount , transmute} ;
102
+ use cast:: transmute;
110
103
use raw;
111
104
use realstd:: str:: StrAllocating ;
112
105
@@ -115,21 +108,6 @@ mod tests {
115
108
assert_eq ! ( 1 u, unsafe { :: cast:: transmute_copy( & 1 ) } ) ;
116
109
}
117
110
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
-
133
111
#[ test]
134
112
fn test_transmute ( ) {
135
113
unsafe {
0 commit comments