std: Move the iterator param on FromIterator and Extendable to the method.

If they are on the trait then it is extremely annoying to use them as
generic parameters to a function, e.g. with the iterator param on the trait
itself, if one was to pass an Extendable<int> to a function that filled it
either from a Range or a Map<VecIterator>, one needs to write something
like:

    fn foo<E: Extendable<int, Range<int>> +
              Extendable<int, Map<&'self int, int, VecIterator<int>>>
          (e: &mut E, ...) { ... }

since using a generic, i.e. `foo<E: Extendable<int, I>, I: Iterator<int>>`
means that `foo` takes 2 type parameters, and the caller has to specify them
(which doesn't work anyway, as they'll mismatch with the iterators used in
`foo` itself).

This patch changes it to:

    fn foo<E: Extendable<int>>(e: &mut E, ...) { ... }
This commit is contained in:
Huon Wilson 2013-08-13 23:08:14 +10:00
parent 58021be454
commit 53487a0246
11 changed files with 53 additions and 54 deletions

View File

@ -483,8 +483,8 @@ msgstr ""
#, no-wrap
msgid ""
"~~~\n"
"impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {\n"
" pub fn from_iterator(iterator: &mut T) -> ~[A] {\n"
"impl<A> FromIterator<A> for ~[A] {\n"
" pub fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> ~[A] {\n"
" let (lower, _) = iterator.size_hint();\n"
" let mut xs = with_capacity(lower);\n"
" for x in iterator {\n"

View File

