Skip to content

Commit

Permalink
Make Slider and DragValue compatible with NonZeroUsize etc (emi…
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored and hacknus committed Oct 30, 2024
1 parent dd3c716 commit d356233
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/emath/src/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ macro_rules! impl_numeric_integer {
};
}

macro_rules! impl_numeric_non_zero_unsigned {
($t: path) => {
impl Numeric for $t {
const INTEGRAL: bool = true;
const MIN: Self = Self::MIN;
const MAX: Self = Self::MAX;

#[inline(always)]
fn to_f64(self) -> f64 {
self.get() as f64
}

#[inline(always)]
fn from_f64(num: f64) -> Self {
Self::new(num.round().max(1.0) as _).unwrap_or(Self::MIN)
}
}
};
}

impl_numeric_float!(f32);
impl_numeric_float!(f64);
impl_numeric_integer!(i8);
Expand All @@ -72,3 +92,9 @@ impl_numeric_integer!(i64);
impl_numeric_integer!(u64);
impl_numeric_integer!(isize);
impl_numeric_integer!(usize);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU8);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU16);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU32);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU64);
impl_numeric_non_zero_unsigned!(std::num::NonZeroU128);
impl_numeric_non_zero_unsigned!(std::num::NonZeroUsize);

0 comments on commit d356233

Please sign in to comment.