refactor: clean up by using cfg-if where possible
Signed-off-by: Stephen Waits <steve@waits.net>
This commit is contained in:
1 parent
b70449a9c2
commit
2809dbc280
5 files changed
+38
-28
No files matched your search
+12
-7
@@ -53,13 +53,15 @@ use crate::vector::{self, Vector};
|
||||
// **** Features ****
|
||||
//
|
||||
|
||||
/// `FloatType` is a type alias for either `f32` or `f64` depending on cargo features
|
||||
#[cfg(feature = "f32")]
|
||||
type FloatType = f32;
|
||||
|
||||
/// `FloatType` is a type alias for either `f32` or `f64` depending on cargo features
|
||||
#[cfg(not(feature = "f32"))]
|
||||
type FloatType = f64;
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "f32")] {
|
||||
/// `FloatType` is a type alias for either `f32` or `f64` depending on cargo features
|
||||
type FloatType = f32;
|
||||
} else {
|
||||
/// `FloatType` is a type alias for either `f32` or `f64` depending on cargo features
|
||||
type FloatType = f64;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// **** Constants ****
|
||||
@@ -263,6 +265,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::cast_lossless)]
|
||||
fn proptest_add(x0 in -1_000_000_000..1_000_000_000i32, y0 in -1_000_000_000..1_000_000_000i32, h0 in 0..1_000_000_000i32, x1 in -1_000_000_000..1_000_000_000i32, y1 in -1_000_000_000..1_000_000_000i32, h1 in 0..1_000_000i32) {
|
||||
// convert our integer range inputs to FloatType
|
||||
let (fx0,fy0,fh0) = (x0 as FloatType / 1_000.0, y0 as FloatType / 1_000.0, h0 as FloatType / 1_000.0);
|
||||
@@ -277,6 +280,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::cast_lossless)]
|
||||
fn proptest_sub(x0 in -1_000_000_000..1_000_000_000i32, y0 in -1_000_000_000..1_000_000_000i32, h0 in 0..1_000_000_000i32, x1 in -1_000_000_000..1_000_000_000i32, y1 in -1_000_000_000..1_000_000_000i32, h1 in 0..1_000_000i32) {
|
||||
// convert our integer range inputs to FloatType
|
||||
let (fx0,fy0,fh0) = (x0 as FloatType / 1_000.0, y0 as FloatType / 1_000.0, h0 as FloatType / 1_000.0);
|
||||
@@ -291,6 +295,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::cast_lossless)]
|
||||
fn proptest_mul(x in -1_000_000_000..1_000_000_000i32, y in -1_000_000_000..1_000_000_000i32, h in 0..1_000_000_000i32, m in -1_000_000_000..1_000_000_000i32) {
|
||||
// convert our integer range inputs to FloatType
|
||||
let (fx,fy,fh,fm) = (x as FloatType / 1_000.0, y as FloatType / 1_000.0, h as FloatType / 1_000.0, m as FloatType / 1_000.0);
|
||||
|
||||
+16
-14
@@ -124,11 +124,13 @@ impl<const N: usize> NetworkCoordinate<N> {
|
||||
#[must_use]
|
||||
pub fn estimated_rtt(&self, rhs: &Self) -> Duration {
|
||||
// estimated rss is euclidean distance between the two plus the sum of the heights
|
||||
#[cfg(feature = "f32")]
|
||||
return Duration::from_secs_f32((self.heightvec - rhs.heightvec).len() / 1000.0);
|
||||
|
||||
#[cfg(not(feature = "f32"))]
|
||||
return Duration::from_secs_f64((self.heightvec - rhs.heightvec).len() / 1000.0);
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "f32")] {
|
||||
Duration::from_secs_f32((self.heightvec - rhs.heightvec).len() / 1000.0)
|
||||
} else {
|
||||
Duration::from_secs_f64((self.heightvec - rhs.heightvec).len() / 1000.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Given another Vivaldi [`NetworkCoordinate`], adjust our coordinateto better represent the actual round
|
||||
@@ -190,15 +192,15 @@ impl<const N: usize> NetworkCoordinate<N> {
|
||||
///
|
||||
pub fn update(&mut self, rhs: &Self, rtt: Duration) -> &Self {
|
||||
// convert Durations into FloatType as fractional milliseconds for convenience
|
||||
#[cfg(feature = "f32")]
|
||||
let rtt_ms = rtt.as_secs_f32() * 1000.0;
|
||||
#[cfg(feature = "f32")]
|
||||
let rtt_estimated_ms = self.estimated_rtt(rhs).as_secs_f32() * 1000.0;
|
||||
|
||||
#[cfg(not(feature = "f32"))]
|
||||
let rtt_ms = rtt.as_secs_f64() * 1000.0;
|
||||
#[cfg(not(feature = "f32"))]
|
||||
let rtt_estimated_ms = self.estimated_rtt(rhs).as_secs_f64() * 1000.0;
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "f32")] {
|
||||
let rtt_ms = rtt.as_secs_f32() * 1000.0;
|
||||
let rtt_estimated_ms = self.estimated_rtt(rhs).as_secs_f32() * 1000.0;
|
||||
} else {
|
||||
let rtt_ms = rtt.as_secs_f64() * 1000.0;
|
||||
let rtt_estimated_ms = self.estimated_rtt(rhs).as_secs_f64() * 1000.0;
|
||||
}
|
||||
}
|
||||
|
||||
// rtt needs to be positive
|
||||
if rtt_ms < 0.0 {
|
||||
|
||||
Reference in new issue
Block a user