Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt code to the new semantics of the for-loop #13

Merged
merged 1 commit into from
Feb 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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