File tree 2 files changed +52
-0
lines changed
2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ use futures_core::future::Future;
10
10
use futures_executor;
11
11
use std:: thread;
12
12
13
+ pub use crate :: interleave_pending:: InterleavePending ;
14
+
13
15
/// Additional combinators for testing futures.
14
16
pub trait FutureTestExt : Future {
15
17
/// Asserts that the given is not moved after being polled.
@@ -79,6 +81,34 @@ pub trait FutureTestExt: Future {
79
81
{
80
82
thread:: spawn ( || futures_executor:: block_on ( self ) ) ;
81
83
}
84
+
85
+ /// Introduces an extra [`Poll::Pending`](futures_core::task::Poll::Pending)
86
+ /// in between each call to poll.
87
+ ///
88
+ /// # Examples
89
+ ///
90
+ /// ```
91
+ /// #![feature(async_await)]
92
+ /// use futures::task::Poll;
93
+ /// use futures::future::{self, Future};
94
+ /// use futures_test::task::noop_context;
95
+ /// use futures_test::future::FutureTestExt;
96
+ /// use pin_utils::pin_mut;
97
+ ///
98
+ /// let future = future::ready(1).interleave_pending();
99
+ /// pin_mut!(future);
100
+ ///
101
+ /// let mut cx = noop_context();
102
+ ///
103
+ /// assert_eq!(future.as_mut().poll(&mut cx), Poll::Pending);
104
+ /// assert_eq!(future.as_mut().poll(&mut cx), Poll::Ready(1));
105
+ /// ```
106
+ fn interleave_pending ( self ) -> InterleavePending < Self >
107
+ where
108
+ Self : Sized ,
109
+ {
110
+ InterleavePending :: new ( self )
111
+ }
82
112
}
83
113
84
114
impl < Fut > FutureTestExt for Fut where Fut : Future { }
Original file line number Diff line number Diff line change
1
+ use futures_core:: future:: Future ;
1
2
use futures_core:: stream:: Stream ;
2
3
use futures_io:: { self as io, AsyncBufRead , AsyncRead , AsyncWrite } ;
3
4
use pin_utils:: { unsafe_pinned, unsafe_unpinned} ;
@@ -63,6 +64,27 @@ impl<T> InterleavePending<T> {
63
64
}
64
65
}
65
66
67
+ impl < Fut : Future > Future for InterleavePending < Fut > {
68
+ type Output = Fut :: Output ;
69
+
70
+ fn poll (
71
+ mut self : Pin < & mut Self > ,
72
+ cx : & mut Context < ' _ > ,
73
+ ) -> Poll < Self :: Output > {
74
+ if * self . as_mut ( ) . pended ( ) {
75
+ let next = self . as_mut ( ) . inner ( ) . poll ( cx) ;
76
+ if next. is_ready ( ) {
77
+ * self . pended ( ) = false ;
78
+ }
79
+ next
80
+ } else {
81
+ cx. waker ( ) . wake_by_ref ( ) ;
82
+ * self . pended ( ) = true ;
83
+ Poll :: Pending
84
+ }
85
+ }
86
+ }
87
+
66
88
impl < St : Stream > Stream for InterleavePending < St > {
67
89
type Item = St :: Item ;
68
90
You can’t perform that action at this time.
0 commit comments