Skip to content

Commit

Permalink
Remove some debug assertions, use Duration::new
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Jun 22, 2022
1 parent 1e36fe2 commit 80456cd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ impl Duration {
// region: constructors
/// Create a new `Duration` without checking the validity of the components.
pub(crate) const fn new_unchecked(seconds: i64, nanoseconds: i32) -> Self {
if seconds < 0 {
debug_assert!(nanoseconds <= 0);
debug_assert!(nanoseconds > -1_000_000_000);
} else if seconds > 0 {
debug_assert!(nanoseconds >= 0);
debug_assert!(nanoseconds < 1_000_000_000);
} else {
debug_assert!(nanoseconds.unsigned_abs() < 1_000_000_000);
}
// if seconds < 0 {
// debug_assert!(nanoseconds <= 0);
// debug_assert!(nanoseconds > -1_000_000_000);
// } else if seconds > 0 {
// debug_assert!(nanoseconds >= 0);
// debug_assert!(nanoseconds < 1_000_000_000);
// } else {
// debug_assert!(nanoseconds.unsigned_abs() < 1_000_000_000);
// }

Self {
seconds,
Expand Down
3 changes: 2 additions & 1 deletion src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,8 @@ impl Sub for Time {

cascade!(nanosecond_diff in 0..1_000_000_000 => second_diff);

Duration::new_unchecked(
// TODO(jhpratt) use `new_unchecked` and ensure validity
Duration::new(
hour_diff as i64 * 3_600 + minute_diff as i64 * 60 + second_diff as i64,
nanosecond_diff,
)
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,12 @@ fn ordering() {
assert!(time!(12:00) > time!(11:00));
assert_eq!(time!(0:00), time!(0:00));
}

#[test]
fn issue_481() {
assert_eq!(time!(0:00) - time!(01:00:00.1), (-3600.1).seconds());
assert_eq!(
time!(0:00) - time!(23:59:59.999_999_999),
(-86_399.999_999_999).seconds()
);
}

0 comments on commit 80456cd

Please sign in to comment.