File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change 1
1
#[ doc = "Unsafe operations" ] ;
2
2
3
- export reinterpret_cast, forget;
3
+ export reinterpret_cast, forget, transmute ;
4
4
5
5
#[ abi = "rust-intrinsic" ]
6
6
native mod rusti {
@@ -27,11 +27,38 @@ reinterpret_cast on managed pointer types.
27
27
#[ inline( always) ]
28
28
unsafe fn forget < T > ( -thing : T ) { rusti:: forget ( thing) ; }
29
29
30
+ #[ doc = "
31
+ Transform a value of one type into a value of another type.
32
+ Both types must have the same size and alignment.
33
+
34
+ # Example
35
+
36
+ assert transmute(\" L\" ) == [76u8, 0u8];
37
+ " ]
38
+ unsafe fn transmute < L , G > ( -thing : L ) -> G {
39
+ let newthing = reinterpret_cast ( thing) ;
40
+ forget ( thing) ;
41
+ ret newthing;
42
+ }
43
+
30
44
#[ cfg( test) ]
31
45
mod tests {
32
46
33
47
#[ test]
34
48
fn test_reinterpret_cast ( ) unsafe {
35
49
assert reinterpret_cast ( 1 ) == 1 u;
36
50
}
51
+
52
+ #[ test]
53
+ fn test_transmute ( ) unsafe {
54
+ let x = @1 ;
55
+ let x: * int = transmute ( x) ;
56
+ assert * x == 1 ;
57
+ let _x: @int = transmute ( x) ;
58
+ }
59
+
60
+ #[ test]
61
+ fn test_transmute2 ( ) unsafe {
62
+ assert transmute ( "L" ) == [ 76u8 , 0u8 ] ;
63
+ }
37
64
}
You can’t perform that action at this time.
0 commit comments