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

1// devela::sys::os::linux::structs::timespec
2
3#![cfg_attr(not(feature = "unsafe_syscall"), allow(dead_code))]
4
5use crate::Duration;
6
7/// Represents the [`timespec`] structure from libc.
8/// Time in seconds and nanoseconds.
9///
10/// [`timespec`]: https://man7.org/linux/man-pages/man3/timespec.3type.html
11#[derive(Clone, Copy, Debug, Default, PartialEq)]
12#[repr(C)]
13pub struct LinuxTimespec {
14    /// Number of whole seconds.
15    pub tv_sec: isize,
16    /// Number of nanoseconds.
17    pub tv_nsec: isize,
18}
19
20impl LinuxTimespec {
21    /// Returns a new `LinuxTimespec` with the given `seconds` and `nanoseconds`.
22    #[must_use]
23    pub const fn new(seconds: isize, nanoseconds: isize) -> Self {
24        Self { tv_sec: seconds, tv_nsec: nanoseconds }
25    }
26
27    /// Returns a new `LinuxTimespec` with the given `duration`.
28    #[must_use]
29    pub const fn with(duration: Duration) -> Self {
30        Self {
31            tv_sec: duration.as_secs() as isize,
32            tv_nsec: duration.subsec_nanos() as isize,
33        }
34    }
35
36    /// Returns a raw pointer to self.
37    #[must_use]
38    pub const fn as_ptr(&self) -> *const Self {
39        self as *const Self
40    }
41
42    /// Returns a raw mutable pointer to self.
43    #[must_use]
44    pub fn as_mut_ptr(&mut self) -> *mut Self {
45        self as *mut Self
46    }
47}
48
49impl From<Duration> for LinuxTimespec {
50    #[must_use]
51    fn from(duration: Duration) -> Self {
52        Self::with(duration)
53    }
54}