devela/sys/os/linux/structs/
timespec.rs

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
// devela::sys::os::linux::structs::timespec

#![cfg_attr(not(feature = "unsafe_syscall"), allow(dead_code))]

use crate::Duration;

/// Represents the [`timespec`] structure from libc.
/// Time in seconds and nanoseconds.
///
/// [`timespec`]: https://man7.org/linux/man-pages/man3/timespec.3type.html
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[repr(C)]
pub struct LinuxTimespec {
    /// Number of whole seconds.
    pub tv_sec: isize,
    /// Number of nanoseconds.
    pub tv_nsec: isize,
}

impl LinuxTimespec {
    /// Returns a new `LinuxTimespec` with the given `seconds` and `nanoseconds`.
    #[must_use]
    pub const fn new(seconds: isize, nanoseconds: isize) -> Self {
        Self { tv_sec: seconds, tv_nsec: nanoseconds }
    }

    /// Returns a new `LinuxTimespec` with the given `duration`.
    #[must_use]
    pub const fn with(duration: Duration) -> Self {
        Self {
            tv_sec: duration.as_secs() as isize,
            tv_nsec: duration.subsec_nanos() as isize,
        }
    }

    /// Returns a raw pointer to self.
    #[must_use]
    pub const fn as_ptr(&self) -> *const Self {
        self as *const Self
    }

    /// Returns a raw mutable pointer to self.
    #[must_use]
    pub fn as_mut_ptr(&mut self) -> *mut Self {
        self as *mut Self
    }
}

impl From<Duration> for LinuxTimespec {
    #[must_use]
    fn from(duration: Duration) -> Self {
        Self::with(duration)
    }
}