Skip to content

Commit 8f5b5f9

Browse files
committed
std: Add Default/IntoIterator/ToOwned to the prelude
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
1 parent 9d2ac9b commit 8f5b5f9

File tree

25 files changed

+241
-289
lines changed

25 files changed

+241
-289
lines changed

src/liballoc/arc.rs

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ use core::atomic;
7777
use core::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
7878
use core::fmt;
7979
use core::cmp::Ordering;
80-
use core::default::Default;
8180
use core::mem::{min_align_of, size_of};
8281
use core::mem;
8382
use core::nonzero::NonZero;

src/liballoc/boxed.rs

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ use core::prelude::*;
5050

5151
use core::any::Any;
5252
use core::cmp::Ordering;
53-
use core::default::Default;
5453
use core::fmt;
5554
use core::hash::{self, Hash};
5655
use core::mem;

src/libcollections/binary_heap.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@
152152

153153
use core::prelude::*;
154154

155-
use core::default::Default;
156-
use core::iter::{FromIterator, IntoIterator};
155+
use core::iter::{FromIterator};
157156
use core::mem::{zeroed, replace, swap};
158157
use core::ptr;
159158

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

253-
/// Creates a consuming iterator, that is, one that moves each value out of
254-
/// the binary heap in arbitrary order. The binary heap cannot be used
255-
/// after calling this.
256-
///
257-
/// # Examples
258-
///
259-
/// ```
260-
/// # #![feature(collections)]
261-
/// use std::collections::BinaryHeap;
262-
/// let heap = BinaryHeap::from_vec(vec![1, 2, 3, 4]);
263-
///
264-
/// // Print 1, 2, 3, 4 in arbitrary order
265-
/// for x in heap.into_iter() {
266-
/// // x has type i32, not &i32
267-
/// println!("{}", x);
268-
/// }
269-
/// ```
270-
#[stable(feature = "rust1", since = "1.0.0")]
271-
pub fn into_iter(self) -> IntoIter<T> {
272-
IntoIter { iter: self.data.into_iter() }
273-
}
274-
275252
/// Returns the greatest item in the binary heap, or `None` if it is empty.
276253
///
277254
/// # Examples
@@ -675,8 +652,25 @@ impl<T: Ord> IntoIterator for BinaryHeap<T> {
675652
type Item = T;
676653
type IntoIter = IntoIter<T>;
677654

655+
/// Creates a consuming iterator, that is, one that moves each value out of
656+
/// the binary heap in arbitrary order. The binary heap cannot be used
657+
/// after calling this.
658+
///
659+
/// # Examples
660+
///
661+
/// ```
662+
/// # #![feature(collections)]
663+
/// use std::collections::BinaryHeap;
664+
/// let heap = BinaryHeap::from_vec(vec![1, 2, 3, 4]);
665+
///
666+
/// // Print 1, 2, 3, 4 in arbitrary order
667+
/// for x in heap.into_iter() {
668+
/// // x has type i32, not &i32
669+
/// println!("{}", x);
670+
/// }
671+
/// ```
678672
fn into_iter(self) -> IntoIter<T> {
679-
self.into_iter()
673+
IntoIter { iter: self.data.into_iter() }
680674
}
681675
}
682676

src/libcollections/bit.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,11 @@ use core::prelude::*;
8585

8686
use core::cmp::Ordering;
8787
use core::cmp;
88-
use core::default::Default;
8988
use core::fmt;
9089
use core::hash;
9190
use core::iter::RandomAccessIterator;
9291
use core::iter::{Chain, Enumerate, Repeat, Skip, Take, repeat, Cloned};
93-
use core::iter::{self, FromIterator, IntoIterator};
92+
use core::iter::{self, FromIterator};
9493
use core::ops::Index;
9594
use core::slice;
9695
use core::{u8, u32, usize};

src/libcollections/btree/map.rs

+26-32
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ use self::Entry::*;
2020
use core::prelude::*;
2121

