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

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

Closed
wants to merge 2 commits into from
Closed
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 doc/po/tutorial-container.md.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions doc/tutorial-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
}
}
Expand Down Expand Up @@ -1163,4 +1163,3 @@ mod tests {
}
}
}

8 changes: 4 additions & 4 deletions src/libextra/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,17 @@ impl<'self, T> Iterator<&'self T> for PriorityQueueIterator<'self, T> {
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);

q
}
}

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();
Expand Down
8 changes: 4 additions & 4 deletions src/libextra/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,17 @@ impl<A: Eq> Eq for RingBuf<A> {
}
}

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);
deq
}
}

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);
}
Expand Down
16 changes: 8 additions & 8 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,34 +835,34 @@ fn remove<K: TotalOrd, V>(node: &mut Option<~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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,9 @@ pub fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,

let check_move: &fn(@pat, Option<@pat>) = |p, sub| {
// check legality of moving out of the enum
if sub.is_some() {

// x @ Foo(*) is legal, but x @ Foo(y) isn't.
if sub.map_move_default(false, |p| pat_contains_bindings(def_map, p)) {
tcx.sess.span_err(
p.span,
"cannot bind by-move with sub-bindings");
Expand Down
15 changes: 15 additions & 0 deletions src/librustc/middle/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,18 @@ pub fn pat_binding_ids(dm: resolve::DefMap, pat: @pat) -> ~[NodeId] {
pat_bindings(dm, pat, |_bm, b_id, _sp, _pt| found.push(b_id) );
return found;
}

/// Checks if the pattern contains any patterns that bind something to
/// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(*)`.
pub fn pat_contains_bindings(dm: resolve::DefMap, pat: @pat) -> bool {
let mut contains_bindings = false;
do walk_pat(pat) |p| {
if pat_is_binding(dm, p) {
contains_bindings = true;
false // there's at least one binding, can short circuit now.
} else {
true
}
};
contains_bindings
}
16 changes: 8 additions & 8 deletions src/libstd/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,17 +605,17 @@ impl<K> Iterator<K> for HashSetMoveIterator<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);
map
}
}

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);
}
Expand Down Expand Up @@ -753,17 +753,17 @@ impl<T:Hash + Eq + Clone> Clone for 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);
set
}
}

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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/libstd/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ use clone::Clone;
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
Expand Down Expand Up @@ -353,7 +353,7 @@ pub trait Iterator<A> {
/// 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)
}

Expand Down
8 changes: 4 additions & 4 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2111,19 +2111,19 @@ impl Clone for @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);
buf
}
}

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);
Expand Down
16 changes: 8 additions & 8 deletions src/libstd/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,16 @@ impl<T> TrieMap<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);
}
Expand Down Expand Up @@ -294,16 +294,16 @@ impl TrieSet {
}
}

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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2316,8 +2316,8 @@ impl<T> Iterator<T> for MoveRevIterator<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 {
Expand All @@ -2327,8 +2327,8 @@ impl<A, T: Iterator<A>> FromIterator<A, T> for ~[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);
Expand Down