|
|
|
|
|
|
|
pub trait Clampable: Sized { |
|
|
|
fn clamp_hard_min(self, min: f64) -> Self; |
|
|
|
fn clamp_hard_max(self, max: f64) -> Self; |
|
} |
|
|
|
|
|
macro_rules! impl_clampable_float { |
|
($($ty:ty),*) => { |
|
$( |
|
impl Clampable for $ty { |
|
#[inline(always)] |
|
fn clamp_hard_min(self, min: f64) -> Self { |
|
self.max(min as $ty) |
|
} |
|
#[inline(always)] |
|
fn clamp_hard_max(self, max: f64) -> Self { |
|
self.min(max as $ty) |
|
} |
|
} |
|
)* |
|
}; |
|
} |
|
impl_clampable_float!(f32, f64); |
|
|
|
macro_rules! impl_clampable_int { |
|
($($ty:ty),*) => { |
|
$( |
|
impl Clampable for $ty { |
|
#[inline(always)] |
|
fn clamp_hard_min(self, min: f64) -> Self { |
|
|
|
|
|
<$ty>::try_from(min.ceil() as i64).ok().map_or(self, |min_val| self.max(min_val)) |
|
} |
|
#[inline(always)] |
|
fn clamp_hard_max(self, max: f64) -> Self { |
|
<$ty>::try_from(max.floor() as i64).ok().map_or(self, |max_val| self.min(max_val)) |
|
} |
|
} |
|
)* |
|
}; |
|
} |
|
|
|
impl_clampable_int!(u32, u64, i32, i64); |
|
|
|
|
|
use glam::DVec2; |
|
impl Clampable for DVec2 { |
|
#[inline(always)] |
|
fn clamp_hard_min(self, min: f64) -> Self { |
|
self.max(DVec2::splat(min)) |
|
} |
|
#[inline(always)] |
|
fn clamp_hard_max(self, max: f64) -> Self { |
|
self.min(DVec2::splat(max)) |
|
} |
|
} |
|
|