@ -224,8 +224,8 @@ implementing the `FromIterator` trait. For example, the implementation for
vectors is as follows:
~~~
impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
pub fn from_iterator(iterator: &mut T) -> ~[A] {
impl<A> FromIterator<A> for ~[A] {
pub fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> ~[A] {
let (lower, _) = iterator.size_hint();
let mut xs = with_capacity(lower);
for x in iterator {

View File

@ -573,16 +573,16 @@ impl<A> DoubleEndedIterator<A> for MoveIterator<A> {
fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
}
impl<A, T: Iterator<A>> FromIterator<A, T> for DList<A> {
fn from_iterator(iterator: &mut T) -> DList<A> {
impl<A> FromIterator<A> for DList<A> {
fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> DList<A> {
let mut ret = DList::new();
ret.extend(iterator);
ret
}
}
impl<A, T: Iterator<A>> Extendable<A, T> for DList<A> {
fn extend(&mut self, iterator: &mut T) {
impl<A> Extendable<A> for DList<A> {
fn extend<T: Iterator<A>>(&mut self, iterator: &mut T) {
for elt in *iterator { self.push_back(elt); }
}
}
@ -1163,4 +1163,3 @@ fn bench_iter_mut_rev(b: &mut test::BenchHarness) {
}
}
}

View File

@ -190,8 +190,8 @@ fn next(&mut self) -> Option<(&'self T)> { self.iter.next() }
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
}
impl<T: Ord, Iter: Iterator<T>> FromIterator<T, Iter> for PriorityQueue<T> {
fn from_iterator(iter: &mut Iter) -> PriorityQueue<T> {
impl<T: Ord> FromIterator<T> for PriorityQueue<T> {
fn from_iterator<Iter: Iterator<T>>(iter: &mut Iter) -> PriorityQueue<T> {
let mut q = PriorityQueue::new();
q.extend(iter);
@ -199,8 +199,8 @@ fn from_iterator(iter: &mut Iter) -> PriorityQueue<T> {
}
}
impl<T: Ord, Iter: Iterator<T>> Extendable<T, Iter> for PriorityQueue<T> {
fn extend(&mut self, iter: &mut Iter) {
impl<T: Ord> Extendable<T> for PriorityQueue<T> {
fn extend<Iter: Iterator<T>>(&mut self, iter: &mut Iter) {
let (lower, _) = iter.size_hint();
let len = self.capacity();

View File

@ -322,8 +322,8 @@ fn ne(&self, other: &RingBuf<A>) -> bool {
}
}
impl<A, T: Iterator<A>> FromIterator<A, T> for RingBuf<A> {
fn from_iterator(iterator: &mut T) -> RingBuf<A> {
impl<A> FromIterator<A> for RingBuf<A> {
fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> RingBuf<A> {
let (lower, _) = iterator.size_hint();
let mut deq = RingBuf::with_capacity(lower);
deq.extend(iterator);
@ -331,8 +331,8 @@ fn from_iterator(iterator: &mut T) -> RingBuf<A> {
}
}
impl<A, T: Iterator<A>> Extendable<A, T> for RingBuf<A> {
fn extend(&mut self, iterator: &mut T) {
impl<A> Extendable<A> for RingBuf<A> {
fn extend<T: Iterator<A>>(&mut self, iterator: &mut T) {
for elt in *iterator {
self.push_back(elt);
}

View File

@ -835,34 +835,34 @@ fn heir_swap<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>,
};
}
impl<K: TotalOrd, V, T: Iterator<(K, V)>> FromIterator<(K, V), T> for TreeMap<K, V> {
fn from_iterator(iter: &mut T) -> TreeMap<K, V> {
impl<K: TotalOrd, V> FromIterator<(K, V)> for TreeMap<K, V> {
fn from_iterator<T: Iterator<(K, V)>>(iter: &mut T) -> TreeMap<K, V> {
let mut map = TreeMap::new();
map.extend(iter);
map
}
}
impl<K: TotalOrd, V, T: Iterator<(K, V)>> Extendable<(K, V), T> for TreeMap<K, V> {
impl<K: TotalOrd, V> Extendable<(K, V)> for TreeMap<K, V> {
#[inline]
fn extend(&mut self, iter: &mut T) {
fn extend<T: Iterator<(K, V)>>(&mut self, iter: &mut T) {
for (k, v) in *iter {
self.insert(k, v);
}
}
}
impl<T: TotalOrd, Iter: Iterator<T>> FromIterator<T, Iter> for TreeSet<T> {
fn from_iterator(iter: &mut Iter) -> TreeSet<T> {
impl<T: TotalOrd> FromIterator<T> for TreeSet<T> {
fn from_iterator<Iter: Iterator<T>>(iter: &mut Iter) -> TreeSet<T> {
let mut set = TreeSet::new();
set.extend(iter);
set
}
}
impl<T: TotalOrd, Iter: Iterator<T>> Extendable<T, Iter> for TreeSet<T> {
impl<T: TotalOrd> Extendable<T> for TreeSet<T> {
#[inline]
fn extend(&mut self, iter: &mut Iter) {
fn extend<Iter: Iterator<T>>(&mut self, iter: &mut Iter) {
for elem in *iter {
self.insert(elem);
}

View File

@ -605,8 +605,8 @@ fn next(&mut self) -> Option<K> {
}
}
impl<K: Eq + Hash, V, T: Iterator<(K, V)>> FromIterator<(K, V), T> for HashMap<K, V> {
fn from_iterator(iter: &mut T) -> HashMap<K, V> {
impl<K: Eq + Hash, V> FromIterator<(K, V)> for HashMap<K, V> {
fn from_iterator<T: Iterator<(K, V)>>(iter: &mut T) -> HashMap<K, V> {
let (lower, _) = iter.size_hint();
let mut map = HashMap::with_capacity(lower);
map.extend(iter);
@ -614,8 +614,8 @@ fn from_iterator(iter: &mut T) -> HashMap<K, V> {
}
}
impl<K: Eq + Hash, V, T: Iterator<(K, V)>> Extendable<(K, V), T> for HashMap<K, V> {
fn extend(&mut self, iter: &mut T) {
impl<K: Eq + Hash, V> Extendable<(K, V)> for HashMap<K, V> {
fn extend<T: Iterator<(K, V)>>(&mut self, iter: &mut T) {
for (k, v) in *iter {
self.insert(k, v);
}
@ -753,8 +753,8 @@ fn clone(&self) -> HashSet<T> {
}
}
impl<K: Eq + Hash, T: Iterator<K>> FromIterator<K, T> for HashSet<K> {
fn from_iterator(iter: &mut T) -> HashSet<K> {
impl<K: Eq + Hash> FromIterator<K> for HashSet<K> {
fn from_iterator<T: Iterator<K>>(iter: &mut T) -> HashSet<K> {
let (lower, _) = iter.size_hint();
let mut set = HashSet::with_capacity(lower);
set.extend(iter);
@ -762,8 +762,8 @@ fn from_iterator(iter: &mut T) -> HashSet<K> {
}
}
impl<K: Eq + Hash, T: Iterator<K>> Extendable<K, T> for HashSet<K> {
fn extend(&mut self, iter: &mut T) {
impl<K: Eq + Hash> Extendable<K> for HashSet<K> {
fn extend<T: Iterator<K>>(&mut self, iter: &mut T) {
for k in *iter {
self.insert(k);
}

View File

@ -26,15 +26,15 @@
use uint;
/// Conversion from an `Iterator`
pub trait FromIterator<A, T: Iterator<A>> {
pub trait FromIterator<A> {
/// Build a container with elements from an external iterator.
fn from_iterator(iterator: &mut T) -> Self;
fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> Self;
}
/// A type growable from an `Iterator` implementation
pub trait Extendable<A, T: Iterator<A>>: FromIterator<A, T> {
pub trait Extendable<A>: FromIterator<A> {
/// Extend a container with the elements yielded by an iterator
fn extend(&mut self, iterator: &mut T);
fn extend<T: Iterator<A>>(&mut self, iterator: &mut T);
}
/// An interface for dealing with "external iterators". These types of iterators
@ -353,7 +353,7 @@ fn advance(&mut self, f: &fn(A) -> bool) -> bool {
/// assert!(a == b);
/// ~~~
#[inline]
fn collect<B: FromIterator<A, Self>>(&mut self) -> B {
fn collect<B: FromIterator<A>>(&mut self) -> B {
FromIterator::from_iterator(self)
}

View File

@ -2111,9 +2111,9 @@ fn clone(&self) -> @str {
}
}
impl<T: Iterator<char>> FromIterator<char, T> for ~str {
impl FromIterator<char> for ~str {
#[inline]
fn from_iterator(iterator: &mut T) -> ~str {
fn from_iterator<T: Iterator<char>>(iterator: &mut T) -> ~str {
let (lower, _) = iterator.size_hint();
let mut buf = with_capacity(lower);
buf.extend(iterator);
@ -2121,9 +2121,9 @@ fn from_iterator(iterator: &mut T) -> ~str {
}
}
impl<T: Iterator<char>> Extendable<char, T> for ~str {
impl Extendable<char> for ~str {
#[inline]
fn extend(&mut self, iterator: &mut T) {
fn extend<T: Iterator<char>>(&mut self, iterator: &mut T) {
let (lower, _) = iterator.size_hint();
let reserve = lower + self.len();
self.reserve_at_least(reserve);

View File

@ -205,16 +205,16 @@ pub fn upper_bound_iter<'a>(&'a self, key: uint) -> TrieMapIterator<'a, T> {
}
}
impl<T, Iter: Iterator<(uint, T)>> FromIterator<(uint, T), Iter> for TrieMap<T> {
fn from_iterator(iter: &mut Iter) -> TrieMap<T> {
impl<T> FromIterator<(uint, T)> for TrieMap<T> {
fn from_iterator<Iter: Iterator<(uint, T)>>(iter: &mut Iter) -> TrieMap<T> {
let mut map = TrieMap::new();
map.extend(iter);
map
}
}
impl<T, Iter: Iterator<(uint, T)>> Extendable<(uint, T), Iter> for TrieMap<T> {
fn extend(&mut self, iter: &mut Iter) {
impl<T> Extendable<(uint, T)> for TrieMap<T> {
fn extend<Iter: Iterator<(uint, T)>>(&mut self, iter: &mut Iter) {
for (k, v) in *iter {
self.insert(k, v);
}
@ -294,16 +294,16 @@ pub fn upper_bound_iter<'a>(&'a self, val: uint) -> TrieSetIterator<'a> {
}
}
impl<Iter: Iterator<uint>> FromIterator<uint, Iter> for TrieSet {
fn from_iterator(iter: &mut Iter) -> TrieSet {
impl FromIterator<uint> for TrieSet {
fn from_iterator<Iter: Iterator<uint>>(iter: &mut Iter) -> TrieSet {
let mut set = TrieSet::new();
set.extend(iter);
set
}
}
impl<Iter: Iterator<uint>> Extendable<uint, Iter> for TrieSet {
fn extend(&mut self, iter: &mut Iter) {
impl Extendable<uint> for TrieSet {
fn extend<Iter: Iterator<uint>>(&mut self, iter: &mut Iter) {
for elem in *iter {
self.insert(elem);
}

View File

@ -2316,8 +2316,8 @@ fn next(&mut self) -> Option<T> {
}
}
impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
fn from_iterator(iterator: &mut T) -> ~[A] {
impl<A> FromIterator<A> for ~[A] {
fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> ~[A] {
let (lower, _) = iterator.size_hint();
let mut xs = with_capacity(lower);
for x in *iterator {
@ -2327,8 +2327,8 @@ fn from_iterator(iterator: &mut T) -> ~[A] {
}
}
impl<A, T: Iterator<A>> Extendable<A, T> for ~[A] {
fn extend(&mut self, iterator: &mut T) {
impl<A> Extendable<A> for ~[A] {
fn extend<T: Iterator<A>>(&mut self, iterator: &mut T) {
let (lower, _) = iterator.size_hint();
let len = self.len();
self.reserve(len + lower);