Skip to content

Commit

Permalink
Support both f32/f64 for map_position_linear()
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Aug 20, 2023
1 parent 6f06bd0 commit 72db2bb
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
//! Receiving and processing sensor data from devices
//! .
use std::{borrow::Borrow, cmp::Ordering, ops::RangeInclusive};
use std::{
borrow::Borrow,
cmp::Ordering,
ops::{Add, Mul, RangeInclusive, Sub},
};

use float_cmp::approx_eq;
use is_sorted::IsSorted as _;
Expand Down Expand Up @@ -149,9 +153,12 @@ impl SliderInput {
}

#[must_use]
pub fn map_position_linear(self, min_value: f32, max_value: f32) -> f32 {
pub fn map_position_linear<T>(self, min_value: T, max_value: T) -> T
where
T: From<f32> + Sub<Output = T> + Mul<Output = T> + Add<Output = T> + Copy,
{
let Self { position } = self;
min_value + position * (max_value - min_value)
min_value + T::from(position) * (max_value - min_value)
}

/// Interpret the position as a ratio for adjusting the volume of a signal.
Expand Down Expand Up @@ -248,7 +255,10 @@ impl CenterSliderInput {

#[must_use]
#[inline]
pub fn map_position_linear(self, min_value: f32, center_value: f32, max_value: f32) -> f32 {
pub fn map_position_linear<T>(self, min_value: T, center_value: T, max_value: T) -> T
where
T: From<f32> + Sub<Output = T> + Mul<Output = T> + Add<Output = T> + Copy + PartialOrd,
{
debug_assert!(
(min_value <= center_value && center_value <= max_value)
|| (min_value >= center_value && center_value >= max_value)
Expand All @@ -259,8 +269,8 @@ impl CenterSliderInput {
.unwrap_or(Ordering::Equal)
{
Ordering::Equal => center_value,
Ordering::Less => position * (center_value - min_value) + center_value,
Ordering::Greater => position * (max_value - center_value) + center_value,
Ordering::Less => T::from(position) * (center_value - min_value) + center_value,
Ordering::Greater => T::from(position) * (max_value - center_value) + center_value,
}
}

Expand Down

0 comments on commit 72db2bb

Please sign in to comment.