danwi 0.4.2

Zero-cost dimensional analysis library with SI units, compile-time checking, and no_std support
Documentation
use super::Quantity;
use crate::{dimension::*, scalar::Scalar};
use core::cmp::Ordering;

// Because values are always stored in the SI base unit, equality and
// ordering reduce to direct scalar comparison: no scaling, no allocation.
// `Self` on both sides means comparisons require the same kind.

impl<S: Scalar, D: Dimensions, K> PartialEq for Quantity<S, D, K> {
    #[inline(always)]
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl<S: Scalar, D: Dimensions, K> PartialOrd for Quantity<S, D, K> {
    #[inline(always)]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.value.partial_cmp(&other.value)
    }
}

// Direct comparison against a raw scalar is only meaningful when the
// quantity is dimensionless. Both directions, for both `==` and `<`.

macro_rules! dimensionless_cmp {
    ($($fl:ty : $feature:literal),*) => {
        $(
            #[cfg(feature = $feature)]
            impl PartialEq<$fl> for Quantity<$fl, Dimensionless> {
                #[inline(always)]
                fn eq(&self, other: &$fl) -> bool {
                    self.value == *other
                }
            }

            #[cfg(feature = $feature)]
            impl PartialEq<Quantity<$fl, Dimensionless>> for $fl {
                #[inline(always)]
                fn eq(&self, other: &Quantity<$fl, Dimensionless>) -> bool {
                    *self == other.value
                }
            }

            #[cfg(feature = $feature)]
            impl PartialOrd<$fl> for Quantity<$fl, Dimensionless> {
                #[inline(always)]
                fn partial_cmp(&self, other: &$fl) -> Option<Ordering> {
                    self.value.partial_cmp(other)
                }
            }

            #[cfg(feature = $feature)]
            impl PartialOrd<Quantity<$fl, Dimensionless>> for $fl {
                #[inline(always)]
                fn partial_cmp(&self, other: &Quantity<$fl, Dimensionless>) -> Option<Ordering> {
                    self.partial_cmp(&other.value)
                }
            }
        )*
    };
}

dimensionless_cmp!(f32: "f32", f64: "f64");