diff --git a/src/adaptors.rs b/src/adaptors.rs index 3d7886e0c..cf2fd3503 100644 --- a/src/adaptors.rs +++ b/src/adaptors.rs @@ -287,7 +287,7 @@ impl Iterator for Dedup where #[inline] fn next(&mut self) -> Option { - for elt in self.iter { + for elt in self.iter.by_ref() { match self.last { Some(ref x) if x == &elt => continue, None => { @@ -391,7 +391,7 @@ impl K> type Item = (K, Vec); fn next(&mut self) -> Option<(K, Vec)> { - for elt in self.iter { + for elt in self.iter.by_ref() { let key = (self.key)(&elt); match self.current_key.take() { None => {} diff --git a/src/lib.rs b/src/lib.rs index 44347da80..833394319 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -428,9 +428,10 @@ pub trait Itertools : Iterator { /// Find the position and value of the first element satisfying a predicate. fn find_position

(&mut self, mut pred: P) -> Option<(usize, Self::Item)> where P: FnMut(&Self::Item) -> bool, + Self: Sized, { let mut index = 0us; - for elt in *self { + for elt in IteratorExt::by_ref(self) { if pred(&elt) { return Some((index, elt)) } @@ -477,9 +478,10 @@ pub trait Itertools : Iterator { /// "hi".chars().map(|c| cnt += 1).drain(); /// ``` /// - fn drain(&mut self) + fn drain(&mut self) where + Self: Sized { - for _ in *self { /* nothing */ } + for _ in self.by_ref() { /* nothing */ } } /// Run the closure **f** eagerly on each element of the iterator. @@ -487,7 +489,8 @@ pub trait Itertools : Iterator { /// Consumes the iterator until its end. /// /// **Note: This method is deprecated, use .foreach() instead.** - fn apply(&mut self, f: F) + fn apply(&mut self, f: F) where + Self: Sized { self.foreach(f) } @@ -495,9 +498,10 @@ pub trait Itertools : Iterator { /// Run the closure **f** eagerly on each element of the iterator. /// /// Consumes the iterator until its end. - fn foreach(&mut self, mut f: F) + fn foreach(&mut self, mut f: F) where + Self: Sized { - for elt in *self { f(elt) } + for elt in self.by_ref() { f(elt) } } /// **.collec_vec()** is simply a type specialization of **.collect()**, @@ -517,7 +521,7 @@ impl Itertools for T where T: Iterator { } /// Return the number of elements written. #[inline] pub fn write<'a, A: 'a, I: Iterator, J: Iterator> - (mut to: I, mut from: J) -> usize + (mut to: I, from: J) -> usize { let mut count = 0; for elt in from { diff --git a/tests/tests.rs b/tests/tests.rs index f61a8502e..241c49263 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -4,7 +4,7 @@ //! option. This file may not be copied, modified, or distributed //! except according to those terms. -#![feature(core, collections, test)] +#![feature(core, test)] #[macro_use] extern crate itertools; @@ -276,20 +276,17 @@ fn rciter() { #[test] fn slice() { - - let it = 0..10; - assert_iters_equal(it.slice(..3), 0..3); - assert_iters_equal(it.slice(3..7), 3..7); - assert_iters_equal(it.slice(3..27), 3..10); - assert_iters_equal(it.slice(44..), 0..0); + assert_iters_equal((0..10).slice(..3), 0..3); + assert_iters_equal((0..10).slice(3..7), 3..7); + assert_iters_equal((0..10).slice(3..27), 3..10); + assert_iters_equal((0..10).slice(44..), 0..0); } #[test] fn step() { - let it = 0..10; - assert_iters_equal(it.step(1), it); - assert_iters_equal(it.step(2), it.filter(|x: &i32| *x % 2 == 0)); - assert_iters_equal(it.step(10), 0..1); + assert_iters_equal((0..10).step(1), (0..10)); + assert_iters_equal((0..10).step(2), (0..10).filter(|x: &i32| *x % 2 == 0)); + assert_iters_equal((0..10).step(10), 0..1); } #[test] diff --git a/tests/zip.rs b/tests/zip.rs index b93b55c89..1ebcf2c01 100644 --- a/tests/zip.rs +++ b/tests/zip.rs @@ -15,11 +15,10 @@ fn test_zip_longest_size_hint() { let c = count(0, 1); let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; let v2 = &[10, 11, 12]; - let vi = v.iter(); - assert_eq!(c.zip_longest(vi).size_hint(), (std::usize::MAX, None)); + assert_eq!(c.zip_longest(v.iter()).size_hint(), (std::usize::MAX, None)); - assert_eq!(vi.zip_longest(v2.iter()).size_hint(), (10, Some(10))); + assert_eq!(v.iter().zip_longest(v2.iter()).size_hint(), (10, Some(10))); } #[test] fn test_double_ended_zip_longest() {