Skip to content

Commit 8697580

Browse files
committed
Add try_from constructors to Duration
1 parent 2ceb3a7 commit 8697580

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

embassy-time/src/duration.rs

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ impl Duration {
6464

6565
/// Creates a duration from the specified number of nanoseconds, rounding up.
6666
/// 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 {
6868
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),
7070
}
7171
}
7272

@@ -90,6 +90,82 @@ impl Duration {
9090
}
9191
}
9292

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+
93169
/// Creates a duration corresponding to the specified Hz.
94170
/// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1
95171
/// tick. Doing so will not deadlock, but will certainly not produce the desired output.

0 commit comments

Comments
 (0)