@@ -20,37 +20,36 @@ use sys;
20
20
use unstable:: intrinsics;
21
21
use util:: swap;
22
22
23
- #[ cfg( not( test) ) ] use ops:: { Add , Sub } ;
24
- #[ cfg( not( test) ) ] use num:: Int ;
25
-
26
23
#[ cfg( not( test) ) ] use cmp:: { Eq , Ord } ;
27
24
28
- /// Calculate the offset from a pointer
25
+ /// Calculate the offset from a pointer. The count *must* be in bounds or
26
+ /// otherwise the loads of this address are undefined.
29
27
#[ inline]
30
28
#[ cfg( stage0) ]
31
- pub fn offset < T > ( ptr : * T , count : int ) -> * T {
29
+ pub unsafe fn offset < T > ( ptr : * T , count : int ) -> * T {
32
30
( ptr as uint + ( count as uint ) * sys:: size_of :: < T > ( ) ) as * T
33
31
}
34
32
35
33
/// Calculate the offset from a mut pointer
36
34
#[ inline]
37
35
#[ cfg( stage0) ]
38
- pub fn mut_offset < T > ( ptr : * mut T , count : int ) -> * mut T {
36
+ pub unsafe fn mut_offset < T > ( ptr : * mut T , count : int ) -> * mut T {
39
37
( ptr as uint + ( count as uint ) * sys:: size_of :: < T > ( ) ) as * mut T
40
38
}
41
39
42
40
/// Calculate the offset from a pointer
43
41
#[ inline]
44
42
#[ cfg( not( stage0) ) ]
45
- pub fn offset < T > ( ptr : * T , count : int ) -> * T {
46
- unsafe { intrinsics:: offset ( ptr, count) }
43
+ pub unsafe fn offset < T > ( ptr : * T , count : int ) -> * T {
44
+ intrinsics:: offset ( ptr, count)
47
45
}
48
46
49
- /// Calculate the offset from a mut pointer
47
+ /// Calculate the offset from a mut pointer. The count *must* be in bounds or
48
+ /// otherwise the loads of this address are undefined.
50
49
#[ inline]
51
50
#[ cfg( not( stage0) ) ]
52
- pub fn mut_offset < T > ( ptr : * mut T , count : int ) -> * mut T {
53
- unsafe { intrinsics:: offset ( ptr as * T , count) as * mut T }
51
+ pub unsafe fn mut_offset < T > ( ptr : * mut T , count : int ) -> * mut T {
52
+ intrinsics:: offset ( ptr as * T , count) as * mut T
54
53
}
55
54
56
55
/// Return the offset of the first null pointer in `buf`.
@@ -293,8 +292,7 @@ pub trait RawPtr<T> {
293
292
fn is_not_null ( & self ) -> bool ;
294
293
fn to_uint ( & self ) -> uint ;
295
294
unsafe fn to_option ( & self ) -> Option < & T > ;
296
- fn offset ( & self , count : int ) -> Self ;
297
- unsafe fn offset_inbounds ( self , count : int ) -> Self ;
295
+ unsafe fn offset ( self , count : int ) -> Self ;
298
296
}
299
297
300
298
/// Extension methods for immutable pointers
@@ -332,16 +330,10 @@ impl<T> RawPtr<T> for *T {
332
330
}
333
331
}
334
332
335
- /// Calculates the offset from a pointer.
336
- #[ inline]
337
- fn offset ( & self , count : int ) -> * T { offset ( * self , count) }
338
-
339
333
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
340
334
/// the object, or one-byte-past-the-end.
341
335
#[ inline]
342
- unsafe fn offset_inbounds ( self , count : int ) -> * T {
343
- intrinsics:: offset_inbounds ( self , count)
344
- }
336
+ unsafe fn offset ( self , count : int ) -> * T { offset ( self , count) }
345
337
}
346
338
347
339
/// Extension methods for mutable pointers
@@ -379,20 +371,14 @@ impl<T> RawPtr<T> for *mut T {
379
371
}
380
372
}
381
373
382
- /// Calculates the offset from a mutable pointer.
383
- #[ inline]
384
- fn offset ( & self , count : int ) -> * mut T { mut_offset ( * self , count) }
385
-
386
374
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
387
375
/// the object, or one-byte-past-the-end. An arithmetic overflow is also
388
376
/// undefined behaviour.
389
377
///
390
378
/// This method should be preferred over `offset` when the guarantee can be
391
379
/// satisfied, to enable better optimization.
392
380
#[ inline]
393
- unsafe fn offset_inbounds ( self , count : int ) -> * mut T {
394
- intrinsics:: offset_inbounds ( self as * T , count) as * mut T
395
- }
381
+ unsafe fn offset ( self , count : int ) -> * mut T { mut_offset ( self , count) }
396
382
}
397
383
398
384
// Equality for pointers
@@ -513,46 +499,6 @@ impl<T> Ord for *mut T {
513
499
}
514
500
}
515
501
516
- #[ cfg( not( test) ) ]
517
- impl < T , I : Int > Add < I , * T > for * T {
518
- /// Add an integer value to a pointer to get an offset pointer.
519
- /// Is calculated according to the size of the type pointed to.
520
- #[ inline]
521
- fn add ( & self , rhs : & I ) -> * T {
522
- self . offset ( rhs. to_int ( ) as int )
523
- }
524
- }
525
-
526
- #[ cfg( not( test) ) ]
527
- impl < T , I : Int > Sub < I , * T > for * T {
528
- /// Subtract an integer value from a pointer to get an offset pointer.
529
- /// Is calculated according to the size of the type pointed to.
530
- #[ inline]
531
- fn sub ( & self , rhs : & I ) -> * T {
532
- self . offset ( -rhs. to_int ( ) as int )
533
- }
534
- }
535
-
536
- #[ cfg( not( test) ) ]
537
- impl < T , I : Int > Add < I , * mut T > for * mut T {
538
- /// Add an integer value to a pointer to get an offset pointer.
539
- /// Is calculated according to the size of the type pointed to.
540
- #[ inline]
541
- fn add ( & self , rhs : & I ) -> * mut T {
542
- self . offset ( rhs. to_int ( ) as int )
543
- }
544
- }
545
-
546
- #[ cfg( not( test) ) ]
547
- impl < T , I : Int > Sub < I , * mut T > for * mut T {
548
- /// Subtract an integer value from a pointer to get an offset pointer.
549
- /// Is calculated according to the size of the type pointed to.
550
- #[ inline]
551
- fn sub ( & self , rhs : & I ) -> * mut T {
552
- self . offset ( -rhs. to_int ( ) as int )
553
- }
554
- }
555
-
556
502
#[ cfg( test) ]
557
503
pub mod ptr_tests {
558
504
use super :: * ;
@@ -635,15 +581,15 @@ pub mod ptr_tests {
635
581
assert ! ( p. is_null( ) ) ;
636
582
assert ! ( !p. is_not_null( ) ) ;
637
583
638
- let q = offset ( p, 1 ) ;
584
+ let q = unsafe { offset ( p, 1 ) } ;
639
585
assert ! ( !q. is_null( ) ) ;
640
586
assert ! ( q. is_not_null( ) ) ;
641
587
642
588
let mp: * mut int = mut_null ( ) ;
643
589
assert ! ( mp. is_null( ) ) ;
644
590
assert ! ( !mp. is_not_null( ) ) ;
645
591
646
- let mq = mp. offset ( 1 ) ;
592
+ let mq = unsafe { mp. offset ( 1 ) } ;
647
593
assert ! ( !mq. is_null( ) ) ;
648
594
assert ! ( mq. is_not_null( ) ) ;
649
595
}
@@ -672,20 +618,20 @@ pub mod ptr_tests {
672
618
unsafe {
673
619
let xs = ~[ 5 , ..16 ] ;
674
620
let mut ptr = to_ptr ( xs) ;
675
- let end = ptr + 16 ;
621
+ let end = ptr. offset ( 16 ) ;
676
622
677
623
while ptr < end {
678
624
assert_eq ! ( * ptr, 5 ) ;
679
- ptr = ptr + 1 u ;
625
+ ptr = ptr. offset ( 1 ) ;
680
626
}
681
627
682
628
let mut xs_mut = xs. clone ( ) ;
683
629
let mut m_ptr = to_mut_ptr ( xs_mut) ;
684
- let m_end = m_ptr + 16i16 ;
630
+ let m_end = m_ptr. offset ( 16 ) ;
685
631
686
632
while m_ptr < m_end {
687
633
* m_ptr += 5 ;
688
- m_ptr = m_ptr + 1u8 ;
634
+ m_ptr = m_ptr. offset ( 1 ) ;
689
635
}
690
636
691
637
assert_eq ! ( xs_mut, ~[ 10 , ..16 ] ) ;
@@ -702,17 +648,17 @@ pub mod ptr_tests {
702
648
let ptr = to_ptr ( xs) ;
703
649
704
650
while idx >= 0i8 {
705
- assert_eq ! ( * ( ptr + idx ) , idx as int) ;
651
+ assert_eq ! ( * ( ptr. offset ( idx as int ) ) , idx as int) ;
706
652
idx = idx - 1i8 ;
707
653
}
708
654
709
655
let mut xs_mut = xs. clone ( ) ;
710
656
let m_start = to_mut_ptr ( xs_mut) ;
711
- let mut m_ptr = m_start + 9u32 ;
657
+ let mut m_ptr = m_start. offset ( 9 ) ;
712
658
713
659
while m_ptr >= m_start {
714
660
* m_ptr += * m_ptr;
715
- m_ptr = m_ptr - 1i8 ;
661
+ m_ptr = m_ptr. offset ( - 1 ) ;
716
662
}
717
663
718
664
assert_eq ! ( xs_mut, ~[ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 ] ) ;
0 commit comments