Skip to content

Commit

Permalink
rollup merge of #24541: alexcrichton/issue-24538
Browse files Browse the repository at this point in the history
This is an implementation of [RFC 1030][rfc] which adds these traits to the
prelude and additionally removes all inherent `into_iter` methods on collections
in favor of the trait implementation (which is now accessible by default).

[rfc]: rust-lang/rfcs#1030

This is technically a breaking change due to the prelude additions and removal
of inherent methods, but it is expected that essentially no code breaks in
practice.

[breaking-change]
Closes #24538
  • Loading branch information
alexcrichton committed Apr 21, 2015
2 parents acc06e7 + a0745a1 commit 3fca886
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 57 deletions.
51 changes: 23 additions & 28 deletions map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,33 +914,6 @@ impl<K, V, S> HashMap<K, V, S>
IterMut { inner: self.table.iter_mut() }
}

/// Creates a consuming iterator, that is, one that moves each key-value
/// pair out of the map in arbitrary order. The map cannot be used after
/// calling this.
///
/// # Examples
///
/// ```
/// use std::collections::HashMap;
///
/// let mut map = HashMap::new();
/// map.insert("a", 1);
/// map.insert("b", 2);
/// map.insert("c", 3);
///
/// // Not possible with .iter()
/// let vec: Vec<(&str, isize)> = map.into_iter().collect();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_iter(self) -> IntoIter<K, V> {
fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two;

IntoIter {
inner: self.table.into_iter().map(last_two)
}
}

/// Gets the given key's corresponding entry in the map for in-place manipulation.
#[stable(feature = "rust1", since = "1.0.0")]
pub fn entry(&mut self, key: K) -> Entry<K, V> {
Expand Down Expand Up @@ -1388,8 +1361,30 @@ impl<K, V, S> IntoIterator for HashMap<K, V, S>
type Item = (K, V);
type IntoIter = IntoIter<K, V>;

/// Creates a consuming iterator, that is, one that moves each key-value
/// pair out of the map in arbitrary order. The map cannot be used after
/// calling this.
///
/// # Examples
///
/// ```
/// use std::collections::HashMap;
///
/// let mut map = HashMap::new();
/// map.insert("a", 1);
/// map.insert("b", 2);
/// map.insert("c", 3);
///
/// // Not possible with .iter()
/// let vec: Vec<(&str, isize)> = map.into_iter().collect();
/// ```
fn into_iter(self) -> IntoIter<K, V> {
self.into_iter()
fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two;

IntoIter {
inner: self.table.into_iter().map(last_two)
}
}
}

Expand Down
53 changes: 24 additions & 29 deletions set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,34 +269,6 @@ impl<T, S> HashSet<T, S>
Iter { iter: self.map.keys() }
}

/// Creates a consuming iterator, that is, one that moves each value out
/// of the set in arbitrary order. The set cannot be used after calling
/// this.
///
/// # Examples
///
/// ```
/// use std::collections::HashSet;
/// let mut set = HashSet::new();
/// set.insert("a".to_string());
/// set.insert("b".to_string());
///
/// // Not possible to collect to a Vec<String> with a regular `.iter()`.
/// let v: Vec<String> = set.into_iter().collect();
///
/// // Will print in an arbitrary order.
/// for x in v.iter() {
/// println!("{}", x);
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_iter(self) -> IntoIter<T> {
fn first<A, B>((a, _): (A, B)) -> A { a }
let first: fn((T, ())) -> T = first;

IntoIter { iter: self.map.into_iter().map(first) }
}

/// Visit the values representing the difference.
///
/// # Examples
Expand Down Expand Up @@ -848,8 +820,31 @@ impl<T, S> IntoIterator for HashSet<T, S>
type Item = T;
type IntoIter = IntoIter<T>;

/// Creates a consuming iterator, that is, one that moves each value out
/// of the set in arbitrary order. The set cannot be used after calling
/// this.
///
/// # Examples
///
/// ```
/// use std::collections::HashSet;
/// let mut set = HashSet::new();
/// set.insert("a".to_string());
/// set.insert("b".to_string());
///
/// // Not possible to collect to a Vec<String> with a regular `.iter()`.
/// let v: Vec<String> = set.into_iter().collect();
///
/// // Will print in an arbitrary order.
/// for x in v.iter() {
/// println!("{}", x);
/// }
/// ```
fn into_iter(self) -> IntoIter<T> {
self.into_iter()
fn first<A, B>((a, _): (A, B)) -> A { a }
let first: fn((T, ())) -> T = first;

IntoIter { iter: self.map.into_iter().map(first) }
}
}

Expand Down

0 comments on commit 3fca886

Please sign in to comment.