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

Create a Nibbles newtype #440

Open
rkuris opened this issue Dec 15, 2023 · 1 comment
Open

Create a Nibbles newtype #440

rkuris opened this issue Dec 15, 2023 · 1 comment

Comments

@rkuris
Copy link
Collaborator

rkuris commented Dec 15, 2023

We're very sloppy about what is a nibble and what is an array of constructed u8 arrays. Consider making this more explicit within the codebase by wrapping nibbles into a newtype.

Originally posted by @richardpringle in #435 (comment)

          I think this would be great if we had `Item = Nibble` where `#[repr(transparent)] struct Nibble(u8)` (where you can only create the nibble if it's within the correct bounds).

Instead, I think something like this would make the call more explicit

/// CAUTION: only use with nibble iterators  
pub(crate) fn nibbles_into_bytes<Iter: Iterator<Item = u8>>(iter: Iter) -> Vec<u8> {
    let mut data = Vec::with_capacity(self.size_hint().0 / 2);

    while let (Some(hi), Some(lo)) = (self.next(), self.next()) {
        data.push((hi << 4) + lo);
    }

    data
}
@rkuris
Copy link
Collaborator Author

rkuris commented Jun 10, 2024

This is partially solved by new design, but could use a 👀 to see if we can do better.

@rkuris rkuris removed the good first issue Good for newcomers label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

1 participant