@@ -95,36 +95,6 @@ use option::{Some, None, Option};
95
95
96
96
#[ cfg( not( test) ) ] use cmp:: { PartialEq , Eq , PartialOrd , Equiv } ;
97
97
98
- /// Return the offset of the first null pointer in `buf`.
99
- #[ inline]
100
- pub unsafe fn buf_len < T > ( buf : * * T ) -> uint {
101
- position ( buf, |i| * i == null ( ) )
102
- }
103
-
104
- impl < T > Clone for * T {
105
- #[ inline]
106
- fn clone ( & self ) -> * T {
107
- * self
108
- }
109
- }
110
-
111
- impl < T > Clone for * mut T {
112
- #[ inline]
113
- fn clone ( & self ) -> * mut T {
114
- * self
115
- }
116
- }
117
-
118
- /// Return the first offset `i` such that `f(buf[i]) == true`.
119
- #[ inline]
120
- pub unsafe fn position < T > ( buf : * T , f: |& T | -> bool) -> uint {
121
- let mut i = 0 ;
122
- loop {
123
- if f ( & ( * buf. offset ( i as int ) ) ) { return i; }
124
- else { i += 1 ; }
125
- }
126
- }
127
-
128
98
/// Create a null pointer.
129
99
///
130
100
/// # Example
@@ -136,6 +106,7 @@ pub unsafe fn position<T>(buf: *T, f: |&T| -> bool) -> uint {
136
106
/// assert!(p.is_null());
137
107
/// ```
138
108
#[ inline]
109
+ #[ unstable = "may need a different name after pending changes to pointer types" ]
139
110
pub fn null < T > ( ) -> * T { 0 as * T }
140
111
141
112
/// Create an unsafe mutable null pointer.
@@ -149,6 +120,7 @@ pub fn null<T>() -> *T { 0 as *T }
149
120
/// assert!(p.is_null());
150
121
/// ```
151
122
#[ inline]
123
+ #[ unstable = "may need a different name after pending changes to pointer types" ]
152
124
pub fn mut_null < T > ( ) -> * mut T { 0 as * mut T }
153
125
154
126
/// Copies data from one location to another.
@@ -174,6 +146,7 @@ pub fn mut_null<T>() -> *mut T { 0 as *mut T }
174
146
/// ```
175
147
///
176
148
#[ inline]
149
+ #[ unstable]
177
150
pub unsafe fn copy_memory < T > ( dst : * mut T , src : * T , count : uint ) {
178
151
intrinsics:: copy_memory ( dst, src, count)
179
152
}
@@ -215,6 +188,7 @@ pub unsafe fn copy_memory<T>(dst: *mut T, src: *T, count: uint) {
215
188
/// If the source and destination overlap then the behavior of this
216
189
/// function is undefined.
217
190
#[ inline]
191
+ #[ unstable]
218
192
pub unsafe fn copy_nonoverlapping_memory < T > ( dst : * mut T ,
219
193
src : * T ,
220
194
count : uint ) {
@@ -224,19 +198,23 @@ pub unsafe fn copy_nonoverlapping_memory<T>(dst: *mut T,
224
198
/// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
225
199
/// bytes of memory starting at `dst` to `c`.
226
200
#[ inline]
201
+ #[ experimental = "uncertain about naming and semantics" ]
227
202
pub unsafe fn set_memory < T > ( dst : * mut T , c : u8 , count : uint ) {
228
203
intrinsics:: set_memory ( dst, c, count)
229
204
}
230
205
231
206
/// Zeroes out `count * size_of::<T>` bytes of memory at `dst`
232
207
#[ inline]
208
+ #[ experimental = "uncertain about naming and semantics" ]
209
+ #[ allow( experimental) ]
233
210
pub unsafe fn zero_memory < T > ( dst : * mut T , count : uint ) {
234
211
set_memory ( dst, 0 , count) ;
235
212
}
236
213
237
214
/// Swap the values at two mutable locations of the same type, without
238
215
/// deinitialising either. They may overlap.
239
216
#[ inline]
217
+ #[ unstable]
240
218
pub unsafe fn swap < T > ( x : * mut T , y : * mut T ) {
241
219
// Give ourselves some scratch space to work with
242
220
let mut tmp: T = mem:: uninitialized ( ) ;
@@ -255,13 +233,15 @@ pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
255
233
/// Replace the value at a mutable location with a new one, returning the old
256
234
/// value, without deinitialising either.
257
235
#[ inline]
236
+ #[ unstable]
258
237
pub unsafe fn replace < T > ( dest : * mut T , mut src : T ) -> T {
259
238
mem:: swap ( mem:: transmute ( dest) , & mut src) ; // cannot overlap
260
239
src
261
240
}
262
241
263
242
/// Reads the value from `*src` and returns it.
264
243
#[ inline( always) ]
244
+ #[ unstable]
265
245
pub unsafe fn read < T > ( src : * T ) -> T {
266
246
let mut tmp: T = mem:: uninitialized ( ) ;
267
247
copy_nonoverlapping_memory ( & mut tmp, src, 1 ) ;
@@ -271,6 +251,8 @@ pub unsafe fn read<T>(src: *T) -> T {
271
251
/// Reads the value from `*src` and nulls it out.
272
252
/// This currently prevents destructors from executing.
273
253
#[ inline( always) ]
254
+ #[ experimental]
255
+ #[ allow( experimental) ]
274
256
pub unsafe fn read_and_zero < T > ( dest : * mut T ) -> T {
275
257
// Copy the data out from `dest`:
276
258
let tmp = read ( & * dest) ;
@@ -281,9 +263,22 @@ pub unsafe fn read_and_zero<T>(dest: *mut T) -> T {
281
263
tmp
282
264
}
283
265
266
+ /// Unsafely overwrite a memory location with the given value without destroying
267
+ /// the old value.
268
+ ///
269
+ /// This operation is unsafe because it does not destroy the previous value
270
+ /// contained at the location `dst`. This could leak allocations or resources,
271
+ /// so care must be taken to previously deallocate the value at `dst`.
272
+ #[ inline]
273
+ #[ unstable]
274
+ pub unsafe fn write < T > ( dst : * mut T , src : T ) {
275
+ intrinsics:: move_val_init ( & mut * dst, src)
276
+ }
277
+
284
278
/// Given a **T (pointer to an array of pointers),
285
279
/// iterate through each *T, up to the provided `len`,
286
280
/// passing to the provided callback function
281
+ #[ deprecated = "old-style iteration. use a loop and RawPtr::offset" ]
287
282
pub unsafe fn array_each_with_len < T > ( arr : * * T , len : uint , cb: |* T |) {
288
283
if arr. is_null ( ) {
289
284
fail ! ( "ptr::array_each_with_len failure: arr input is null pointer" ) ;
@@ -303,6 +298,8 @@ pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) {
303
298
///
304
299
/// This will only work with a null-terminated
305
300
/// pointer array.
301
+ #[ deprecated = "old-style iteration. use a loop and RawPtr::offset" ]
302
+ #[ allow( deprecated) ]
306
303
pub unsafe fn array_each < T > ( arr : * * T , cb: |* T |) {
307
304
if arr. is_null ( ) {
308
305
fail ! ( "ptr::array_each_with_len failure: arr input is null pointer" ) ;
@@ -311,6 +308,25 @@ pub unsafe fn array_each<T>(arr: **T, cb: |*T|) {
311
308
array_each_with_len ( arr, len, cb) ;
312
309
}
313
310
311
+ /// Return the offset of the first null pointer in `buf`.
312
+ #[ inline]
313
+ #[ deprecated = "use a loop and RawPtr::offset" ]
314
+ #[ allow( deprecated) ]
315
+ pub unsafe fn buf_len < T > ( buf : * * T ) -> uint {
316
+ position ( buf, |i| * i == null ( ) )
317
+ }
318
+
319
+ /// Return the first offset `i` such that `f(buf[i]) == true`.
320
+ #[ inline]
321
+ #[ deprecated = "old-style iteration. use a loop and RawPtr::offset" ]
322
+ pub unsafe fn position < T > ( buf : * T , f: |& T | -> bool) -> uint {
323
+ let mut i = 0 ;
324
+ loop {
325
+ if f ( & ( * buf. offset ( i as int ) ) ) { return i; }
326
+ else { i += 1 ; }
327
+ }
328
+ }
329
+
314
330
/// Methods on raw pointers
315
331
pub trait RawPtr < T > {
316
332
/// Returns the null pointer.
@@ -426,6 +442,20 @@ impl<T> Equiv<*T> for *mut T {
426
442
}
427
443
}
428
444
445
+ impl < T > Clone for * T {
446
+ #[ inline]
447
+ fn clone ( & self ) -> * T {
448
+ * self
449
+ }
450
+ }
451
+
452
+ impl < T > Clone for * mut T {
453
+ #[ inline]
454
+ fn clone ( & self ) -> * mut T {
455
+ * self
456
+ }
457
+ }
458
+
429
459
// Equality for extern "C" fn pointers
430
460
#[ cfg( not( test) ) ]
431
461
mod externfnpointers {
0 commit comments