Skip to content

Commit 66d6708

Browse files
committed
Split iterator sources into different modules
1 parent 773b73c commit 66d6708

File tree

8 files changed

+644
-614
lines changed

8 files changed

+644
-614
lines changed

library/core/src/iter/sources.rs

Lines changed: 16 additions & 614 deletions
Large diffs are not rendered by default.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use crate::{fmt, iter::{FusedIterator, TrustedLen}, marker};
2+
3+
/// Creates an iterator that yields nothing.
4+
///
5+
/// # Examples
6+
///
7+
/// Basic usage:
8+
///
9+
/// ```
10+
/// use std::iter;
11+
///
12+
/// // this could have been an iterator over i32, but alas, it's just not.
13+
/// let mut nope = iter::empty::<i32>();
14+
///
15+
/// assert_eq!(None, nope.next());
16+
/// ```
17+
#[stable(feature = "iter_empty", since = "1.2.0")]
18+
#[rustc_const_stable(feature = "const_iter_empty", since = "1.32.0")]
19+
pub const fn empty<T>() -> Empty<T> {
20+
Empty(marker::PhantomData)
21+
}
22+
23+
/// An iterator that yields nothing.
24+
///
25+
/// This `struct` is created by the [`empty()`] function. See its documentation for more.
26+
#[stable(feature = "iter_empty", since = "1.2.0")]
27+
pub struct Empty<T>(marker::PhantomData<T>);
28+
29+
#[stable(feature = "iter_empty_send_sync", since = "1.42.0")]
30+
unsafe impl<T> Send for Empty<T> {}
31+
#[stable(feature = "iter_empty_send_sync", since = "1.42.0")]
32+
unsafe impl<T> Sync for Empty<T> {}
33+
34+
#[stable(feature = "core_impl_debug", since = "1.9.0")]
35+
impl<T> fmt::Debug for Empty<T> {
36+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37+
f.pad("Empty")
38+
}
39+
}
40+
41+
#[stable(feature = "iter_empty", since = "1.2.0")]
42+
impl<T> Iterator for Empty<T> {
43+
type Item = T;
44+
45+
fn next(&mut self) -> Option<T> {
46+
None
47+
}
48+
49+
fn size_hint(&self) -> (usize, Option<usize>) {
50+
(0, Some(0))
51+
}
52+
}
53+
54+
#[stable(feature = "iter_empty", since = "1.2.0")]
55+
impl<T> DoubleEndedIterator for Empty<T> {
56+
fn next_back(&mut self) -> Option<T> {
57+
None
58+
}
59+
}
60+
61+
#[stable(feature = "iter_empty", since = "1.2.0")]
62+
impl<T> ExactSizeIterator for Empty<T> {
63+
fn len(&self) -> usize {
64+
0
65+
}
66+
}
67+
68+
#[unstable(feature = "trusted_len", issue = "37572")]
69+
unsafe impl<T> TrustedLen for Empty<T> {}
70+
71+
#[stable(feature = "fused", since = "1.26.0")]
72+
impl<T> FusedIterator for Empty<T> {}
73+
74+
// not #[derive] because that adds a Clone bound on T,
75+
// which isn't necessary.
76+
#[stable(feature = "iter_empty", since = "1.2.0")]
77+
impl<T> Clone for Empty<T> {
78+
fn clone(&self) -> Empty<T> {
79+
Empty(marker::PhantomData)
80+
}
81+
}
82+
83+
// not #[derive] because that adds a Default bound on T,
84+
// which isn't necessary.
85+
#[stable(feature = "iter_empty", since = "1.2.0")]
86+
impl<T> Default for Empty<T> {
87+
fn default() -> Empty<T> {
88+
Empty(marker::PhantomData)
89+
}
90+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use crate::fmt;
2+
3+
/// Creates a new iterator where each iteration calls the provided closure
4+
/// `F: FnMut() -> Option<T>`.
5+
///
6+
/// This allows creating a custom iterator with any behavior
7+
/// without using the more verbose syntax of creating a dedicated type
8+
/// and implementing the [`Iterator`] trait for it.
9+
///
10+
/// Note that the `FromFn` iterator doesn’t make assumptions about the behavior of the closure,
11+
/// and therefore conservatively does not implement [`FusedIterator`],
12+
/// or override [`Iterator::size_hint()`] from its default `(0, None)`.
13+
///
14+
/// The closure can use captures and its environment to track state across iterations. Depending on
15+
/// how the iterator is used, this may require specifying the [`move`] keyword on the closure.
16+
///
17+
/// [`move`]: ../../std/keyword.move.html
18+
///
19+
/// # Examples
20+
///
21+
/// Let’s re-implement the counter iterator from [module-level documentation]:
22+
///
23+
/// [module-level documentation]: crate::iter
24+
///
25+
/// ```
26+
/// let mut count = 0;
27+
/// let counter = std::iter::from_fn(move || {
28+
/// // Increment our count. This is why we started at zero.
29+
/// count += 1;
30+
///
31+
/// // Check to see if we've finished counting or not.
32+
/// if count < 6 {
33+
/// Some(count)
34+
/// } else {
35+
/// None
36+
/// }
37+
/// });
38+
/// assert_eq!(counter.collect::<Vec<_>>(), &[1, 2, 3, 4, 5]);
39+
/// ```
40+
#[inline]
41+
#[stable(feature = "iter_from_fn", since = "1.34.0")]
42+
pub fn from_fn<T, F>(f: F) -> FromFn<F>
43+
where
44+
F: FnMut() -> Option<T>,
45+
{
46+
FromFn(f)
47+
}
48+
49+
/// An iterator where each iteration calls the provided closure `F: FnMut() -> Option<T>`.
50+
///
51+
/// This `struct` is created by the [`iter::from_fn()`] function.
52+
/// See its documentation for more.
53+
///
54+
/// [`iter::from_fn()`]: from_fn
55+
#[derive(Clone)]
56+
#[stable(feature = "iter_from_fn", since = "1.34.0")]
57+
pub struct FromFn<F>(F);
58+
59+
#[stable(feature = "iter_from_fn", since = "1.34.0")]
60+
impl<T, F> Iterator for FromFn<F>
61+
where
62+
F: FnMut() -> Option<T>,
63+
{
64+
type Item = T;
65+
66+
#[inline]
67+
fn next(&mut self) -> Option<Self::Item> {
68+
(self.0)()
69+
}
70+
}
71+
72+
#[stable(feature = "iter_from_fn", since = "1.34.0")]
73+
impl<F> fmt::Debug for FromFn<F> {
74+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75+
f.debug_struct("FromFn").finish()
76+
}
77+
}

