From b507a8bfeb9605ea195ec20002b6b0053ef9ed28 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Tue, 20 Aug 2024 18:41:07 -0400 Subject: [PATCH] Try to golf down the amount of code in Layout --- core/src/alloc/layout.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/core/src/alloc/layout.rs b/core/src/alloc/layout.rs index c2521ffe9cc96..ad3f9d8087897 100644 --- a/core/src/alloc/layout.rs +++ b/core/src/alloc/layout.rs @@ -68,19 +68,14 @@ impl Layout { pub const fn from_size_align(size: usize, align: usize) -> Result { if Layout::is_size_align_valid(size, align) { // SAFETY: Layout::is_size_align_valid checks the preconditions for this call. - let layout = unsafe { Layout::from_size_align_unchecked(size, align) }; - Ok(layout) + unsafe { Ok(Layout { size, align: mem::transmute(align) }) } } else { Err(LayoutError) } } const fn is_size_align_valid(size: usize, align: usize) -> bool { - if !align.is_power_of_two() { - return false; - } - // SAFETY: Precondition checked directly above. - let align = unsafe { Alignment::new_unchecked(align) }; + let Some(align) = Alignment::new(align) else { return false }; if size > Self::max_size_for_align(align) { return false; } @@ -139,7 +134,7 @@ impl Layout { ) => Layout::is_size_align_valid(size, align) ); // SAFETY: the caller is required to uphold the preconditions. - unsafe { Layout { size, align: Alignment::new_unchecked(align) } } + unsafe { Layout { size, align: mem::transmute(align) } } } /// The minimum size in bytes for a memory block of this layout.