Skip to content

Commit 90b2734

Browse files
Port timespec to x32
1 parent 1f69c1d commit 90b2734

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/sys/time.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{cmp, fmt, ops};
22
use std::time::Duration;
33
use std::convert::From;
4-
use libc::{c_long, timespec, timeval};
4+
use libc::{timespec, timeval};
55
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
66
pub use libc::{time_t, suseconds_t};
77

@@ -62,6 +62,13 @@ const TS_MAX_SECONDS: i64 = ::std::isize::MAX as i64;
6262

6363
const TS_MIN_SECONDS: i64 = -TS_MAX_SECONDS;
6464

65+
// x32 compatibility
66+
// See https://sourceware.org/bugzilla/show_bug.cgi?id=16437
67+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
68+
type timespec_tv_nsec_t = i64;
69+
#[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))]
70+
type timespec_tv_nsec_t = libc::c_long;
71+
6572
impl From<timespec> for TimeSpec {
6673
fn from(ts: timespec) -> Self {
6774
Self(ts)
@@ -73,7 +80,7 @@ impl From<Duration> for TimeSpec {
7380
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
7481
TimeSpec(timespec {
7582
tv_sec: duration.as_secs() as time_t,
76-
tv_nsec: duration.subsec_nanos() as c_long
83+
tv_nsec: duration.subsec_nanos() as timespec_tv_nsec_t
7784
})
7885
}
7986
}
@@ -148,7 +155,7 @@ impl TimeValLike for TimeSpec {
148155
"TimeSpec out of bounds");
149156
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
150157
TimeSpec(timespec {tv_sec: secs as time_t,
151-
tv_nsec: nanos as c_long })
158+
tv_nsec: nanos as timespec_tv_nsec_t })
152159
}
153160

154161
fn num_seconds(&self) -> i64 {
@@ -175,9 +182,9 @@ impl TimeValLike for TimeSpec {
175182
}
176183

177184
impl TimeSpec {
178-
fn nanos_mod_sec(&self) -> c_long {
185+
fn nanos_mod_sec(&self) -> timespec_tv_nsec_t {
179186
if self.tv_sec() < 0 && self.tv_nsec() > 0 {
180-
self.tv_nsec() - NANOS_PER_SEC as c_long
187+
self.tv_nsec() - NANOS_PER_SEC as timespec_tv_nsec_t
181188
} else {
182189
self.tv_nsec()
183190
}
@@ -188,7 +195,7 @@ impl TimeSpec {
188195
self.0.tv_sec
189196
}
190197

191-
pub fn tv_nsec(&self) -> c_long {
198+
pub fn tv_nsec(&self) -> timespec_tv_nsec_t {
192199
self.0.tv_nsec
193200
}
194201
}

0 commit comments

Comments
 (0)