1+ use std:: convert:: identity;
2+ use std:: marker:: Unpin ;
13use std:: pin:: Pin ;
24use std:: task:: { Context , Poll } ;
35
@@ -99,58 +101,52 @@ fn merge_works_with_unfused_streams() {
99101 assert_eq ! ( xs, vec![ 92 , 92 ] ) ;
100102}
101103
102- #[ test]
103- fn flat_map_doesnt_poll_completed_inner_stream ( ) {
104- async_std:: task:: block_on ( async {
105- use async_std:: prelude:: * ;
106- use async_std:: task:: * ;
107- use std:: convert:: identity;
108- use std:: marker:: Unpin ;
109- use std:: pin:: Pin ;
104+ struct S < T > ( T ) ;
110105
111- struct S < T > ( T ) ;
106+ impl < T : Stream + Unpin > Stream for S < T > {
107+ type Item = T :: Item ;
112108
113- impl < T : Stream + Unpin > Stream for S < T > {
114- type Item = T :: Item ;
109+ fn poll_next ( mut self : Pin < & mut Self > , ctx : & mut Context ) -> Poll < Option < Self :: Item > > {
110+ unsafe { Pin :: new_unchecked ( & mut self . 0 ) } . poll_next ( ctx)
111+ }
112+ }
115113
116- fn poll_next ( mut self : Pin < & mut Self > , ctx : & mut Context ) -> Poll < Option < Self :: Item > > {
117- unsafe { Pin :: new_unchecked ( & mut self . 0 ) } . poll_next ( ctx)
118- }
119- }
114+ struct StrictOnce {
115+ polled : bool ,
116+ }
120117
121- struct StrictOnce {
122- polled : bool ,
123- } ;
118+ impl Stream for StrictOnce {
119+ type Item = ( ) ;
124120
125- impl Stream for StrictOnce {
126- type Item = ( ) ;
121+ fn poll_next ( mut self : Pin < & mut Self > , _: & mut Context ) -> Poll < Option < Self :: Item > > {
122+ assert ! ( !self . polled, "Polled after completion!" ) ;
123+ self . polled = true ;
124+ Poll :: Ready ( None )
125+ }
126+ }
127127
128- fn poll_next ( mut self : Pin < & mut Self > , _: & mut Context ) -> Poll < Option < Self :: Item > > {
129- assert ! ( !self . polled, "Polled after completion!" ) ;
130- self . polled = true ;
131- Poll :: Ready ( None )
132- }
133- }
128+ struct Interchanger {
129+ polled : bool ,
130+ }
134131
135- struct Interchanger {
136- polled : bool ,
137- } ;
138-
139- impl Stream for Interchanger {
140- type Item = S < Box < dyn Stream < Item = ( ) > + Unpin > > ;
141-
142- fn poll_next ( mut self : Pin < & mut Self > , ctx : & mut Context ) -> Poll < Option < Self :: Item > > {
143- if self . polled {
144- self . polled = false ;
145- ctx. waker ( ) . wake_by_ref ( ) ;
146- Poll :: Pending
147- } else {
148- self . polled = true ;
149- Poll :: Ready ( Some ( S ( Box :: new ( StrictOnce { polled : false } ) ) ) )
150- }
151- }
132+ impl Stream for Interchanger {
133+ type Item = S < Box < dyn Stream < Item = ( ) > + Unpin > > ;
134+
135+ fn poll_next ( mut self : Pin < & mut Self > , ctx : & mut Context ) -> Poll < Option < Self :: Item > > {
136+ if self . polled {
137+ self . polled = false ;
138+ ctx. waker ( ) . wake_by_ref ( ) ;
139+ Poll :: Pending
140+ } else {
141+ self . polled = true ;
142+ Poll :: Ready ( Some ( S ( Box :: new ( StrictOnce { polled : false } ) ) ) )
152143 }
144+ }
145+ }
153146
147+ #[ test]
148+ fn flat_map_doesnt_poll_completed_inner_stream ( ) {
149+ task:: block_on ( async {
154150 assert_eq ! (
155151 Interchanger { polled: false }
156152 . take( 2 )
@@ -161,3 +157,17 @@ fn flat_map_doesnt_poll_completed_inner_stream() {
161157 ) ;
162158 } ) ;
163159}
160+
161+ #[ test]
162+ fn flatten_doesnt_poll_completed_inner_stream ( ) {
163+ task:: block_on ( async {
164+ assert_eq ! (
165+ Interchanger { polled: false }
166+ . take( 2 )
167+ . flatten( )
168+ . count( )
169+ . await ,
170+ 0
171+ ) ;
172+ } ) ;
173+ }
0 commit comments