Hmm, I haven’t really played around with const generics much, but I guess, you could maybe implement a custom InRange type, so you could use it like this:
type Hour= InRange<0, 23>;
let hour=Hour::new(17);
That ::new() function would contain the assertions and could be const, but I don’t know, if that actually makes it execute at compile-time, when called in normal runtime code. Might be worth trying to implement it, just to see how it behaves.
Was that an open question or did you have a solution in mind? 😅
What would definitely work in Rust, though, is to implement a macro which checks the constraint and generates a compile_error!() when it’s wrong. Typically, you’d use a (function-like) proc_macro for this, but in this case, you could even have a macro_rules! macro with 24 success cases and then a catch-all error case.
Well, and of course, it may also be fine (or even necessary) to check such numbers at runtime. For that, just a wrapper type with a ::new() function would work.
Was that an open question or did you have a solution in mind?
More open. I saw it land in stable some time back and haven’t gotten around to playing with it. I honestly haven’t done it much, because usually enums are plentywhen there are a finite set of options.
And yeah, I was thinking of runtime checks with const bounds, like this:
pubstructBoundedI32<const MIN: i32, const MAX: i32>(i32);
impl<const MIN: i32, const MAX: i32> BoundedI32<{ MIN }, { MAX }> {
pubconst MIN: i32 = MAX;
pubconst MAX: i32 = MAX;
pubfnnew(n: i32) ->Self {
// or an assertBoundedI32(n.min(Self::MAX).max(Self::MIN))
}
}
I’m not sure how magic Ada gets with things, so maybe it’s a lot nicer there, but I honestly can’t see how it could really improve on handling runtime checks.
Rust can do that too with const generics, no?
I don’t know much about Ada though, but I hear it rocks. Rust has a lot stronger community though, and that carries a lot of weight.
Hmm, I haven’t really played around with const generics much, but I guess, you could maybe implement a custom
InRange
type, so you could use it like this:type Hour = InRange<0, 23>; let hour = Hour::new(17);
That
::new()
function would contain the assertions and could beconst
, but I don’t know, if that actually makes it execute at compile-time, when called in normal runtime code. Might be worth trying to implement it, just to see how it behaves.Was that an open question or did you have a solution in mind? 😅
What would definitely work in Rust, though, is to implement a macro which checks the constraint and generates a
compile_error!()
when it’s wrong. Typically, you’d use a (function-like) proc_macro for this, but in this case, you could even have amacro_rules!
macro with 24 success cases and then a catch-all error case.Well, and of course, it may also be fine (or even necessary) to check such numbers at runtime. For that, just a wrapper type with a
::new()
function would work.More open. I saw it land in stable some time back and haven’t gotten around to playing with it. I honestly haven’t done it much, because usually enums are plentywhen there are a finite set of options.
And yeah, I was thinking of runtime checks with const bounds, like this:
pub struct BoundedI32<const MIN: i32, const MAX: i32>(i32); impl<const MIN: i32, const MAX: i32> BoundedI32<{ MIN }, { MAX }> { pub const MIN: i32 = MAX; pub const MAX: i32 = MAX; pub fn new(n: i32) -> Self { // or an assert BoundedI32(n.min(Self::MAX).max(Self::MIN)) } }
I’m not sure how magic Ada gets with things, so maybe it’s a lot nicer there, but I honestly can’t see how it could really improve on handling runtime checks.