@@ -15,6 +15,8 @@ use option::{Option, Some, None};
15
15
use sys;
16
16
use unstable:: intrinsics;
17
17
use util:: swap;
18
+ use ops:: { Add , Sub } ;
19
+ use num:: Int ;
18
20
19
21
#[ cfg( not( test) ) ] use cmp:: { Eq , Ord } ;
20
22
use uint;
@@ -384,6 +386,46 @@ impl<T> Ord for *const T {
384
386
}
385
387
}
386
388
389
+ #[ cfg( not( test) ) ]
390
+ impl < T , I : Int > Add < I , * T > for * T {
391
+ /// Add an integer value to a pointer to get an offset pointer.
392
+ /// Is calculated according to the size of the type pointed to.
393
+ #[ inline]
394
+ pub fn add ( & self , rhs : & I ) -> * T {
395
+ self . offset ( rhs. to_int ( ) as uint )
396
+ }
397
+ }
398
+
399
+ #[ cfg( not( test) ) ]
400
+ impl < T , I : Int > Sub < I , * T > for * T {
401
+ /// Subtract an integer value from a pointer to get an offset pointer.
402
+ /// Is calculated according to the size of the type pointed to.
403
+ #[ inline]
404
+ pub fn sub ( & self , rhs : & I ) -> * T {
405
+ self . offset ( -rhs. to_int ( ) as uint )
406
+ }
407
+ }
408
+
409
+ #[ cfg( not( test) ) ]
410
+ impl < T , I : Int > Add < I , * mut T > for * mut T {
411
+ /// Add an integer value to a pointer to get an offset pointer.
412
+ /// Is calculated according to the size of the type pointed to.
413
+ #[ inline]
414
+ pub fn add ( & self , rhs : & I ) -> * mut T {
415
+ self . offset ( rhs. to_int ( ) as uint )
416
+ }
417
+ }
418
+
419
+ #[ cfg( not( test) ) ]
420
+ impl < T , I : Int > Sub < I , * mut T > for * mut T {
421
+ /// Subtract an integer value from a pointer to get an offset pointer.
422
+ /// Is calculated according to the size of the type pointed to.
423
+ #[ inline]
424
+ pub fn sub ( & self , rhs : & I ) -> * mut T {
425
+ self . offset ( -rhs. to_int ( ) as uint )
426
+ }
427
+ }
428
+
387
429
#[ cfg( test) ]
388
430
pub mod ptr_tests {
389
431
use super :: * ;
@@ -501,6 +543,60 @@ pub mod ptr_tests {
501
543
}
502
544
}
503
545
546
+ #[ test]
547
+ fn test_ptr_addition ( ) {
548
+ use vec:: raw:: * ;
549
+
550
+ unsafe {
551
+ let xs = ~[ 5 , ..16 ] ;
552
+ let mut ptr = to_ptr ( xs) ;
553
+ let end = ptr + 16 ;
554
+
555
+ while ptr < end {
556
+ assert_eq ! ( * ptr, 5 ) ;
557
+ ptr = ptr + 1 u;
558
+ }
559
+
560
+ let mut xs_mut = xs. clone ( ) ;
561
+ let mut m_ptr = to_mut_ptr ( xs_mut) ;
562
+ let m_end = m_ptr + 16i16 ;
563
+
564
+ while m_ptr < m_end {
565
+ * m_ptr += 5 ;
566
+ m_ptr = m_ptr + 1u8 ;
567
+ }
568
+
569
+ assert_eq ! ( xs_mut, ~[ 10 , ..16 ] ) ;
570
+ }
571
+ }
572
+
573
+ #[ test]
574
+ fn test_ptr_subtraction ( ) {
575
+ use vec:: raw:: * ;
576
+
577
+ unsafe {
578
+ let xs = ~[ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ;
579
+ let mut idx = 9i8 ;
580
+ let ptr = to_ptr ( xs) ;
581
+
582
+ while idx >= 0i8 {
583
+ assert_eq ! ( * ( ptr + idx) , idx as int) ;
584
+ idx = idx - 1i8 ;
585
+ }
586
+
587
+ let mut xs_mut = xs. clone ( ) ;
588
+ let mut m_start = to_mut_ptr ( xs_mut) ;
589
+ let mut m_ptr = m_start + 9u32 ;
590
+
591
+ while m_ptr >= m_start {
592
+ * m_ptr += * m_ptr;
593
+ m_ptr = m_ptr - 1i8 ;
594
+ }
595
+
596
+ assert_eq ! ( xs_mut, ~[ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 , 18 ] ) ;
597
+ }
598
+ }
599
+
504
600
#[ test]
505
601
fn test_ptr_array_each_with_len ( ) {
506
602
unsafe {
0 commit comments