2222
use core::cmp::Ordering;
23-
use core::default::Default;
2423
use core::fmt::Debug;
2524
use core::hash::{Hash, Hasher};
26-
use core::iter::{Map, FromIterator, IntoIterator};
25+
use core::iter::{Map, FromIterator};
2726
use core::ops::Index;
2827
use core::{iter, fmt, mem, usize};
2928
use Bound::{self, Included, Excluded, Unbounded};
@@ -472,8 +471,32 @@ impl<K, V> IntoIterator for BTreeMap<K, V> {
472471
type Item = (K, V);
473472
type IntoIter = IntoIter<K, V>;
474473

474+
/// Gets an owning iterator over the entries of the map.
475+
///
476+
/// # Examples
477+
///
478+
/// ```
479+
/// use std::collections::BTreeMap;
480+
///
481+
/// let mut map = BTreeMap::new();
482+
/// map.insert(1, "a");
483+
/// map.insert(2, "b");
484+
/// map.insert(3, "c");
485+
///
486+
/// for (key, value) in map.into_iter() {
487+
/// println!("{}: {}", key, value);
488+
/// }
489+
/// ```
475490
fn into_iter(self) -> IntoIter<K, V> {
476-
self.into_iter()
491+
let len = self.len();
492+
let mut lca = VecDeque::new();
493+
lca.push_back(Traverse::traverse(self.root));
494+
IntoIter {
495+
inner: AbsIter {
496+
traversals: lca,
497+
size: len,
498+
}
499+
}
477500
}
478501
}
479502

@@ -1264,35 +1287,6 @@ impl<K, V> BTreeMap<K, V> {
12641287
}
12651288
}
12661289

1267-
/// Gets an owning iterator over the entries of the map.
1268-
///
1269-
/// # Examples
1270-
///
1271-
/// ```
1272-
/// use std::collections::BTreeMap;
1273-
///
1274-
/// let mut map = BTreeMap::new();
1275-
/// map.insert(1, "a");
1276-
/// map.insert(2, "b");
1277-
/// map.insert(3, "c");
1278-
///
1279-
/// for (key, value) in map.into_iter() {
1280-
/// println!("{}: {}", key, value);
1281-
/// }
1282-
/// ```
1283-
#[stable(feature = "rust1", since = "1.0.0")]
1284-
pub fn into_iter(self) -> IntoIter<K, V> {
1285-
let len = self.len();
1286-
let mut lca = VecDeque::new();
1287-
lca.push_back(Traverse::traverse(self.root));
1288-
IntoIter {
1289-
inner: AbsIter {
1290-
traversals: lca,
1291-
size: len,
1292-
}
1293-
}
1294-
}
1295-
12961290
/// Gets an iterator over the keys of the map.
12971291
///
12981292
/// # Examples

src/libcollections/btree/set.rs

+18-24
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
use core::prelude::*;
1515

1616
use core::cmp::Ordering::{self, Less, Greater, Equal};
17-
use core::default::Default;
1817
use core::fmt::Debug;
1918
use core::fmt;
20-
use core::iter::{Peekable, Map, FromIterator, IntoIterator};
19+
use core::iter::{Peekable, Map, FromIterator};
2120
use core::ops::{BitOr, BitAnd, BitXor, Sub};
2221

2322
use borrow::Borrow;
@@ -132,27 +131,6 @@ impl<T> BTreeSet<T> {
132131
pub fn iter(&self) -> Iter<T> {
133132
Iter { iter: self.map.keys() }
134133
}
135-
136-
/// Gets an iterator for moving out the BtreeSet's contents.
137-
///
138-
/// # Examples
139-
///
140-
/// ```
141-
/// # #![feature(core)]
142-
/// use std::collections::BTreeSet;
143-
///
144-
/// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
145-
///
146-
/// let v: Vec<usize> = set.into_iter().collect();
147-
/// assert_eq!(v, [1, 2, 3, 4]);
148-
/// ```
149-
#[stable(feature = "rust1", since = "1.0.0")]
150-
pub fn into_iter(self) -> IntoIter<T> {
151-
fn first<A, B>((a, _): (A, B)) -> A { a }
152-
let first: fn((T, ())) -> T = first; // coerce to fn pointer
153-
154-
IntoIter { iter: self.map.into_iter().map(first) }
155-
}
156134
}
157135

