I currently try to make a crate that tracks the range an integer can be after performing operations with it. The intention is to know at compile time if an overflow can happen. I want to include this information in the type definition.
I talked with T-Dark on the Rust Discord about it who also gave me the general idea of doing it like that. However, as this is not possible yet, the user was sure a feature like that intended to land some day but there is also currently no issue about it.
That playground snippet also uses std::any::type_name() to show what happens to your bounds during addition.
fn main() {
let a = new_bounded::<128>();
let b = BoundedU32::<0, 64>::new(45);
println!(
"{} + {} = {}",
a.type_name(),
b.type_name(),
(a + b).type_name()
);
// Error: attempt to compute `64_u32 + u32::MAX`, which would overflow
// let c = b + new_bounded::<{u32::max_value()}>();
}
Thanks, that is a good workaround! However, this solution is limited to u8, u16, u32 and u64 (if compiled on 64bit machines), right? Though I can include the unsigned variants with an offset.
I can work with that for now, still I'd be interested if a planned feature without the workaround is out there to track.
The relevant feature in your case is generic_const_exprs, which sounds like it's going to be incomplete for a while yet. You can follow progress on implementing this and other const generics features at the working group repository: