1- use core:: ops:: { Generator , GeneratorState } ;
2- use core:: iter:: { Iterator , FusedIterator } ;
1+ use core:: iter:: { FusedIterator , Iterator } ;
32use core:: marker:: Unpin ;
3+ use core:: ops:: { Coroutine , CoroutineState } ;
44use core:: pin:: Pin ;
55
6- /// `GenIterReturn<G>` holds a generator `G` or the return value of `G`,
6+ /// `GenIterReturn<G>` holds a coroutine `G` or the return value of `G`,
77/// `&mut GenIterReturn<G>` acts as an iterator.
8- ///
8+ ///
99/// Differences with `GenIter<G>`:
10- /// 1. able to get return value of a generator
11- /// 2. safe to call `next()` after generator is done without panic
10+ /// 1. able to get return value of a coroutine
11+ /// 2. safe to call `next()` after coroutine is done without panic
1212/// 3. maybe less efficient than `GenIter<G>`
1313#[ derive( Copy , Clone , Debug ) ]
14- pub struct GenIterReturn < G : Generator + Unpin > ( Result < G :: Return , G > ) ;
14+ pub struct GenIterReturn < G : Coroutine + Unpin > ( Result < G :: Return , G > ) ;
1515
16- impl < G : Generator + Unpin > GenIterReturn < G > {
16+ impl < G : Coroutine + Unpin > GenIterReturn < G > {
1717 #[ inline]
1818 pub fn new ( g : G ) -> Self {
1919 GenIterReturn ( Err ( g) )
@@ -37,43 +37,43 @@ impl<G: Generator + Unpin> GenIterReturn<G> {
3737/// in which return value cannot be got.
3838/// ```compile_fail
3939/// // !!INVALID CODE!!
40- /// # #![feature(generators )]
40+ /// # #![feature(coroutines )]
4141/// # use gen_iter::gen_iter_return;
4242/// let mut g = gen_iter_return!({ yield 1; return "done"; });
4343/// for v in g {} // invalid, because `GenIterReturn<G>` is not `Iterator`
4444/// let ret = g.return_or_self(); // g is dropped after for loop
4545/// ```
46- impl < G : Generator + Unpin > Iterator for & mut GenIterReturn < G > {
46+ impl < G : Coroutine + Unpin > Iterator for & mut GenIterReturn < G > {
4747 type Item = G :: Yield ;
4848
4949 #[ inline]
5050 fn next ( & mut self ) -> Option < Self :: Item > {
5151 match self . 0 {
5252 Ok ( _) => None ,
5353 Err ( ref mut g) => match Pin :: new ( g) . resume ( ( ) ) {
54- GeneratorState :: Yielded ( y) => Some ( y) ,
55- GeneratorState :: Complete ( r) => {
54+ CoroutineState :: Yielded ( y) => Some ( y) ,
55+ CoroutineState :: Complete ( r) => {
5656 self . 0 = Ok ( r) ;
5757 None
5858 } ,
59- }
59+ } ,
6060 }
6161 }
6262}
6363
6464/// `GenIterReturn<G>` satisfies the trait `FusedIterator`
65- impl < G : Generator + Unpin > FusedIterator for & mut GenIterReturn < G > { }
65+ impl < G : Coroutine + Unpin > FusedIterator for & mut GenIterReturn < G > { }
6666
67- impl < G : Generator + Unpin > From < G > for GenIterReturn < G > {
67+ impl < G : Coroutine + Unpin > From < G > for GenIterReturn < G > {
6868 #[ inline]
6969 fn from ( g : G ) -> Self {
7070 GenIterReturn :: new ( g)
7171 }
7272}
7373
74- /// macro to simplify iterator - via - generator with return value construction
74+ /// macro to simplify iterator - via - coroutine with return value construction
7575/// ```
76- /// #![feature(generators )]
76+ /// #![feature(coroutines )]
7777///
7878/// use gen_iter::gen_iter_return;
7979///
@@ -84,18 +84,18 @@ impl<G: Generator + Unpin> From<G> for GenIterReturn<G> {
8484/// });
8585///
8686/// assert_eq!((&mut g).collect::<Vec<_>>(), [1, 2]); // use `&mut g` as an iterator
87- /// assert_eq!(g.is_done(), true); // check whether generator is done
87+ /// assert_eq!(g.is_done(), true); // check whether the coroutine is done
8888/// assert_eq!((&mut g).next(), None); // safe to call `next()` after done
89- /// assert_eq!(g.return_or_self().ok(), Some("done")); // get return value of generator
89+ /// assert_eq!(g.return_or_self().ok(), Some("done")); // get return value of the coroutine
9090/// ```
9191#[ macro_export]
9292macro_rules! gen_iter_return {
9393 ( $block: block) => {
94- $crate:: GenIterReturn :: new( || $block)
94+ $crate:: GenIterReturn :: new( # [ coroutine ] || $block)
9595 } ;
9696 ( move $block: block) => {
97- $crate:: GenIterReturn :: new( move || $block)
98- }
97+ $crate:: GenIterReturn :: new( # [ coroutine ] move || $block)
98+ } ;
9999}
100100
101101#[ cfg( test) ]
@@ -106,17 +106,20 @@ mod tests {
106106 /// and show that it won't panic when call `next()` even exhausted.
107107 #[ test]
108108 fn it_works ( ) {
109- let mut g = GenIterReturn :: new ( || {
110- yield 1 ;
111- return "done" ;
112- } ) ;
109+ let mut g = GenIterReturn :: new (
110+ #[ coroutine]
111+ || {
112+ yield 1 ;
113+ return "done" ;
114+ } ,
115+ ) ;
113116
114117 assert_eq ! ( ( & mut g) . next( ) , Some ( 1 ) ) ;
115118 assert_eq ! ( g. is_done( ) , false ) ;
116119
117120 g = match g. return_or_self ( ) {
118- Ok ( _) => panic ! ( "generator is done but should not" ) ,
119- Err ( g) => g
121+ Ok ( _) => panic ! ( "coroutine is done but should not" ) ,
122+ Err ( g) => g,
120123 } ;
121124
122125 assert_eq ! ( ( & mut g) . next( ) , None ) ;
@@ -128,11 +131,14 @@ mod tests {
128131 }
129132
130133 #[ test]
131- fn from_generator ( ) {
132- let mut g = GenIterReturn :: from ( || {
133- yield 1 ;
134- return "done" ;
135- } ) ;
134+ fn from_coroutine ( ) {
135+ let mut g = GenIterReturn :: from (
136+ #[ coroutine]
137+ || {
138+ yield 1 ;
139+ return "done" ;
140+ } ,
141+ ) ;
136142
137143 assert_eq ! ( ( & mut g) . next( ) , Some ( 1 ) ) ;
138144 assert_eq ! ( ( & mut g) . next( ) , None ) ;
0 commit comments