158136
impl<T: Ord> BTreeSet<T> {
@@ -500,8 +478,24 @@ impl<T> IntoIterator for BTreeSet<T> {
500478
type Item = T;
501479
type IntoIter = IntoIter<T>;
502480

481+
/// Gets an iterator for moving out the BtreeSet's contents.
482+
///
483+
/// # Examples
484+
///
485+
/// ```
486+
/// # #![feature(core)]
487+
/// use std::collections::BTreeSet;
488+
///
489+
/// let set: BTreeSet<usize> = [1, 2, 3, 4].iter().cloned().collect();
490+
///
491+
/// let v: Vec<usize> = set.into_iter().collect();
492+
/// assert_eq!(v, [1, 2, 3, 4]);
493+
/// ```
503494
fn into_iter(self) -> IntoIter<T> {
504-
self.into_iter()
495+
fn first<A, B>((a, _): (A, B)) -> A { a }
496+
let first: fn((T, ())) -> T = first; // coerce to fn pointer
497+
498+
IntoIter { iter: self.map.into_iter().map(first) }
505499
}
506500
}
507501

src/libcollections/enum_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use core::prelude::*;
1717
use core::marker;
1818
use core::fmt;
19-
use core::iter::{FromIterator, IntoIterator};
19+
use core::iter::{FromIterator};
2020
use core::ops::{Sub, BitOr, BitAnd, BitXor};
2121

2222
// FIXME(contentions): implement union family of methods? (general design may be wrong here)

src/libcollections/linked_list.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ use core::prelude::*;
2525

2626
use alloc::boxed::Box;
2727
use core::cmp::Ordering;
28-
use core::default::Default;
2928
use core::fmt;
3029
use core::hash::{Hasher, Hash};
31-
use core::iter::{self, FromIterator, IntoIterator};
30+
use core::iter::{self, FromIterator};
3231
use core::mem;
3332
use core::ptr;
3433

@@ -296,13 +295,6 @@ impl<T> LinkedList<T> {
296295
}
297296
}
298297

299-
/// Consumes the list into an iterator yielding elements by value.
300-
#[inline]
301-
#[stable(feature = "rust1", since = "1.0.0")]
302-
pub fn into_iter(self) -> IntoIter<T> {
303-
IntoIter{list: self}
304-
}
305-
306298
/// Returns `true` if the `LinkedList` is empty.
307299
///
308300
/// This operation should compute in O(1) time.
@@ -852,8 +844,10 @@ impl<T> IntoIterator for LinkedList<T> {
852844
type Item = T;
853845
type IntoIter = IntoIter<T>;
854846

847+
/// Consumes the list into an iterator yielding elements by value.
848+
#[inline]
855849
fn into_iter(self) -> IntoIter<T> {
856-
self.into_iter()
850+
IntoIter{list: self}
857851
}
858852
}
859853

@@ -941,7 +935,7 @@ impl<A: Hash> Hash for LinkedList<A> {
941935
#[cfg(test)]
942936
mod test {
943937
use std::clone::Clone;
944-
use std::iter::Iterator;
938+
use std::iter::{Iterator, IntoIterator};
945939
use std::option::Option::{Some, None, self};
946940
use std::__rand::{thread_rng, Rng};
947941
use std::thread;

src/libcollections/string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
use core::prelude::*;
1818

19-
use core::default::Default;
2019
use core::fmt;
2120
use core::hash;
22-
use core::iter::{IntoIterator, FromIterator};
21+
use core::iter::FromIterator;
2322
use core::mem;
2423
use core::ops::{self, Deref, Add, Index};
2524
use core::ptr;
2625
use core::slice;
26+
#[allow(deprecated)] use core::str::Str;
2727
use core::str::pattern::Pattern;
2828
use unicode::str as unicode_str;
2929
use unicode::str::Utf16Item;

0 commit comments

Comments
 (0)