@@ -102,10 +102,10 @@ pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
102
102
103
103
/**
104
104
* Swap the values at two mutable locations of the same type, without
105
- * deinitialising or copying either one .
105
+ * deinitialising either. They may overlap .
106
106
*/
107
107
#[ inline]
108
- pub unsafe fn swap_ptr < T > ( x : * mut T , y : * mut T ) {
108
+ pub unsafe fn swap < T > ( x : * mut T , y : * mut T ) {
109
109
// Give ourselves some scratch space to work with
110
110
let mut tmp: T = mem:: uninit ( ) ;
111
111
let t: * mut T = & mut tmp;
@@ -122,19 +122,19 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
122
122
123
123
/**
124
124
* Replace the value at a mutable location with a new one, returning the old
125
- * value, without deinitialising or copying either one .
125
+ * value, without deinitialising either.
126
126
*/
127
127
#[ inline]
128
- pub unsafe fn replace_ptr < T > ( dest : * mut T , mut src : T ) -> T {
128
+ pub unsafe fn replace < T > ( dest : * mut T , mut src : T ) -> T {
129
129
mem:: swap ( cast:: transmute ( dest) , & mut src) ; // cannot overlap
130
130
src
131
131
}
132
132
133
133
/**
134
- * Reads the value from `*src` and returns it. Does not copy `*src`.
134
+ * Reads the value from `*src` and returns it.
135
135
*/
136
136
#[ inline( always) ]
137
- pub unsafe fn read_ptr < T > ( src : * T ) -> T {
137
+ pub unsafe fn read < T > ( src : * T ) -> T {
138
138
let mut tmp: T = mem:: uninit ( ) ;
139
139
copy_nonoverlapping_memory ( & mut tmp, src, 1 ) ;
140
140
tmp
@@ -145,28 +145,16 @@ pub unsafe fn read_ptr<T>(src: *T) -> T {
145
145
* This currently prevents destructors from executing.
146
146
*/
147
147
#[ inline( always) ]
148
- pub unsafe fn read_and_zero_ptr < T > ( dest : * mut T ) -> T {
148
+ pub unsafe fn read_and_zero < T > ( dest : * mut T ) -> T {
149
149
// Copy the data out from `dest`:
150
- let tmp = read_ptr ( & * dest) ;
150
+ let tmp = read ( & * dest) ;
151
151
152
152
// Now zero out `dest`:
153
153
zero_memory ( dest, 1 ) ;
154
154
155
155
tmp
156
156
}
157
157
158
- /// Transform a region pointer - &T - to an unsafe pointer - *T.
159
- #[ inline]
160
- pub fn to_unsafe_ptr < T > ( thing : & T ) -> * T {
161
- thing as * T
162
- }
163
-
164
- /// Transform a mutable region pointer - &mut T - to a mutable unsafe pointer - *mut T.
165
- #[ inline]
166
- pub fn to_mut_unsafe_ptr < T > ( thing : & mut T ) -> * mut T {
167
- thing as * mut T
168
- }
169
-
170
158
/**
171
159
Given a **T (pointer to an array of pointers),
172
160
iterate through each *T, up to the provided `len`,
@@ -176,7 +164,7 @@ pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
176
164
*/
177
165
pub unsafe fn array_each_with_len < T > ( arr : * * T , len : uint , cb : |* T |) {
178
166
debug ! ( "array_each_with_len: before iterate" ) ;
179
- if arr as uint == 0 {
167
+ if arr. is_null ( ) {
180
168
fail ! ( "ptr::array_each_with_len failure: arr input is null pointer" ) ;
181
169
}
182
170
//let start_ptr = *arr;
@@ -197,108 +185,82 @@ pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) {
197
185
Dragons be here.
198
186
*/
199
187
pub unsafe fn array_each < T > ( arr : * * T , cb: |* T |) {
200
- if arr as uint == 0 {
188
+ if arr. is_null ( ) {
201
189
fail ! ( "ptr::array_each_with_len failure: arr input is null pointer" ) ;
202
190
}
203
191
let len = buf_len ( arr) ;
204
192
debug ! ( "array_each inferred len: {}" , len) ;
205
193
array_each_with_len ( arr, len, cb) ;
206
194
}
207
195
208
- # [ allow ( missing_doc ) ]
196
+ /// Extension methods for raw pointers.
209
197
pub trait RawPtr < T > {
198
+ /// Returns the null pointer.
210
199
fn null ( ) -> Self ;
200
+ /// Returns true if the pointer is equal to the null pointer.
211
201
fn is_null ( & self ) -> bool ;
212
- fn is_not_null ( & self ) -> bool ;
202
+ /// Returns true if the pointer is not equal to the null pointer.
203
+ fn is_not_null ( & self ) -> bool { !self . is_null ( ) }
204
+ /// Returns the value of this pointer (ie, the address it points to)
213
205
fn to_uint ( & self ) -> uint ;
206
+ /// Returns `None` if the pointer is null, or else returns the value wrapped
207
+ /// in `Some`.
208
+ ///
209
+ /// # Safety Notes
210
+ ///
211
+ /// While this method is useful for null-safety, it is important to note
212
+ /// that this is still an unsafe operation because the returned value could
213
+ /// be pointing to invalid memory.
214
214
unsafe fn to_option ( & self ) -> Option < & T > ;
215
+ /// Calculates the offset from a pointer. The offset *must* be in-bounds of
216
+ /// the object, or one-byte-past-the-end.
215
217
unsafe fn offset ( self , count : int ) -> Self ;
216
218
}
217
219
218
- /// Extension methods for immutable pointers
219
220
impl < T > RawPtr < T > for * T {
220
- /// Returns the null pointer.
221
221
#[ inline]
222
222
fn null ( ) -> * T { null ( ) }
223
223
224
- /// Returns true if the pointer is equal to the null pointer.
225
224
#[ inline]
226
225
fn is_null ( & self ) -> bool { * self == RawPtr :: null ( ) }
227
226
228
- /// Returns true if the pointer is not equal to the null pointer.
229
227
#[ inline]
230
- fn is_not_null ( & self ) -> bool { * self != RawPtr :: null ( ) }
228
+ fn to_uint ( & self ) -> uint { * self as uint }
231
229
232
- /// Returns the address of this pointer.
233
230
#[ inline]
234
- fn to_uint ( & self ) -> uint { * self as uint }
231
+ unsafe fn offset ( self , count : int ) -> * T { intrinsics :: offset ( self , count ) }
235
232
236
- ///
237
- /// Returns `None` if the pointer is null, or else returns the value wrapped
238
- /// in `Some`.
239
- ///
240
- /// # Safety Notes
241
- ///
242
- /// While this method is useful for null-safety, it is important to note
243
- /// that this is still an unsafe operation because the returned value could
244
- /// be pointing to invalid memory.
245
- ///
246
233
#[ inline]
247
234
unsafe fn to_option ( & self ) -> Option < & T > {
248
- if self . is_null ( ) { None } else {
235
+ if self . is_null ( ) {
236
+ None
237
+ } else {
249
238
Some ( cast:: transmute ( * self ) )
250
239
}
251
240
}
252
-
253
- /// Calculates the offset from a pointer. The offset *must* be in-bounds of
254
- /// the object, or one-byte-past-the-end.
255
- #[ inline]
256
- unsafe fn offset ( self , count : int ) -> * T { intrinsics:: offset ( self , count) }
257
241
}
258
242
259
- /// Extension methods for mutable pointers
260
243
impl < T > RawPtr < T > for * mut T {
261
- /// Returns the null pointer.
262
244
#[ inline]
263
245
fn null ( ) -> * mut T { mut_null ( ) }
264
246
265
- /// Returns true if the pointer is equal to the null pointer.
266
247
#[ inline]
267
248
fn is_null ( & self ) -> bool { * self == RawPtr :: null ( ) }
268
249
269
- /// Returns true if the pointer is not equal to the null pointer.
270
250
#[ inline]
271
- fn is_not_null ( & self ) -> bool { * self != RawPtr :: null ( ) }
251
+ fn to_uint ( & self ) -> uint { * self as uint }
272
252
273
- /// Returns the address of this pointer.
274
253
#[ inline]
275
- fn to_uint ( & self ) -> uint { * self as uint }
254
+ unsafe fn offset ( self , count : int ) -> * mut T { intrinsics :: offset ( self as * T , count ) as * mut T }
276
255
277
- ///
278
- /// Returns `None` if the pointer is null, or else returns the value wrapped
279
- /// in `Some`.
280
- ///
281
- /// # Safety Notes
282
- ///
283
- /// While this method is useful for null-safety, it is important to note
284
- /// that this is still an unsafe operation because the returned value could
285
- /// be pointing to invalid memory.
286
- ///
287
256
#[ inline]
288
257
unsafe fn to_option ( & self ) -> Option < & T > {
289
- if self . is_null ( ) { None } else {
258
+ if self . is_null ( ) {
259
+ None
260
+ } else {
290
261
Some ( cast:: transmute ( * self ) )
291
262
}
292
263
}
293
-
294
- /// Calculates the offset from a pointer. The offset *must* be in-bounds of
295
- /// the object, or one-byte-past-the-end. An arithmetic overflow is also
296
- /// undefined behaviour.
297
- ///
298
- /// This method should be preferred over `offset` when the guarantee can be
299
- /// satisfied, to enable better optimization.
300
- #[ inline]
301
- unsafe fn offset ( self , count : int ) -> * mut T { intrinsics:: offset ( self as * T , count) as * mut T }
302
264
}
303
265
304
266
// Equality for pointers
0 commit comments