1
0
Fork 0

Rename Normalize to NormalizebleRange

To hopefully make the use of trait a bit clearer. This is where higher
kinded types would have been nice.
This commit is contained in:
Robbert van der Helm 2022-01-24 20:22:31 +01:00
parent 64f7dc8148
commit edf9e96975

View file

@ -29,7 +29,9 @@ pub enum Range<T> {
Linear { min: T, max: T },
}
trait Normalize<T> {
/// A normalizable range for type `T`, where `self` is expected to be a type `R<T>`. Higher kinded
/// types would have made this trait definition a lot clearer.
trait NormalizebleRange<T> {
/// Normalize an unnormalized value. Will be clamped to the bounds of the range if the
/// normalized value exceeds `[0, 1]`.
fn normalize(&self, unnormalized: T) -> f32;
@ -58,7 +60,7 @@ pub struct PlainParam<T: AtomicType> {
pub string_to_value: Option<Box<dyn Fn(&str) -> T>>,
}
impl Normalize<f32> for Range<f32> {
impl NormalizebleRange<f32> for Range<f32> {
fn normalize(&self, unnormalized: f32) -> f32 {
match &self {
Range::Linear { min, max } => (unnormalized - min) / (max - min),
@ -72,7 +74,7 @@ impl Normalize<f32> for Range<f32> {
}
}
impl Normalize<i32> for Range<i32> {
impl NormalizebleRange<i32> for Range<i32> {
fn normalize(&self, unnormalized: i32) -> f32 {
match &self {
Range::Linear { min, max } => (unnormalized - min) as f32 / (max - min) as f32,
@ -125,12 +127,3 @@ mod tests {
assert_eq!(range.unnormalize(0.75), 5);
}
}
fn foo(param: &Param) -> f32 {
match param {
Param::FloatParam(p) => p
.range
.normalize(p.value.load(std::sync::atomic::Ordering::Relaxed)),
Param::IntParam(_) => todo!(),
}
}