From 662fb77877ad306318091ac46a37865d94176b5e Mon Sep 17 00:00:00 2001 From: Mike Boutin Date: Thu, 21 Dec 2017 14:00:08 -0500 Subject: [PATCH] Add static assertions to ensure Quantity implements appropriate marker traits. Assert that Quantity implements `Clone`, `Copy`, `Send`, `Sync`, and `hash::Hash` as appropriate based on the underlying storage type. Closes #29. --- Cargo.toml | 3 ++- src/lib.rs | 3 +++ src/tests.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bc1f76f8..4d0fadfb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,9 @@ num = "0.1" typenum = "1.9.0" [dev-dependencies] -quickcheck = "0.5.0" approx = "0.1.1" +quickcheck = "0.5.0" +static_assertions = "0.2.5" [features] default = ["f32", "f64", "si", "std"] diff --git a/src/lib.rs b/src/lib.rs index b38dae59..9dac431e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -165,6 +165,9 @@ extern crate approx; #[cfg(test)] #[macro_use] extern crate quickcheck; +#[cfg(test)] +#[macro_use] +extern crate static_assertions; // Conditionally import `core` or `std` based on feature selection. #[doc(hidden)] diff --git a/src/tests.rs b/src/tests.rs index 6e0775a6..734b365c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -998,3 +998,35 @@ mod quantities_macro { } } } + +mod static_checks { + storage_types! { + types: Float; + + use tests::*; + + assert_impl!(q; Quantity, U, V>, Clone, Copy, Send, Sync); + } + + storage_types! { + types: PrimInt, Rational, Rational32, Rational64; + + use tests::*; + + #[cfg(feature = "std")] + assert_impl!(q; Quantity, U, V>, Clone, Copy, Send, Sync, ::lib::hash::Hash); + #[cfg(not(feature = "std"))] + assert_impl!(q; Quantity, U, V>, Clone, Copy, Send, Sync, ::lib::hash::Hash); + } + + storage_types! { + types: BigInt, BigUint, BigRational; + + use tests::*; + + #[cfg(feature = "std")] + assert_impl!(q; Quantity, U, V>, Clone, Send, Sync, ::lib::hash::Hash); + #[cfg(not(feature = "std"))] + assert_impl!(q; Quantity, U, V>, Clone, Send, Sync, ::lib::hash::Hash); + } +}