Skip to content

Commit 46333d5

Browse files
committed
Deprecate Zero and One traits
1 parent 0da49dc commit 46333d5

File tree

28 files changed

+233
-230
lines changed

28 files changed

+233
-230
lines changed

src/libcollections/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl Iterator<(uint, uint)> for ElementSwaps {
228228
#[inline]
229229
fn size_hint(&self) -> (uint, Option<uint>) {
230230
// For a vector of size n, there are exactly n! permutations.
231-
let n = range(2, self.sdir.len() + 1).product();
231+
let n = range(2, self.sdir.len() + 1).product(1);
232232
(n - self.swaps_made, Some(n - self.swaps_made))
233233
}
234234
}

src/libcollections/str.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<S: Str> StrVector for [S] {
116116
}
117117

118118
// `len` calculation may overflow but push_str will check boundaries
119-
let len = self.iter().map(|s| s.as_slice().len()).sum();
119+
let len = self.iter().map(|s| s.as_slice().len()).sum(0);
120120

121121
let mut result = String::with_capacity(len);
122122

@@ -140,7 +140,7 @@ impl<S: Str> StrVector for [S] {
140140
// this is wrong without the guarantee that `self` is non-empty
141141
// `len` calculation may overflow but push_str but will check boundaries
142142
let len = sep.len() * (self.len() - 1)
143-
+ self.iter().map(|s| s.as_slice().len()).sum();
143+
+ self.iter().map(|s| s.as_slice().len()).sum(0);
144144
let mut result = String::with_capacity(len);
145145
let mut first = true;
146146

@@ -2151,7 +2151,7 @@ mod tests {
21512151
#[test]
21522152
fn test_str_container() {
21532153
fn sum_len(v: &[&str]) -> uint {
2154-
v.iter().map(|x| x.len()).sum()
2154+
v.iter().map(|x| x.len()).sum(0)
21552155
}
21562156

21572157
let s = String::from_str("01234");

src/libcore/fmt/float.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use char;
1414
use fmt;
1515
use iter::{range, DoubleEndedIterator};
1616
use num::{Float, FPNaN, FPInfinite, ToPrimitive, Primitive};
17-
use num::{Zero, One, cast};
17+
use num::cast;
1818
use result::Ok;
1919
use slice::{mod, SlicePrelude};
2020
use str::StrPrelude;
@@ -97,8 +97,8 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
9797
_ => ()
9898
}
9999

100-
let _0: T = Zero::zero();
101-
let _1: T = One::one();
100+
let _0: T = Float::zero();
101+
let _1: T = Float::one();
102102

103103
match num.classify() {
104104
FPNaN => return f("NaN".as_bytes()),

src/libcore/fmt/num.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use fmt;
1818
use iter::DoubleEndedIterator;
19-
use num::{Int, cast, zero};
19+
use num::{Int, cast};
2020
use slice::SlicePrelude;
2121

2222
/// A type that represents a specific radix
@@ -38,7 +38,7 @@ trait GenericRadix {
3838
let mut buf = [0u8, ..64];
3939
let base = cast(self.base()).unwrap();
4040
let mut curr = buf.len();
41-
let is_positive = x >= zero();
41+
let is_positive = x >= Int::zero();
4242
if is_positive {
4343
// Accumulate each digit of the number from the least significant
4444
// to the most significant figure.
@@ -47,7 +47,7 @@ trait GenericRadix {
4747
x = x / base; // Deaccumulate the number.
4848
*byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer.
4949
curr -= 1;
50-
if x == zero() { break; } // No more digits left to accumulate.
50+
if x == Int::zero() { break; } // No more digits left to accumulate.
5151
}
5252
} else {
5353
// Do the same as above, but accounting for two's complement.
@@ -56,7 +56,7 @@ trait GenericRadix {
5656
x = x / base; // Deaccumulate the number.
5757
*byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer.
5858
curr -= 1;
59-
if x == zero() { break; } // No more digits left to accumulate.
59+
if x == Int::zero() { break; } // No more digits left to accumulate.
6060
}
6161
}
6262
f.pad_integral(is_positive, self.prefix(), buf[curr..])

src/libcore/iter.rs

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

6161
use clone::Clone;
6262
use cmp;
63-
use cmp::{PartialOrd, Ord};
63+
use cmp::Ord;
6464
use mem;
65-
use num::{Zero, One, ToPrimitive, Int};
66-
use ops::{Add, Mul, Sub};
65+
use num::{ToPrimitive, Int};
66+
use ops::{Add, Mul};
6767
use option::{Option, Some, None};
6868
use uint;
6969
#[deprecated = "renamed to Extend"] pub use self::Extend as Extendable;
@@ -402,7 +402,7 @@ pub trait Iterator<A> {
402402
/// .inspect(|&x| println!("filtering {}", x))
403403
/// .filter(|&x| x % 2 == 0)
404404
/// .inspect(|&x| println!("{} made it through", x))
405-
/// .sum();
405+
/// .sum(0);
406406
/// println!("{}", sum);
407407
/// ```
408408
#[inline]
@@ -780,16 +780,15 @@ pub trait AdditiveIterator<A> {
780780
///
781781
/// let a = [1i, 2, 3, 4, 5];
782782
/// let mut it = a.iter().map(|&x| x);
783-
/// assert!(it.sum() == 15);
783+
/// assert!(it.sum(0) == 15);
784784
/// ```
785-
fn sum(&mut self) -> A;
785+
fn sum(&mut self, init: A) -> A;
786786
}
787787

788-
impl<A: Add<A, A> + Zero, T: Iterator<A>> AdditiveIterator<A> for T {
788+
impl<A: Add<A, A>, T: Iterator<A>> AdditiveIterator<A> for T {
789789
#[inline]
790-
fn sum(&mut self) -> A {
791-
let zero: A = Zero::zero();
792-
self.fold(zero, |s, x| s + x)
790+
fn sum(&mut self, init: A) -> A {
791+
self.fold(init, |s, x| s + x)
793792
}
794793
}
795794

@@ -803,20 +802,19 @@ pub trait MultiplicativeIterator<A> {
803802
/// use std::iter::{count, MultiplicativeIterator};
804803
///
805804
/// fn factorial(n: uint) -> uint {
806-
/// count(1u, 1).take_while(|&i| i <= n).product()
805+
/// count(1u, 1).take_while(|&i| i <= n).product(1)
807806
/// }
808807
/// assert!(factorial(0) == 1);
809808
/// assert!(factorial(1) == 1);
810809
/// assert!(factorial(5) == 120);
811810
/// ```
812-
fn product(&mut self) -> A;
811+
fn product(&mut self, init: A) -> A;
813812
}
814813

815-
impl<A: Mul<A, A> + One, T: Iterator<A>> MultiplicativeIterator<A> for T {
814+
impl<A: Mul<A, A>, T: Iterator<A>> MultiplicativeIterator<A> for T {
816815
#[inline]
817-
fn product(&mut self) -> A {
818-
let one: A = One::one();
819-
self.fold(one, |p, x| p * x)
816+
fn product(&mut self, init: A) -> A {
817+
self.fold(init, |p, x| p * x)
820818
}
821819
}
822820

@@ -1905,7 +1903,7 @@ impl<A: Add<A, A> + Clone> Iterator<A> for Counter<A> {
19051903
pub struct Range<A> {
19061904
state: A,
19071905
stop: A,
1908-
one: A
1906+
one: A,
19091907
}
19101908

19111909
/// Returns an iterator over the given range [start, stop) (that is, starting
@@ -1922,12 +1920,16 @@ pub struct Range<A> {
19221920
/// }
19231921
/// ```
19241922
#[inline]
1925-
pub fn range<A: Add<A, A> + PartialOrd + Clone + One>(start: A, stop: A) -> Range<A> {
1926-
Range{state: start, stop: stop, one: One::one()}
1923+
pub fn range<A: Int>(start: A, stop: A) -> Range<A> {
1924+
Range {
1925+
state: start,
1926+
stop: stop,
1927+
one: Int::one(),
1928+
}
19271929
}
19281930

19291931
// FIXME: #10414: Unfortunate type bound
1930-
impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for Range<A> {
1932+
impl<A: Int + ToPrimitive> Iterator<A> for Range<A> {
19311933
#[inline]
19321934
fn next(&mut self) -> Option<A> {
19331935
if self.state < self.stop {
@@ -1974,7 +1976,7 @@ impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for Range<A> {
19741976

19751977
/// `Int` is required to ensure the range will be the same regardless of
19761978
/// the direction it is consumed.
1977-
impl<A: Int + PartialOrd + Clone + ToPrimitive> DoubleEndedIterator<A> for Range<A> {
1979+
impl<A: Int + ToPrimitive> DoubleEndedIterator<A> for Range<A> {
19781980
#[inline]
19791981
fn next_back(&mut self) -> Option<A> {
19801982
if self.stop > self.state {
@@ -1995,12 +1997,14 @@ pub struct RangeInclusive<A> {
19951997

19961998
/// Return an iterator over the range [start, stop]
19971999
#[inline]
1998-
pub fn range_inclusive<A: Add<A, A> + PartialOrd + Clone + One>(start: A, stop: A)
1999-
-> RangeInclusive<A> {
2000-
RangeInclusive{range: range(start, stop), done: false}
2000+
pub fn range_inclusive<A: Int>(start: A, stop: A) -> RangeInclusive<A> {
2001+
RangeInclusive {
2002+
range: range(start, stop),
2003+
done: false,
2004+
}
20012005
}
20022006

2003-
impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for RangeInclusive<A> {
2007+
impl<A: Int + ToPrimitive> Iterator<A> for RangeInclusive<A> {
20042008
#[inline]
20052009
fn next(&mut self) -> Option<A> {
20062010
match self.range.next() {
@@ -2032,8 +2036,7 @@ impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for RangeInclu
20322036
}
20332037
}
20342038

2035-
impl<A: Sub<A, A> + Int + PartialOrd + Clone + ToPrimitive> DoubleEndedIterator<A>
2036-
for RangeInclusive<A> {
2039+
impl<A: Int + ToPrimitive> DoubleEndedIterator<A> for RangeInclusive<A> {
20372040
#[inline]
20382041
fn next_back(&mut self) -> Option<A> {
20392042
if self.range.stop > self.range.state {
@@ -2061,7 +2064,7 @@ pub struct RangeStep<A> {
20612064
/// Return an iterator over the range [start, stop) by `step`. It handles overflow by stopping.
20622065
#[inline]
20632066
pub fn range_step<A: Int>(start: A, stop: A, step: A) -> RangeStep<A> {
2064-
let rev = step < Zero::zero();
2067+
let rev = step < Int::zero();
20652068
RangeStep{state: start, stop: stop, step: step, rev: rev}
20662069
}
20672070

@@ -2094,8 +2097,14 @@ pub struct RangeStepInclusive<A> {
20942097
/// Return an iterator over the range [start, stop] by `step`. It handles overflow by stopping.
20952098
#[inline]
20962099
pub fn range_step_inclusive<A: Int>(start: A, stop: A, step: A) -> RangeStepInclusive<A> {
2097-
let rev = step < Zero::zero();
2098-
RangeStepInclusive{state: start, stop: stop, step: step, rev: rev, done: false}
2100+
let rev = step < Int::zero();
2101+
RangeStepInclusive {
2102+
state: start,
2103+
stop: stop,
2104+
step: step,
2105+
rev: rev,
2106+
done: false,
2107+
}
20992108
}
21002109

21012110
impl<A: Int> Iterator<A> for RangeStepInclusive<A> {

src/libcore/num/f32.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,15 @@ impl Float for f32 {
114114
#[inline]
115115
fn neg_infinity() -> f32 { NEG_INFINITY }
116116

117+
#[inline]
118+
fn zero() -> f32 { 0.0 }
119+
117120
#[inline]
118121
fn neg_zero() -> f32 { -0.0 }
119122

123+
#[inline]
124+
fn one() -> f32 { 1.0 }
125+
120126
/// Returns `true` if the number is NaN.
121127
#[inline]
122128
fn is_nan(self) -> bool { self != self }

src/libcore/num/f64.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,15 @@ impl Float for f64 {
120120
#[inline]
121121
fn neg_infinity() -> f64 { NEG_INFINITY }
122122

123+
#[inline]
124+
fn zero() -> f64 { 0.0 }
125+
123126
#[inline]
124127
fn neg_zero() -> f64 { -0.0 }
125128

129+
#[inline]
130+
fn one() -> f64 { 1.0 }
131+
126132
/// Returns `true` if the number is NaN.
127133
#[inline]
128134
fn is_nan(self) -> bool { self != self }

0 commit comments

Comments
 (0)