Skip to content

Commit

Permalink
std: Add Default/IntoIterator/ToOwned to the prelude
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 17, 2015
1 parent 9d2ac9b commit ad96559
Show file tree
Hide file tree
Showing 25 changed files with 240 additions and 287 deletions.
1 change: 0 additions & 1 deletion src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ use core::atomic;
use core::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
use core::fmt;
use core::cmp::Ordering;
use core::default::Default;
use core::mem::{min_align_of, size_of};
use core::mem;
use core::nonzero::NonZero;
Expand Down
1 change: 0 additions & 1 deletion src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use core::prelude::*;

use core::any::Any;
use core::cmp::Ordering;
use core::default::Default;
use core::fmt;
use core::hash::{self, Hash};
use core::mem;
Expand Down
44 changes: 19 additions & 25 deletions src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@

use core::prelude::*;

use core::default::Default;
use core::iter::{FromIterator, IntoIterator};
use core::iter::{FromIterator};
use core::mem::{zeroed, replace, swap};
use core::ptr;

Expand Down Expand Up @@ -250,28 +249,6 @@ impl<T: Ord> BinaryHeap<T> {
Iter { iter: self.data.iter() }
}

/// Creates a consuming iterator, that is, one that moves each value out of
/// the binary heap in arbitrary order. The binary heap cannot be used
/// after calling this.
///
/// # Examples
///
/// ```
/// # #![feature(collections)]
/// use std::collections::BinaryHeap;
/// let heap = BinaryHeap::from_vec(vec![1, 2, 3, 4]);
///
/// // Print 1, 2, 3, 4 in arbitrary order
/// for x in heap.into_iter() {
/// // x has type i32, not &i32
/// println!("{}", x);
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_iter(self) -> IntoIter<T> {
IntoIter { iter: self.data.into_iter() }
}

/// Returns the greatest item in the binary heap, or `None` if it is empty.
///
/// # Examples
Expand Down Expand Up @@ -675,8 +652,25 @@ impl<T: Ord> IntoIterator for BinaryHeap<T> {
type Item = T;
type IntoIter = IntoIter<T>;

/// Creates a consuming iterator, that is, one that moves each value out of
/// the binary heap in arbitrary order. The binary heap cannot be used
/// after calling this.
///
/// # Examples
///
/// ```
/// # #![feature(collections)]
/// use std::collections::BinaryHeap;
/// let heap = BinaryHeap::from_vec(vec![1, 2, 3, 4]);
///
/// // Print 1, 2, 3, 4 in arbitrary order
/// for x in heap.into_iter() {
/// // x has type i32, not &i32
/// println!("{}", x);
/// }
/// ```
fn into_iter(self) -> IntoIter<T> {
self.into_iter()
IntoIter { iter: self.data.into_iter() }
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/libcollections/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,11 @@ use core::prelude::*;

use core::cmp::Ordering;
use core::cmp;
use core::default::Default;
use core::fmt;
use core::hash;
use core::iter::RandomAccessIterator;
use core::iter::{Chain, Enumerate, Repeat, Skip, Take, repeat, Cloned};
use core::iter::{self, FromIterator, IntoIterator};
use core::iter::{self, FromIterator};
use core::ops::Index;
use core::slice;
use core::{u8, u32, usize};
Expand Down
58 changes: 26 additions & 32 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ use self::Entry::*;
use core::prelude::*;

use core::cmp::Ordering;
use core::default::Default;
use core::fmt::Debug;
use core::hash::{Hash, Hasher};
use core::iter::{Map, FromIterator, IntoIterator};
use core::iter::{Map, FromIterator};
use core::ops::Index;
use core::{iter, fmt, mem, usize};
use Bound::{self, Included, Excluded, Unbounded};
Expand Down Expand Up @@ -472,8 +471,32 @@ impl<K, V> IntoIterator for BTreeMap<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;

/// Gets an owning iterator over the entries of the map.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut map = BTreeMap::new();
/// map.insert(1, "a");
/// map.insert(2, "b");
/// map.insert(3, "c");
///
/// for (key, value) in map.into_iter() {
/// println!("{}: {}", key, value);
/// }
/// ```
fn into_iter(self) -> IntoIter<K, V> {
self.into_iter()
let len = self.len();
let mut lca = VecDeque::new();
lca.push_back(Traverse::traverse(self.root));
IntoIter {
inner: AbsIter {
traversals: lca,
size: len,
}
}
}
}

Expand Down Expand Up @@ -1264,35 +1287,6 @@ impl<K, V> BTreeMap<K, V> {
}
}

