Skip to content

Commit

Permalink
Rollup merge of #105526 - Xiretza:iter-from-generator-derive, r=scottmcm
Browse files Browse the repository at this point in the history
libcore: make result of iter::from_generator Clone

`@rustbot` label +A-generators
  • Loading branch information
matthiaskrgr authored Jan 14, 2023
2 parents 4b51adf + 17a0740 commit 085d2f1
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions library/core/src/iter/sources/from_generator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::fmt;
use crate::ops::{Generator, GeneratorState};
use crate::pin::Pin;

Expand All @@ -23,14 +24,21 @@ use crate::pin::Pin;
/// ```
#[inline]
#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")]
pub fn from_generator<G: Generator<Return = ()> + Unpin>(
generator: G,
) -> impl Iterator<Item = G::Yield> {
pub fn from_generator<G: Generator<Return = ()> + Unpin>(generator: G) -> FromGenerator<G> {
FromGenerator(generator)
}

struct FromGenerator<G>(G);
/// An iterator over the values yielded by an underlying generator.
///
/// This `struct` is created by the [`iter::from_generator()`] function. See its documentation for
/// more.
///
/// [`iter::from_generator()`]: from_generator
#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")]
#[derive(Clone)]
pub struct FromGenerator<G>(G);

#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")]
impl<G: Generator<Return = ()> + Unpin> Iterator for FromGenerator<G> {
type Item = G::Yield;

Expand All @@ -41,3 +49,10 @@ impl<G: Generator<Return = ()> + Unpin> Iterator for FromGenerator<G> {
}
}
}

#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")]
impl<G> fmt::Debug for FromGenerator<G> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FromGenerator").finish()
}
}

0 comments on commit 085d2f1

Please sign in to comment.