Skip to content

Commit

Permalink
Merge branch 'main' into add-caching-to-semver-check
Browse files Browse the repository at this point in the history
  • Loading branch information
rinde authored Mar 21, 2023
2 parents cc58e89 + 5f52e08 commit cfcbe04
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage = "https://github.com/moia-oss/tinytime.rs"
repository = "https://github.com/moia-oss/tinytime.rs"
documentation = "https://docs.rs/tinytime"
edition = "2021"
version = "0.2.1"
version = "0.3.0"
license = "MIT OR Apache-2.0"

[dependencies]
Expand Down
150 changes: 126 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,47 +700,97 @@ impl AddAssign<Time> for Time {

impl AddAssign<Duration> for Time {
fn add_assign(&mut self, rhs: Duration) {
debug_assert!(
self.0.checked_add(rhs.0 as usize).is_some(),
"overflow detected: {self:?} += {rhs:?}"
);
self.0 += rhs.0 as usize;
if rhs.is_negative() {
debug_assert!(
self.0.checked_sub(rhs.0.unsigned_abs()).is_some(),
"overflow detected: {self:?} + {rhs:?}"
);
self.0 -= rhs.0.unsigned_abs();
} else {
debug_assert!(
self.0.checked_add(rhs.0.unsigned_abs()).is_some(),
"overflow detected: {self:?} + {rhs:?}"
);
self.0 += rhs.0.unsigned_abs();
}
}
}

impl Add<Duration> for Time {
type Output = Time;

fn add(self, rhs: Duration) -> Self::Output {
debug_assert!(
isize::try_from(self.0).is_ok() && self.0.checked_add(rhs.0 as usize).is_some(),
"overflow detected: {self:?} + {rhs:?}"
);
Time(((self.0 as isize) + rhs.0) as usize)
if rhs.is_negative() {
debug_assert!(
self.0.checked_sub(rhs.0.unsigned_abs()).is_some(),
"overflow detected: {self:?} + {rhs:?}"
);
Time(self.0 - rhs.0.unsigned_abs())
} else {
debug_assert!(
self.0.checked_add(rhs.0.unsigned_abs()).is_some(),
"overflow detected: {self:?} + {rhs:?}"
);
Time(self.0 + rhs.0.unsigned_abs())
}
}
}

impl Sub<Duration> for Time {
type Output = Time;

fn sub(self, rhs: Duration) -> Self::Output {
debug_assert!(
isize::try_from(self.0).is_ok() && (self.0 as isize).checked_sub(rhs.0).is_some(),
"overflow detected: {self:?} - {rhs:?}"
);
Time(((self.0 as isize) - rhs.0) as usize)
if rhs.is_negative() {
debug_assert!(
self.0.checked_add(rhs.0.unsigned_abs()).is_some(),
"overflow detected: {self:?} + {rhs:?}"
);
Time(self.0 + rhs.0.unsigned_abs())
} else {
debug_assert!(
self.0.checked_sub(rhs.0.unsigned_abs()).is_some(),
"overflow detected: {self:?} + {rhs:?}"
);
Time(self.0 - rhs.0.unsigned_abs())
}
}
}

impl SubAssign<Duration> for Time {
fn sub_assign(&mut self, rhs: Duration) {
if rhs.is_negative() {
debug_assert!(
self.0.checked_add(rhs.0.unsigned_abs()).is_some(),
"overflow detected: {self:?} + {rhs:?}"
);
self.0 += rhs.0.unsigned_abs();
} else {
debug_assert!(
self.0.checked_sub(rhs.0.unsigned_abs()).is_some(),
"overflow detected: {self:?} + {rhs:?}"
);
self.0 -= rhs.0.unsigned_abs()
}
}
}

impl Sub<Time> for Time {
type Output = Duration;

fn sub(self, rhs: Time) -> Self::Output {
debug_assert!(
isize::try_from(self.0).is_ok() && isize::try_from(rhs.0).is_ok(),
"overflow detected: {self:?} - {rhs:?}"
);
Duration(self.0 as isize - rhs.0 as isize)
if self > rhs {
debug_assert!(
isize::try_from(self.0 - rhs.0).is_ok(),
"Overflow detected, Time instances are too far apart: {self:?} - {rhs:?}"
);
Duration((self.0 - rhs.0) as isize)
} else {
debug_assert!(
isize::try_from(rhs.0 - self.0).is_ok(),
"Overflow detected, Time instances are too far apart: {self:?} - {rhs:?}"
);
Duration(-((rhs.0 - self.0) as isize))
}
}
}

Expand Down Expand Up @@ -1190,12 +1240,64 @@ mod duration_test {
}

#[test]
fn time_add_assign_duration() {
let mut time = Time::millis(1);
fn time_add_duration() {
let mut small_time = Time::millis(1);
let expected_time = Time::millis(3);
let duration = Duration::millis(2);
time += duration;
// small time: add
assert_eq!(expected_time, small_time + duration);
// small time: add assign
small_time += duration;
assert_eq!(expected_time, small_time);

let mut big_time = Time::hours(2_600_000_000_000);
assert!(big_time > Time::EPOCH + Duration::MAX);
// big time: add
assert_eq!(
Time::hours(2_600_000_000_010),
big_time + Duration::hours(10)
);
// big time: add assign
big_time += Duration::hours(10);
assert_eq!(Time::hours(2_600_000_000_010), big_time);
}

#[test]
fn time_sub_duration() {
let mut small_time = Time::millis(10);
let expected_time = Time::millis(3);
assert_eq!(expected_time, time);
let duration = Duration::millis(7);
// small time: sub
assert_eq!(expected_time, small_time - duration);
// small time: sub assign
small_time -= duration;
assert_eq!(expected_time, small_time);

let mut big_time = Time::hours(2_600_000_000_010);
assert!(big_time > Time::EPOCH + Duration::MAX);
// big time: sub
assert_eq!(
Time::hours(2_600_000_000_002),
big_time - Duration::hours(8)
);
// big time: sub assign
big_time -= Duration::hours(8);
assert_eq!(Time::hours(2_600_000_000_002), big_time);
}

#[test]
fn time_sub_time() {
// small numbers
let small_time = Time::minutes(7);
let small_time2 = Time::minutes(3);
assert_eq!(Duration::minutes(4), small_time - small_time2);
assert_eq!(Duration::minutes(-4), small_time2 - small_time);

// big numbers
let big_time = Time::hours(2_600_000_000_000);
let big_time2 = Time::hours(2_600_000_000_012);
assert_eq!(Duration::hours(-12), big_time - big_time2);
assert_eq!(Duration::hours(12), big_time2 - big_time);
}

#[test]
Expand Down

0 comments on commit cfcbe04

Please sign in to comment.