Skip to content

Commit

Permalink
Use chunks instead of chunks_exact
Browse files Browse the repository at this point in the history
The `chunks_exact` method returns chunks of exactly 4 bytes, and any leftover bytes at the end of the slice are ignored.

Since we know that the input slice will always be a multiple of 4 bytes per the assert, we can use the `chunks` method instead, which is slightly faster because it doesn't need to redundantly check for leftover bytes.
  • Loading branch information
jqnatividad committed Dec 3, 2023
1 parent c66195c commit e209e92
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ macro_rules! from_err {
/// Converts a &[u8] into an iterator of `u32`s
pub fn to_u32(s: &[u8]) -> impl ExactSizeIterator<Item = u32> + '_ {
assert_eq!(s.len() % 4, 0);
s.chunks_exact(4)
s.chunks(4)
.map(|data| u32::from_le_bytes(data.try_into().unwrap()))
}

Expand Down

0 comments on commit e209e92

Please sign in to comment.