Skip to content

Commit

Permalink
Deprecate Zero and One traits
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Nov 12, 2014
1 parent 0da49dc commit 46333d5
Show file tree
Hide file tree
Showing 28 changed files with 233 additions and 230 deletions.
2 changes: 1 addition & 1 deletion src/libcollections/slice.rs
Expand Up @@ -228,7 +228,7 @@ impl Iterator<(uint, uint)> for ElementSwaps {
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
// For a vector of size n, there are exactly n! permutations.
let n = range(2, self.sdir.len() + 1).product();
let n = range(2, self.sdir.len() + 1).product(1);
(n - self.swaps_made, Some(n - self.swaps_made))
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libcollections/str.rs
Expand Up @@ -116,7 +116,7 @@ impl<S: Str> StrVector for [S] {
}

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

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

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

Expand Down Expand Up @@ -2151,7 +2151,7 @@ mod tests {
#[test]
fn test_str_container() {
fn sum_len(v: &[&str]) -> uint {
v.iter().map(|x| x.len()).sum()
v.iter().map(|x| x.len()).sum(0)
}

let s = String::from_str("01234");
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/fmt/float.rs
Expand Up @@ -14,7 +14,7 @@ use char;
use fmt;
use iter::{range, DoubleEndedIterator};
use num::{Float, FPNaN, FPInfinite, ToPrimitive, Primitive};
use num::{Zero, One, cast};
use num::cast;
use result::Ok;
use slice::{mod, SlicePrelude};
use str::StrPrelude;
Expand Down Expand Up @@ -97,8 +97,8 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
_ => ()
}

let _0: T = Zero::zero();
let _1: T = One::one();
let _0: T = Float::zero();
let _1: T = Float::one();

match num.classify() {
FPNaN => return f("NaN".as_bytes()),
Expand Down
8 changes: 4 additions & 4 deletions src/libcore/fmt/num.rs
Expand Up @@ -16,7 +16,7 @@

use fmt;
use iter::DoubleEndedIterator;
use num::{Int, cast, zero};
use num::{Int, cast};
use slice::SlicePrelude;

/// A type that represents a specific radix
Expand All @@ -38,7 +38,7 @@ trait GenericRadix {
let mut buf = [0u8, ..64];
let base = cast(self.base()).unwrap();
let mut curr = buf.len();
let is_positive = x >= zero();
let is_positive = x >= Int::zero();
if is_positive {
// Accumulate each digit of the number from the least significant
// to the most significant figure.
Expand All @@ -47,7 +47,7 @@ trait GenericRadix {
x = x / base; // Deaccumulate the number.
*byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer.
curr -= 1;
if x == zero() { break; } // No more digits left to accumulate.
if x == Int::zero() { break; } // No more digits left to accumulate.
}
} else {
// Do the same as above, but accounting for two's complement.
Expand All @@ -56,7 +56,7 @@ trait GenericRadix {
x = x / base; // Deaccumulate the number.
*byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer.
curr -= 1;
if x == zero() { break; } // No more digits left to accumulate.
if x == Int::zero() { break; } // No more digits left to accumulate.
}
}
f.pad_integral(is_positive, self.prefix(), buf[curr..])
Expand Down
69 changes: 39 additions & 30 deletions src/libcore/iter.rs
Expand Up @@ -60,10 +60,10 @@ This `for` loop syntax can be applied to any iterator over any type.

use clone::Clone;
use cmp;
use cmp::{PartialOrd, Ord};
use cmp::Ord;
use mem;
use num::{Zero, One, ToPrimitive, Int};
use ops::{Add, Mul, Sub};
use num::{ToPrimitive, Int};
use ops::{Add, Mul};
use option::{Option, Some, None};
use uint;
#[deprecated = "renamed to Extend"] pub use self::Extend as Extendable;
Expand Down Expand Up @@ -402,7 +402,7 @@ pub trait Iterator<A> {
/// .inspect(|&x| println!("filtering {}", x))
/// .filter(|&x| x % 2 == 0)
/// .inspect(|&x| println!("{} made it through", x))
/// .sum();
/// .sum(0);
/// println!("{}", sum);
/// ```
#[inline]
Expand Down Expand Up @@ -780,16 +780,15 @@ pub trait AdditiveIterator<A> {
///
/// let a = [1i, 2, 3, 4, 5];
/// let mut it = a.iter().map(|&x| x);
/// assert!(it.sum() == 15);
/// assert!(it.sum(0) == 15);
/// ```
fn sum(&mut self) -> A;
fn sum(&mut self, init: A) -> A;
}

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

Expand All @@ -803,20 +802,19 @@ pub trait MultiplicativeIterator<A> {
/// use std::iter::{count, MultiplicativeIterator};
///
/// fn factorial(n: uint) -> uint {
/// count(1u, 1).take_while(|&i| i <= n).product()
/// count(1u, 1).take_while(|&i| i <= n).product(1)
/// }
/// assert!(factorial(0) == 1);
/// assert!(factorial(1) == 1);
/// assert!(factorial(5) == 120);
/// ```
fn product(&mut self) -> A;
fn product(&mut self, init: A) -> A;
}

impl<A: Mul<A, A> + One, T: Iterator<A>> MultiplicativeIterator<A> for T {
impl<A: Mul<A, A>, T: Iterator<A>> MultiplicativeIterator<A> for T {
#[inline]
fn product(&mut self) -> A {
let one: A = One::one();
self.fold(one, |p, x| p * x)
fn product(&mut self, init: A) -> A {
self.fold(init, |p, x| p * x)
}
}

Expand Down Expand Up @@ -1905,7 +1903,7 @@ impl<A: Add<A, A> + Clone> Iterator<A> for Counter<A> {
pub struct Range<A> {
state: A,
stop: A,
one: A
one: A,
}

/// Returns an iterator over the given range [start, stop) (that is, starting
Expand All @@ -1922,12 +1920,16 @@ pub struct Range<A> {
/// }
/// ```
#[inline]
pub fn range<A: Add<A, A> + PartialOrd + Clone + One>(start: A, stop: A) -> Range<A> {
Range{state: start, stop: stop, one: One::one()}
pub fn range<A: Int>(start: A, stop: A) -> Range<A> {
Range {
state: start,
stop: stop,
one: Int::one(),
}
}

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

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

/// Return an iterator over the range [start, stop]
#[inline]
pub fn range_inclusive<A: Add<A, A> + PartialOrd + Clone + One>(start: A, stop: A)
-> RangeInclusive<A> {
RangeInclusive{range: range(start, stop), done: false}
pub fn range_inclusive<A: Int>(start: A, stop: A) -> RangeInclusive<A> {
RangeInclusive {
range: range(start, stop),
done: false,
}
}

impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for RangeInclusive<A> {
impl<A: Int + ToPrimitive> Iterator<A> for RangeInclusive<A> {
#[inline]
fn next(&mut self) -> Option<A> {
match self.range.next() {
Expand Down Expand Up @@ -2032,8 +2036,7 @@ impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for RangeInclu
}
}

impl<A: Sub<A, A> + Int + PartialOrd + Clone + ToPrimitive> DoubleEndedIterator<A>
for RangeInclusive<A> {
impl<A: Int + ToPrimitive> DoubleEndedIterator<A> for RangeInclusive<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
if self.range.stop > self.range.state {
Expand Down Expand Up @@ -2061,7 +2064,7 @@ pub struct RangeStep<A> {
/// Return an iterator over the range [start, stop) by `step`. It handles overflow by stopping.
#[inline]
pub fn range_step<A: Int>(start: A, stop: A, step: A) -> RangeStep<A> {
let rev = step < Zero::zero();
let rev = step < Int::zero();
RangeStep{state: start, stop: stop, step: step, rev: rev}
}

Expand Down Expand Up @@ -2094,8 +2097,14 @@ pub struct RangeStepInclusive<A> {
/// Return an iterator over the range [start, stop] by `step`. It handles overflow by stopping.
#[inline]
pub fn range_step_inclusive<A: Int>(start: A, stop: A, step: A) -> RangeStepInclusive<A> {
let rev = step < Zero::zero();
RangeStepInclusive{state: start, stop: stop, step: step, rev: rev, done: false}
let rev = step < Int::zero();
RangeStepInclusive {
state: start,
stop: stop,
step: step,
rev: rev,
done: false,
}
}

impl<A: Int> Iterator<A> for RangeStepInclusive<A> {
Expand Down
6 changes: 6 additions & 0 deletions src/libcore/num/f32.rs
Expand Up @@ -114,9 +114,15 @@ impl Float for f32 {
#[inline]
fn neg_infinity() -> f32 { NEG_INFINITY }

#[inline]
fn zero() -> f32 { 0.0 }

#[inline]
fn neg_zero() -> f32 { -0.0 }

#[inline]
fn one() -> f32 { 1.0 }

/// Returns `true` if the number is NaN.
#[inline]
fn is_nan(self) -> bool { self != self }
Expand Down
6 changes: 6 additions & 0 deletions src/libcore/num/f64.rs
Expand Up @@ -120,9 +120,15 @@ impl Float for f64 {
#[inline]
fn neg_infinity() -> f64 { NEG_INFINITY }

#[inline]
fn zero() -> f64 { 0.0 }

#[inline]
fn neg_zero() -> f64 { -0.0 }

#[inline]
fn one() -> f64 { 1.0 }

/// Returns `true` if the number is NaN.
#[inline]
fn is_nan(self) -> bool { self != self }
Expand Down

0 comments on commit 46333d5

Please sign in to comment.