@@ -64,9 +64,9 @@ impl Duration {
64
64
65
65
/// Creates a duration from the specified number of nanoseconds, rounding up.
66
66
/// NOTE: Delays this small may be inaccurate.
67
- pub const fn from_nanos ( micros : u64 ) -> Duration {
67
+ pub const fn from_nanos ( nanoseconds : u64 ) -> Duration {
68
68
Duration {
69
- ticks : div_ceil ( micros * ( TICK_HZ / GCD_1G ) , 1_000_000_000 / GCD_1G ) ,
69
+ ticks : div_ceil ( nanoseconds * ( TICK_HZ / GCD_1G ) , 1_000_000_000 / GCD_1G ) ,
70
70
}
71
71
}
72
72
@@ -90,6 +90,82 @@ impl Duration {
90
90
}
91
91
}
92
92
93
+ /// Try to create a duration from the specified number of seconds, rounding up.
94
+ /// Fails if the number of seconds is too large.
95
+ pub const fn try_from_secs ( secs : u64 ) -> Option < Duration > {
96
+ let Some ( ticks) = secs. checked_mul ( TICK_HZ ) else {
97
+ return None ;
98
+ } ;
99
+ Some ( Duration { ticks } )
100
+ }
101
+
102
+ /// Try to create a duration from the specified number of milliseconds, rounding up.
103
+ /// Fails if the number of milliseconds is too large.
104
+ pub const fn try_from_millis ( millis : u64 ) -> Option < Duration > {
105
+ let Some ( value) = millis. checked_mul ( TICK_HZ / GCD_1K ) else {
106
+ return None ;
107
+ } ;
108
+ Some ( Duration {
109
+ ticks : div_ceil ( value, 1000 / GCD_1K ) ,
110
+ } )
111
+ }
112
+
113
+ /// Try to create a duration from the specified number of microseconds, rounding up.
114
+ /// Fails if the number of microseconds is too large.
115
+ /// NOTE: Delays this small may be inaccurate.
116
+ pub const fn try_from_micros ( micros : u64 ) -> Option < Duration > {
117
+ let Some ( value) = micros. checked_mul ( TICK_HZ / GCD_1M ) else {
118
+ return None ;
119
+ } ;
120
+ Some ( Duration {
121
+ ticks : div_ceil ( value, 1_000_000 / GCD_1M ) ,
122
+ } )
123
+ }
124
+
125
+ /// Try to create a duration from the specified number of nanoseconds, rounding up.
126
+ /// Fails if the number of nanoseconds is too large.
127
+ /// NOTE: Delays this small may be inaccurate.
128
+ pub const fn try_from_nanos ( nanoseconds : u64 ) -> Option < Duration > {
129
+ let Some ( value) = nanoseconds. checked_mul ( TICK_HZ / GCD_1G ) else {
130
+ return None ;
131
+ } ;
132
+ Some ( Duration {
133
+ ticks : div_ceil ( value, 1_000_000_000 / GCD_1G ) ,
134
+ } )
135
+ }
136
+
137
+ /// Try to create a duration from the specified number of seconds, rounding down.
138
+ /// Fails if the number of seconds is too large.
139
+ pub const fn try_from_secs_floor ( secs : u64 ) -> Option < Duration > {
140
+ let Some ( ticks) = secs. checked_mul ( TICK_HZ ) else {
141
+ return None ;
142
+ } ;
143
+ Some ( Duration { ticks } )
144
+ }
145
+
146
+ /// Try to create a duration from the specified number of milliseconds, rounding down.
147
+ /// Fails if the number of milliseconds is too large.
148
+ pub const fn try_from_millis_floor ( millis : u64 ) -> Option < Duration > {
149
+ let Some ( value) = millis. checked_mul ( TICK_HZ / GCD_1K ) else {
150
+ return None ;
151
+ } ;
152
+ Some ( Duration {
153
+ ticks : value / ( 1000 / GCD_1K ) ,
154
+ } )
155
+ }
156
+
157
+ /// Try to create a duration from the specified number of microseconds, rounding down.
158
+ /// Fails if the number of microseconds is too large.
159
+ /// NOTE: Delays this small may be inaccurate.
160
+ pub const fn try_from_micros_floor ( micros : u64 ) -> Option < Duration > {
161
+ let Some ( value) = micros. checked_mul ( TICK_HZ / GCD_1M ) else {
162
+ return None ;
163
+ } ;
164
+ Some ( Duration {
165
+ ticks : value / ( 1_000_000 / GCD_1M ) ,
166
+ } )
167
+ }
168
+
93
169
/// Creates a duration corresponding to the specified Hz.
94
170
/// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1
95
171
/// tick. Doing so will not deadlock, but will certainly not produce the desired output.
0 commit comments