From a8b5d4b7f113975e35b1924af608989e0cd620cd Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 10 Dec 2022 09:27:07 +0100 Subject: [PATCH 1/2] libcore: make result type of iter::from_generator concrete This allows for propagating trait impls on the iterator type. --- .../core/src/iter/sources/from_generator.rs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/library/core/src/iter/sources/from_generator.rs b/library/core/src/iter/sources/from_generator.rs index 8e7cbd34a4f..a974d3c892d 100644 --- a/library/core/src/iter/sources/from_generator.rs +++ b/library/core/src/iter/sources/from_generator.rs @@ -1,3 +1,4 @@ +use crate::fmt; use crate::ops::{Generator, GeneratorState}; use crate::pin::Pin; @@ -23,14 +24,20 @@ /// ``` #[inline] #[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")] -pub fn from_generator + Unpin>( - generator: G, -) -> impl Iterator { +pub fn from_generator + Unpin>(generator: G) -> FromGenerator { FromGenerator(generator) } -struct FromGenerator(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")] +pub struct FromGenerator(G); +#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")] impl + Unpin> Iterator for FromGenerator { type Item = G::Yield; @@ -41,3 +48,10 @@ fn next(&mut self) -> Option { } } } + +#[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")] +impl fmt::Debug for FromGenerator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FromGenerator").finish() + } +} From 17a0740ebb1798ee29fc39c1ec025f0949144541 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 10 Dec 2022 09:03:14 +0100 Subject: [PATCH 2/2] libcore: make result of iter::from_generator Clone This is currently only relevant with #![feature(generator_clone)]. --- library/core/src/iter/sources/from_generator.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/iter/sources/from_generator.rs b/library/core/src/iter/sources/from_generator.rs index a974d3c892d..4cbe731b222 100644 --- a/library/core/src/iter/sources/from_generator.rs +++ b/library/core/src/iter/sources/from_generator.rs @@ -35,6 +35,7 @@ pub fn from_generator + Unpin>(generator: G) -> FromGe /// /// [`iter::from_generator()`]: from_generator #[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")] +#[derive(Clone)] pub struct FromGenerator(G); #[unstable(feature = "iter_from_generator", issue = "43122", reason = "generators are unstable")]