/// Gets an owning iterator over the entries of the map.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut map = BTreeMap::new();
/// map.insert(1, "a");
/// map.insert(2, "b");
/// map.insert(3, "c");
///
/// for (key, value) in map.into_iter() {
/// println!("{}: {}", key, value);
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_iter(self) -> IntoIter<K, V> {
let len = self.len();
let mut lca = VecDeque::new();
lca.push_back(Traverse::traverse(self.root));
IntoIter {
inner: AbsIter {
traversals: lca,
size: len,
}
}
}

/// Gets an iterator over the keys of the map.
///
/// # Examples
Expand Down
42 changes: 18 additions & 24 deletions src/libcollections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
use core::prelude::*;

use core::cmp::Ordering::{self, Less, Greater, Equal};
use core::default::Default;
use core::fmt::Debug;
use core::fmt;
use core::iter::{Peekable, Map, FromIterator, IntoIterator};
use core::iter::{Peekable, Map, FromIterator};
use core::ops::{BitOr, BitAnd, BitXor, Sub};

use borrow::Borrow;
Expand Down Expand Up @@ -132,27 +131,6 @@ impl<T> BTreeSet<T> {
pub fn iter(&self) -> Iter<T> {
Iter { iter: self.map.keys() }
}

/// Gets an iterator for moving out the BtreeSet's contents.
///
/// # Examples
///
/// ```
/// # #![feature(core)]
/// use std::collections::BTreeSet;
///
/// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
///
/// let v: Vec<usize> = set.into_iter().collect();
/// assert_eq!(v, [1, 2, 3, 4]);
/// ```
#[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; // coerce to fn pointer

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

impl<T: Ord> BTreeSet<T> {
Expand Down Expand Up @@ -500,8 +478,24 @@ impl<T> IntoIterator for BTreeSet<T> {
type Item = T;
type IntoIter = IntoIter<T>;

/// Gets an iterator for moving out the BtreeSet's contents.
///
/// # Examples
///
/// ```
/// # #![feature(core)]
/// use std::collections::BTreeSet;
///
/// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
///
/// let v: Vec<usize> = set.into_iter().collect();
/// assert_eq!(v, [1, 2, 3, 4]);
/// ```
fn into_iter(self) -> IntoIter<T> {
self.into_iter()
fn first<A, B>((a, _): (A, B)) -> A { a }
let first: fn((T, ())) -> T = first; // coerce to fn pointer

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

Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use core::prelude::*;
use core::marker;
use core::fmt;
use core::iter::{FromIterator, IntoIterator};
use core::iter::{FromIterator};
use core::ops::{Sub, BitOr, BitAnd, BitXor};

// FIXME(contentions): implement union family of methods? (general design may be wrong here)
Expand Down
14 changes: 4 additions & 10 deletions src/libcollections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ use core::prelude::*;

use alloc::boxed::Box;
use core::cmp::Ordering;
use core::default::Default;
use core::fmt;
use core::hash::{Hasher, Hash};
use core::iter::{self, FromIterator, IntoIterator};
use core::iter::{self, FromIterator};
use core::mem;
use core::ptr;

Expand Down Expand Up @@ -296,13 +295,6 @@ impl<T> LinkedList<T> {
}
}

/// Consumes the list into an iterator yielding elements by value.
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_iter(self) -> IntoIter<T> {
IntoIter{list: self}
}

/// Returns `true` if the `LinkedList` is empty.
///
/// This operation should compute in O(1) time.
Expand Down Expand Up @@ -852,8 +844,10 @@ impl<T> IntoIterator for LinkedList<T> {
type Item = T;
type IntoIter = IntoIter<T>;

/// Consumes the list into an iterator yielding elements by value.
#[inline]
fn into_iter(self) -> IntoIter<T> {
self.into_iter()
IntoIter{list: self}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

use core::prelude::*;

use core::default::Default;
use core::fmt;
use core::hash;
use core::iter::{IntoIterator, FromIterator};
use core::iter::FromIterator;
use core::mem;
use core::ops::{self, Deref, Add, Index};
use core::ptr;
use core::slice;
#[allow(deprecated)] use core::str::Str;
use core::str::pattern::Pattern;
use unicode::str as unicode_str;
use unicode::str::Utf16Item;
Expand Down
Loading

0 comments on commit ad96559

Please sign in to comment.