-
Notifications
You must be signed in to change notification settings - Fork 10
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
Conversation
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 PR came to be from the review of #430 After applying this diff, the code in that PR use nibbles::IntoBytes;
/// ... then in `find_next_result`:
let key = nibble_iter_from_parents(parents).chain(node_path_iter).nibbles_into_bytes(); |
There was a problem hiding this 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.
firewood/src/nibbles.rs
Outdated
data | ||
} | ||
} | ||
impl<T: Iterator<Item = u8>> IntoBytes for T {} |
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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
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
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.