1
1
//! Unsafe pointer utility functions
2
2
3
- export addr_of;
4
- export to_unsafe_ptr;
5
- export to_const_unsafe_ptr;
6
- export to_mut_unsafe_ptr;
7
- export mut_addr_of;
8
- export offset;
9
- export const_offset;
10
- export mut_offset;
11
- export null;
12
- export mut_null;
13
- export is_null;
14
- export is_not_null;
15
- export memcpy;
16
- export memmove;
17
- export memset;
18
- export to_uint;
19
- export ref_eq;
20
- export buf_len;
21
- export position;
22
- export Ptr ;
23
-
24
3
use cmp:: { Eq , Ord } ;
25
4
use libc:: { c_void, size_t} ;
26
5
@@ -49,47 +28,47 @@ extern mod rusti {
49
28
50
29
/// Get an unsafe pointer to a value
51
30
#[ inline( always) ]
52
- pure fn addr_of < T > ( val : T ) -> * T { unsafe { rusti:: addr_of ( val) } }
31
+ pub pure fn addr_of < T > ( val : T ) -> * T { unsafe { rusti:: addr_of ( val) } }
53
32
54
33
/// Get an unsafe mut pointer to a value
55
34
#[ inline( always) ]
56
- pure fn mut_addr_of < T > ( val : T ) -> * mut T {
35
+ pub pure fn mut_addr_of < T > ( val : T ) -> * mut T {
57
36
unsafe {
58
37
cast:: reinterpret_cast ( & rusti:: addr_of ( val) )
59
38
}
60
39
}
61
40
62
41
/// Calculate the offset from a pointer
63
42
#[ inline( always) ]
64
- fn offset < T > ( ptr : * T , count : uint ) -> * T {
43
+ pub fn offset < T > ( ptr : * T , count : uint ) -> * T {
65
44
unsafe {
66
45
( ptr as uint + count * sys:: size_of :: < T > ( ) ) as * T
67
46
}
68
47
}
69
48
70
49
/// Calculate the offset from a const pointer
71
50
#[ inline( always) ]
72
- fn const_offset < T > ( ptr : * const T , count : uint ) -> * const T {
51
+ pub fn const_offset < T > ( ptr : * const T , count : uint ) -> * const T {
73
52
unsafe {
74
53
( ptr as uint + count * sys:: size_of :: < T > ( ) ) as * T
75
54
}
76
55
}
77
56
78
57
/// Calculate the offset from a mut pointer
79
58
#[ inline( always) ]
80
- fn mut_offset < T > ( ptr : * mut T , count : uint ) -> * mut T {
59
+ pub fn mut_offset < T > ( ptr : * mut T , count : uint ) -> * mut T {
81
60
( ptr as uint + count * sys:: size_of :: < T > ( ) ) as * mut T
82
61
}
83
62
84
63
/// Return the offset of the first null pointer in `buf`.
85
64
#[ inline( always) ]
86
- unsafe fn buf_len < T > ( buf : * * T ) -> uint {
65
+ pub unsafe fn buf_len < T > ( buf : * * T ) -> uint {
87
66
position ( buf, |i| i == null ( ) )
88
67
}
89
68
90
69
/// Return the first offset `i` such that `f(buf[i]) == true`.
91
70
#[ inline( always) ]
92
- unsafe fn position < T > ( buf : * T , f : fn ( T ) -> bool ) -> uint {
71
+ pub unsafe fn position < T > ( buf : * T , f : fn ( T ) -> bool ) -> uint {
93
72
let mut i = 0 u;
94
73
loop {
95
74
if f ( * offset ( buf, i) ) { return i; }
@@ -99,17 +78,17 @@ unsafe fn position<T>(buf: *T, f: fn(T) -> bool) -> uint {
99
78
100
79
/// Create an unsafe null pointer
101
80
#[ inline( always) ]
102
- pure fn null < T > ( ) -> * T { unsafe { cast:: reinterpret_cast ( & 0 u) } }
81
+ pub pure fn null < T > ( ) -> * T { unsafe { cast:: reinterpret_cast ( & 0 u) } }
103
82
104
83
/// Create an unsafe mutable null pointer
105
84
#[ inline( always) ]
106
- pure fn mut_null < T > ( ) -> * mut T { unsafe { cast:: reinterpret_cast ( & 0 u) } }
85
+ pub pure fn mut_null < T > ( ) -> * mut T { unsafe { cast:: reinterpret_cast ( & 0 u) } }
107
86
108
87
/// Returns true if the pointer is equal to the null pointer.
109
- pure fn is_null < T > ( ptr : * const T ) -> bool { ptr == null ( ) }
88
+ pub pure fn is_null < T > ( ptr : * const T ) -> bool { ptr == null ( ) }
110
89
111
90
/// Returns true if the pointer is not equal to the null pointer.
112
- pure fn is_not_null < T > ( ptr : * const T ) -> bool { !is_null ( ptr) }
91
+ pub pure fn is_not_null < T > ( ptr : * const T ) -> bool { !is_null ( ptr) }
113
92
114
93
/**
115
94
* Copies data from one location to another
@@ -118,7 +97,7 @@ pure fn is_not_null<T>(ptr: *const T) -> bool { !is_null(ptr) }
118
97
* and destination may not overlap.
119
98
*/
120
99
#[ inline( always) ]
121
- unsafe fn memcpy < T > ( dst : * mut T , src : * const T , count : uint ) {
100
+ pub unsafe fn memcpy < T > ( dst : * mut T , src : * const T , count : uint ) {
122
101
let n = count * sys:: size_of :: < T > ( ) ;
123
102
libc_:: memcpy ( dst as * mut c_void , src as * c_void , n as size_t ) ;
124
103
}
@@ -130,13 +109,13 @@ unsafe fn memcpy<T>(dst: *mut T, src: *const T, count: uint) {
130
109
* and destination may overlap.
131
110
*/
132
111
#[ inline( always) ]
133
- unsafe fn memmove < T > ( dst : * mut T , src : * const T , count : uint ) {
112
+ pub unsafe fn memmove < T > ( dst : * mut T , src : * const T , count : uint ) {
134
113
let n = count * sys:: size_of :: < T > ( ) ;
135
114
libc_:: memmove ( dst as * mut c_void , src as * c_void , n as size_t ) ;
136
115
}
137
116
138
117
#[ inline( always) ]
139
- unsafe fn memset < T > ( dst : * mut T , c : int , count : uint ) {
118
+ pub unsafe fn memset < T > ( dst : * mut T , c : int , count : uint ) {
140
119
let n = count * sys:: size_of :: < T > ( ) ;
141
120
libc_:: memset ( dst as * mut c_void , c as libc:: c_int , n as size_t ) ;
142
121
}
@@ -148,7 +127,7 @@ unsafe fn memset<T>(dst: *mut T, c: int, count: uint) {
148
127
reinterpret_cast.
149
128
*/
150
129
#[ inline( always) ]
151
- fn to_unsafe_ptr < T > ( thing : & T ) -> * T {
130
+ pub fn to_unsafe_ptr < T > ( thing : & T ) -> * T {
152
131
unsafe { cast:: reinterpret_cast ( & thing) }
153
132
}
154
133
@@ -158,7 +137,7 @@ fn to_unsafe_ptr<T>(thing: &T) -> *T {
158
137
reinterpret_cast.
159
138
*/
160
139
#[ inline( always) ]
161
- fn to_const_unsafe_ptr < T > ( thing : & const T ) -> * const T {
140
+ pub fn to_const_unsafe_ptr < T > ( thing : & const T ) -> * const T {
162
141
unsafe { cast:: reinterpret_cast ( & thing) }
163
142
}
164
143
@@ -168,7 +147,7 @@ fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
168
147
reinterpret_cast.
169
148
*/
170
149
#[ inline( always) ]
171
- fn to_mut_unsafe_ptr < T > ( thing : & mut T ) -> * mut T {
150
+ pub fn to_mut_unsafe_ptr < T > ( thing : & mut T ) -> * mut T {
172
151
unsafe { cast:: reinterpret_cast ( & thing) }
173
152
}
174
153
@@ -180,17 +159,17 @@ fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
180
159
(I couldn't think of a cutesy name for this one.)
181
160
*/
182
161
#[ inline( always) ]
183
- fn to_uint < T > ( thing : & T ) -> uint unsafe {
162
+ pub fn to_uint < T > ( thing : & T ) -> uint unsafe {
184
163
cast:: reinterpret_cast ( & thing)
185
164
}
186
165
187
166
/// Determine if two borrowed pointers point to the same thing.
188
167
#[ inline( always) ]
189
- fn ref_eq < T > ( thing : & a /T , other : & b /T ) -> bool {
168
+ pub fn ref_eq < T > ( thing : & a /T , other : & b /T ) -> bool {
190
169
to_uint ( thing) == to_uint ( other)
191
170
}
192
171
193
- trait Ptr {
172
+ pub trait Ptr {
194
173
pure fn is_null ( ) -> bool ;
195
174
pure fn is_not_null ( ) -> bool ;
196
175
}
@@ -253,7 +232,7 @@ impl<T:Ord> &const T : Ord {
253
232
}
254
233
255
234
#[ test]
256
- fn test ( ) {
235
+ pub fn test ( ) {
257
236
unsafe {
258
237
type Pair = { mut fst : int , mut snd : int } ;
259
238
let p = { mut fst: 10 , mut snd: 20 } ;
@@ -285,7 +264,7 @@ fn test() {
285
264
}
286
265
287
266
#[ test]
288
- fn test_position ( ) {
267
+ pub fn test_position ( ) {
289
268
use str:: as_c_str;
290
269
use libc:: c_char;
291
270
@@ -298,7 +277,7 @@ fn test_position() {
298
277
}
299
278
300
279
#[ test]
301
- fn test_buf_len ( ) {
280
+ pub fn test_buf_len ( ) {
302
281
let s0 = ~"hello";
303
282
let s1 = ~"there";
304
283
let s2 = ~"thing";
@@ -316,7 +295,7 @@ fn test_buf_len() {
316
295
}
317
296
318
297
#[ test]
319
- fn test_is_null ( ) {
298
+ pub fn test_is_null ( ) {
320
299
let p: * int = ptr:: null ( ) ;
321
300
assert p. is_null ( ) ;
322
301
assert !p. is_not_null ( ) ;
0 commit comments