Skip to content

Commit

Permalink
Add Sub implementation that takes rhs by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jun 2, 2023
1 parent b271084 commit ec731c2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,15 @@ impl<Tz: TimeZone> Sub<DateTime<Tz>> for DateTime<Tz> {
}
}

impl<Tz: TimeZone> Sub<&DateTime<Tz>> for DateTime<Tz> {
type Output = OldDuration;

#[inline]
fn sub(self, rhs: &DateTime<Tz>) -> OldDuration {
self.signed_duration_since(rhs)
}
}

impl<Tz: TimeZone> Add<Days> for DateTime<Tz> {
type Output = DateTime<Tz>;

Expand Down
4 changes: 4 additions & 0 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ fn signed_duration_since_autoref() {
let diff1 = dt1.signed_duration_since(dt2); // Copy/consume
let diff2 = dt2.signed_duration_since(&dt1); // Take by reference
assert_eq!(diff1, -diff2);

let diff1 = dt1 - &dt2; // We can choose to substract rhs by reference
let diff2 = dt2 - dt1; // Or consume rhs
assert_eq!(diff1, -diff2);
}

#[test]
Expand Down

0 comments on commit ec731c2

Please sign in to comment.