futures_time/time/
duration.rs

1use crate::{
2    future::IntoFuture,
3    stream::{Interval, IntoStream},
4    task::Sleep,
5};
6
7use std::ops::{Add, AddAssign, Sub, SubAssign};
8
9use super::Instant;
10
11/// A Duration type to represent a span of time, typically used for system
12/// timeouts.
13///
14/// This type wraps `std::time::Duration` so we can implement traits on it
15/// without coherence issues, just like if we were implementing this in the
16/// stdlib.
17#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Clone, Copy)]
18pub struct Duration(pub(crate) std::time::Duration);
19impl Duration {
20    /// Creates a new `Duration` from the specified number of whole seconds and
21    /// additional nanoseconds.
22    #[must_use]
23    #[inline]
24    pub fn new(secs: u64, nanos: u32) -> Duration {
25        std::time::Duration::new(secs, nanos).into()
26    }
27
28    /// Creates a new `Duration` from the specified number of whole seconds.
29    #[must_use]
30    #[inline]
31    pub fn from_secs(secs: u64) -> Duration {
32        std::time::Duration::from_secs(secs).into()
33    }
34
35    /// Creates a new `Duration` from the specified number of milliseconds.
36    #[must_use]
37    #[inline]
38    pub fn from_millis(millis: u64) -> Self {
39        std::time::Duration::from_millis(millis).into()
40    }
41
42    /// Creates a new `Duration` from the specified number of microseconds.
43    #[must_use]
44    #[inline]
45    pub fn from_micros(micros: u64) -> Self {
46        std::time::Duration::from_micros(micros).into()
47    }
48
49    /// Creates a new `Duration` from the specified number of seconds represented
50    /// as `f64`.
51    ///
52    /// # Panics
53    /// This constructor will panic if `secs` is not finite, negative or overflows `Duration`.
54    ///
55    /// # Examples
56    /// ```
57    /// use futures_time::time::Duration;
58    ///
59    /// let dur = Duration::from_secs_f64(2.7);
60    /// assert_eq!(dur, Duration::new(2, 700_000_000));
61    /// ```
62    #[must_use]
63    #[inline]
64    pub fn from_secs_f64(secs: f64) -> Duration {
65        std::time::Duration::from_secs_f64(secs).into()
66    }
67
68    /// Creates a new `Duration` from the specified number of seconds represented
69    /// as `f32`.
70    ///
71    /// # Panics
72    /// This constructor will panic if `secs` is not finite, negative or overflows `Duration`.
73    #[must_use]
74    #[inline]
75    pub fn from_secs_f32(secs: f32) -> Duration {
76        std::time::Duration::from_secs_f32(secs).into()
77    }
78}
79
80impl std::ops::Deref for Duration {
81    type Target = std::time::Duration;
82
83    fn deref(&self) -> &Self::Target {
84        &self.0
85    }
86}
87
88impl std::ops::DerefMut for Duration {
89    fn deref_mut(&mut self) -> &mut Self::Target {
90        &mut self.0
91    }
92}
93
94impl From<std::time::Duration> for Duration {
95    fn from(inner: std::time::Duration) -> Self {
96        Self(inner)
97    }
98}
99
100impl Into<std::time::Duration> for Duration {
101    fn into(self) -> std::time::Duration {
102        self.0
103    }
104}
105
106impl Add<Duration> for Duration {
107    type Output = Self;
108
109    fn add(self, rhs: Duration) -> Self::Output {
110        (self.0 + rhs.0).into()
111    }
112}
113
114impl AddAssign<Duration> for Duration {
115    fn add_assign(&mut self, rhs: Duration) {
116        *self = (self.0 + rhs.0).into()
117    }
118}
119
120impl Sub<Duration> for Duration {
121    type Output = Self;
122
123    fn sub(self, rhs: Duration) -> Self::Output {
124        (self.0 - rhs.0).into()
125    }
126}
127
128impl SubAssign<Duration> for Duration {
129    fn sub_assign(&mut self, rhs: Duration) {
130        *self = (self.0 - rhs.0).into()
131    }
132}
133
134impl IntoFuture for Duration {
135    type Output = Instant;
136
137    type IntoFuture = Sleep;
138
139    fn into_future(self) -> Self::IntoFuture {
140        crate::task::sleep(self)
141    }
142}
143
144impl IntoStream for Duration {
145    type Item = Instant;
146
147    type IntoStream = Interval;
148
149    fn into_stream(self) -> Self::IntoStream {
150        crate::stream::interval(self)
151    }
152}