I had been working through Casey Muratori's Handmade Hero course and writing it in Rust: GitHub - jehugaleahsa/handmade-hero-rs: Casey Muratori's handmade hero - but in Rust! Because I like hard-mode. · GitHub. Honestly, I haven't touched it since last Fall, but I always think about how I could improve what I had.
I had Clippy warnings turned all the way up, to help me learn best practices. One thing I learned is using from/try_from anywhere I could to avoid as casts. Handmade Hero is about making video games, so f32/f64 operations are extremely common.
One question I have for the community is if there is a good way to avoid my many, many #[allow(clippy::possible_truncation)], #[allow(clippy::cast_sign_loss)], etc. when working with floating point numbers. It's mostly going from f32 to u8, or f32 to usize, etc. and less about going from integers to floats.
Is there a popular library out there that wraps floating points with types that are checked to make from/try_from safe? Those types would provide guarantees like non-negative, allow guaranteeing integers after trunc or round is called, fitting within a u8 value range, etc.?
I assume such a library would have a little overhead guaranteeing each thing, but that's okay. I could also see such a library providing "saturating"/clamping conversions, too, which would avoid some of the cost.
Does such a library exist? Am I just missing a concept that stdlib already provides?