From 0754e27e8a2f292facf594bbd377fba4993c2555 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 24 Jan 2024 14:06:16 +0000 Subject: [PATCH] Adjusted MIN_MILLISECONDS and MIN_*_FULL - 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 https://github.com/chronotope/chrono/pull/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 https://github.com/chronotope/chrono/pull/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. --- crates/rubedo/src/chrono.rs | 18 ++++++++++++------ crates/rubedo/src/tests/chrono.rs | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/rubedo/src/chrono.rs b/crates/rubedo/src/chrono.rs index e0dbcfd..ac01017 100644 --- a/crates/rubedo/src/chrono.rs +++ b/crates/rubedo/src/chrono.rs @@ -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 + /// . + 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 @@ -137,8 +139,10 @@ 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 + /// . + 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 @@ -146,8 +150,10 @@ pub trait DurationExt { /// 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 + /// . + 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 diff --git a/crates/rubedo/src/tests/chrono.rs b/crates/rubedo/src/tests/chrono.rs index b0a67d5..12d5414 100644 --- a/crates/rubedo/src/tests/chrono.rs +++ b/crates/rubedo/src/tests/chrono.rs @@ -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 @@ -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