library/core/src/iter/sources/once.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use crate::iter::{FusedIterator, TrustedLen};
2+
3+
/// Creates an iterator that yields an element exactly once.
4+
///
5+
/// This is commonly used to adapt a single value into a [`chain()`] of other
6+
/// kinds of iteration. Maybe you have an iterator that covers almost
7+
/// everything, but you need an extra special case. Maybe you have a function
8+
/// which works on iterators, but you only need to process one value.
9+
///
10+
/// [`chain()`]: Iterator::chain
11+
///
12+
/// # Examples
13+
///
14+
/// Basic usage:
15+
///
16+
/// ```
17+
/// use std::iter;
18+
///
19+
/// // one is the loneliest number
20+
/// let mut one = iter::once(1);
21+
///
22+
/// assert_eq!(Some(1), one.next());
23+
///
24+
/// // just one, that's all we get
25+
/// assert_eq!(None, one.next());
26+
/// ```
27+
///
28+
/// Chaining together with another iterator. Let's say that we want to iterate
29+
/// over each file of the `.foo` directory, but also a configuration file,
30+
/// `.foorc`:
31+
///
32+
/// ```no_run
33+
/// use std::iter;
34+
/// use std::fs;
35+
/// use std::path::PathBuf;
36+
///
37+
/// let dirs = fs::read_dir(".foo").unwrap();
38+
///
39+
/// // we need to convert from an iterator of DirEntry-s to an iterator of
40+
/// // PathBufs, so we use map
41+
/// let dirs = dirs.map(|file| file.unwrap().path());
42+
///
43+
/// // now, our iterator just for our config file
44+
/// let config = iter::once(PathBuf::from(".foorc"));
45+
///
46+
/// // chain the two iterators together into one big iterator
47+
/// let files = dirs.chain(config);
48+
///
49+
/// // this will give us all of the files in .foo as well as .foorc
50+
/// for f in files {
51+
/// println!("{:?}", f);
52+
/// }
53+
/// ```
54+
#[stable(feature = "iter_once", since = "1.2.0")]
55+
pub fn once<T>(value: T) -> Once<T> {
56+
Once { inner: Some(value).into_iter() }
57+
}
58+
59+
/// An iterator that yields an element exactly once.
60+
///
61+
/// This `struct` is created by the [`once()`] function. See its documentation for more.
62+
#[derive(Clone, Debug)]
63+
#[stable(feature = "iter_once", since = "1.2.0")]
64+
pub struct Once<T> {
65+
inner: crate::option::IntoIter<T>,
66+
}
67+
68+
#[stable(feature = "iter_once", since = "1.2.0")]
69+
impl<T> Iterator for Once<T> {
70+
type Item = T;
71+
72+
fn next(&mut self) -> Option<T> {
73+
self.inner.next()
74+
}
75+
76+
fn size_hint(&self) -> (usize, Option<usize>) {
77+
self.inner.size_hint()
78+
}
79+
}
80+
81+
#[stable(feature = "iter_once", since = "1.2.0")]
82+
impl<T> DoubleEndedIterator for Once<T> {
83+
fn next_back(&mut self) -> Option<T> {
84+
self.inner.next_back()
85+
}
86+
}
87+
88+
#[stable(feature = "iter_once", since = "1.2.0")]
89+
impl<T> ExactSizeIterator for Once<T> {
90+
fn len(&self) -> usize {
91+
self.inner.len()
92+
}
93+
}
94+
95+
#[unstable(feature = "trusted_len", issue = "37572")]
96+
unsafe impl<T> TrustedLen for Once<T> {}
97+
98+
#[stable(feature = "fused", since = "1.26.0")]
99+
impl<T> FusedIterator for Once<T> {}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use crate::iter::{FusedIterator, TrustedLen};
2+
3+
/// Creates an iterator that lazily generates a value exactly once by invoking
4+
/// the provided closure.
5+
///
6+
/// This is commonly used to adapt a single value generator into a [`chain()`] of
7+
/// other kinds of iteration. Maybe you have an iterator that covers almost
8+
/// everything, but you need an extra special case. Maybe you have a function
9+
/// which works on iterators, but you only need to process one value.
10+
///
11+
/// Unlike [`once()`], this function will lazily generate the value on request.
12+
///
13+
/// [`chain()`]: Iterator::chain
14+
///
15+
/// # Examples
16+
///
17+
/// Basic usage:
18+
///
19+
/// ```
20+
/// use std::iter;
21+
///
22+
/// // one is the loneliest number
23+
/// let mut one = iter::once_with(|| 1);
24+
///
25+
/// assert_eq!(Some(1), one.next());
26+
///
27+
/// // just one, that's all we get
28+
/// assert_eq!(None, one.next());
29+
/// ```
30+
///
31+
/// Chaining together with another iterator. Let's say that we want to iterate
32+
/// over each file of the `.foo` directory, but also a configuration file,
33+
/// `.foorc`:
34+
///
35+
/// ```no_run
36+
/// use std::iter;
37+
/// use std::fs;
38+
/// use std::path::PathBuf;
39+
///
40+
/// let dirs = fs::read_dir(".foo").unwrap();
41+
///
42+
/// // we need to convert from an iterator of DirEntry-s to an iterator of
43+
/// // PathBufs, so we use map
44+
/// let dirs = dirs.map(|file| file.unwrap().path());
45+
///
46+
/// // now, our iterator just for our config file
47+
/// let config = iter::once_with(|| PathBuf::from(".foorc"));
48+
///
49+
/// // chain the two iterators together into one big iterator
50+
/// let files = dirs.chain(config);
51+
///
52+
/// // this will give us all of the files in .foo as well as .foorc
53+
/// for f in files {
54+
/// println!("{:?}", f);
55+
/// }
56+
/// ```
57+
#[inline]
58+
#[stable(feature = "iter_once_with", since = "1.43.0")]
59+
pub fn once_with<A, F: FnOnce() -> A>(gen: F) -> OnceWith<F> {
60+
OnceWith { gen: Some(gen) }
61+
}
62+
63+
/// An iterator that yields a single element of type `A` by
64+
/// applying the provided closure `F: FnOnce() -> A`.
65+
///
66+
/// This `struct` is created by the [`once_with()`] function.
67+
/// See its documentation for more.
68+
#[derive(Clone, Debug)]
69+
#[stable(feature = "iter_once_with", since = "1.43.0")]
70+
pub struct OnceWith<F> {
71+
gen: Option<F>,
72+
}
73+
74+
#[stable(feature = "iter_once_with", since = "1.43.0")]
75+
impl<A, F: FnOnce() -> A> Iterator for OnceWith<F> {
76+
type Item = A;
77+
78+
#[inline]
79+
fn next(&mut self) -> Option<A> {
80+
let f = self.gen.take()?;
81+
Some(f())
82+
}
83+
84+
#[inline]
85+
fn size_hint(&self) -> (usize, Option<usize>) {
86+
self.gen.iter().size_hint()
87+
}
88+
}
89+
90+
#[stable(feature = "iter_once_with", since = "1.43.0")]
91+
impl<A, F: FnOnce() -> A> DoubleEndedIterator for OnceWith<F> {
92+
fn next_back(&mut self) -> Option<A> {
93+
self.next()
94+
}
95+
}
96+
97+
#[stable(feature = "iter_once_with", since = "1.43.0")]
98+
impl<A, F: FnOnce() -> A> ExactSizeIterator for OnceWith<F> {
99+
fn len(&self) -> usize {
100+
self.gen.iter().len()
101+
}
102+
}
103+
104+
#[stable(feature = "iter_once_with", since = "1.43.0")]
105+
impl<A, F: FnOnce() -> A> FusedIterator for OnceWith<F> {}
106+
107+
#[stable(feature = "iter_once_with", since = "1.43.0")]
108+
unsafe impl<A, F: FnOnce() -> A> TrustedLen for OnceWith<F> {}

0 commit comments

Comments
 (0)