diff options
| author | Joakim Hulthe <joakim.hulthe@mullvad.net> | 2025-02-10 17:01:17 +0100 |
|---|---|---|
| committer | Joakim Hulthe <joakim.hulthe@mullvad.net> | 2025-02-25 13:37:28 +0100 |
| commit | 78e4e44308bb00b972e78b80a6b0a69d32181c72 (patch) | |
| tree | f7817c38e58bfc4bd5d8b00c394ce4ad2ada8c2b | |
| parent | 9de617dfd667e13bf0cd2e3c213e0f6f656797a3 (diff) | |
| download | mullvadvpn-78e4e44308bb00b972e78b80a6b0a69d32181c72.tar.xz mullvadvpn-78e4e44308bb00b972e78b80a6b0a69d32181c72.zip | |
Replace libc with nix in talpid-time::unix
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | talpid-time/Cargo.toml | 2 | ||||
| -rw-r--r-- | talpid-time/src/unix.rs | 31 |
3 files changed, 17 insertions, 18 deletions
diff --git a/Cargo.lock b/Cargo.lock index 4afca015ea..2bddb8bef9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4705,7 +4705,7 @@ dependencies = [ name = "talpid-time" version = "0.0.0" dependencies = [ - "libc", + "nix 0.29.0", "tokio", ] diff --git a/talpid-time/Cargo.toml b/talpid-time/Cargo.toml index de5c13ec60..48742abbf3 100644 --- a/talpid-time/Cargo.toml +++ b/talpid-time/Cargo.toml @@ -19,4 +19,4 @@ test = [] tokio = { workspace = true, features = ["time"] } [target.'cfg(unix)'.dependencies] -libc = "0.2" +nix = { version = "0.29", features = ["time"] } diff --git a/talpid-time/src/unix.rs b/talpid-time/src/unix.rs index 5b247b2f5e..a2ea70b0af 100644 --- a/talpid-time/src/unix.rs +++ b/talpid-time/src/unix.rs @@ -1,17 +1,20 @@ -use libc::{c_long, clock_gettime, clockid_t, timespec}; -use std::{mem::MaybeUninit, time::Duration}; +use nix::{ + sys::time::TimeSpec, + time::{clock_gettime, ClockId}, +}; +use std::{ffi::c_long, time::Duration}; const NSEC_PER_SEC: c_long = 1_000_000_000; #[cfg(any(target_os = "macos", target_os = "ios"))] -const CLOCK_ID: clockid_t = libc::CLOCK_MONOTONIC; +const CLOCK_ID: ClockId = ClockId::CLOCK_MONOTONIC; #[cfg(any(target_os = "linux", target_os = "android"))] -const CLOCK_ID: clockid_t = libc::CLOCK_BOOTTIME; +const CLOCK_ID: ClockId = ClockId::CLOCK_BOOTTIME; #[derive(Clone, Copy)] pub struct Instant { - t: timespec, + t: TimeSpec, } impl Instant { @@ -24,15 +27,15 @@ impl Instant { // * `tv_sec >= 0` // * `tv_nsec < NSEC_PER_SEC` - let (tv_sec, tv_nsec) = if self.t.tv_nsec < earlier.t.tv_nsec { + let (tv_sec, tv_nsec) = if self.t.tv_nsec() < earlier.t.tv_nsec() { ( - self.t.tv_sec - earlier.t.tv_sec - 1, - NSEC_PER_SEC - earlier.t.tv_nsec + self.t.tv_nsec, + self.t.tv_sec() - earlier.t.tv_sec() - 1, + NSEC_PER_SEC - earlier.t.tv_nsec() + self.t.tv_nsec(), ) } else { ( - self.t.tv_sec - earlier.t.tv_sec, - self.t.tv_nsec - earlier.t.tv_nsec, + self.t.tv_sec() - earlier.t.tv_sec(), + self.t.tv_nsec() - earlier.t.tv_nsec(), ) }; @@ -49,10 +52,6 @@ impl Instant { } } -fn now() -> timespec { - let mut t = MaybeUninit::zeroed(); - unsafe { - clock_gettime(CLOCK_ID, t.as_mut_ptr()); - t.assume_init() - } +fn now() -> TimeSpec { + clock_gettime(CLOCK_ID).expect("Clock ID is valid") } |
