From d04fa4a88b1350536a9c29e1e85eea56b2a4b16e Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Thu, 24 Oct 2024 14:58:04 -0700 Subject: [PATCH] math: Add integer-to-float conversions for `PositiveSign`. --- .../src/math/restricted_number.rs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/all-is-cubes-base/src/math/restricted_number.rs b/all-is-cubes-base/src/math/restricted_number.rs index 7ca0b325c..36e86fd6e 100644 --- a/all-is-cubes-base/src/math/restricted_number.rs +++ b/all-is-cubes-base/src/math/restricted_number.rs @@ -533,6 +533,41 @@ where } } +/// Unsigned integers can be infallibly converted to `PositiveSign`. +mod integer_to_positive_sign { + use super::*; + macro_rules! integer_to_positive_sign { + ($int:ident, $float:ident) => { + impl From<$int> for PositiveSign<$float> { + #[inline] + fn from(value: $int) -> PositiveSign<$float> { + PositiveSign(value.into()) + } + } + }; + } + // This is almost just a list of `T: FloatCore + From`, + // but that would be open to weird adversarial implementations. + integer_to_positive_sign!(bool, f32); + integer_to_positive_sign!(bool, f64); + integer_to_positive_sign!(u8, f32); + integer_to_positive_sign!(u8, f64); + integer_to_positive_sign!(u16, f32); + integer_to_positive_sign!(u16, f64); + integer_to_positive_sign!(u32, f64); +} + +impl From for ZeroOne { + #[inline] + fn from(value: bool) -> Self { + if value { + ZeroOne(T::one()) + } else { + ZeroOne(T::zero()) + } + } +} + #[cfg(feature = "arbitrary")] #[mutants::skip] #[allow(clippy::missing_inline_in_public_items)]