13
13
use cast;
14
14
use clone:: Clone ;
15
15
use option:: { Option , Some , None } ;
16
+ #[ cfg( stage0) ]
16
17
use sys;
17
18
use unstable:: intrinsics;
18
19
use util:: swap;
@@ -25,20 +26,44 @@ use uint;
25
26
26
27
/// Calculate the offset from a pointer
27
28
#[ inline]
28
- pub fn offset < T > ( ptr : * T , count : uint ) -> * T {
29
- ( ptr as uint + count * sys:: size_of :: < T > ( ) ) as * T
29
+ #[ cfg( stage0) ]
30
+ pub fn offset < T > ( ptr : * T , count : int ) -> * T {
31
+ ( ptr as uint + ( count as uint ) * sys:: size_of :: < T > ( ) ) as * T
30
32
}
31
33
32
34
/// Calculate the offset from a const pointer
33
35
#[ inline]
34
- pub fn const_offset < T > ( ptr : * const T , count : uint ) -> * const T {
35
- ( ptr as uint + count * sys:: size_of :: < T > ( ) ) as * T
36
+ #[ cfg( stage0) ]
37
+ pub fn const_offset < T > ( ptr : * const T , count : int ) -> * const T {
38
+ ( ptr as uint + ( count as uint ) * sys:: size_of :: < T > ( ) ) as * T
36
39
}
37
40
38
41
/// Calculate the offset from a mut pointer
39
42
#[ inline]
40
- pub fn mut_offset < T > ( ptr : * mut T , count : uint ) -> * mut T {
41
- ( ptr as uint + count * sys:: size_of :: < T > ( ) ) as * mut T
43
+ #[ cfg( stage0) ]
44
+ pub fn mut_offset < T > ( ptr : * mut T , count : int ) -> * mut T {
45
+ ( ptr as uint + ( count as uint ) * sys:: size_of :: < T > ( ) ) as * mut T
46
+ }
47
+
48
+ /// Calculate the offset from a pointer
49
+ #[ inline]
50
+ #[ cfg( not( stage0) ) ]
51
+ pub fn offset < T > ( ptr : * T , count : int ) -> * T {
52
+ unsafe { intrinsics:: offset ( ptr, count) }
53
+ }
54
+
55
+ /// Calculate the offset from a const pointer
56
+ #[ inline]
57
+ #[ cfg( not( stage0) ) ]
58
+ pub fn const_offset < T > ( ptr : * const T , count : int ) -> * const T {
59
+ unsafe { intrinsics:: offset ( ptr as * T , count) }
60
+ }
61
+
62
+ /// Calculate the offset from a mut pointer
63
+ #[ inline]
64
+ #[ cfg( not( stage0) ) ]
65
+ pub fn mut_offset < T > ( ptr : * mut T , count : int ) -> * mut T {
66
+ unsafe { intrinsics:: offset ( ptr as * T , count) as * mut T }
42
67
}
43
68
44
69
/// Return the offset of the first null pointer in `buf`.
@@ -58,7 +83,7 @@ impl<T> Clone for *T {
58
83
pub unsafe fn position < T > ( buf : * T , f : & fn ( & T ) -> bool ) -> uint {
59
84
let mut i = 0 ;
60
85
loop {
61
- if f ( & ( * offset ( buf, i) ) ) { return i; }
86
+ if f ( & ( * offset ( buf, i as int ) ) ) { return i; }
62
87
else { i += 1 ; }
63
88
}
64
89
}
@@ -242,7 +267,7 @@ pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: &fn(*T)) {
242
267
}
243
268
//let start_ptr = *arr;
244
269
uint:: iterate ( 0 , len, |e| {
245
- let n = offset ( arr, e) ;
270
+ let n = offset ( arr, e as int ) ;
246
271
cb ( * n) ;
247
272
true
248
273
} ) ;
@@ -273,7 +298,7 @@ pub trait RawPtr<T> {
273
298
fn is_null ( & self ) -> bool ;
274
299
fn is_not_null ( & self ) -> bool ;
275
300
unsafe fn to_option ( & self ) -> Option < & T > ;
276
- fn offset ( & self , count : uint ) -> Self ;
301
+ fn offset ( & self , count : int ) -> Self ;
277
302
}
278
303
279
304
/// Extension methods for immutable pointers
@@ -305,7 +330,7 @@ impl<T> RawPtr<T> for *T {
305
330
306
331
/// Calculates the offset from a pointer.
307
332
#[ inline]
308
- fn offset ( & self , count : uint ) -> * T { offset ( * self , count) }
333
+ fn offset ( & self , count : int ) -> * T { offset ( * self , count) }
309
334
}
310
335
311
336
/// Extension methods for mutable pointers
@@ -337,7 +362,7 @@ impl<T> RawPtr<T> for *mut T {
337
362
338
363
/// Calculates the offset from a mutable pointer.
339
364
#[ inline]
340
- fn offset ( & self , count : uint ) -> * mut T { mut_offset ( * self , count) }
365
+ fn offset ( & self , count : int ) -> * mut T { mut_offset ( * self , count) }
341
366
}
342
367
343
368
// Equality for pointers
@@ -378,7 +403,7 @@ impl<T, I: Int> Add<I, *T> for *T {
378
403
/// Is calculated according to the size of the type pointed to.
379
404
#[ inline]
380
405
pub fn add ( & self , rhs : & I ) -> * T {
381
- self . offset ( rhs. to_int ( ) as uint )
406
+ self . offset ( rhs. to_int ( ) as int )
382
407
}
383
408
}
384
409
@@ -388,7 +413,7 @@ impl<T, I: Int> Sub<I, *T> for *T {
388
413
/// Is calculated according to the size of the type pointed to.
389
414
#[ inline]
390
415
pub fn sub ( & self , rhs : & I ) -> * T {
391
- self . offset ( -rhs. to_int ( ) as uint )
416
+ self . offset ( -rhs. to_int ( ) as int )
392
417
}
393
418
}
394
419
@@ -398,7 +423,7 @@ impl<T, I: Int> Add<I, *mut T> for *mut T {
398
423
/// Is calculated according to the size of the type pointed to.
399
424
#[ inline]
400
425
pub fn add ( & self , rhs : & I ) -> * mut T {
401
- self . offset ( rhs. to_int ( ) as uint )
426
+ self . offset ( rhs. to_int ( ) as int )
402
427
}
403
428
}
404
429
@@ -408,7 +433,7 @@ impl<T, I: Int> Sub<I, *mut T> for *mut T {
408
433
/// Is calculated according to the size of the type pointed to.
409
434
#[ inline]
410
435
pub fn sub ( & self , rhs : & I ) -> * mut T {
411
- self . offset ( -rhs. to_int ( ) as uint )
436
+ self . offset ( -rhs. to_int ( ) as int )
412
437
}
413
438
}
414
439
@@ -445,14 +470,14 @@ pub mod ptr_tests {
445
470
let v0 = ~[ 32000u16 , 32001u16 , 32002u16 ] ;
446
471
let mut v1 = ~[ 0u16 , 0u16 , 0u16 ] ;
447
472
448
- copy_memory ( mut_offset ( vec:: raw:: to_mut_ptr ( v1) , 1 u ) ,
449
- offset ( vec:: raw:: to_ptr ( v0) , 1 u ) , 1 u ) ;
473
+ copy_memory ( mut_offset ( vec:: raw:: to_mut_ptr ( v1) , 1 ) ,
474
+ offset ( vec:: raw:: to_ptr ( v0) , 1 ) , 1 ) ;
450
475
assert ! ( ( v1[ 0 ] == 0u16 && v1[ 1 ] == 32001u16 && v1[ 2 ] == 0u16 ) ) ;
451
476
copy_memory ( vec:: raw:: to_mut_ptr ( v1) ,
452
- offset ( vec:: raw:: to_ptr ( v0) , 2 u ) , 1 u ) ;
477
+ offset ( vec:: raw:: to_ptr ( v0) , 2 ) , 1 ) ;
453
478
assert ! ( ( v1[ 0 ] == 32002u16 && v1[ 1 ] == 32001u16 &&
454
479
v1[ 2 ] == 0u16 ) ) ;
455
- copy_memory ( mut_offset ( vec:: raw:: to_mut_ptr ( v1) , 2 u ) ,
480
+ copy_memory ( mut_offset ( vec:: raw:: to_mut_ptr ( v1) , 2 ) ,
456
481
vec:: raw:: to_ptr ( v0) , 1 u) ;
457
482
assert ! ( ( v1[ 0 ] == 32002u16 && v1[ 1 ] == 32001u16 &&
458
483
v1[ 2 ] == 32000u16 ) ) ;
@@ -495,15 +520,15 @@ pub mod ptr_tests {
495
520
assert ! ( p. is_null( ) ) ;
496
521
assert ! ( !p. is_not_null( ) ) ;
497
522
498
- let q = offset ( p, 1 u ) ;
523
+ let q = offset ( p, 1 ) ;
499
524
assert ! ( !q. is_null( ) ) ;
500
525
assert ! ( q. is_not_null( ) ) ;
501
526
502
527
let mp: * mut int = mut_null ( ) ;
503
528
assert ! ( mp. is_null( ) ) ;
504
529
assert ! ( !mp. is_not_null( ) ) ;
505
530
506
- let mq = mp. offset ( 1 u ) ;
531
+ let mq = mp. offset ( 1 ) ;
507
532
assert ! ( !mq. is_null( ) ) ;
508
533
assert ! ( mq. is_not_null( ) ) ;
509
534
}
0 commit comments