Skip to content

Commit d356233

Browse files
emilkhacknus
authored andcommitted
Make Slider and DragValue compatible with NonZeroUsize etc (emilk#5105)
1 parent dd3c716 commit d356233

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

crates/emath/src/numeric.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ macro_rules! impl_numeric_integer {
6060
};
6161
}
6262

63+
macro_rules! impl_numeric_non_zero_unsigned {
64+
($t: path) => {
65+
impl Numeric for $t {
66+
const INTEGRAL: bool = true;
67+
const MIN: Self = Self::MIN;
68+
const MAX: Self = Self::MAX;
69+
70+
#[inline(always)]
71+
fn to_f64(self) -> f64 {
72+
self.get() as f64
73+
}
74+
75+
#[inline(always)]
76+
fn from_f64(num: f64) -> Self {
77+
Self::new(num.round().max(1.0) as _).unwrap_or(Self::MIN)
78+
}
79+
}
80+
};
81+
}
82+
6383
impl_numeric_float!(f32);
6484
impl_numeric_float!(f64);
6585
impl_numeric_integer!(i8);
@@ -72,3 +92,9 @@ impl_numeric_integer!(i64);
7292
impl_numeric_integer!(u64);
7393
impl_numeric_integer!(isize);
7494
impl_numeric_integer!(usize);
95+
impl_numeric_non_zero_unsigned!(std::num::NonZeroU8);
96+
impl_numeric_non_zero_unsigned!(std::num::NonZeroU16);
97+
impl_numeric_non_zero_unsigned!(std::num::NonZeroU32);
98+
impl_numeric_non_zero_unsigned!(std::num::NonZeroU64);
99+
impl_numeric_non_zero_unsigned!(std::num::NonZeroU128);
100+
impl_numeric_non_zero_unsigned!(std::num::NonZeroUsize);

0 commit comments

Comments
 (0)