time/ext/numerical_duration.rs
1use crate::convert::*;
2use crate::Duration;
3
4/// Sealed trait to prevent downstream implementations.
5mod sealed {
6 /// A trait that cannot be implemented by downstream users.
7 pub trait Sealed {}
8 impl Sealed for i64 {}
9 impl Sealed for f64 {}
10}
11
12/// Create [`Duration`]s from numeric literals.
13///
14/// # Examples
15///
16/// Basic construction of [`Duration`]s.
17///
18/// ```rust
19/// # use time::{Duration, ext::NumericalDuration};
20/// assert_eq!(5.nanoseconds(), Duration::nanoseconds(5));
21/// assert_eq!(5.microseconds(), Duration::microseconds(5));
22/// assert_eq!(5.milliseconds(), Duration::milliseconds(5));
23/// assert_eq!(5.seconds(), Duration::seconds(5));
24/// assert_eq!(5.minutes(), Duration::minutes(5));
25/// assert_eq!(5.hours(), Duration::hours(5));
26/// assert_eq!(5.days(), Duration::days(5));
27/// assert_eq!(5.weeks(), Duration::weeks(5));
28/// ```
29///
30/// Signed integers work as well!
31///
32/// ```rust
33/// # use time::{Duration, ext::NumericalDuration};
34/// assert_eq!((-5).nanoseconds(), Duration::nanoseconds(-5));
35/// assert_eq!((-5).microseconds(), Duration::microseconds(-5));
36/// assert_eq!((-5).milliseconds(), Duration::milliseconds(-5));
37/// assert_eq!((-5).seconds(), Duration::seconds(-5));
38/// assert_eq!((-5).minutes(), Duration::minutes(-5));
39/// assert_eq!((-5).hours(), Duration::hours(-5));
40/// assert_eq!((-5).days(), Duration::days(-5));
41/// assert_eq!((-5).weeks(), Duration::weeks(-5));
42/// ```
43///
44/// Just like any other [`Duration`], they can be added, subtracted, etc.
45///
46/// ```rust
47/// # use time::ext::NumericalDuration;
48/// assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds());
49/// assert_eq!(2.seconds() - 500.milliseconds(), 1_500.milliseconds());
50/// ```
51///
52/// When called on floating point values, any remainder of the floating point value will be
53/// truncated. Keep in mind that floating point numbers are inherently imprecise and have
54/// limited capacity.
55#[diagnostic::on_unimplemented(note = "this extension trait is intended to be used with numeric \
56 literals, such as `5.seconds()`")]
57pub trait NumericalDuration: sealed::Sealed {
58 /// Create a [`Duration`] from the number of nanoseconds.
59 fn nanoseconds(self) -> Duration;
60 /// Create a [`Duration`] from the number of microseconds.
61 fn microseconds(self) -> Duration;
62 /// Create a [`Duration`] from the number of milliseconds.
63 fn milliseconds(self) -> Duration;
64 /// Create a [`Duration`] from the number of seconds.
65 fn seconds(self) -> Duration;
66 /// Create a [`Duration`] from the number of minutes.
67 fn minutes(self) -> Duration;
68 /// Create a [`Duration`] from the number of hours.
69 fn hours(self) -> Duration;
70 /// Create a [`Duration`] from the number of days.
71 fn days(self) -> Duration;
72 /// Create a [`Duration`] from the number of weeks.
73 fn weeks(self) -> Duration;
74}
75
76impl NumericalDuration for i64 {
77 #[inline]
78 fn nanoseconds(self) -> Duration {
79 Duration::nanoseconds(self)
80 }
81
82 #[inline]
83 fn microseconds(self) -> Duration {
84 Duration::microseconds(self)
85 }
86
87 #[inline]
88 fn milliseconds(self) -> Duration {
89 Duration::milliseconds(self)
90 }
91
92 #[inline]
93 fn seconds(self) -> Duration {
94 Duration::seconds(self)
95 }
96
97 #[inline]
98 #[track_caller]
99 fn minutes(self) -> Duration {
100 Duration::minutes(self)
101 }
102
103 #[inline]
104 #[track_caller]
105 fn hours(self) -> Duration {
106 Duration::hours(self)
107 }
108
109 #[inline]
110 #[track_caller]
111 fn days(self) -> Duration {
112 Duration::days(self)
113 }
114
115 #[inline]
116 #[track_caller]
117 fn weeks(self) -> Duration {
118 Duration::weeks(self)
119 }
120}
121
122impl NumericalDuration for f64 {
123 #[inline]
124 fn nanoseconds(self) -> Duration {
125 Duration::nanoseconds(self as i64)
126 }
127
128 #[inline]
129 fn microseconds(self) -> Duration {
130 Duration::nanoseconds((self * Nanosecond::per_t::<Self>(Microsecond)) as i64)
131 }
132
133 #[inline]
134 fn milliseconds(self) -> Duration {
135 Duration::nanoseconds((self * Nanosecond::per_t::<Self>(Millisecond)) as i64)
136 }
137
138 #[inline]
139 fn seconds(self) -> Duration {
140 Duration::nanoseconds((self * Nanosecond::per_t::<Self>(Second)) as i64)
141 }
142
143 #[inline]
144 #[track_caller]
145 fn minutes(self) -> Duration {
146 Duration::nanoseconds((self * Nanosecond::per_t::<Self>(Minute)) as i64)
147 }
148
149 #[inline]
150 #[track_caller]
151 fn hours(self) -> Duration {
152 Duration::nanoseconds((self * Nanosecond::per_t::<Self>(Hour)) as i64)
153 }
154
155 #[inline]
156 #[track_caller]
157 fn days(self) -> Duration {
158 Duration::nanoseconds((self * Nanosecond::per_t::<Self>(Day)) as i64)
159 }
160
161 #[inline]
162 #[track_caller]
163 fn weeks(self) -> Duration {
164 Duration::nanoseconds((self * Nanosecond::per_t::<Self>(Week)) as i64)
165 }
166}