From 2809dbc280dc27fac06c0acff769cde21e33ebb9 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 13 Feb 2023 10:41:49 -0700 Subject: [PATCH] refactor: clean up by using cfg-if where possible Signed-off-by: Stephen Waits --- .build.yml | 14 +++++++------- Cargo.toml | 1 + README.md | 2 ++ src/height_vector.rs | 19 ++++++++++++------- src/network_coordinate.rs | 30 ++++++++++++++++-------------- 5 files changed, 38 insertions(+), 28 deletions(-) diff --git a/.build.yml b/.build.yml index 5c1fc1d..9145c35 100644 --- a/.build.yml +++ b/.build.yml @@ -14,11 +14,11 @@ tasks: - format: | rustup default stable cd vivaldi-nc/ - cargo fmt --verbose --check + cargo fmt --verbose --check --all - lint: | rustup default stable cd vivaldi-nc/ - cargo clippy --verbose + cargo clippy --verbose --all-targets --all-features - coverage: | rustup default stable cd vivaldi-nc/ @@ -26,18 +26,18 @@ tasks: - stable: | rustup default stable cd vivaldi-nc/ - cargo build --verbose + cargo build --verbose --all-targets --all-features cargo test --verbose --all-features - nightly: | rustup default nightly cd vivaldi-nc/ - cargo clippy --verbose ||: - cargo build --verbose ||: - cargo test --verbose --all-features ||: + cargo clippy --verbose --all-targets --all-features ||: + cargo build --verbose --all-targets --all-features ||: + cargo test --verbose --all-targets --all-features ||: - bench: | rustup default stable cd vivaldi-nc/ - cargo bench --verbose --all-features + cargo bench --verbose --all-targets --all-features - examples: | rustup default stable cd vivaldi-nc/ diff --git a/Cargo.toml b/Cargo.toml index 1a42a7b..eee0e7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ harness = false [dependencies] array-init = "2.1.0" +cfg-if = "1.0.0" num-traits = "0.2.15" rand = "0.8.5" serde = {version = "1.0.152", features = ["serde_derive"]} diff --git a/README.md b/README.md index 4f7b7ee..7f074b0 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,8 @@ This crate depends on: operations for the internal `Vector` so much nicer. Once we have `feature(array_zip)` stabilized, we could use that instead. Until then, this works well with zero performance impact. +- [`cfg-if`](https://crates.io/crates/cfg-if): Compile-time only macro which + makes using `cfg` parameters much nicer in code. ## Design Goals & Alternatives diff --git a/src/height_vector.rs b/src/height_vector.rs index b087cae..0630bdb 100644 --- a/src/height_vector.rs +++ b/src/height_vector.rs @@ -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); diff --git a/src/network_coordinate.rs b/src/network_coordinate.rs index fdd7c8e..959d62e 100644 --- a/src/network_coordinate.rs +++ b/src/network_coordinate.rs @@ -124,11 +124,13 @@ impl NetworkCoordinate { #[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 NetworkCoordinate { /// 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 {