Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions block-buffer/src/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,12 @@ impl Sealed for super::Eager {
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]) {
let nb = data.len() / N::USIZE;
let blocks_len = nb * N::USIZE;
let tail_len = data.len() - blocks_len;

// SAFETY: we guarantee that created slices do not point outside of `data`
unsafe {
let blocks_ptr = data.as_ptr() as *const Array<u8, N>;
let tail_ptr = data.as_ptr().add(blocks_len);
(
slice::from_raw_parts(blocks_ptr, nb),
slice::from_raw_parts(tail_ptr, tail_len),
)
let (blocks_raw, tail) = data.split_at_unchecked(blocks_len);
let blocks = slice::from_raw_parts(blocks_raw.as_ptr().cast(), nb);
(blocks, tail)
Copy link
Member

Choose a reason for hiding this comment

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

It's worth to use Array::slice_as_chunks here. It's probably worth to update the Lazy method to it as well.

}
}
}
Expand Down Expand Up @@ -99,21 +96,18 @@ impl Sealed for super::Lazy {
if data.is_empty() {
return (&[], &[]);
}
let (nb, tail_len) = if data.len() % N::USIZE == 0 {
(data.len() / N::USIZE - 1, N::USIZE)
let nb = if data.len() % N::USIZE == 0 {
data.len() / N::USIZE - 1
} else {
let nb = data.len() / N::USIZE;
(nb, data.len() - nb * N::USIZE)
data.len() / N::USIZE
};
let blocks_len = nb * N::USIZE;

// SAFETY: we guarantee that created slices do not point outside of `data`
unsafe {
let blocks_ptr = data.as_ptr() as *const Array<u8, N>;
let tail_ptr = data.as_ptr().add(blocks_len);
(
slice::from_raw_parts(blocks_ptr, nb),
slice::from_raw_parts(tail_ptr, tail_len),
)
let (blocks_raw, tail) = data.split_at_unchecked(blocks_len);
let blocks = slice::from_raw_parts(blocks_raw.as_ptr().cast(), nb);
(blocks, tail)
}
}
}