danlehmann / arbitrary-int

A modern and lightweight implementation of arbitrary integers for Rust
MIT License
31 stars 12 forks source link

Feature request: new_with_u32 #44

Open danlehmann opened 2 months ago

danlehmann commented 2 months ago

In our codebase we see this pattern quite a lot:

let x: u32;
u7::new(x as u8);

This is problematic as the u8-to-u7 is range-checked, but the u32-to-u8 is not. This can be addressed by u7::new(x.try_into().unwrap()), but that's a) clunky and b) not const safe.

A good fix would be to provide new_from_u32 (or from_u32?) and try_new_from_u32/try_from_u32 (as well as other built-in types).