-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Panic instead of Result on to_slice
.
#43
Comments
When we have const generics we could have this sweetness: pub fn encode_to_slice<const N: usize, const M: usize>(input: &[u8;N], output: &mut [u8;M]) {
if N * 2 != M {
panic!("Invalid output length");
}
for (byte, (i, j)) in input.iter().zip(generate_iter(input.len() * 2)) {
let (high, low) = byte2hex(*byte, HEX_CHARS_LOWER);
output[i] = high;
output[j] = low;
}
} |
According to The Rust Book, maybe we should panic here instead of returning an error, as it can be considered as a "contract violation". Also, for the const generics stuff, i would prefer having compile-time contracts on const values instead of "runtime" checks on const values :/ |
Yeah me too. I was trying different versions like: pub fn encode_to_slice<const N: usize>(input: &[u8;N], output: &mut [u8;N*2]) { But they didn't compile. In general, yeah, you should propagate errors. But, since I "statically" know that I'm providing the right length of the buffers in my case I will always edit: and also, when const generics are used I'm pretty sure the conditional would be compiled away, because the whole expression |
Hi, sorry to barge in, I stumble on a related issue: the doc say the output length must be at least twice the inputs, but the code actually needs exactly twice the length. That was confusing. |
Hey,
I'm curious about the decision to return
Result
instead of panicking here:rust-hex/src/lib.rs
Line 350 in be0c32f
Providing the wrong lengths of buffers doesn't seem like a runtime error that one can recover from. It seems more like a logical error by the developer. Adding the overhead of this check and later matching on the result seems unnecessary.
Thanks!
The text was updated successfully, but these errors were encountered: