Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flipperzero::furi::time::Duration::MAX #178

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Changed

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

### Removed

Expand Down
22 changes: 11 additions & 11 deletions crates/flipperzero/src/furi/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use flipperzero_sys as sys;
use ufmt::derive::uDebug;

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

const NANOS_PER_SEC_F: f64 = 1_000_000_000_f64;
const NANOS_PER_SEC: u64 = 1_000_000_000;
Expand Down Expand Up @@ -102,7 +101,7 @@ impl Instant {
/// represented as `Instant` (which means it's inside the bounds of the underlying
/// data structure), `None` otherwise.
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
if duration.0 <= MAX_DURATION_TICKS {
if duration.0 <= MAX_INTERVAL_DURATION_TICKS {
Some(Instant(self.0.wrapping_add(duration.0)))
} else {
None
Expand All @@ -113,7 +112,7 @@ impl Instant {
/// represented as `Instant` (which means it's inside the bounds of the underlying
/// data structure), `None` otherwise.
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
if duration.0 <= MAX_DURATION_TICKS {
if duration.0 <= MAX_INTERVAL_DURATION_TICKS {
Some(Instant(self.0.wrapping_sub(duration.0)))
} else {
None
Expand All @@ -134,10 +133,10 @@ impl Ord for Instant {
Ordering::Equal
} else {
// We use modular arithmetic to define ordering.
// This requires a maximum `Duration` value of `MAX_DURATION_TICKS`.
// This requires a maximum `Duration` value of `MAX_INTERVAL_DURATION_TICKS`.
self.0
.wrapping_sub(other.0)
.cmp(&MAX_DURATION_TICKS)
.cmp(&MAX_INTERVAL_DURATION_TICKS)
.reverse()
}
}
Expand Down Expand Up @@ -195,7 +194,7 @@ impl Sub<Instant> for Instant {
///
/// Each `Duration` is composed of a whole number of "ticks", the length of which depends
/// on the firmware's tick frequency. While a `Duration` can contain any value that
/// is at most [`u32::MAX`] ticks, only the range `[Duration::ZERO..=DURATION::MAX]` can
/// is at most [`u32::MAX`] ticks, only the range `[Duration::ZERO..=DURATION::MAX/2]` can
/// be used with [`Instant`].
#[derive(Clone, Copy, Debug, uDebug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Duration(pub(super) u32);
Expand All @@ -209,7 +208,7 @@ impl Duration {
/// May vary by platform as necessary. Must be able to contain the difference between
/// two instances of [`Instant`]. This constraint gives it a value of about 24 days in
/// practice on stock firmware.
pub const MAX: Duration = Duration(MAX_DURATION_TICKS);
pub const MAX: Duration = Duration(u32::MAX);

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

#[flipperzero_test::tests]
mod tests {
use super::{ticks_to_ns, Duration, Instant, MAX_DURATION_TICKS};
use super::{ticks_to_ns, Duration, Instant, MAX_INTERVAL_DURATION_TICKS};
use crate::println;

#[cfg(feature = "alloc")]
Expand Down Expand Up @@ -607,7 +606,8 @@ mod tests {
start: Option<T>,
op: impl Fn(&T, Duration) -> Option<T>,
) {
const DURATIONS: [Duration; 2] = [Duration(MAX_DURATION_TICKS >> 1), Duration(50)];
const DURATIONS: [Duration; 2] =
[Duration(MAX_INTERVAL_DURATION_TICKS >> 1), Duration(50)];
if let Some(start) = start {
assert_eq!(
op(&start, DURATIONS.into_iter().sum()),
Expand Down
Loading