From 795b630af1468bd92475b21844d449ec3ba76581 Mon Sep 17 00:00:00 2001 From: John Bell Date: Fri, 6 Jun 2025 20:58:27 +0100 Subject: [PATCH] fix: enable using unit macro in foreign crate under test --- src/lib.rs | 4 ++ src/quantity.rs | 7 +-- src/test_only.rs | 19 +++++++ src/unit.rs | 137 ++++++++++++++++++++++++----------------------- 4 files changed, 98 insertions(+), 69 deletions(-) create mode 100644 src/test_only.rs diff --git a/src/lib.rs b/src/lib.rs index 0b0f1d1d..4bb0c18a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -361,6 +361,10 @@ mod system; #[macro_use] mod quantity; +#[macro_use] +#[doc(hidden)] +mod test_only; + #[macro_use] mod unit; diff --git a/src/quantity.rs b/src/quantity.rs index c476ab72..25502622 100644 --- a/src/quantity.rs +++ b/src/quantity.rs @@ -137,9 +137,10 @@ macro_rules! quantity { where V: $crate::Conversion, { - /// Check unit validity to ensure the unit is valid for the underlying storage type. - #[cfg(test)] - fn is_valid() -> bool; + _test_only! { + /// Check unit validity to ensure the unit is valid for the underlying storage type. + fn is_valid() -> bool; + } } unit_units! { diff --git a/src/test_only.rs b/src/test_only.rs new file mode 100644 index 00000000..815049bf --- /dev/null +++ b/src/test_only.rs @@ -0,0 +1,19 @@ +/// Use this in place of #[cfg(test)] in macro output. This will ensure that the code is acatually +/// only outputted when running this crate in test mode +#[cfg(test)] +#[macro_export] +#[doc(hidden)] +macro_rules! _test_only { + ($($tt:tt)*) => { + $($tt)* + } +} + +/// Use this in place of #[cfg(test)] in macro output. This will ensure that the code is acatually +/// only outputted when running this crate in test mode +#[cfg(not(test))] +#[macro_export] +#[doc(hidden)] +macro_rules! _test_only { + ($($tt:tt)*) => {}; +} diff --git a/src/unit.rs b/src/unit.rs index 4ed131a3..a0eb8577 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -165,16 +165,17 @@ macro_rules! unit_units { } impl super::Conversion for super::$unit { - #[cfg(test)] - #[inline(always)] - #[allow(clippy::eq_op)] - fn is_valid() -> bool { - use $crate::num::ToPrimitive; + _test_only! { + #[inline(always)] + #[allow(clippy::eq_op)] + fn is_valid() -> bool { + use $crate::num::ToPrimitive; - let r = Some($coefficient); - let c = >::coefficient().to_f64(); + let r = Some($coefficient); + let c = >::coefficient().to_f64(); - r == c + r == c + } } })+ } @@ -205,27 +206,28 @@ macro_rules! unit_units { } impl super::Conversion for super::$unit { - #[cfg(test)] - #[inline(always)] - #[allow(clippy::eq_op)] - fn is_valid() -> bool { - use $crate::num::{FromPrimitive, ToPrimitive}; - - if let Some(conversion) = Self::T::from_f64($coefficient) { - // Fractional conversion factors will end up being truncated. - if conversion.numer() >= conversion.denom() { - if let Some(numer) = conversion.numer().to_f64() { - if let Some(denom) = conversion.denom().to_f64() { - let r = $coefficient; - let c = numer / denom; - - return r == c + _test_only! { + #[inline(always)] + #[allow(clippy::eq_op)] + fn is_valid() -> bool { + use $crate::num::{FromPrimitive, ToPrimitive}; + + if let Some(conversion) = Self::T::from_f64($coefficient) { + // Fractional conversion factors will end up being truncated. + if conversion.numer() >= conversion.denom() { + if let Some(numer) = conversion.numer().to_f64() { + if let Some(denom) = conversion.denom().to_f64() { + let r = $coefficient; + let c = numer / denom; + + return r == c + } } } } - } - false + false + } } })+ } @@ -261,26 +263,27 @@ macro_rules! unit_units { } impl super::Conversion for super::$unit { - #[cfg(test)] - #[inline(always)] - #[allow(clippy::eq_op)] - fn is_valid() -> bool { - use $crate::num::{FromPrimitive, ToPrimitive}; - - if let Some(conversion) = $crate::num::rational::Ratio::<$crate::num::BigInt>::from_f64($coefficient) { - if conversion.numer() >= conversion.denom() { - if let Some(numer) = conversion.numer().to_f64() { - if let Some(denom) = conversion.denom().to_f64() { - let r = $coefficient; - let c = numer / denom; - - return r == c + _test_only! { + #[inline(always)] + #[allow(clippy::eq_op)] + fn is_valid() -> bool { + use $crate::num::{FromPrimitive, ToPrimitive}; + + if let Some(conversion) = $crate::num::rational::Ratio::<$crate::num::BigInt>::from_f64($coefficient) { + if conversion.numer() >= conversion.denom() { + if let Some(numer) = conversion.numer().to_f64() { + if let Some(denom) = conversion.denom().to_f64() { + let r = $coefficient; + let c = numer / denom; + + return r == c + } } } } - } - false + false + } } })+ } @@ -310,27 +313,28 @@ macro_rules! unit_units { } impl super::Conversion for super::$unit { - #[cfg(test)] - #[inline(always)] - #[allow(clippy::eq_op)] - fn is_valid() -> bool { - use $crate::num::{FromPrimitive, ToPrimitive}; - - if let Some(conversion) = Self::T::from_f64($coefficient) { - // Factional conversion factors will end up being truncated. - if conversion.numer() >= conversion.denom() { - if let Some(numer) = conversion.numer().to_f64() { - if let Some(denom) = conversion.denom().to_f64() { - let r = $coefficient; - let c = numer / denom; - - return r == c + _test_only! { + #[inline(always)] + #[allow(clippy::eq_op)] + fn is_valid() -> bool { + use $crate::num::{FromPrimitive, ToPrimitive}; + + if let Some(conversion) = Self::T::from_f64($coefficient) { + // Factional conversion factors will end up being truncated. + if conversion.numer() >= conversion.denom() { + if let Some(numer) = conversion.numer().to_f64() { + if let Some(denom) = conversion.denom().to_f64() { + let r = $coefficient; + let c = numer / denom; + + return r == c + } } } } - } - false + false + } } })+ } @@ -355,16 +359,17 @@ macro_rules! unit_units { } impl super::Conversion for super::$unit { - #[cfg(test)] - #[inline(always)] - #[allow(clippy::eq_op)] - fn is_valid() -> bool { - use $crate::num::ToPrimitive; + _test_only! { + #[inline(always)] + #[allow(clippy::eq_op)] + fn is_valid() -> bool { + use $crate::num::ToPrimitive; - let r = Some($coefficient); - let c = >::coefficient().to_f64(); + let r = Some($coefficient); + let c = >::coefficient().to_f64(); - r == c + r == c + } } })+ }