Skip to content

Commit 88eaae9

Browse files
oli-obkeholk
authored andcommitted
Add iter macro (movable generator)
1 parent 03eb454 commit 88eaae9

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

Diff for: library/core/src/iter/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ pub use self::adapters::{Intersperse, IntersperseWith};
420420
issue = "42168"
421421
)]
422422
pub use self::range::Step;
423+
#[unstable(feature = "iter_macro", issue = "none", reason = "generators are unstable")]
424+
pub use self::sources::iter;
423425
#[stable(feature = "iter_empty", since = "1.2.0")]
424426
pub use self::sources::{Empty, empty};
425427
#[unstable(

Diff for: library/core/src/iter/sources.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod empty;
22
mod from_coroutine;
33
mod from_fn;
4+
mod generator;
45
mod once;
56
mod once_with;
67
mod repeat;
@@ -18,6 +19,8 @@ pub use self::empty::{Empty, empty};
1819
pub use self::from_coroutine::{FromCoroutine, from_coroutine};
1920
#[stable(feature = "iter_from_fn", since = "1.34.0")]
2021
pub use self::from_fn::{FromFn, from_fn};
22+
#[unstable(feature = "iter_macro", issue = "none", reason = "generators are unstable")]
23+
pub use self::generator::iter;
2124
#[stable(feature = "iter_once", since = "1.2.0")]
2225
pub use self::once::{Once, once};
2326
#[stable(feature = "iter_once_with", since = "1.43.0")]

Diff for: library/core/src/iter/sources/generator.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// Creates a new closure that returns an iterator where each iteration steps the given
2+
/// generator to the next `yield` statement.
3+
///
4+
/// Similar to [`iter::from_fn`], but allows arbitrary control flow.
5+
///
6+
/// [`iter::from_fn`]: crate::iter::from_fn
7+
///
8+
/// # Examples
9+
///
10+
/// ```
11+
/// #![feature(iter_macro, coroutines)]
12+
///
13+
/// let it = std::iter::iter!{
14+
/// yield 1;
15+
/// yield 2;
16+
/// yield 3;
17+
/// };
18+
/// let v: Vec<_> = it.collect();
19+
/// assert_eq!(v, [1, 2, 3]);
20+
/// ```
21+
#[unstable(feature = "iter_macro", issue = "none", reason = "generators are unstable")]
22+
#[allow_internal_unstable(coroutines, iter_from_coroutine)]
23+
pub macro iter($($t:tt)*) {
24+
|| $crate::iter::from_coroutine(#[coroutine] || { $($t)* })
25+
}

Diff for: tests/ui/iterators/generator.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//@ run-pass
2+
3+
#![feature(iter_macro)]
4+
// FIXME(iter_macro): make `yield` within it legal
5+
#![feature(coroutines)]
6+
7+
use std::iter::iter;
8+
9+
fn main() {
10+
let i = iter! {
11+
yield 0;
12+
for x in 5..10 {
13+
yield x * 2;
14+
}
15+
};
16+
let mut i = i();
17+
assert_eq!(i.next(), Some(0));
18+
assert_eq!(i.next(), Some(10));
19+
assert_eq!(i.next(), Some(12));
20+
assert_eq!(i.next(), Some(14));
21+
assert_eq!(i.next(), Some(16));
22+
assert_eq!(i.next(), Some(18));
23+
assert_eq!(i.next(), None);
24+
// FIXME(iter_macro): desugar to `gen` instead of coroutines,
25+
// as the latter panic after resuming iteration.
26+
//assert_eq!(i.next(), None);
27+
//assert_eq!(i.next(), None);
28+
}

0 commit comments

Comments
 (0)