1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! `Timespec` and related types, which are used by multiple public API
//! modules.

use crate::backend::c;

/// `struct timespec`
#[cfg(not(fix_y2038))]
pub type Timespec = c::timespec;

/// `struct timespec`
#[cfg(fix_y2038)]
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct Timespec {
    /// Seconds.
    pub tv_sec: Secs,

    /// Nanoseconds. Must be less than 1_000_000_000.
    pub tv_nsec: Nsecs,
}

/// A type for the `tv_sec` field of [`Timespec`].
#[cfg(not(fix_y2038))]
#[allow(deprecated)]
pub type Secs = c::time_t;

/// A type for the `tv_sec` field of [`Timespec`].
#[cfg(fix_y2038)]
pub type Secs = i64;

/// A type for the `tv_nsec` field of [`Timespec`].
#[cfg(all(libc, target_arch = "x86_64", target_pointer_width = "32"))]
pub type Nsecs = i64;

/// A type for the `tv_nsec` field of [`Timespec`].
#[cfg(all(libc, not(all(target_arch = "x86_64", target_pointer_width = "32"))))]
pub type Nsecs = c::c_long;

/// A type for the `tv_nsec` field of [`Timespec`].
#[cfg(linux_raw)]
pub type Nsecs = i64;

/// On 32-bit glibc platforms, `timespec` has anonymous padding fields, which
/// Rust doesn't support yet (see `unnamed_fields`), so we define our own
/// struct with explicit padding, with bidirectional `From` impls.
#[cfg(fix_y2038)]
#[repr(C)]
#[derive(Debug, Clone)]
pub(crate) struct LibcTimespec {
    pub(crate) tv_sec: Secs,

    #[cfg(target_endian = "big")]
    padding: core::mem::MaybeUninit<u32>,

    pub(crate) tv_nsec: Nsecs,

    #[cfg(target_endian = "little")]
    padding: core::mem::MaybeUninit<u32>,
}

#[cfg(fix_y2038)]
impl From<LibcTimespec> for Timespec {
    #[inline]
    fn from(t: LibcTimespec) -> Self {
        Self {
            tv_sec: t.tv_sec,
            tv_nsec: t.tv_nsec,
        }
    }
}

#[cfg(fix_y2038)]
impl From<Timespec> for LibcTimespec {
    #[inline]
    fn from(t: Timespec) -> Self {
        Self {
            tv_sec: t.tv_sec,
            tv_nsec: t.tv_nsec,
            padding: core::mem::MaybeUninit::uninit(),
        }
    }
}

#[test]
fn test_sizes() {
    assert_eq_size!(Secs, u64);
    const_assert!(core::mem::size_of::<Timespec>() >= core::mem::size_of::<(u64, u32)>());
    const_assert!(core::mem::size_of::<Nsecs>() >= 4);

    let mut t = Timespec {
        tv_sec: 0,
        tv_nsec: 0,
    };

    // `tv_nsec` needs to be able to hold nanoseconds up to a second.
    t.tv_nsec = 999_999_999_u32 as _;
    assert_eq!(t.tv_nsec as u64, 999_999_999_u64);

    // `tv_sec` needs to be able to hold more than 32-bits of seconds.
    t.tv_sec = 0x1_0000_0000_u64 as _;
    assert_eq!(t.tv_sec as u64, 0x1_0000_0000_u64);
}

// Test that our workarounds are needed.
#[cfg(fix_y2038)]
#[test]
#[allow(deprecated)]
fn test_fix_y2038() {
    assert_eq_size!(libc::time_t, u32);
}