refactor: clean up by using cfg-if where possible

Signed-off-by: Stephen Waits <steve@waits.net>
This commit is contained in:
swaits committed 2023-02-13 10:41:49 -07:00
1 parent b70449a9c2
commit 2809dbc280
5 files changed
+38 -28

No files matched your search

+7 -7
View File
@@ -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/
+1
View File
@@ -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"]}
+2
View File
@@ -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
+12 -7
View File
@@ -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
View File
@@ -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 {