1
- #[ cfg( all( any( feature = "client" , feature = "server" ) , feature = "http2" ) ) ]
1
+ #[ cfg( any(
2
+ all( any( feature = "client" , feature = "server" ) , feature = "http2" ) ,
3
+ all( feature = "server" , feature = "http1" ) ,
4
+ ) ) ]
2
5
use std:: time:: Duration ;
3
6
use std:: { fmt, sync:: Arc } ;
4
7
use std:: { pin:: Pin , time:: Instant } ;
@@ -13,46 +16,19 @@ pub(crate) enum Time {
13
16
Empty ,
14
17
}
15
18
19
+ #[ cfg( all( feature = "server" , feature = "http1" ) ) ]
20
+ #[ derive( Clone , Copy , Debug ) ]
21
+ pub ( crate ) enum Dur {
22
+ Default ( Option < Duration > ) ,
23
+ Configured ( Option < Duration > ) ,
24
+ }
25
+
16
26
impl fmt:: Debug for Time {
17
27
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
18
28
f. debug_struct ( "Time" ) . finish ( )
19
29
}
20
30
}
21
31
22
- /*
23
- pub(crate) fn timeout<F>(tim: Tim, future: F, duration: Duration) -> HyperTimeout<F> {
24
- HyperTimeout { sleep: tim.sleep(duration), future: future }
25
- }
26
-
27
- pin_project_lite::pin_project! {
28
- pub(crate) struct HyperTimeout<F> {
29
- sleep: Box<dyn Sleep>,
30
- #[pin]
31
- future: F
32
- }
33
- }
34
-
35
- pub(crate) struct Timeout;
36
-
37
- impl<F> Future for HyperTimeout<F> where F: Future {
38
-
39
- type Output = Result<F::Output, Timeout>;
40
-
41
- fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output>{
42
- let mut this = self.project();
43
- if let Poll::Ready(v) = this.future.poll(ctx) {
44
- return Poll::Ready(Ok(v));
45
- }
46
-
47
- if let Poll::Ready(_) = Pin::new(&mut this.sleep).poll(ctx) {
48
- return Poll::Ready(Err(Timeout));
49
- }
50
-
51
- return Poll::Pending;
52
- }
53
- }
54
- */
55
-
56
32
impl Time {
57
33
#[ cfg( all( any( feature = "client" , feature = "server" ) , feature = "http2" ) ) ]
58
34
pub ( crate ) fn sleep ( & self , duration : Duration ) -> Pin < Box < dyn Sleep > > {
@@ -83,3 +59,36 @@ impl Time {
83
59
}
84
60
}
85
61
}
62
+
63
+ #[ cfg( all( feature = "server" , feature = "http1" ) ) ]
64
+ macro_rules! check_timer {
65
+ ( $me: ident. $opt: ident, $timer: ident, $closure: expr) => {
66
+ match $me. $opt {
67
+ Dur :: Default ( Some ( dur) ) => match $me. $timer {
68
+ Time :: Empty => {
69
+ warn!( concat!(
70
+ "timeout `" ,
71
+ stringify!( $opt) ,
72
+ "` has default, but no timer set" ,
73
+ ) ) ;
74
+ }
75
+ Time :: Timer ( ..) => {
76
+ $closure( dur) ;
77
+ }
78
+ } ,
79
+ Dur :: Configured ( Some ( dur) ) => match $me. $timer {
80
+ Time :: Empty => panic!( concat!(
81
+ "timeout `" ,
82
+ stringify!( $opt) ,
83
+ "` set, but no timer set" ,
84
+ ) ) ,
85
+ Time :: Timer ( ..) => {
86
+ $closure( dur) ;
87
+ }
88
+ } ,
89
+ Dur :: Default ( None ) | Dur :: Configured ( None ) => {
90
+ // do nothing
91
+ }
92
+ }
93
+ } ;
94
+ }
0 commit comments