/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hyper-1.8.1/src/common/time.rs
Line | Count | Source |
1 | | #[cfg(any( |
2 | | all(any(feature = "client", feature = "server"), feature = "http2"), |
3 | | all(feature = "server", feature = "http1"), |
4 | | ))] |
5 | | use std::time::Duration; |
6 | | use std::{fmt, sync::Arc}; |
7 | | use std::{pin::Pin, time::Instant}; |
8 | | |
9 | | use crate::rt::Sleep; |
10 | | use crate::rt::Timer; |
11 | | |
12 | | /// A user-provided timer to time background tasks. |
13 | | #[derive(Clone)] |
14 | | pub(crate) enum Time { |
15 | | Timer(Arc<dyn Timer + Send + Sync>), |
16 | | Empty, |
17 | | } |
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 | | |
26 | | impl fmt::Debug for Time { |
27 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
28 | 0 | f.debug_struct("Time").finish() |
29 | 0 | } |
30 | | } |
31 | | |
32 | | impl Time { |
33 | | #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] |
34 | 0 | pub(crate) fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> { |
35 | 0 | match *self { |
36 | | Time::Empty => { |
37 | 0 | panic!("You must supply a timer.") |
38 | | } |
39 | 0 | Time::Timer(ref t) => t.sleep(duration), |
40 | | } |
41 | 0 | } |
42 | | |
43 | | #[cfg(all(feature = "server", feature = "http1"))] |
44 | 0 | pub(crate) fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>> { |
45 | 0 | match *self { |
46 | | Time::Empty => { |
47 | 0 | panic!("You must supply a timer.") |
48 | | } |
49 | 0 | Time::Timer(ref t) => t.sleep_until(deadline), |
50 | | } |
51 | 0 | } |
52 | | |
53 | 0 | pub(crate) fn now(&self) -> Instant { |
54 | 0 | match *self { |
55 | 0 | Time::Empty => Instant::now(), |
56 | 0 | Time::Timer(ref t) => t.now(), |
57 | | } |
58 | 0 | } |
59 | | |
60 | 0 | pub(crate) fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) { |
61 | 0 | match *self { |
62 | | Time::Empty => { |
63 | 0 | panic!("You must supply a timer.") |
64 | | } |
65 | 0 | Time::Timer(ref t) => t.reset(sleep, new_deadline), |
66 | | } |
67 | 0 | } |
68 | | |
69 | | #[cfg(all(feature = "server", feature = "http1"))] |
70 | 0 | pub(crate) fn check(&self, dur: Dur, name: &'static str) -> Option<Duration> { |
71 | 0 | match dur { |
72 | 0 | Dur::Default(Some(dur)) => match self { |
73 | | Time::Empty => { |
74 | | warn!("timeout `{}` has default, but no timer set", name,); |
75 | 0 | None |
76 | | } |
77 | 0 | Time::Timer(..) => Some(dur), |
78 | | }, |
79 | 0 | Dur::Configured(Some(dur)) => match self { |
80 | 0 | Time::Empty => panic!("timeout `{}` set, but no timer set", name,), |
81 | 0 | Time::Timer(..) => Some(dur), |
82 | | }, |
83 | 0 | Dur::Default(None) | Dur::Configured(None) => None, |
84 | | } |
85 | 0 | } |
86 | | } |