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#[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash, Clone, Copy)]
18pub struct Duration(pub(crate) std::time::Duration);
19impl Duration {
20 #[must_use]
23 #[inline]
24 pub fn new(secs: u64, nanos: u32) -> Duration {
25 std::time::Duration::new(secs, nanos).into()
26 }
27
28 #[must_use]
30 #[inline]
31 pub fn from_secs(secs: u64) -> Duration {
32 std::time::Duration::from_secs(secs).into()
33 }
34
35 #[must_use]
37 #[inline]
38 pub fn from_millis(millis: u64) -> Self {
39 std::time::Duration::from_millis(millis).into()
40 }
41
42 #[must_use]
44 #[inline]
45 pub fn from_micros(micros: u64) -> Self {
46 std::time::Duration::from_micros(micros).into()
47 }
48
49 #[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 #[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}