Skip to content

Commit e51cc08

Browse files
committed
Move checked arithmetic operators into Int trait
1 parent 7e57cd8 commit e51cc08

File tree

20 files changed

+324
-371
lines changed

20 files changed

+324
-371
lines changed

src/etc/vim/syntax/rust.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ syn keyword rustTrait FromIterator IntoIterator Extend ExactSize
9797
syn keyword rustTrait Iterator DoubleEndedIterator
9898
syn keyword rustTrait RandomAccessIterator CloneableIterator
9999
syn keyword rustTrait OrdIterator MutableDoubleEndedIterator
100-
syn keyword rustTrait Num NumCast CheckedAdd CheckedSub CheckedMul CheckedDiv
100+
syn keyword rustTrait Num NumCast
101101
syn keyword rustTrait Signed Unsigned Primitive Int Float
102102
syn keyword rustTrait FloatMath ToPrimitive FromPrimitive
103103
syn keyword rustTrait Box

src/libarena/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl Drop for Arena {
132132

133133
#[inline]
134134
fn round_up(base: uint, align: uint) -> uint {
135-
(base.checked_add(&(align - 1))).unwrap() & !(align - 1)
135+
(base.checked_add(align - 1)).unwrap() & !(align - 1)
136136
}
137137

138138
// Walk down a chunk, running the destructors for any objects stored
@@ -376,8 +376,8 @@ fn calculate_size<T>(capacity: uint) -> uint {
376376
let mut size = mem::size_of::<TypedArenaChunk<T>>();
377377
size = round_up(size, mem::min_align_of::<T>());
378378
let elem_size = mem::size_of::<T>();
379-
let elems_size = elem_size.checked_mul(&capacity).unwrap();
380-
size = size.checked_add(&elems_size).unwrap();
379+
let elems_size = elem_size.checked_mul(capacity).unwrap();
380+
size = size.checked_add(elems_size).unwrap();
381381
size
382382
}
383383

@@ -432,7 +432,7 @@ impl<T> TypedArenaChunk<T> {
432432
#[inline]
433433
fn end(&self) -> *const u8 {
434434
unsafe {
435-
let size = mem::size_of::<T>().checked_mul(&self.capacity).unwrap();
435+
let size = mem::size_of::<T>().checked_mul(self.capacity).unwrap();
436436
self.start().offset(size as int)
437437
}
438438
}
@@ -481,7 +481,7 @@ impl<T> TypedArena<T> {
481481
fn grow(&self) {
482482
unsafe {
483483
let chunk = *self.first.borrow_mut();
484-
let new_capacity = (*chunk).capacity.checked_mul(&2).unwrap();
484+
let new_capacity = (*chunk).capacity.checked_mul(2).unwrap();
485485
let chunk = TypedArenaChunk::<T>::new(chunk, new_capacity);
486486
self.ptr.set((*chunk).start() as *const T);
487487
self.end.set((*chunk).end() as *const T);

src/libcollections/vec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<T> Vec<T> {
161161
} else if capacity == 0 {
162162
Vec::new()
163163
} else {
164-
let size = capacity.checked_mul(&mem::size_of::<T>())
164+
let size = capacity.checked_mul(mem::size_of::<T>())
165165
.expect("capacity overflow");
166166
let ptr = unsafe { allocate(size, mem::min_align_of::<T>()) };
167167
Vec { ptr: ptr as *mut T, len: 0, cap: capacity }
@@ -601,7 +601,7 @@ impl<T> Vec<T> {
601601
#[unstable = "matches collection reform specification, waiting for dust to settle"]
602602
pub fn reserve(&mut self, additional: uint) {
603603
if self.cap - self.len < additional {
604-
match self.len.checked_add(&additional) {
604+
match self.len.checked_add(additional) {
605605
None => panic!("Vec::reserve: `uint` overflow"),
606606
// if the checked_add
607607
Some(new_cap) => {
@@ -638,7 +638,7 @@ impl<T> Vec<T> {
638638
#[unstable = "matches collection reform specification, waiting for dust to settle"]
639639
pub fn reserve_exact(&mut self, additional: uint) {
640640
if self.cap - self.len < additional {
641-
match self.len.checked_add(&additional) {
641+
match self.len.checked_add(additional) {
642642
None => panic!("Vec::reserve: `uint` overflow"),
643643
Some(new_cap) => self.grow_capacity(new_cap)
644644
}
@@ -971,7 +971,7 @@ impl<T> Vec<T> {
971971
pub fn push(&mut self, value: T) {
972972
if mem::size_of::<T>() == 0 {
973973
// zero-size types consume no memory, so we can't rely on the address space running out
974-
self.len = self.len.checked_add(&1).expect("length overflow");
974+
self.len = self.len.checked_add(1).expect("length overflow");
975975
unsafe { mem::forget(value); }
976976
return
977977
}
@@ -1064,7 +1064,7 @@ impl<T> Vec<T> {
10641064
if mem::size_of::<T>() == 0 { return }
10651065

10661066
if capacity > self.cap {
1067-
let size = capacity.checked_mul(&mem::size_of::<T>())
1067+
let size = capacity.checked_mul(mem::size_of::<T>())
10681068
.expect("capacity overflow");
10691069
unsafe {
10701070
self.ptr = alloc_or_realloc(self.ptr, self.cap * mem::size_of::<T>(), size);

src/libcore/iter.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ This `for` loop syntax can be applied to any iterator over any type.
6060

6161
use clone::Clone;
6262
use cmp;
63-
use cmp::{PartialEq, PartialOrd, Ord};
63+
use cmp::{PartialOrd, Ord};
6464
use mem;
65-
use num::{Zero, One, CheckedAdd, CheckedSub, ToPrimitive, Int};
65+
use num::{Zero, One, ToPrimitive, Int};
6666
use ops::{Add, Mul, Sub};
6767
use option::{Option, Some, None};
6868
use uint;
@@ -1093,7 +1093,7 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for Chain<T, U> {
10931093
let lower = a_lower.saturating_add(b_lower);
10941094

10951095
let upper = match (a_upper, b_upper) {
1096-
(Some(x), Some(y)) => x.checked_add(&y),
1096+
(Some(x), Some(y)) => x.checked_add(y),
10971097
_ => None
10981098
};
10991099

@@ -1415,7 +1415,7 @@ impl<A, T: Iterator<A>> Iterator<A> for Peekable<A, T> {
14151415
if self.peeked.is_some() {
14161416
let lo = lo.saturating_add(1);
14171417
let hi = match hi {
1418-
Some(x) => x.checked_add(&1),
1418+
Some(x) => x.checked_add(1),
14191419
None => None
14201420
};
14211421
(lo, hi)
@@ -1680,7 +1680,7 @@ impl<'a, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for FlatMap<'a, A, T,
16801680
let (blo, bhi) = self.backiter.as_ref().map_or((0, Some(0)), |it| it.size_hint());
16811681
let lo = flo.saturating_add(blo);
16821682
match (self.iter.size_hint(), fhi, bhi) {
1683-
((0, Some(0)), Some(a), Some(b)) => (lo, a.checked_add(&b)),
1683+
((0, Some(0)), Some(a), Some(b)) => (lo, a.checked_add(b)),
16841684
_ => (lo, None)
16851685
}
16861686
}
@@ -1946,15 +1946,15 @@ impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for Range<A> {
19461946
// the i64/u64 might lie within their range.
19471947
let bound = match self.state.to_i64() {
19481948
Some(a) => {
1949-
let sz = self.stop.to_i64().map(|b| b.checked_sub(&a));
1949+
let sz = self.stop.to_i64().map(|b| b.checked_sub(a));
19501950
match sz {
19511951
Some(Some(bound)) => bound.to_uint(),
19521952
_ => None,
19531953
}
19541954
},
19551955
None => match self.state.to_u64() {
19561956
Some(a) => {
1957-
let sz = self.stop.to_u64().map(|b| b.checked_sub(&a));
1957+
let sz = self.stop.to_u64().map(|b| b.checked_sub(a));
19581958
match sz {
19591959
Some(Some(bound)) => bound.to_uint(),
19601960
_ => None
@@ -2024,7 +2024,7 @@ impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for RangeInclu
20242024
} else {
20252025
let lo = lo.saturating_add(1);
20262026
let hi = match hi {
2027-
Some(x) => x.checked_add(&1),
2027+
Some(x) => x.checked_add(1),
20282028
None => None
20292029
};
20302030
(lo, hi)
@@ -2060,18 +2060,17 @@ pub struct RangeStep<A> {
20602060

20612061
/// Return an iterator over the range [start, stop) by `step`. It handles overflow by stopping.
20622062
#[inline]
2063-
pub fn range_step<A: CheckedAdd + PartialOrd +
2064-
Clone + Zero>(start: A, stop: A, step: A) -> RangeStep<A> {
2063+
pub fn range_step<A: Int>(start: A, stop: A, step: A) -> RangeStep<A> {
20652064
let rev = step < Zero::zero();
20662065
RangeStep{state: start, stop: stop, step: step, rev: rev}
20672066
}
20682067

2069-
impl<A: CheckedAdd + PartialOrd + Clone> Iterator<A> for RangeStep<A> {
2068+
impl<A: Int> Iterator<A> for RangeStep<A> {
20702069
#[inline]
20712070
fn next(&mut self) -> Option<A> {
20722071
if (self.rev && self.state > self.stop) || (!self.rev && self.state < self.stop) {
2073-
let result = self.state.clone();
2074-
match self.state.checked_add(&self.step) {
2072+
let result = self.state;
2073+
match self.state.checked_add(self.step) {
20752074
Some(x) => self.state = x,
20762075
None => self.state = self.stop.clone()
20772076
}
@@ -2094,19 +2093,18 @@ pub struct RangeStepInclusive<A> {
20942093

20952094
/// Return an iterator over the range [start, stop] by `step`. It handles overflow by stopping.
20962095
#[inline]
2097-
pub fn range_step_inclusive<A: CheckedAdd + PartialOrd + Clone + Zero>(start: A, stop: A,
2098-
step: A) -> RangeStepInclusive<A> {
2096+
pub fn range_step_inclusive<A: Int>(start: A, stop: A, step: A) -> RangeStepInclusive<A> {
20992097
let rev = step < Zero::zero();
21002098
RangeStepInclusive{state: start, stop: stop, step: step, rev: rev, done: false}
21012099
}
21022100

2103-
impl<A: CheckedAdd + PartialOrd + Clone + PartialEq> Iterator<A> for RangeStepInclusive<A> {
2101+
impl<A: Int> Iterator<A> for RangeStepInclusive<A> {
21042102
#[inline]
21052103
fn next(&mut self) -> Option<A> {
21062104
if !self.done && ((self.rev && self.state >= self.stop) ||
21072105
(!self.rev && self.state <= self.stop)) {
2108-
let result = self.state.clone();
2109-
match self.state.checked_add(&self.step) {
2106+
let result = self.state;
2107+
match self.state.checked_add(self.step) {
21102108
Some(x) => self.state = x,
21112109
None => self.done = true
21122110
}

0 commit comments

Comments
 (0)