1- use std:: pin:: Pin ;
21use std:: time:: Duration ;
32
4- use futures_timer:: Delay ;
5- use pin_utils:: unsafe_pinned;
3+ use futures_timer:: TryFutureExt ;
64
75use crate :: future:: Future ;
86use crate :: io;
9- use crate :: task:: { Context , Poll } ;
107
118/// Awaits an I/O future or times out after a duration of time.
129///
@@ -33,39 +30,5 @@ pub async fn timeout<F, T>(dur: Duration, f: F) -> io::Result<T>
3330where
3431 F : Future < Output = io:: Result < T > > ,
3532{
36- let f = TimeoutFuture {
37- future : f,
38- delay : Delay :: new ( dur) ,
39- } ;
40- f. await
41- }
42-
43- struct TimeoutFuture < F > {
44- future : F ,
45- delay : Delay ,
46- }
47-
48- impl < F > TimeoutFuture < F > {
49- unsafe_pinned ! ( future: F ) ;
50- unsafe_pinned ! ( delay: Delay ) ;
51- }
52-
53- impl < F , T > Future for TimeoutFuture < F >
54- where
55- F : Future < Output = io:: Result < T > > ,
56- {
57- type Output = F :: Output ;
58-
59- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
60- match self . as_mut ( ) . future ( ) . poll ( cx) {
61- Poll :: Ready ( v) => Poll :: Ready ( v) ,
62- Poll :: Pending => match self . delay ( ) . poll ( cx) {
63- Poll :: Ready ( _) => Poll :: Ready ( Err ( io:: Error :: new (
64- io:: ErrorKind :: TimedOut ,
65- "I/O operation has timed out" ,
66- ) ) ) ,
67- Poll :: Pending => Poll :: Pending ,
68- } ,
69- }
70- }
33+ f. timeout ( dur) . await
7134}
0 commit comments