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
+7
-7
@@ -14,11 +14,11 @@ tasks:
|
|||||||
- format: |
|
- format: |
|
||||||
rustup default stable
|
rustup default stable
|
||||||
cd vivaldi-nc/
|
cd vivaldi-nc/
|
||||||
cargo fmt --verbose --check
|
cargo fmt --verbose --check --all
|
||||||
- lint: |
|
- lint: |
|
||||||
rustup default stable
|
rustup default stable
|
||||||
cd vivaldi-nc/
|
cd vivaldi-nc/
|
||||||
cargo clippy --verbose
|
cargo clippy --verbose --all-targets --all-features
|
||||||
- coverage: |
|
- coverage: |
|
||||||
rustup default stable
|
rustup default stable
|
||||||
cd vivaldi-nc/
|
cd vivaldi-nc/
|
||||||
@@ -26,18 +26,18 @@ tasks:
|
|||||||
- stable: |
|
- stable: |
|
||||||
rustup default stable
|
rustup default stable
|
||||||
cd vivaldi-nc/
|
cd vivaldi-nc/
|
||||||
cargo build --verbose
|
cargo build --verbose --all-targets --all-features
|
||||||
cargo test --verbose --all-features
|
cargo test --verbose --all-features
|
||||||
- nightly: |
|
- nightly: |
|
||||||
rustup default nightly
|
rustup default nightly
|
||||||
cd vivaldi-nc/
|
cd vivaldi-nc/
|
||||||
cargo clippy --verbose ||:
|
cargo clippy --verbose --all-targets --all-features ||:
|
||||||
cargo build --verbose ||:
|
cargo build --verbose --all-targets --all-features ||:
|
||||||
cargo test --verbose --all-features ||:
|
cargo test --verbose --all-targets --all-features ||:
|
||||||
- bench: |
|
- bench: |
|
||||||
rustup default stable
|
rustup default stable
|
||||||
cd vivaldi-nc/
|
cd vivaldi-nc/
|
||||||
cargo bench --verbose --all-features
|
cargo bench --verbose --all-targets --all-features
|
||||||
- examples: |
|
- examples: |
|
||||||
rustup default stable
|
rustup default stable
|
||||||
cd vivaldi-nc/
|
cd vivaldi-nc/
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ harness = false
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
array-init = "2.1.0"
|
array-init = "2.1.0"
|
||||||
|
cfg-if = "1.0.0"
|
||||||
num-traits = "0.2.15"
|
num-traits = "0.2.15"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
serde = {version = "1.0.152", features = ["serde_derive"]}
|
serde = {version = "1.0.152", features = ["serde_derive"]}
|
||||||
|
|||||||
@@ -192,6 +192,8 @@ This crate depends on:
|
|||||||
operations for the internal `Vector` so much nicer. Once we have
|
operations for the internal `Vector` so much nicer. Once we have
|
||||||
`feature(array_zip)` stabilized, we could use that instead. Until then, this
|
`feature(array_zip)` stabilized, we could use that instead. Until then, this
|
||||||
works well with zero performance impact.
|
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
|
## Design Goals & Alternatives
|
||||||
|
|
||||||
|
|||||||
+12
-7
@@ -53,13 +53,15 @@ use crate::vector::{self, Vector};
|
|||||||
// **** Features ****
|
// **** Features ****
|
||||||
//
|
//
|
||||||
|
|
||||||
/// `FloatType` is a type alias for either `f32` or `f64` depending on cargo features
|
cfg_if::cfg_if! {
|
||||||
#[cfg(feature = "f32")]
|
if #[cfg(feature = "f32")] {
|
||||||
type FloatType = f32;
|
/// `FloatType` is a type alias for either `f32` or `f64` depending on cargo features
|
||||||
|
type FloatType = f32;
|
||||||
/// `FloatType` is a type alias for either `f32` or `f64` depending on cargo features
|
} else {
|
||||||
#[cfg(not(feature = "f32"))]
|
/// `FloatType` is a type alias for either `f32` or `f64` depending on cargo features
|
||||||
type FloatType = f64;
|
type FloatType = f64;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// **** Constants ****
|
// **** Constants ****
|
||||||
@@ -263,6 +265,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[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) {
|
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
|
// 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);
|
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]
|
#[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) {
|
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
|
// 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);
|
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]
|
#[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) {
|
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
|
// 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);
|
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]
|
#[must_use]
|
||||||
pub fn estimated_rtt(&self, rhs: &Self) -> Duration {
|
pub fn estimated_rtt(&self, rhs: &Self) -> Duration {
|
||||||
// estimated rss is euclidean distance between the two plus the sum of the heights
|
// estimated rss is euclidean distance between the two plus the sum of the heights
|
||||||
#[cfg(feature = "f32")]
|
cfg_if::cfg_if! {
|
||||||
return Duration::from_secs_f32((self.heightvec - rhs.heightvec).len() / 1000.0);
|
if #[cfg(feature = "f32")] {
|
||||||
|
Duration::from_secs_f32((self.heightvec - rhs.heightvec).len() / 1000.0)
|
||||||
#[cfg(not(feature = "f32"))]
|
} else {
|
||||||
return Duration::from_secs_f64((self.heightvec - rhs.heightvec).len() / 1000.0);
|
Duration::from_secs_f64((self.heightvec - rhs.heightvec).len() / 1000.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given another Vivaldi [`NetworkCoordinate`], adjust our coordinateto better represent the actual round
|
/// 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 {
|
pub fn update(&mut self, rhs: &Self, rtt: Duration) -> &Self {
|
||||||
// convert Durations into FloatType as fractional milliseconds for convenience
|
// convert Durations into FloatType as fractional milliseconds for convenience
|
||||||
#[cfg(feature = "f32")]
|
cfg_if::cfg_if! {
|
||||||
let rtt_ms = rtt.as_secs_f32() * 1000.0;
|
if #[cfg(feature = "f32")] {
|
||||||
#[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;
|
let rtt_estimated_ms = self.estimated_rtt(rhs).as_secs_f32() * 1000.0;
|
||||||
|
} else {
|
||||||
#[cfg(not(feature = "f32"))]
|
let rtt_ms = rtt.as_secs_f64() * 1000.0;
|
||||||
let rtt_ms = rtt.as_secs_f64() * 1000.0;
|
let rtt_estimated_ms = self.estimated_rtt(rhs).as_secs_f64() * 1000.0;
|
||||||
#[cfg(not(feature = "f32"))]
|
}
|
||||||
let rtt_estimated_ms = self.estimated_rtt(rhs).as_secs_f64() * 1000.0;
|
}
|
||||||
|
|
||||||
// rtt needs to be positive
|
// rtt needs to be positive
|
||||||
if rtt_ms < 0.0 {
|
if rtt_ms < 0.0 {
|
||||||
|
|||||||
Reference in new issue
Block a user