Skip to content

Commit 5e88ffc

Browse files
oli-obkjieyouxutraviscross
authored andcommitted
Add iter macro
This adds an `iter!` macro that can be used to create movable generators. This also adds a yield_expr feature so the `yield` keyword can be used within iter! macro bodies. This was needed because several unstable features each need `yield` expressions, so this allows us to stabilize them separately from any individual feature. Co-authored-by: Oli Scherer <[email protected]> Co-authored-by: Jieyou Xu <[email protected]> Co-authored-by: Travis Cross <[email protected]>
1 parent 504f596 commit 5e88ffc

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

core/src/iter/mod.rs

Lines changed: 2 additions & 0 deletions
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(

core/src/iter/sources.rs

Lines changed: 3 additions & 0 deletions
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")]

core/src/iter/sources/generator.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
/// # #[cfg(not(bootstrap))]
13+
/// # {
14+
///
15+
/// let it = std::iter::iter!{|| {
16+
/// yield 1;
17+
/// yield 2;
18+
/// yield 3;
19+
/// } }();
20+
/// let v: Vec<_> = it.collect();
21+
/// assert_eq!(v, [1, 2, 3]);
22+
/// # }
23+
/// ```
24+
#[unstable(feature = "iter_macro", issue = "none", reason = "generators are unstable")]
25+
#[allow_internal_unstable(coroutines, iter_from_coroutine)]
26+
#[cfg_attr(not(bootstrap), rustc_builtin_macro)]
27+
pub macro iter($($t:tt)*) {
28+
/* compiler-builtin */
29+
}

0 commit comments

Comments
 (0)