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

Add nibbles_into_bytes for an arbitrary iterator #435

Merged
merged 6 commits into from
Dec 15, 2023

Conversation

rkuris
Copy link
Collaborator

@rkuris rkuris commented Dec 13, 2023

This method is useful for converting nibbles back into their corresponding bytes.

Added some simple tests, both in the success case and the failure case.

This method is useful for converting nibbles back into their
corresponding bytes.

Added some simple tests, both in the success case and the failure case.
@rkuris
Copy link
Collaborator Author

rkuris commented Dec 13, 2023

This PR came to be from the review of #430

After applying this diff, the code in that PR key_from_nibbles_iter moves here, so the one caller can just do:

use nibbles::IntoBytes;
/// ... then in `find_next_result`:
let key = nibble_iter_from_parents(parents).chain(node_path_iter).nibbles_into_bytes();

Copy link
Contributor

@richardpringle richardpringle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we identify where this would be used outside of stream.rs before we merge? If the use case only exists in one module, I think the abstraction should live there until we find more use cases.

data
}
}
impl<T: Iterator<Item = u8>> IntoBytes for T {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #440 for this larger change

@rkuris
Copy link
Collaborator Author

rkuris commented Dec 15, 2023

Could we identify where this would be used outside of stream.rs before we merge? If the use case only exists in one module, I think the abstraction should live there until we find more use cases.

I couldn't find any very quickly so I moved this to stream.rs and make it less visible elsewhere. Lets hope that when someone does find the other use cases that they also find this code.

 - Move code from nibbles.rs to stream.rs
 - Change visibility from pub(crate) to private
 - Add caution about only using it with nibbles iterators
@rkuris rkuris merged commit 57e8698 into main Dec 15, 2023
5 checks passed
@rkuris rkuris deleted the rkuris/nibbles-into-bytes branch December 15, 2023 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants