Skip to content

Commit

Permalink
Auto merge of #116386 - elichai:patch-2, r=thomcc
Browse files Browse the repository at this point in the history
Add missing inline attributes to Duration trait impls

Currently `Duration::checked_add` is marked `#[inline]` but it's trait relative `Add::add` is not.
Leading to a case where:
```rust
pub fn foo() -> Duration {
    Duration::from_secs(10) + Duration::from_millis(6)
}

pub fn bar() -> Duration {
    Duration::from_secs(10).checked_add(Duration::from_millis(6)).expect("overflow when adding durations")
}
```
compiles to:
```asm

playground::foo:
	movl	$10, %edi
	xorl	%esi, %esi
	xorl	%edx, %edx
	movl	$6000000, %ecx
	jmpq	*<core::time::Duration as core::ops::arith::Add>::add@GOTPCREL(%rip)

playground::bar:
	movl	$10, %eax
	movl	$6000000, %edx
	retq
```
(The same happens for all arithmetic operation)
  • Loading branch information
bors committed Oct 4, 2023
2 parents 79f38b7 + 92c9bcd commit 4910642
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions library/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,13 +910,15 @@ impl Duration {
impl Add for Duration {
type Output = Duration;

#[inline]
fn add(self, rhs: Duration) -> Duration {
self.checked_add(rhs).expect("overflow when adding durations")
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl AddAssign for Duration {
#[inline]
fn add_assign(&mut self, rhs: Duration) {
*self = *self + rhs;
}
Expand All @@ -926,13 +928,15 @@ impl AddAssign for Duration {
impl Sub for Duration {
type Output = Duration;

#[inline]
fn sub(self, rhs: Duration) -> Duration {
self.checked_sub(rhs).expect("overflow when subtracting durations")
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl SubAssign for Duration {
#[inline]
fn sub_assign(&mut self, rhs: Duration) {
*self = *self - rhs;
}
Expand All @@ -942,6 +946,7 @@ impl SubAssign for Duration {
impl Mul<u32> for Duration {
type Output = Duration;

#[inline]
fn mul(self, rhs: u32) -> Duration {
self.checked_mul(rhs).expect("overflow when multiplying duration by scalar")
}
Expand All @@ -951,13 +956,15 @@ impl Mul<u32> for Duration {
impl Mul<Duration> for u32 {
type Output = Duration;

#[inline]
fn mul(self, rhs: Duration) -> Duration {
rhs * self
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl MulAssign<u32> for Duration {
#[inline]
fn mul_assign(&mut self, rhs: u32) {
*self = *self * rhs;
}
Expand All @@ -967,13 +974,15 @@ impl MulAssign<u32> for Duration {
impl Div<u32> for Duration {
type Output = Duration;

#[inline]
fn div(self, rhs: u32) -> Duration {
self.checked_div(rhs).expect("divide by zero error when dividing duration by scalar")
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl DivAssign<u32> for Duration {
#[inline]
fn div_assign(&mut self, rhs: u32) {
*self = *self / rhs;
}
Expand Down

0 comments on commit 4910642

Please sign in to comment.