Skip to content
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

Consider how this project would use const-generics before 1.0 release #61

Open
Luro02 opened this issue Jun 6, 2021 · 0 comments
Open
Projects
Milestone

Comments

@Luro02
Copy link
Contributor

Luro02 commented Jun 6, 2021

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?).

@KokaKiwi KokaKiwi added this to To do in v1.0 via automation Jun 6, 2021
@KokaKiwi KokaKiwi added this to the 1.0 milestone Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
v1.0
  
To do
Development

No branches or pull requests

2 participants