about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorAaron Turon <aturon@mozilla.com>2014-06-05 23:18:51 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-06-06 19:51:31 -0700
commit1bde6e3fcb32ca00cf8a8dfa0977e47f7f4a77bf (patch)
treef5b2c5f46adc3cdc83e596445cc91e5112ea68b6 /src/libcore
parent1bc29924dc8f88c2c118b688f25ffa7c6a212276 (diff)
downloadrust-1bde6e3fcb32ca00cf8a8dfa0977e47f7f4a77bf.tar.gz
rust-1bde6e3fcb32ca00cf8a8dfa0977e47f7f4a77bf.zip
Rename Iterator::len to count
This commit carries out the request from issue #14678:

> The method `Iterator::len()` is surprising, as all the other uses of
> `len()` do not consume the value. `len()` would make more sense to be
> called `count()`, but that would collide with the current
> `Iterator::count(|T| -> bool) -> unit` method. That method, however, is
> a bit redundant, and can be easily replaced with
> `iter.filter(|x| x < 5).count()`.
> After this change, we could then define the `len()` method
> on `iter::ExactSize`.

Closes #14678.

[breaking-change]
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/iter.rs48
-rw-r--r--src/libcore/slice.rs3
-rw-r--r--src/libcore/str.rs3
3 files changed, 25 insertions, 29 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs
index 875c852d8ae..64c53b658ef 100644
--- a/src/libcore/iter.rs
+++ b/src/libcore/iter.rs
@@ -529,11 +529,11 @@ pub trait Iterator<A> {
     /// ```rust
     /// let a = [1, 2, 3, 4, 5];
     /// let mut it = a.iter();
-    /// assert!(it.len() == 5);
-    /// assert!(it.len() == 0);
+    /// assert!(it.count() == 5);
+    /// assert!(it.count() == 0);
     /// ```
     #[inline]
-    fn len(&mut self) -> uint {
+    fn count(&mut self) -> uint {
         self.fold(0, |cnt, _x| cnt + 1)
     }
 
@@ -591,16 +591,6 @@ pub trait Iterator<A> {
         None
     }
 
-    /// Count the number of elements satisfying the specified predicate
-    #[inline]
-    fn count(&mut self, predicate: |A| -> bool) -> uint {
-        let mut i = 0;
-        for x in *self {
-            if predicate(x) { i += 1 }
-        }
-        i
-    }
-
     /// Return the element that gives the maximum value from the
     /// specified function.
     ///
@@ -738,6 +728,14 @@ pub trait ExactSize<A> : DoubleEndedIterator<A> {
         }
         None
     }
+
+    #[inline]
+    /// Return the exact length of the iterator.
+    fn len(&self) -> uint {
+        let (lower, upper) = self.size_hint();
+        assert!(upper == Some(lower));
+        lower
+    }
 }
 
 // All adaptors that preserve the size of the wrapped iterator are fine
@@ -2594,9 +2592,9 @@ mod tests {
     #[test]
     fn test_iterator_len() {
         let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
-        assert_eq!(v.slice(0, 4).iter().len(), 4);
-        assert_eq!(v.slice(0, 10).iter().len(), 10);
-        assert_eq!(v.slice(0, 0).iter().len(), 0);
+        assert_eq!(v.slice(0, 4).iter().count(), 4);
+        assert_eq!(v.slice(0, 10).iter().count(), 10);
+        assert_eq!(v.slice(0, 0).iter().count(), 0);
     }
 
     #[test]
@@ -2712,9 +2710,9 @@ mod tests {
     #[test]
     fn test_count() {
         let xs = &[1, 2, 2, 1, 5, 9, 0, 2];
-        assert_eq!(xs.iter().count(|x| *x == 2), 3);
-        assert_eq!(xs.iter().count(|x| *x == 5), 1);
-        assert_eq!(xs.iter().count(|x| *x == 95), 0);
+        assert_eq!(xs.iter().filter(|x| **x == 2).count(), 3);
+        assert_eq!(xs.iter().filter(|x| **x == 5).count(), 1);
+        assert_eq!(xs.iter().filter(|x| **x == 95).count(), 0);
     }
 
     #[test]
@@ -3044,10 +3042,10 @@ mod tests {
         assert!(range(-10i, -1).collect::<Vec<int>>() ==
                    vec![-10, -9, -8, -7, -6, -5, -4, -3, -2]);
         assert!(range(0i, 5).rev().collect::<Vec<int>>() == vec![4, 3, 2, 1, 0]);
-        assert_eq!(range(200, -5).len(), 0);
-        assert_eq!(range(200, -5).rev().len(), 0);
-        assert_eq!(range(200, 200).len(), 0);
-        assert_eq!(range(200, 200).rev().len(), 0);
+        assert_eq!(range(200, -5).count(), 0);
+        assert_eq!(range(200, -5).rev().count(), 0);
+        assert_eq!(range(200, 200).count(), 0);
+        assert_eq!(range(200, 200).rev().count(), 0);
 
         assert_eq!(range(0i, 100).size_hint(), (100, Some(100)));
         // this test is only meaningful when sizeof uint < sizeof u64
@@ -3062,8 +3060,8 @@ mod tests {
                 vec![0i, 1, 2, 3, 4, 5]);
         assert!(range_inclusive(0i, 5).rev().collect::<Vec<int>>() ==
                 vec![5i, 4, 3, 2, 1, 0]);
-        assert_eq!(range_inclusive(200, -5).len(), 0);
-        assert_eq!(range_inclusive(200, -5).rev().len(), 0);
+        assert_eq!(range_inclusive(200, -5).count(), 0);
+        assert_eq!(range_inclusive(200, -5).rev().count(), 0);
         assert!(range_inclusive(200, 200).collect::<Vec<int>>() == vec![200]);
         assert!(range_inclusive(200, 200).rev().collect::<Vec<int>>() == vec![200]);
     }
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs
index 0257911e8c0..4dea1fd75a4 100644
--- a/src/libcore/slice.rs
+++ b/src/libcore/slice.rs
@@ -252,7 +252,7 @@ pub mod traits {
     use super::*;
 
     use cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering, Equiv};
-    use iter::{order, Iterator};
+    use iter::order;
     use container::Container;
 
     impl<'a,T:PartialEq> PartialEq for &'a [T] {
@@ -1141,7 +1141,6 @@ impl<'a, T:Clone> MutableCloneableVector<T> for &'a mut [T] {
 /// Unsafe operations
 pub mod raw {
     use mem::transmute;
-    use iter::Iterator;
     use ptr::RawPtr;
     use raw::Slice;
     use option::{None, Option, Some};
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index c08f30152d5..936b698d4b1 100644
--- a/src/libcore/str.rs
+++ b/src/libcore/str.rs
@@ -867,7 +867,6 @@ static TAG_CONT_U8: u8 = 128u8;
 pub mod raw {
     use mem;
     use container::Container;
-    use iter::Iterator;
     use ptr::RawPtr;
     use raw::Slice;
     use slice::{ImmutableVector};
@@ -1725,7 +1724,7 @@ impl<'a> StrSlice<'a> for &'a str {
     fn is_alphanumeric(&self) -> bool { self.chars().all(char::is_alphanumeric) }
 
     #[inline]
-    fn char_len(&self) -> uint { self.chars().len() }
+    fn char_len(&self) -> uint { self.chars().count() }
 
     #[inline]
     fn slice(&self, begin: uint, end: uint) -> &'a str {