From ee1d93ba06bd9d7b70819421b7edd2c758b8336d Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Wed, 12 Oct 2022 16:42:11 -0500 Subject: [PATCH] Rollup review comment suggestions from #4302 --- crates/compiler/serialize/src/bytes.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/compiler/serialize/src/bytes.rs b/crates/compiler/serialize/src/bytes.rs index 58d4b3d5b5..cf0c9060c2 100644 --- a/crates/compiler/serialize/src/bytes.rs +++ b/crates/compiler/serialize/src/bytes.rs @@ -11,7 +11,7 @@ pub fn serialize_slice( written: usize, ) -> io::Result { let alignment = std::mem::align_of::(); - let padding_bytes = round_to_multiple_of(written, alignment) - written; + let padding_bytes = next_multiple_of(written, alignment) - written; for _ in 0..padding_bytes { writer.write_all(&[0])?; @@ -27,7 +27,7 @@ pub fn deserialize_slice(bytes: &[u8], length: usize, mut offset: usize let alignment = std::mem::align_of::(); let size = std::mem::size_of::(); - offset = round_to_multiple_of(offset, alignment); + offset = next_multiple_of(offset, alignment); let byte_length = length * size; let byte_slice = &bytes[offset..][..byte_length]; @@ -199,8 +199,12 @@ unsafe fn slice_as_bytes(slice: &[T]) -> &[u8] { std::slice::from_raw_parts(ptr as *const u8, byte_length) } -fn round_to_multiple_of(value: usize, base: usize) -> usize { - (value + (base - 1)) / base * base +// TODO check on https://github.com/rust-lang/rust/issues/88581 some time in the future +pub const fn next_multiple_of(lhs: usize, rhs: usize) -> usize { + match lhs % rhs { + 0 => lhs, + r => lhs + (rhs - r), + } } #[cfg(test)]