1
1
//! Unsafe operations
2
2
3
- export reinterpret_cast, forget, bump_box_refcount, transmute;
4
- export transmute_mut, transmute_immut, transmute_region, transmute_mut_region;
5
- export transmute_mut_unsafe, transmute_immut_unsafe;
6
-
7
- export copy_lifetime, copy_lifetime_vec;
8
-
9
3
#[ abi = "rust-intrinsic" ]
10
4
extern mod rusti {
11
5
#[ legacy_exports] ;
@@ -15,7 +9,7 @@ extern mod rusti {
15
9
16
10
/// Casts the value at `src` to U. The two types must have the same length.
17
11
#[ inline( always) ]
18
- unsafe fn reinterpret_cast < T , U > ( src : & T ) -> U {
12
+ pub unsafe fn reinterpret_cast < T , U > ( src : & T ) -> U {
19
13
rusti:: reinterpret_cast ( * src)
20
14
}
21
15
@@ -28,15 +22,15 @@ unsafe fn reinterpret_cast<T, U>(src: &T) -> U {
28
22
* reinterpret_cast on managed pointer types.
29
23
*/
30
24
#[ inline( always) ]
31
- unsafe fn forget < T > ( -thing : T ) { rusti:: forget ( move thing) ; }
25
+ pub unsafe fn forget < T > ( -thing : T ) { rusti:: forget ( move thing) ; }
32
26
33
27
/**
34
28
* Force-increment the reference count on a shared box. If used
35
29
* carelessly, this can leak the box. Use this in conjunction with transmute
36
30
* and/or reinterpret_cast when such calls would otherwise scramble a box's
37
31
* reference count
38
32
*/
39
- unsafe fn bump_box_refcount < T > ( +t : @T ) { forget ( move t) ; }
33
+ pub unsafe fn bump_box_refcount < T > ( +t : @T ) { forget ( move t) ; }
40
34
41
35
/**
42
36
* Transform a value of one type into a value of another type.
@@ -47,47 +41,53 @@ unsafe fn bump_box_refcount<T>(+t: @T) { forget(move t); }
47
41
* assert transmute("L") == ~[76u8, 0u8];
48
42
*/
49
43
#[ inline( always) ]
50
- unsafe fn transmute < L , G > ( -thing : L ) -> G {
44
+ pub unsafe fn transmute < L , G > ( -thing : L ) -> G {
51
45
let newthing: G = reinterpret_cast ( & thing) ;
52
46
forget ( move thing) ;
53
47
move newthing
54
48
}
55
49
56
50
/// Coerce an immutable reference to be mutable.
57
51
#[ inline( always) ]
58
- unsafe fn transmute_mut < T > ( +ptr : & a /T ) -> & a/mut T { transmute ( move ptr) }
52
+ pub unsafe fn transmute_mut < T > ( +ptr : & a /T ) -> & a/mut T { transmute ( move ptr) }
59
53
60
54
/// Coerce a mutable reference to be immutable.
61
55
#[ inline( always) ]
62
- unsafe fn transmute_immut < T > ( +ptr : & a/mut T ) -> & a /T { transmute ( move ptr) }
56
+ pub unsafe fn transmute_immut < T > ( +ptr : & a/mut T ) -> & a /T {
57
+ transmute ( move ptr)
58
+ }
63
59
64
60
/// Coerce a borrowed pointer to have an arbitrary associated region.
65
61
#[ inline( always) ]
66
- unsafe fn transmute_region < T > ( +ptr : & a /T ) -> & b /T { transmute ( move ptr) }
62
+ pub unsafe fn transmute_region < T > ( +ptr : & a /T ) -> & b /T { transmute ( move ptr) }
67
63
68
64
/// Coerce an immutable reference to be mutable.
69
65
#[ inline( always) ]
70
- unsafe fn transmute_mut_unsafe < T > ( +ptr : * const T ) -> * mut T { transmute ( ptr) }
66
+ pub unsafe fn transmute_mut_unsafe < T > ( +ptr : * const T ) -> * mut T {
67
+ transmute ( ptr)
68
+ }
71
69
72
70
/// Coerce an immutable reference to be mutable.
73
71
#[ inline( always) ]
74
- unsafe fn transmute_immut_unsafe < T > ( +ptr : * const T ) -> * T { transmute ( ptr) }
72
+ pub unsafe fn transmute_immut_unsafe < T > ( +ptr : * const T ) -> * T {
73
+ transmute ( ptr)
74
+ }
75
75
76
76
/// Coerce a borrowed mutable pointer to have an arbitrary associated region.
77
77
#[ inline( always) ]
78
- unsafe fn transmute_mut_region < T > ( +ptr : & a/mut T ) -> & b/mut T {
78
+ pub unsafe fn transmute_mut_region < T > ( +ptr : & a/mut T ) -> & b/mut T {
79
79
transmute ( move ptr)
80
80
}
81
81
82
82
/// Transforms lifetime of the second pointer to match the first.
83
83
#[ inline( always) ]
84
- unsafe fn copy_lifetime < S , T > ( _ptr : & a /S , ptr : & T ) -> & a /T {
84
+ pub unsafe fn copy_lifetime < S , T > ( _ptr : & a /S , ptr : & T ) -> & a /T {
85
85
transmute_region ( ptr)
86
86
}
87
87
88
88
/// Transforms lifetime of the second pointer to match the first.
89
89
#[ inline( always) ]
90
- unsafe fn copy_lifetime_vec < S , T > ( _ptr : & a/[ S ] , ptr : & T ) -> & a /T {
90
+ pub unsafe fn copy_lifetime_vec < S , T > ( _ptr : & a/[ S ] , ptr : & T ) -> & a /T {
91
91
transmute_region ( ptr)
92
92
}
93
93
@@ -97,16 +97,14 @@ unsafe fn copy_lifetime_vec<S,T>(_ptr: &a/[S], ptr: &T) -> &a/T {
97
97
****************************************************************************/
98
98
99
99
#[ cfg( test) ]
100
- mod tests {
101
- #[ legacy_exports] ;
102
-
100
+ pub mod tests {
103
101
#[ test]
104
- fn test_reinterpret_cast ( ) {
102
+ pub fn test_reinterpret_cast ( ) {
105
103
assert 1 u == unsafe { reinterpret_cast ( & 1 ) } ;
106
104
}
107
105
108
106
#[ test]
109
- fn test_bump_box_refcount ( ) {
107
+ pub fn test_bump_box_refcount ( ) {
110
108
unsafe {
111
109
let box = @~"box box box"; // refcount 1
112
110
bump_box_refcount ( box) ; // refcount 2
@@ -121,7 +119,7 @@ mod tests {
121
119
}
122
120
123
121
#[ test]
124
- fn test_transmute ( ) {
122
+ pub fn test_transmute ( ) {
125
123
unsafe {
126
124
let x = @1 ;
127
125
let x: * int = transmute ( x) ;
@@ -131,7 +129,7 @@ mod tests {
131
129
}
132
130
133
131
#[ test]
134
- fn test_transmute2 ( ) {
132
+ pub fn test_transmute2 ( ) {
135
133
unsafe {
136
134
assert ~[ 76u8 , 0u8 ] == transmute ( ~"L ") ;
137
135
}
0 commit comments