Skip to content

Commit f0cfd1e

Browse files
committed
time: use libc::timeval for TimeVal
1 parent a5936e4 commit f0cfd1e

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

src/sys/time.rs

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
use std::{fmt, ops};
2-
use libc::{time_t, suseconds_t};
3-
4-
#[repr(C)]
5-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
6-
pub struct TimeVal {
7-
pub tv_sec: time_t,
8-
pub tv_usec: suseconds_t,
9-
}
2+
use libc::{time_t, suseconds_t, timeval};
103

114
const MICROS_PER_SEC: i64 = 1_000_000;
125
const SECS_PER_MINUTE: i64 = 60;
136
const SECS_PER_HOUR: i64 = 3600;
147

15-
#[cfg(target_pointer_width = "64")]
16-
const MAX_SECONDS: i64 = (::std::i64::MAX / MICROS_PER_SEC) - 1;
8+
#[derive(Clone, Copy)]
9+
pub struct TimeVal(timeval);
1710

18-
#[cfg(target_pointer_width = "32")]
19-
const MAX_SECONDS: i64 = ::std::isize::MAX as i64;
11+
impl AsRef<timeval> for TimeVal {
12+
fn as_ref(&self) -> &timeval {
13+
&self.0
14+
}
15+
}
2016

21-
const MIN_SECONDS: i64 = -MAX_SECONDS;
17+
impl AsMut<timeval> for TimeVal {
18+
fn as_mut(&mut self) -> &mut timeval {
19+
&mut self.0
20+
}
21+
}
2222

2323
impl TimeVal {
2424
#[inline]
@@ -44,8 +44,8 @@ impl TimeVal {
4444

4545
#[inline]
4646
pub fn seconds(seconds: i64) -> TimeVal {
47-
assert!(seconds >= MIN_SECONDS && seconds <= MAX_SECONDS, "TimeVal out of bounds; seconds={}", seconds);
48-
TimeVal { tv_sec: seconds as time_t, tv_usec: 0 }
47+
assert!(seconds >= time_t::min_value() && seconds <= time_t::max_value(), "TimeVal out of bounds; seconds={}", seconds);
48+
TimeVal(timeval { tv_sec: seconds as time_t, tv_usec: 0 })
4949
}
5050

5151
#[inline]
@@ -60,8 +60,8 @@ impl TimeVal {
6060
#[inline]
6161
pub fn microseconds(microseconds: i64) -> TimeVal {
6262
let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
63-
assert!(secs >= MIN_SECONDS && secs <= MAX_SECONDS, "TimeVal out of bounds");
64-
TimeVal { tv_sec: secs as time_t, tv_usec: micros as suseconds_t }
63+
assert!(secs >= time_t::min_value() && secs <= time_t::max_value(), "TimeVal out of bounds; seconds={}", secs);
64+
TimeVal(timeval { tv_sec: secs as time_t, tv_usec: micros as suseconds_t })
6565
}
6666

6767
pub fn num_hours(&self) -> i64 {
@@ -73,10 +73,10 @@ impl TimeVal {
7373
}
7474

7575
pub fn num_seconds(&self) -> i64 {
76-
if self.tv_sec < 0 && self.tv_usec > 0 {
77-
(self.tv_sec + 1) as i64
76+
if self.0.tv_sec < 0 && self.0.tv_usec > 0 {
77+
(self.0.tv_sec + 1) as i64
7878
} else {
79-
self.tv_sec as i64
79+
self.0.tv_sec as i64
8080
}
8181
}
8282

@@ -91,10 +91,10 @@ impl TimeVal {
9191
}
9292

9393
fn micros_mod_sec(&self) -> suseconds_t {
94-
if self.tv_sec < 0 && self.tv_usec > 0 {
95-
self.tv_usec - MICROS_PER_SEC as suseconds_t
94+
if self.0.tv_sec < 0 && self.0.tv_usec > 0 {
95+
self.0.tv_usec - MICROS_PER_SEC as suseconds_t
9696
} else {
97-
self.tv_usec
97+
self.0.tv_usec
9898
}
9999
}
100100
}
@@ -147,32 +147,40 @@ impl ops::Div<i32> for TimeVal {
147147

148148
impl fmt::Display for TimeVal {
149149
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
150-
let (abs, sign) = if self.tv_sec < 0 {
150+
let (abs, sign) = if self.0.tv_sec < 0 {
151151
(-*self, "-")
152152
} else {
153153
(*self, "")
154154
};
155155

156-
let sec = abs.tv_sec;
156+
let sec = abs.0.tv_sec;
157157

158158
try!(write!(f, "{}", sign));
159159

160-
if abs.tv_usec == 0 {
161-
if abs.tv_sec == 1 {
160+
if abs.0.tv_usec == 0 {
161+
if abs.0.tv_sec == 1 {
162162
try!(write!(f, "{} second", sec));
163163
} else {
164164
try!(write!(f, "{} seconds", sec));
165165
}
166-
} else if abs.tv_usec % 1000 == 0 {
167-
try!(write!(f, "{}.{:03} seconds", sec, abs.tv_usec / 1000));
166+
} else if abs.0.tv_usec % 1_000 == 0 {
167+
try!(write!(f, "{}.{:03} seconds", sec, abs.0.tv_usec / 1_000));
168168
} else {
169-
try!(write!(f, "{}.{:06} seconds", sec, abs.tv_usec));
169+
try!(write!(f, "{}.{:06} seconds", sec, abs.0.tv_usec));
170170
}
171171

172172
Ok(())
173173
}
174174
}
175175

176+
impl PartialEq for TimeVal {
177+
fn eq(&self, rhs: &TimeVal) -> bool {
178+
self.0.tv_sec == rhs.0.tv_sec && self.0.tv_usec == rhs.0.tv_usec
179+
}
180+
}
181+
182+
impl Eq for TimeVal { }
183+
176184
#[inline]
177185
fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
178186
(div_floor_64(this, other), mod_floor_64(this, other))
@@ -208,17 +216,16 @@ mod test {
208216
#[test]
209217
pub fn test_time_val() {
210218
assert!(TimeVal::seconds(1) != TimeVal::zero());
211-
assert_eq!(TimeVal::seconds(1) + TimeVal::seconds(2), TimeVal::seconds(3));
212-
assert_eq!(TimeVal::minutes(3) + TimeVal::seconds(2),
213-
TimeVal::seconds(182));
219+
assert!(TimeVal::seconds(1) + TimeVal::seconds(2) == TimeVal::seconds(3));
220+
assert!(TimeVal::minutes(3) + TimeVal::seconds(2) == TimeVal::seconds(182));
214221
}
215222

216223
#[test]
217224
pub fn test_time_val_neg() {
218225
let a = TimeVal::seconds(1) + TimeVal::microseconds(123);
219226
let b = TimeVal::seconds(-1) + TimeVal::microseconds(-123);
220227

221-
assert_eq!(a, -b);
228+
assert!(a == -b);
222229
}
223230

224231
#[test]

0 commit comments

Comments
 (0)