Skip to content

Commit 90db862

Browse files
committed
auto merge of #7631 : MarkJr94/rust/ptr_arithmetic, r=thestinger
Added Add and Sub traits for pointer arithmetic. Any type that is a ```std::num::Int``` can be added to or subtracted from a pointer. Also my additions did not require any unsafe code, and the operators themselves are safe. Fixes #2122.
2 parents 8fa0973 + 294999c commit 90db862

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

src/libstd/ptr.rs

+96
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use option::{Option, Some, None};
1515
use sys;
1616
use unstable::intrinsics;
1717
use util::swap;
18+
use ops::{Add,Sub};
19+
use num::Int;
1820

1921
#[cfg(not(test))] use cmp::{Eq, Ord};
2022
use uint;
@@ -384,6 +386,46 @@ impl<T> Ord for *const T {
384386
}
385387
}
386388

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+
387429
#[cfg(test)]
388430
pub mod ptr_tests {
389431
use super::*;
@@ -501,6 +543,60 @@ pub mod ptr_tests {
501543
}
502544
}
503545

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 + 1u;
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+
504600
#[test]
505601
fn test_ptr_array_each_with_len() {
506602
unsafe {

0 commit comments

Comments
 (0)