Skip to content

Commit 9126a24

Browse files
author
Jorge Aparicio
committed
libstd: convert Duration binops to by value
1 parent 32168fa commit 9126a24

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/libstd/time/duration.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ impl Neg<Duration> for Duration {
276276
}
277277
}
278278

279+
// NOTE(stage0): Remove impl after a snapshot
280+
#[cfg(stage0)]
279281
impl Add<Duration,Duration> for Duration {
280282
fn add(&self, rhs: &Duration) -> Duration {
281283
let mut secs = self.secs + rhs.secs;
@@ -288,6 +290,21 @@ impl Add<Duration,Duration> for Duration {
288290
}
289291
}
290292

293+
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
294+
impl Add<Duration, Duration> for Duration {
295+
fn add(self, rhs: Duration) -> Duration {
296+
let mut secs = self.secs + rhs.secs;
297+
let mut nanos = self.nanos + rhs.nanos;
298+
if nanos >= NANOS_PER_SEC {
299+
nanos -= NANOS_PER_SEC;
300+
secs += 1;
301+
}
302+
Duration { secs: secs, nanos: nanos }
303+
}
304+
}
305+
306+
// NOTE(stage0): Remove impl after a snapshot
307+
#[cfg(stage0)]
291308
impl Sub<Duration,Duration> for Duration {
292309
fn sub(&self, rhs: &Duration) -> Duration {
293310
let mut secs = self.secs - rhs.secs;
@@ -300,6 +317,21 @@ impl Sub<Duration,Duration> for Duration {
300317
}
301318
}
302319

320+
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
321+
impl Sub<Duration, Duration> for Duration {
322+
fn sub(self, rhs: Duration) -> Duration {
323+
let mut secs = self.secs - rhs.secs;
324+
let mut nanos = self.nanos - rhs.nanos;
325+
if nanos < 0 {
326+
nanos += NANOS_PER_SEC;
327+
secs -= 1;
328+
}
329+
Duration { secs: secs, nanos: nanos }
330+
}
331+
}
332+
333+
// NOTE(stage0): Remove impl after a snapshot
334+
#[cfg(stage0)]
303335
impl Mul<i32,Duration> for Duration {
304336
fn mul(&self, rhs: &i32) -> Duration {
305337
// Multiply nanoseconds as i64, because it cannot overflow that way.
@@ -310,6 +342,19 @@ impl Mul<i32,Duration> for Duration {
310342
}
311343
}
312344

345+
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
346+
impl Mul<i32, Duration> for Duration {
347+
fn mul(self, rhs: i32) -> Duration {
348+
// Multiply nanoseconds as i64, because it cannot overflow that way.
349+
let total_nanos = self.nanos as i64 * rhs as i64;
350+
let (extra_secs, nanos) = div_mod_floor_64(total_nanos, NANOS_PER_SEC as i64);
351+
let secs = self.secs * rhs as i64 + extra_secs;
352+
Duration { secs: secs, nanos: nanos as i32 }
353+
}
354+
}
355+
356+
// NOTE(stage0): Remove impl after a snapshot
357+
#[cfg(stage0)]
313358
impl Div<i32,Duration> for Duration {
314359
fn div(&self, rhs: &i32) -> Duration {
315360
let mut secs = self.secs / *rhs as i64;
@@ -328,6 +373,25 @@ impl Div<i32,Duration> for Duration {
328373
}
329374
}
330375

376+
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
377+
impl Div<i32, Duration> for Duration {
378+
fn div(self, rhs: i32) -> Duration {
379+
let mut secs = self.secs / rhs as i64;
380+
let carry = self.secs - secs * rhs as i64;
381+
let extra_nanos = carry * NANOS_PER_SEC as i64 / rhs as i64;
382+
let mut nanos = self.nanos / rhs + extra_nanos as i32;
383+
if nanos >= NANOS_PER_SEC {
384+
nanos -= NANOS_PER_SEC;
385+
secs += 1;
386+
}
387+
if nanos < 0 {
388+
nanos += NANOS_PER_SEC;
389+
secs -= 1;
390+
}
391+
Duration { secs: secs, nanos: nanos }
392+
}
393+
}
394+
331395
impl fmt::Show for Duration {
332396
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
333397
// technically speaking, negative duration is not valid ISO 8601,

0 commit comments

Comments
 (0)