File tree Expand file tree Collapse file tree 2 files changed +17
-14
lines changed Expand file tree Collapse file tree 2 files changed +17
-14
lines changed Original file line number Diff line number Diff line change 1
1
use futures:: future:: Future ;
2
2
use futures:: stream:: Stream ;
3
3
4
+ use core:: pin:: Pin ;
4
5
use core:: task:: { Context , Poll } ;
5
6
6
7
/// Create a future that is immediately ready with a value.
@@ -502,18 +503,21 @@ pub fn poll_fn<F, T>(f: F) -> impl Future<Output = T>
502
503
where
503
504
F : FnMut ( & mut Context ) -> Poll < T > ,
504
505
{
505
- use std:: future:: from_generator;
506
- use std:: future:: get_task_context;
507
-
508
- from_generator ( || {
509
- let mut f = f;
510
- loop {
511
- match get_task_context ( |context : & mut Context | f ( context) ) {
512
- Poll :: Pending => yield ,
513
- Poll :: Ready ( value) => return value,
514
- }
506
+ struct PollFn < F > {
507
+ f : F ,
508
+ }
509
+ impl < F > Unpin for PollFn < F > { }
510
+ impl < T , F > Future for PollFn < F >
511
+ where
512
+ F : FnMut ( & mut Context < ' _ > ) -> Poll < T > ,
513
+ {
514
+ type Output = T ;
515
+
516
+ fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < T > {
517
+ ( & mut self . f ) ( cx)
515
518
}
516
- } )
519
+ }
520
+ PollFn { f }
517
521
}
518
522
519
523
#[ cfg( test) ]
Original file line number Diff line number Diff line change @@ -30,13 +30,12 @@ use pin_utils::pin_mut;
30
30
/// assert_eq!(next(&mut stream).await, None);
31
31
/// # });
32
32
/// ```
33
- pub async fn next < St > ( stream : & mut St ) -> Option < St :: Item >
33
+ pub fn next < ' a , St > ( mut stream : & ' a mut St ) -> impl Future < Output = Option < St :: Item > > + ' a
34
34
where
35
35
St : Stream + Unpin ,
36
36
{
37
37
use crate :: future:: poll_fn;
38
- let future_next = poll_fn ( |context| Pin :: new ( & mut * stream) . poll_next ( context) ) ;
39
- future_next. await
38
+ poll_fn ( move |context| Pin :: new ( & mut stream) . poll_next ( context) )
40
39
}
41
40
42
41
/// Collect all of the values of this stream into a vector, returning a
You can’t perform that action at this time.
0 commit comments