Min and max limits for generic - there has to be a better way

This works, but it looks clunky. I'd like to be able to just write "T:MAX" to get the max bound of a generic type. but that's not implemented. There's a FIXME - Make these constants in the "num" crate where that ought to go. Is there a better way to get these bounds? Having to unwrap a compile time value is silly.

/// Rescale integer value into specified float range
fn scaletof32<T: num::Bounded + num::PrimInt>(val: T, minval: f32, maxval: f32) -> f32 {
    let inmin = T::min_value().to_f32().unwrap();   // range of input
    let inmax = T::max_value().to_f32().unwrap();   // cannot fail at runtime; these are constants
    let fval = val.to_f32().unwrap();               // all ints can conver to floats
    let range = maxval - minval;                    // numerical range to output
    let scale = range / (inmax-inmin);              // scale factor to convert to float
    (fval - inmin) * scale + minval                 // scaled output
}

Even if this were an associated constant T::MIN, you would still need to unwrap after calling to_f32, which returns an Option. It would just turn this:

let inmin = T::min_value().to_f32().unwrap();

into this:

let inmin = T::MIN.to_f32().unwrap();

If you want to avoid the unwrap, you can use the num::cast::AsPrimitive trait for the conversion.

2 Likes