Rollup review comment suggestions from #4302

This commit is contained in:
Ayaz Hafiz 2022-10-12 16:42:11 -05:00
parent b8cbaf6946
commit ee1d93ba06
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58

View file

@ -11,7 +11,7 @@ pub fn serialize_slice<T: Copy>(
written: usize, written: usize,
) -> io::Result<usize> { ) -> io::Result<usize> {
let alignment = std::mem::align_of::<T>(); let alignment = std::mem::align_of::<T>();
let padding_bytes = round_to_multiple_of(written, alignment) - written; let padding_bytes = next_multiple_of(written, alignment) - written;
for _ in 0..padding_bytes { for _ in 0..padding_bytes {
writer.write_all(&[0])?; writer.write_all(&[0])?;
@ -27,7 +27,7 @@ pub fn deserialize_slice<T: Copy>(bytes: &[u8], length: usize, mut offset: usize
let alignment = std::mem::align_of::<T>(); let alignment = std::mem::align_of::<T>();
let size = std::mem::size_of::<T>(); let size = std::mem::size_of::<T>();
offset = round_to_multiple_of(offset, alignment); offset = next_multiple_of(offset, alignment);
let byte_length = length * size; let byte_length = length * size;
let byte_slice = &bytes[offset..][..byte_length]; let byte_slice = &bytes[offset..][..byte_length];
@ -199,8 +199,12 @@ unsafe fn slice_as_bytes<T>(slice: &[T]) -> &[u8] {
std::slice::from_raw_parts(ptr as *const u8, byte_length) std::slice::from_raw_parts(ptr as *const u8, byte_length)
} }
fn round_to_multiple_of(value: usize, base: usize) -> usize { // TODO check on https://github.com/rust-lang/rust/issues/88581 some time in the future
(value + (base - 1)) / base * base pub const fn next_multiple_of(lhs: usize, rhs: usize) -> usize {
match lhs % rhs {
0 => lhs,
r => lhs + (rhs - r),
}
} }
#[cfg(test)] #[cfg(test)]