Skip to content

Commit cb902c2

Browse files
authored
Merge pull request #178 from flipperzero-rs/fix-duration-max
Fix `flipperzero::furi::time::Duration::MAX`
2 parents eda9b30 + 331e48e commit cb902c2

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1414
### Changed
1515

1616
- `flipperzero::dialogs::DialogFileBrowserOptions` now uses native initialization function.
17+
- `flipperzero::time::Duration::MAX` is now the maximum duration representable.
1718

1819
### Removed
1920

crates/flipperzero/src/furi/time.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
55
use flipperzero_sys as sys;
66
use ufmt::derive::uDebug;
77

8-
/// The maximum number of ticks that a [`Duration`] can contain for it to be usable with
9-
/// [`Instant`].
10-
const MAX_DURATION_TICKS: u32 = u32::MAX / 2;
8+
/// Maximum number of ticks a [`Duration`] can contain to be usable with [`Instant`].
9+
const MAX_INTERVAL_DURATION_TICKS: u32 = u32::MAX / 2;
1110

1211
const NANOS_PER_SEC_F: f64 = 1_000_000_000_f64;
1312
const NANOS_PER_SEC: u64 = 1_000_000_000;
@@ -102,7 +101,7 @@ impl Instant {
102101
/// represented as `Instant` (which means it's inside the bounds of the underlying
103102
/// data structure), `None` otherwise.
104103
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
105-
if duration.0 <= MAX_DURATION_TICKS {
104+
if duration.0 <= MAX_INTERVAL_DURATION_TICKS {
106105
Some(Instant(self.0.wrapping_add(duration.0)))
107106
} else {
108107
None
@@ -113,7 +112,7 @@ impl Instant {
113112
/// represented as `Instant` (which means it's inside the bounds of the underlying
114113
/// data structure), `None` otherwise.
115114
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
116-
if duration.0 <= MAX_DURATION_TICKS {
115+
if duration.0 <= MAX_INTERVAL_DURATION_TICKS {
117116
Some(Instant(self.0.wrapping_sub(duration.0)))
118117
} else {
119118
None
@@ -134,10 +133,10 @@ impl Ord for Instant {
134133
Ordering::Equal
135134
} else {
136135
// We use modular arithmetic to define ordering.
137-
// This requires a maximum `Duration` value of `MAX_DURATION_TICKS`.
136+
// This requires a maximum `Duration` value of `MAX_INTERVAL_DURATION_TICKS`.
138137
self.0
139138
.wrapping_sub(other.0)
140-
.cmp(&MAX_DURATION_TICKS)
139+
.cmp(&MAX_INTERVAL_DURATION_TICKS)
141140
.reverse()
142141
}
143142
}
@@ -195,7 +194,7 @@ impl Sub<Instant> for Instant {
195194
///
196195
/// Each `Duration` is composed of a whole number of "ticks", the length of which depends
197196
/// on the firmware's tick frequency. While a `Duration` can contain any value that
198-
/// is at most [`u32::MAX`] ticks, only the range `[Duration::ZERO..=DURATION::MAX]` can
197+
/// is at most [`u32::MAX`] ticks, only the range `[Duration::ZERO..=DURATION::MAX/2]` can
199198
/// be used with [`Instant`].
200199
#[derive(Clone, Copy, Debug, uDebug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
201200
pub struct Duration(pub(super) u32);
@@ -209,7 +208,7 @@ impl Duration {
209208
/// May vary by platform as necessary. Must be able to contain the difference between
210209
/// two instances of [`Instant`]. This constraint gives it a value of about 24 days in
211210
/// practice on stock firmware.
212-
pub const MAX: Duration = Duration(MAX_DURATION_TICKS);
211+
pub const MAX: Duration = Duration(u32::MAX);
213212

214213
/// Creates a new `Duration` from the specified number of whole seconds.
215214
///
@@ -467,7 +466,7 @@ impl<'a> Sum<&'a Duration> for Duration {
467466

468467
#[flipperzero_test::tests]
469468
mod tests {
470-
use super::{ticks_to_ns, Duration, Instant, MAX_DURATION_TICKS};
469+
use super::{ticks_to_ns, Duration, Instant, MAX_INTERVAL_DURATION_TICKS};
471470
use crate::println;
472471

473472
#[cfg(feature = "alloc")]
@@ -607,7 +606,8 @@ mod tests {
607606
start: Option<T>,
608607
op: impl Fn(&T, Duration) -> Option<T>,
609608
) {
610-
const DURATIONS: [Duration; 2] = [Duration(MAX_DURATION_TICKS >> 1), Duration(50)];
609+
const DURATIONS: [Duration; 2] =
610+
[Duration(MAX_INTERVAL_DURATION_TICKS >> 1), Duration(50)];
611611
if let Some(start) = start {
612612
assert_eq!(
613613
op(&start, DURATIONS.into_iter().sum()),

0 commit comments

Comments
 (0)