refactor(client): encapsulate SignedDuration and enhance its API
Hide internal fields to improve type safety and provide helper methods for unit conversions and negative value checks. Fix several feature-gated test compilation issues.
This commit is contained in:
1 parent
bec56d4114
commit
2d6581f31f
12 files changed
+107
-77
No files matched your search
@@ -727,9 +727,8 @@ fn compute_rtt(
|
||||
|
||||
let server_processing = compute_server_processing(ts);
|
||||
|
||||
let adjusted = server_processing.map(|sp| SignedDuration {
|
||||
ns: duration_ns_i128(raw) - duration_ns_i128(sp),
|
||||
});
|
||||
let adjusted = server_processing
|
||||
.map(|sp| SignedDuration::from_nanos(duration_ns_i128(raw) - duration_ns_i128(sp)));
|
||||
let effective = adjusted.unwrap_or_else(|| SignedDuration::from_duration(raw));
|
||||
|
||||
RttSample {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#[cfg(all(target_os = "linux", feature = "ancillary"))]
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -18,8 +18,8 @@ fn server_processing_greater_than_raw_does_not_underflow() {
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
assert_eq!(rtt.adjusted, Some(SignedDuration { ns: -999_999_999 }));
|
||||
assert_eq!(rtt.effective, SignedDuration { ns: -999_999_999 });
|
||||
assert_eq!(rtt.adjusted, Some(SignedDuration::from_nanos(-999_999_999)));
|
||||
assert_eq!(rtt.effective, SignedDuration::from_nanos(-999_999_999));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -117,17 +117,37 @@ pub struct RttSample {
|
||||
pub effective: SignedDuration,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct SignedDuration {
|
||||
pub ns: i128,
|
||||
ns: i128,
|
||||
}
|
||||
|
||||
impl SignedDuration {
|
||||
pub const fn from_nanos(ns: i128) -> Self {
|
||||
Self { ns }
|
||||
}
|
||||
|
||||
pub fn from_duration(duration: Duration) -> Self {
|
||||
Self {
|
||||
ns: i128::try_from(duration.as_nanos()).unwrap_or(i128::MAX),
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn as_nanos(self) -> i128 {
|
||||
self.ns
|
||||
}
|
||||
|
||||
pub const fn as_micros(self) -> i128 {
|
||||
self.ns / 1_000
|
||||
}
|
||||
|
||||
pub const fn as_millis(self) -> i128 {
|
||||
self.ns / 1_000_000
|
||||
}
|
||||
|
||||
pub const fn is_negative(self) -> bool {
|
||||
self.ns < 0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Duration> for SignedDuration {
|
||||
@@ -170,18 +190,3 @@ pub struct PacketMeta {
|
||||
pub ecn: Option<u8>,
|
||||
pub kernel_rx_timestamp: Option<std::time::SystemTime>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::PacketMeta;
|
||||
|
||||
#[test]
|
||||
fn packet_meta_default_is_unavailable() {
|
||||
let meta = PacketMeta::default();
|
||||
|
||||
assert_eq!(meta.traffic_class, None);
|
||||
assert_eq!(meta.dscp, None);
|
||||
assert_eq!(meta.ecn, None);
|
||||
assert_eq!(meta.kernel_rx_timestamp, None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user