Skip to content

Commit

Permalink
emath: use associated constant ONE for trait One
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 11, 2023
1 parent cd63747 commit 4a800d8
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions crates/emath/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,15 @@ pub use {

/// Helper trait to implement [`lerp`] and [`remap`].
pub trait One {
fn one() -> Self;
const ONE: Self;
}

impl One for f32 {
#[inline(always)]
fn one() -> Self {
1.0
}
const ONE: Self = 1.0;
}

impl One for f64 {
#[inline(always)]
fn one() -> Self {
1.0
}
const ONE: Self = 1.0;
}

/// Helper trait to implement [`lerp`] and [`remap`].
Expand Down Expand Up @@ -105,7 +99,7 @@ where
R: Copy + Add<R, Output = R>,
{
let range = range.into();
(T::one() - t) * *range.start() + t * *range.end()
(T::ONE - t) * *range.start() + t * *range.end()
}

/// Where in the range is this value? Returns 0-1 if within the range.
Expand Down Expand Up @@ -172,7 +166,7 @@ where
crate::emath_assert!(from.start() != from.end());
let t = (x - *from.start()) / (*from.end() - *from.start());
// Ensure no numerical inaccuracies sneak in:
if T::one() <= t {
if T::ONE <= t {
*to.end()
} else {
lerp(to, t)
Expand Down

0 comments on commit 4a800d8

Please sign in to comment.