Skip to content

Commit

Permalink
use ? operator instead of match
Browse files Browse the repository at this point in the history
  • Loading branch information
sgeisler committed Nov 16, 2018
1 parent 86ef38b commit f2106d0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
13 changes: 3 additions & 10 deletions src/libstd/sys/redox/time.rs
Expand Up @@ -46,25 +46,18 @@ impl Timespec {
}

fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
let mut secs = match other
let mut secs = other
.as_secs()
.try_into() // <- target type would be `i64`
.ok()
.and_then(|secs| self.t.tv_sec.checked_add(secs))
{
Some(ts) => ts,
None => return None,
};
.and_then(|secs| self.t.tv_sec.checked_add(secs))?;

// Nano calculations can't overflow because nanos are <1B which fit
// in a u32.
let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
if nsec >= NSEC_PER_SEC as u32 {
nsec -= NSEC_PER_SEC as u32;
secs = match secs.checked_add(1) {
Some(ts) => ts,
None => return None,
}
secs = secs.checked_add(1)?;
}
Some(Timespec {
t: syscall::TimeSpec {
Expand Down
13 changes: 3 additions & 10 deletions src/libstd/sys/unix/time.rs
Expand Up @@ -47,25 +47,18 @@ impl Timespec {
}

fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
let mut secs = match other
let mut secs = other
.as_secs()
.try_into() // <- target type would be `libc::time_t`
.ok()
.and_then(|secs| self.t.tv_sec.checked_add(secs))
{
Some(ts) => ts,
None => return None,
};
.and_then(|secs| self.t.tv_sec.checked_add(secs))?;

// Nano calculations can't overflow because nanos are <1B which fit
// in a u32.
let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
if nsec >= NSEC_PER_SEC as u32 {
nsec -= NSEC_PER_SEC as u32;
secs = match secs.checked_add(1) {
Some(ts) => ts,
None => return None,
}
secs = secs.checked_add(1)?;
}
Some(Timespec {
t: libc::timespec {
Expand Down

0 comments on commit f2106d0

Please sign in to comment.