Skip to content

Commit

Permalink
Adapt code to the new semantics of the for-loop
Browse files Browse the repository at this point in the history
See rust-lang/rust#20790 for details.
  • Loading branch information
milibopp committed Feb 3, 2015
1 parent 4e3c3bd commit e304f87
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/adaptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl<I: Iterator> Iterator for Dedup<I> where
#[inline]
fn next(&mut self) -> Option<I::Item>
{
for elt in self.iter {
for elt in self.iter.by_ref() {
match self.last {
Some(ref x) if x == &elt => continue,
None => {
Expand Down Expand Up @@ -391,7 +391,7 @@ impl<K: PartialEq, I: Iterator, F: FnMut(&I::Item) -> K>
type Item = (K, Vec<I::Item>);
fn next(&mut self) -> Option<(K, Vec<I::Item>)>
{
for elt in self.iter {
for elt in self.iter.by_ref() {
let key = (self.key)(&elt);
match self.current_key.take() {
None => {}
Expand Down
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,10 @@ pub trait Itertools : Iterator {
/// Find the position and value of the first element satisfying a predicate.
fn find_position<P>(&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))
}
Expand Down Expand Up @@ -477,27 +478,30 @@ 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.
///
/// Consumes the iterator until its end.
///
/// **Note: This method is deprecated, use .foreach() instead.**
fn apply<F: FnMut(Self::Item)>(&mut self, f: F)
fn apply<F: FnMut(Self::Item)>(&mut self, f: F) where
Self: Sized
{
self.foreach(f)
}

/// Run the closure **f** eagerly on each element of the iterator.
///
/// Consumes the iterator until its end.
fn foreach<F: FnMut(Self::Item)>(&mut self, mut f: F)
fn foreach<F: FnMut(Self::Item)>(&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()**,
Expand All @@ -517,7 +521,7 @@ impl<T: ?Sized> Itertools for T where T: Iterator { }
/// Return the number of elements written.
#[inline]
pub fn write<'a, A: 'a, I: Iterator<Item=&'a mut A>, J: Iterator<Item=A>>
(mut to: I, mut from: J) -> usize
(mut to: I, from: J) -> usize
{
let mut count = 0;
for elt in from {
Expand Down
19 changes: 8 additions & 11 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 2 additions & 3 deletions tests/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit e304f87

Please sign in to comment.