futures_time/time/
duration.rs1use 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#[cfg(not(feature = "web"))]
12use std::time::Duration as HostDuration;
13
14#[cfg(feature = "web")]
15use web_time::Duration as HostDuration;
16
17#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Clone, Copy)]
24pub struct Duration(pub(crate) HostDuration);
25impl Duration {
26 #[must_use]
29 #[inline]
30 pub fn new(secs: u64, nanos: u32) -> Duration {
31 HostDuration::new(secs, nanos).into()
32 }
33
34 #[must_use]
36 #[inline]
37 pub fn from_secs(secs: u64) -> Duration {
38 HostDuration::from_secs(secs).into()
39 }
40
41 #[must_use]
43 #[inline]
44 pub fn from_millis(millis: u64) -> Self {
45 HostDuration::from_millis(millis).into()
46 }
47
48 #[must_use]
50 #[inline]
51 pub fn from_micros(micros: u64) -> Self {
52 HostDuration::from_micros(micros).into()
53 }
54
55 #[must_use]
69 #[inline]
70 pub fn from_secs_f64(secs: f64) -> Duration {
71 HostDuration::from_secs_f64(secs).into()
72 }
73
74 #[must_use]
80 #[inline]
81 pub fn from_secs_f32(secs: f32) -> Duration {
82 HostDuration::from_secs_f32(secs).into()
83 }
84}
85
86impl std::ops::Deref for Duration {
87 type Target = HostDuration;
88
89 fn deref(&self) -> &Self::Target {
90 &self.0
91 }
92}
93
94impl std::ops::DerefMut for Duration {
95 fn deref_mut(&mut self) -> &mut Self::Target {
96 &mut self.0
97 }
98}
99
100impl From<HostDuration> for Duration {
101 fn from(inner: HostDuration) -> Self {
102 Self(inner)
103 }
104}
105
106impl Into<HostDuration> for Duration {
107 fn into(self) -> HostDuration {
108 self.0
109 }
110}
111
112impl Add<Duration> for Duration {
113 type Output = Self;
114
115 fn add(self, rhs: Duration) -> Self::Output {
116 (self.0 + rhs.0).into()
117 }
118}
119
120impl AddAssign<Duration> for Duration {
121 fn add_assign(&mut self, rhs: Duration) {
122 *self = (self.0 + rhs.0).into()
123 }
124}
125
126impl Sub<Duration> for Duration {
127 type Output = Self;
128
129 fn sub(self, rhs: Duration) -> Self::Output {
130 (self.0 - rhs.0).into()
131 }
132}
133
134impl SubAssign<Duration> for Duration {
135 fn sub_assign(&mut self, rhs: Duration) {
136 *self = (self.0 - rhs.0).into()
137 }
138}
139
140impl IntoFuture for Duration {
141 type Output = Instant;
142
143 type IntoFuture = Sleep;
144
145 fn into_future(self) -> Self::IntoFuture {
146 crate::task::sleep(self)
147 }
148}
149
150impl IntoStream for Duration {
151 type Item = Instant;
152
153 type IntoStream = Interval;
154
155 fn into_stream(self) -> Self::IntoStream {
156 crate::stream::interval(self)
157 }
158}