Skip to content

Commit d81de48

Browse files
author
Jonathan Turner
committed
Merge remote-tracking branch 'rust-lang/master' into compiletest-json
2 parents fd0632f + a264f5b commit d81de48

File tree

30 files changed

+5955
-5520
lines changed

30 files changed

+5955
-5520
lines changed

src/libcore/iter.rs

-5,012
This file was deleted.

src/libcore/iter/iterator.rs

+2,112
Large diffs are not rendered by default.

src/libcore/iter/mod.rs

+1,657
Large diffs are not rendered by default.

src/libcore/iter/range.rs

+548
Large diffs are not rendered by default.

src/libcore/iter/sources.rs

+270
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
// Copyright 2013-2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use clone::Clone;
12+
use default::Default;
13+
use fmt;
14+
use marker;
15+
use option::Option::{self, Some, None};
16+
use usize;
17+
18+
use super::{DoubleEndedIterator, IntoIterator, Iterator, ExactSizeIterator};
19+
20+
/// An iterator that repeats an element endlessly.
21+
///
22+
/// This `struct` is created by the [`repeat()`] function. See its documentation for more.
23+
///
24+
/// [`repeat()`]: fn.repeat.html
25+
#[derive(Clone, Debug)]
26+
#[stable(feature = "rust1", since = "1.0.0")]
27+
pub struct Repeat<A> {
28+
element: A
29+
}
30+
31+
#[stable(feature = "rust1", since = "1.0.0")]
32+
impl<A: Clone> Iterator for Repeat<A> {
33+
type Item = A;
34+
35+
#[inline]
36+
fn next(&mut self) -> Option<A> { Some(self.element.clone()) }
37+
#[inline]
38+
fn size_hint(&self) -> (usize, Option<usize>) { (usize::MAX, None) }
39+
}
40+
41+
#[stable(feature = "rust1", since = "1.0.0")]
42+
impl<A: Clone> DoubleEndedIterator for Repeat<A> {
43+
#[inline]
44+
fn next_back(&mut self) -> Option<A> { Some(self.element.clone()) }
45+
}
46+
47+
/// Creates a new iterator that endlessly repeats a single element.
48+
///
49+
/// The `repeat()` function repeats a single value over and over and over and
50+
/// over and over and 🔁.
51+
///
52+
/// Infinite iterators like `repeat()` are often used with adapters like
53+
/// [`take()`], in order to make them finite.
54+
///
55+
/// [`take()`]: trait.Iterator.html#method.take
56+
///
57+
/// # Examples
58+
///
59+
/// Basic usage:
60+
///
61+
/// ```
62+
/// use std::iter;
63+
///
64+
/// // the number four 4ever:
65+
/// let mut fours = iter::repeat(4);
66+
///
67+
/// assert_eq!(Some(4), fours.next());
68+
/// assert_eq!(Some(4), fours.next());
69+
/// assert_eq!(Some(4), fours.next());
70+
/// assert_eq!(Some(4), fours.next());
71+
/// assert_eq!(Some(4), fours.next());
72+
///
73+
/// // yup, still four
74+
/// assert_eq!(Some(4), fours.next());
75+
/// ```
76+
///
77+
/// Going finite with [`take()`]:
78+
///
79+
/// ```
80+
/// use std::iter;
81+
///
82+
/// // that last example was too many fours. Let's only have four fours.
83+
/// let mut four_fours = iter::repeat(4).take(4);
84+
///
85+
/// assert_eq!(Some(4), four_fours.next());
86+
/// assert_eq!(Some(4), four_fours.next());
87+
/// assert_eq!(Some(4), four_fours.next());
88+
/// assert_eq!(Some(4), four_fours.next());
89+
///
90+
/// // ... and now we're done
91+
/// assert_eq!(None, four_fours.next());
92+
/// ```
93+
#[inline]
94+
#[stable(feature = "rust1", since = "1.0.0")]
95+
pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
96+
Repeat{element: elt}
97+
}
98+
99+
/// An iterator that yields nothing.
100+
///
101+
/// This `struct` is created by the [`empty()`] function. See its documentation for more.
102+
///
103+
/// [`empty()`]: fn.empty.html
104+
#[stable(feature = "iter_empty", since = "1.2.0")]
105+
pub struct Empty<T>(marker::PhantomData<T>);
106+
107+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
108+
impl<T> fmt::Debug for Empty<T> {
109+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110+
f.pad("Empty")
111+
}
112+
}
113+
114+
#[stable(feature = "iter_empty", since = "1.2.0")]
115+
impl<T> Iterator for Empty<T> {
116+
type Item = T;
117+
118+
fn next(&mut self) -> Option<T> {
119+
None
120+
}
121+
122+
fn size_hint(&self) -> (usize, Option<usize>){
123+
(0, Some(0))
124+
}
125+
}
126+
127+
#[stable(feature = "iter_empty", since = "1.2.0")]
128+
impl<T> DoubleEndedIterator for Empty<T> {
129+
fn next_back(&mut self) -> Option<T> {
130+
None
131+
}
132+
}
133+
134+
#[stable(feature = "iter_empty", since = "1.2.0")]
135+
impl<T> ExactSizeIterator for Empty<T> {
136+
fn len(&self) -> usize {
137+
0
138+
}
139+
}
140+
141+
// not #[derive] because that adds a Clone bound on T,
142+
// which isn't necessary.
143+
#[stable(feature = "iter_empty", since = "1.2.0")]
144+
impl<T> Clone for Empty<T> {
145+
fn clone(&self) -> Empty<T> {
146+
Empty(marker::PhantomData)
147+
}
148+
}
149+
150+
// not #[derive] because that adds a Default bound on T,
151+
// which isn't necessary.
152+
#[stable(feature = "iter_empty", since = "1.2.0")]
153+
impl<T> Default for Empty<T> {
154+
fn default() -> Empty<T> {
155+
Empty(marker::PhantomData)
156+
}
157+
}
158+
159+
/// Creates an iterator that yields nothing.
160+
///
161+
/// # Examples
162+
///
163+
/// Basic usage:
164+
///
165+
/// ```
166+
/// use std::iter;
167+
///
168+
/// // this could have been an iterator over i32, but alas, it's just not.
169+
/// let mut nope = iter::empty::<i32>();
170+
///
171+
/// assert_eq!(None, nope.next());
172+
/// ```
173+
#[stable(feature = "iter_empty", since = "1.2.0")]
174+
pub fn empty<T>() -> Empty<T> {
175+
Empty(marker::PhantomData)
176+
}
177+
178+
/// An iterator that yields an element exactly once.
179+
///
180+
/// This `struct` is created by the [`once()`] function. See its documentation for more.
181+
///
182+
/// [`once()`]: fn.once.html
183+
#[derive(Clone, Debug)]
184+
#[stable(feature = "iter_once", since = "1.2.0")]
185+
pub struct Once<T> {
186+
inner: ::option::IntoIter<T>
187+
}
188+
189+
#[stable(feature = "iter_once", since = "1.2.0")]
190+
impl<T> Iterator for Once<T> {
191+
type Item = T;
192+
193+
fn next(&mut self) -> Option<T> {
194+
self.inner.next()
195+
}
196+
197+
fn size_hint(&self) -> (usize, Option<usize>) {
198+
self.inner.size_hint()
199+
}
200+
}
201+
202+
#[stable(feature = "iter_once", since = "1.2.0")]
203+
impl<T> DoubleEndedIterator for Once<T> {
204+
fn next_back(&mut self) -> Option<T> {
205+
self.inner.next_back()
206+
}
207+
}
208+
209+
#[stable(feature = "iter_once", since = "1.2.0")]
210+
impl<T> ExactSizeIterator for Once<T> {
211+
fn len(&self) -> usize {
212+
self.inner.len()
213+
}
214+
}
215+
216+
/// Creates an iterator that yields an element exactly once.
217+
///
218+
/// This is commonly used to adapt a single value into a [`chain()`] of other
219+
/// kinds of iteration. Maybe you have an iterator that covers almost
220+
/// everything, but you need an extra special case. Maybe you have a function
221+
/// which works on iterators, but you only need to process one value.
222+
///
223+
/// [`chain()`]: trait.Iterator.html#method.chain
224+
///
225+
/// # Examples
226+
///
227+
/// Basic usage:
228+
///
229+
/// ```
230+
/// use std::iter;
231+
///
232+
/// // one is the loneliest number
233+
/// let mut one = iter::once(1);
234+
///
235+
/// assert_eq!(Some(1), one.next());
236+
///
237+
/// // just one, that's all we get
238+
/// assert_eq!(None, one.next());
239+
/// ```
240+
///
241+
/// Chaining together with another iterator. Let's say that we want to iterate
242+
/// over each file of the `.foo` directory, but also a configuration file,
243+
/// `.foorc`:
244+
///
245+
/// ```no_run
246+
/// use std::iter;
247+
/// use std::fs;
248+
/// use std::path::PathBuf;
249+
///
250+
/// let dirs = fs::read_dir(".foo").unwrap();
251+
///
252+
/// // we need to convert from an iterator of DirEntry-s to an iterator of
253+
/// // PathBufs, so we use map
254+
/// let dirs = dirs.map(|file| file.unwrap().path());
255+
///
256+
/// // now, our iterator just for our config file
257+
/// let config = iter::once(PathBuf::from(".foorc"));
258+
///
259+
/// // chain the two iterators together into one big iterator
260+
/// let files = dirs.chain(config);
261+
///
262+
/// // this will give us all of the files in .foo as well as .foorc
263+
/// for f in files {
264+
/// println!("{:?}", f);
265+
/// }
266+
/// ```
267+
#[stable(feature = "iter_once", since = "1.2.0")]
268+
pub fn once<T>(value: T) -> Once<T> {
269+
Once { inner: Some(value).into_iter() }
270+
}

0 commit comments

Comments
 (0)