We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Once const-generics are further stabilized, one could write an encode/decode function that does not error:
#![feature(const_generics, const_evaluatable_checked)] use std::array::IntoIter; const HEX_CHARS_LOWER: &[u8; 16] = b"0123456789abcdef"; fn generate_iter(len: usize) -> impl Iterator<Item = (usize, usize)> { (0..len).step_by(2).zip((0..len).skip(1).step_by(2)) } const fn byte2hex(byte: u8, table: &[u8; 16]) -> (u8, u8) { let high = table[((byte & 0xf0) >> 4) as usize]; let low = table[(byte & 0x0f) as usize]; (high, low) } pub fn encode<const N: usize>(bytes: [u8; N]) -> [u8; N * 2] { let mut output = [0; { N * 2 }]; for (byte, (i, j)) in IntoIter::new(bytes).zip(generate_iter(N * 2)) { let (high, low) = byte2hex(byte, HEX_CHARS_LOWER); output[i] = high; output[j] = low; } output } fn main() { assert_eq!(encode(*b"kiwi"), *b"6b697769"); }
playground
I think this should be considered before the 1.0 release (this could either be a new function or it could replace an existing one?).
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Once const-generics are further stabilized, one could write an encode/decode function that does not error:
playground
I think this should be considered before the 1.0 release (this could either be a new function or it could replace an existing one?).
The text was updated successfully, but these errors were encountered: