Skip to main content

timestamped_socket/
lib.rs

1//! A library for networking with timestamping support.
2
3use statime_base::Timestamp;
4
5mod control_message;
6/// Types and operations for finding and manipulating network interfaces.
7pub mod interface;
8/// Traits and types related to network addresses.
9pub mod networkaddress;
10mod raw_socket;
11/// Types and operations related to sockets.
12pub mod socket;
13
14/// Turn a C failure (-1 is returned) into a rust Result
15pub(crate) fn cerr(t: libc::c_int) -> std::io::Result<libc::c_int> {
16    match t {
17        -1 => Err(std::io::Error::last_os_error()),
18        _ => Ok(t),
19    }
20}
21
22/// Convert a timespec struct to a timestamp.
23#[cfg_attr(target_os = "macos", allow(unused))]
24#[expect(
25    clippy::cast_sign_loss,
26    reason = "tv_usec is always in range for the nanos field."
27)]
28#[allow(
29    clippy::cast_possible_truncation,
30    reason = "tv_usec is always in range for the conversion."
31)]
32#[allow(
33    clippy::cast_lossless,
34    reason = "cast is not lossless on all platforms."
35)]
36fn timestamp_from_timespec<T>(timespec: libc::timespec) -> Timestamp<T> {
37    Timestamp::from_seconds_nanos_since_unix_epoch(timespec.tv_sec as _, timespec.tv_nsec as _)
38}
39
40/// Convert a timeval struct to a timestamp
41#[expect(
42    clippy::cast_sign_loss,
43    reason = "tv_usec is always in range for the nanos field."
44)]
45#[allow(
46    clippy::cast_possible_truncation,
47    reason = "tv_usec is always in range for the conversion."
48)]
49#[allow(
50    clippy::cast_lossless,
51    reason = "cast is not lossless on all platforms."
52)]
53fn timestamp_from_timeval<T>(timeval: libc::timeval) -> Timestamp<T> {
54    Timestamp::from_seconds_nanos_since_unix_epoch(
55        timeval.tv_sec as _,
56        (timeval.tv_usec * 1000) as _,
57    )
58}