Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ mod system;
#[macro_use]
mod quantity;

#[macro_use]
#[doc(hidden)]
mod test_only;

#[macro_use]
mod unit;

Expand Down
7 changes: 4 additions & 3 deletions src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ macro_rules! quantity {
where
V: $crate::Conversion<V>,
{
/// 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! {
Expand Down
19 changes: 19 additions & 0 deletions src/test_only.rs
Original file line number Diff line number Diff line change
@@ -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)*) => {};
}
137 changes: 71 additions & 66 deletions src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,17 @@ macro_rules! unit_units {
}

impl super::Conversion<V> 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 = <Self as $crate::Conversion<V>>::coefficient().to_f64();
let r = Some($coefficient);
let c = <Self as $crate::Conversion<V>>::coefficient().to_f64();

r == c
r == c
}
}
})+
}
Expand Down Expand Up @@ -205,27 +206,28 @@ macro_rules! unit_units {
}

impl super::Conversion<V> 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
}
}
})+
}
Expand Down Expand Up @@ -261,26 +263,27 @@ macro_rules! unit_units {
}

impl super::Conversion<V> 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
}
}
})+
}
Expand Down Expand Up @@ -310,27 +313,28 @@ macro_rules! unit_units {
}

impl super::Conversion<V> 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
}
}
})+
}
Expand All @@ -355,16 +359,17 @@ macro_rules! unit_units {
}

impl super::Conversion<V> 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 = <Self as $crate::Conversion<V>>::coefficient().to_f64();
let r = Some($coefficient);
let c = <Self as $crate::Conversion<V>>::coefficient().to_f64();

r == c
r == c
}
}
})+
}
Expand Down