about summary refs log tree commit diff
path: root/library/core/src/alloc/layout.rs
diff options
context:
space:
mode:
authorBen Kimock <kimockb@gmail.com>2024-08-20 18:41:07 -0400
committerBen Kimock <kimockb@gmail.com>2024-08-20 18:41:07 -0400
commite6b0f27e00c0a6e0bb137bab837fbd5e9d2b7629 (patch)
tree4b256343de6d5bded285214f03b05e1eafd82668 /library/core/src/alloc/layout.rs
parent7f5d282185674cac26f9f916f0f1ae8ec04e3482 (diff)
downloadrust-e6b0f27e00c0a6e0bb137bab837fbd5e9d2b7629.tar.gz
rust-e6b0f27e00c0a6e0bb137bab837fbd5e9d2b7629.zip
Try to golf down the amount of code in Layout
Diffstat (limited to 'library/core/src/alloc/layout.rs')
-rw-r--r--library/core/src/alloc/layout.rs11
1 files changed, 3 insertions, 8 deletions
diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs
index c2521ffe9cc..ad3f9d80878 100644
--- a/library/core/src/alloc/layout.rs
+++ b/library/core/src/alloc/layout.rs
@@ -68,19 +68,14 @@ impl Layout {
     pub const fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutError> {
         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.