@@ -4,22 +4,24 @@ use std::pin::Pin;
44use std:: time:: Duration ;
55
66use futures_timer:: Delay ;
7+ use pin_project_lite:: pin_project;
78
89use crate :: future:: Future ;
910use crate :: stream:: Stream ;
1011use crate :: task:: { Context , Poll } ;
1112
12- /// A stream with timeout time set
13- #[ derive( Debug ) ]
14- pub struct Timeout < S : Stream > {
15- stream : S ,
16- delay : Delay ,
13+ pin_project ! {
14+ /// A stream with timeout time set
15+ #[ derive( Debug ) ]
16+ pub struct Timeout <S : Stream > {
17+ #[ pin]
18+ stream: S ,
19+ #[ pin]
20+ delay: Delay ,
21+ }
1722}
1823
1924impl < S : Stream > Timeout < S > {
20- pin_utils:: unsafe_pinned!( stream: S ) ;
21- pin_utils:: unsafe_pinned!( delay: Delay ) ;
22-
2325 pub fn new ( stream : S , dur : Duration ) -> Timeout < S > {
2426 let delay = Delay :: new ( dur) ;
2527
@@ -30,11 +32,13 @@ impl<S: Stream> Timeout<S> {
3032impl < S : Stream > Stream for Timeout < S > {
3133 type Item = Result < S :: Item , TimeoutError > ;
3234
33- fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
34- match self . as_mut ( ) . stream ( ) . poll_next ( cx) {
35+ fn poll_next ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
36+ let this = self . project ( ) ;
37+
38+ match this. stream . poll_next ( cx) {
3539 Poll :: Ready ( Some ( v) ) => Poll :: Ready ( Some ( Ok ( v) ) ) ,
3640 Poll :: Ready ( None ) => Poll :: Ready ( None ) ,
37- Poll :: Pending => match self . delay ( ) . poll ( cx) {
41+ Poll :: Pending => match this . delay . poll ( cx) {
3842 Poll :: Ready ( _) => Poll :: Ready ( Some ( Err ( TimeoutError { _private : ( ) } ) ) ) ,
3943 Poll :: Pending => Poll :: Pending ,
4044 } ,
0 commit comments