Skip to content

Commit

Permalink
make FromIterator use IntoIterator
Browse files Browse the repository at this point in the history
This breaks all implementors of FromIterator, as they must now accept IntoIterator instead of Iterator. The fix for this is generally trivial (change the bound, and maybe call into_iter() on the argument to get the old argument).

Users of FromIterator should be unaffected because Iterators are IntoIterator.

[breaking-change]
  • Loading branch information
Gankra committed Feb 18, 2015
1 parent 4a9d190 commit 66613e2
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,8 @@ impl<'a, T: 'a> ExactSizeIterator for Drain<'a, T> {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
fn from_iter<Iter: Iterator<Item=T>>(iter: Iter) -> BinaryHeap<T> {
BinaryHeap::from_vec(iter.collect())
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> BinaryHeap<T> {
BinaryHeap::from_vec(iter.into_iter().collect())
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libcollections/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,9 +924,9 @@ impl Default for BitVec {

#[stable(feature = "rust1", since = "1.0.0")]
impl FromIterator<bool> for BitVec {
fn from_iter<I:Iterator<Item=bool>>(iterator: I) -> BitVec {
let mut ret = BitVec::new();
ret.extend(iterator);
fn from_iter<I: IntoIterator<Item=bool>>(iter: I) -> BitVec {
let mut ret = Bitv::new();
ret.extend(iter);
ret
}
}
Expand Down Expand Up @@ -1134,9 +1134,9 @@ impl Default for BitSet {

#[stable(feature = "rust1", since = "1.0.0")]
impl FromIterator<usize> for BitSet {
fn from_iter<I:Iterator<Item=usize>>(iterator: I) -> BitSet {
let mut ret = BitSet::new();
ret.extend(iterator);
fn from_iter<I: IntoIterator<Item=usize>>(iter: I) -> BitSet {
let mut ret = BitvSet::new();
ret.extend(iter);
ret
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ mod stack {

#[stable(feature = "rust1", since = "1.0.0")]
impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> {
fn from_iter<T: Iterator<Item=(K, V)>>(iter: T) -> BTreeMap<K, V> {
fn from_iter<T: IntoIterator<Item=(K, V)>>(iter: T) -> BTreeMap<K, V> {
let mut map = BTreeMap::new();
map.extend(iter);
map
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ impl<T: Ord> BTreeSet<T> {

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Ord> FromIterator<T> for BTreeSet<T> {
fn from_iter<Iter: Iterator<Item=T>>(iter: Iter) -> BTreeSet<T> {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> BTreeSet<T> {
let mut set = BTreeSet::new();
set.extend(iter);
set
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ impl<E:CLike> Iterator for Iter<E> {
}

impl<E:CLike> FromIterator<E> for EnumSet<E> {
fn from_iter<I:Iterator<Item=E>>(iterator: I) -> EnumSet<E> {
fn from_iter<I: IntoIterator<Item=E>>(iter: I) -> EnumSet<E> {
let mut ret = EnumSet::new();
ret.extend(iterator);
ret.extend(iter);
ret
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libcollections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,9 @@ impl<A> DoubleEndedIterator for IntoIter<A> {

#[stable(feature = "rust1", since = "1.0.0")]
impl<A> FromIterator<A> for LinkedList<A> {
fn from_iter<T: Iterator<Item=A>>(iterator: T) -> LinkedList<A> {
let mut ret = LinkedList::new();
ret.extend(iterator);
fn from_iter<T: IntoIterator<Item=A>>(iter: T) -> LinkedList<A> {
let mut ret = DList::new();
ret.extend(iter);
ret
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,18 +709,18 @@ impl Error for FromUtf16Error {

#[stable(feature = "rust1", since = "1.0.0")]
impl FromIterator<char> for String {
fn from_iter<I:Iterator<Item=char>>(iterator: I) -> String {
fn from_iter<I: IntoIterator<Item=char>>(iter: I) -> String {
let mut buf = String::new();
buf.extend(iterator);
buf.extend(iter);
buf
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> FromIterator<&'a str> for String {
fn from_iter<I:Iterator<Item=&'a str>>(iterator: I) -> String {
fn from_iter<I: IntoIterator<Item=&'a str>>(iter: I) -> String {
let mut buf = String::new();
buf.extend(iterator);
buf.extend(iter);
buf
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,8 @@ impl<T> ops::DerefMut for Vec<T> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> FromIterator<T> for Vec<T> {
#[inline]
fn from_iter<I:Iterator<Item=T>>(mut iterator: I) -> Vec<T> {
fn from_iter<I: IntoIterator<Item=T>>(iterable: I) -> Vec<T> {
let mut iterator = iterable.into_iter();
let (lower, _) = iterator.size_hint();
let mut vector = Vec::with_capacity(lower);

Expand Down Expand Up @@ -1651,7 +1652,7 @@ pub type CowVec<'a, T> = Cow<'a, Vec<T>, [T]>;

#[unstable(feature = "collections")]
impl<'a, T> FromIterator<T> for CowVec<'a, T> where T: Clone {
fn from_iter<I: Iterator<Item=T>>(it: I) -> CowVec<'a, T> {
fn from_iter<I: IntoIterator<Item=T>>(it: I) -> CowVec<'a, T> {
Cow::Owned(FromIterator::from_iter(it))
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/libcollections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,8 @@ impl<A> IndexMut<usize> for VecDeque<A> {

#[stable(feature = "rust1", since = "1.0.0")]
impl<A> FromIterator<A> for VecDeque<A> {
fn from_iter<T: Iterator<Item=A>>(iterator: T) -> VecDeque<A> {
fn from_iter<T: IntoIterator<Item=A>>(iterable: T) -> VecDeque<A> {
let iterator = iterable.into_iter();
let (lower, _) = iterator.size_hint();
let mut deq = VecDeque::with_capacity(lower);
deq.extend(iterator);
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/vec_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ impl<V: fmt::Debug> fmt::Debug for VecMap<V> {

#[stable(feature = "rust1", since = "1.0.0")]
impl<V> FromIterator<(usize, V)> for VecMap<V> {
fn from_iter<Iter: Iterator<Item=(usize, V)>>(iter: Iter) -> VecMap<V> {
fn from_iter<I: IntoIterator<Item=(usize, V)>>(iter: I) -> VecMap<V> {
let mut map = VecMap::new();
map.extend(iter);
map
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ impl<'a, I: Iterator + ?Sized> Iterator for &'a mut I {
#[rustc_on_unimplemented="a collection of type `{Self}` cannot be \
built from an iterator over elements of type `{A}`"]
pub trait FromIterator<A> {
/// Build a container with elements from an external iterator.
/// Build a container with elements from something iterable.
#[stable(feature = "rust1", since = "1.0.0")]
fn from_iter<T: Iterator<Item=A>>(iterator: T) -> Self;
fn from_iter<T: IntoIterator<Item=A>>(iterator: T) -> Self;
}

/// Conversion into an `Iterator`
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ use clone::Clone;
use cmp::{Eq, Ord};
use default::Default;
use iter::{ExactSizeIterator};
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator};
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator, IntoIterator};
use mem;
use ops::{Deref, FnOnce};
use result::Result::{Ok, Err};
Expand Down Expand Up @@ -909,7 +909,7 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn from_iter<I: Iterator<Item=Option<A>>>(iter: I) -> Option<V> {
fn from_iter<I: IntoIterator<Item=Option<A>>>(iter: I) -> Option<V> {
// FIXME(#11084): This could be replaced with Iterator::scan when this
// performance bug is closed.

Expand All @@ -934,7 +934,7 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
}
}

let mut adapter = Adapter { iter: iter, found_none: false };
let mut adapter = Adapter { iter: iter.into_iter(), found_none: false };
let v: V = FromIterator::from_iter(adapter.by_ref());

if adapter.found_none {
Expand Down
7 changes: 4 additions & 3 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ use self::Result::{Ok, Err};

use clone::Clone;
use fmt;
use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator, ExactSizeIterator};
use iter::{Iterator, IteratorExt, DoubleEndedIterator,
FromIterator, ExactSizeIterator, IntoIterator};
use ops::{FnMut, FnOnce};
use option::Option::{self, None, Some};
use slice::AsSlice;
Expand Down Expand Up @@ -906,7 +907,7 @@ impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E> {
/// assert!(res == Ok(vec!(2, 3)));
/// ```
#[inline]
fn from_iter<I: Iterator<Item=Result<A, E>>>(iter: I) -> Result<V, E> {
fn from_iter<I: IntoIterator<Item=Result<A, E>>>(iter: I) -> Result<V, E> {
// FIXME(#11084): This could be replaced with Iterator::scan when this
// performance bug is closed.

Expand All @@ -931,7 +932,7 @@ impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E> {
}
}

let mut adapter = Adapter { iter: iter, err: None };
let mut adapter = Adapter { iter: iter.into_iter(), err: None };
let v: V = FromIterator::from_iter(adapter.by_ref());

match adapter.err {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use middle::ty::*;
use middle::ty;
use std::cmp::Ordering;
use std::fmt;
use std::iter::{range_inclusive, AdditiveIterator, FromIterator, repeat};
use std::iter::{range_inclusive, AdditiveIterator, FromIterator, IntoIterator, repeat};
use std::num::Float;
use std::slice;
use syntax::ast::{self, DUMMY_NODE_ID, NodeId, Pat};
Expand Down Expand Up @@ -94,8 +94,8 @@ impl<'a> fmt::Debug for Matrix<'a> {
}

impl<'a> FromIterator<Vec<&'a Pat>> for Matrix<'a> {
fn from_iter<T: Iterator<Item=Vec<&'a Pat>>>(iterator: T) -> Matrix<'a> {
Matrix(iterator.collect())
fn from_iter<T: IntoIterator<Item=Vec<&'a Pat>>>(iter: T) -> Matrix<'a> {
Matrix(iter.into_iter().collect())
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,8 @@ impl<K, V, S, H> FromIterator<(K, V)> for HashMap<K, V, S>
S: HashState<Hasher=H> + Default,
H: hash::Hasher<Output=u64>
{
fn from_iter<T: Iterator<Item=(K, V)>>(iter: T) -> HashMap<K, V, S> {
fn from_iter<T: IntoIterator<Item=(K, V)>>(iterable: T) -> HashMap<K, V, S> {
let iter = iterable.into_iter();
let lower = iter.size_hint().0;
let mut map = HashMap::with_capacity_and_hash_state(lower,
Default::default());
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ impl<T, S, H> FromIterator<T> for HashSet<T, S>
S: HashState<Hasher=H> + Default,
H: hash::Hasher<Output=u64>
{
fn from_iter<I: Iterator<Item=T>>(iter: I) -> HashSet<T, S> {
fn from_iter<I: IntoIterator<Item=T>>(iterable: I) -> HashSet<T, S> {
let iter = iterable.into_iter();
let lower = iter.size_hint().0;
let mut set = HashSet::with_capacity_and_hash_state(lower, Default::default());
set.extend(iter);
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ impl PathBuf {
}

impl<'a, P: ?Sized + 'a> iter::FromIterator<&'a P> for PathBuf where P: AsPath {
fn from_iter<I: Iterator<Item = &'a P>>(iter: I) -> PathBuf {
fn from_iter<I: IntoIterator<Item = &'a P>>(iter: I) -> PathBuf {
let mut buf = PathBuf::new("");
buf.extend(iter);
buf
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys/common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ impl Wtf8Buf {
/// This replaces surrogate code point pairs with supplementary code points,
/// like concatenating ill-formed UTF-16 strings effectively would.
impl FromIterator<CodePoint> for Wtf8Buf {
fn from_iter<T: Iterator<Item=CodePoint>>(iterator: T) -> Wtf8Buf {
fn from_iter<T: IntoIterator<Item=CodePoint>>(iter: T) -> Wtf8Buf {
let mut string = Wtf8Buf::new();
string.extend(iterator);
string.extend(iter);
string
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/owned_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use std::default::Default;
use std::fmt;
use std::iter::FromIterator;
use std::iter::{IntoIterator, FromIterator};
use std::ops::Deref;
use std::vec;
use serialize::{Encodable, Decodable, Encoder, Decoder};
Expand Down Expand Up @@ -77,8 +77,8 @@ impl<T: Clone> Clone for OwnedSlice<T> {
}

impl<T> FromIterator<T> for OwnedSlice<T> {
fn from_iter<I: Iterator<Item=T>>(iter: I) -> OwnedSlice<T> {
OwnedSlice::from_vec(iter.collect())
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> OwnedSlice<T> {
OwnedSlice::from_vec(iter.into_iter().collect())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/util/small_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum SmallVectorRepr<T> {
}

impl<T> FromIterator<T> for SmallVector<T> {
fn from_iter<I: Iterator<Item=T>>(iter: I) -> SmallVector<T> {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> SmallVector<T> {
let mut v = SmallVector::zero();
v.extend(iter);
v
Expand Down

0 comments on commit 66613e2

Please sign in to comment.