Skip to content

Commit

Permalink
Adjusted MIN_MILLISECONDS and MIN_*_FULL
Browse files Browse the repository at this point in the history
  - Adjusted MIN_MILLISECONDS, MIN_NANOSECONDS_FULL, and
    MIN_MICROSECONDS_FULL due to changes in Chrono 0.4.32 that were not
    previously noticed, which restrict the minimum value of milliseconds
    by 1, by changing the limit from i64::MIN to -i64::MAX. This was
    carried out under chronotope/chrono#1334.
    Notably, Chrono 0.4.32 does still allow creation of a Duration with
    a milliseconds value of i64::MIN, which is why this change was not
    noticed sooner. This will be corrected when Chrono 0.4.34 is
    released, with chronotope/chrono#1385. In
    the meantime, the correction of constant values here means that we
    are now compatible with both versions and also do not suffer from
    the current Chrono bug.
  • Loading branch information
danwilliams committed Jan 24, 2024
1 parent 985aeed commit 0754e27
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 12 additions & 6 deletions crates/rubedo/src/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ pub trait DurationExt {
/// The minimum number of nanoseconds that can be represented by a
/// [`Duration`]. Note that this is the minimum number of nanoseconds that
/// can actually be stored by a [`Duration`], which is more than can be
/// expressed alone using standard Chrono functionality.
const MIN_NANOSECONDS_FULL: i128 = i64::MIN as i128 * 1_000 * 1_000;
/// expressed alone using standard Chrono functionality. The range here
/// notably is from `-i64::MAX` and not `i64::MIN`, due to changes in
/// <https://github.com/chronotope/chrono/pull/1334>.
const MIN_NANOSECONDS_FULL: i128 = -i64::MAX as i128 * 1_000 * 1_000;

/// The minimum number of microseconds that can be represented by a
/// [`Duration`] when expressed alone. Note that this is not the minimum
Expand All @@ -137,17 +139,21 @@ pub trait DurationExt {
/// The minimum number of microseconds that can be represented by a
/// [`Duration`]. Note that this is the minimum number of microseconds that
/// can actually be stored by a [`Duration`], which is more than can be
/// expressed alone using standard Chrono functionality.
const MIN_MICROSECONDS_FULL: i128 = i64::MIN as i128 * 1_000;
/// expressed alone using standard Chrono functionality. The range here
/// notably is from `-i64::MAX` and not `i64::MIN`, due to changes in
/// <https://github.com/chronotope/chrono/pull/1334>.
const MIN_MICROSECONDS_FULL: i128 = -i64::MAX as i128 * 1_000;

/// The minimum number of milliseconds that can be represented by a
/// [`Duration`]. The [`Duration`] struct stores its value as a number of
/// seconds and nanoseconds, but artificially limits the number of seconds
/// so that the milliseconds will never overflow. The minimum number of
/// milliseconds that can be stored is therefore the minimum number of
/// seconds multiplied by one thousand, and the expression of this full
/// value as milliseconds is possible.
const MIN_MILLISECONDS: i64 = i64::MIN;
/// value as milliseconds is possible. The range here notably is from
/// `-i64::MAX` and not `i64::MIN`, due to changes in
/// <https://github.com/chronotope/chrono/pull/1334>.
const MIN_MILLISECONDS: i64 = -i64::MAX;

/// The minimum number of seconds that can be represented by a [`Duration`].
/// The [`Duration`] struct stores its value as a number of seconds and
Expand Down
4 changes: 2 additions & 2 deletions crates/rubedo/src/tests/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ mod duration_ext {
}
#[test]
fn min_nanoseconds_full__overflow() {
assert!(Duration::milliseconds(Duration::MIN_MILLISECONDS).checked_add(&Duration::nanoseconds(1)).is_none());
assert!(Duration::milliseconds(Duration::MIN_MILLISECONDS).checked_sub(&Duration::nanoseconds(1)).is_none());
}

// MIN_MICROSECONDS
Expand All @@ -173,7 +173,7 @@ mod duration_ext {
}
#[test]
fn min_microseconds_full__overflow() {
assert!(Duration::milliseconds(Duration::MIN_MILLISECONDS).checked_add(&Duration::microseconds(1)).is_none());
assert!(Duration::milliseconds(Duration::MIN_MILLISECONDS).checked_sub(&Duration::microseconds(1)).is_none());
}

// MIN_MILLISECONDS
Expand Down

0 comments on commit 0754e27

Please sign in to comment.