Skip to content

Commit e6b0f27

Browse files
committed
Try to golf down the amount of code in Layout
1 parent 7f5d282 commit e6b0f27

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

library/core/src/alloc/layout.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,14 @@ impl Layout {
6868
pub const fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutError> {
6969
if Layout::is_size_align_valid(size, align) {
7070
// SAFETY: Layout::is_size_align_valid checks the preconditions for this call.
71-
let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
72-
Ok(layout)
71+
unsafe { Ok(Layout { size, align: mem::transmute(align) }) }
7372
} else {
7473
Err(LayoutError)
7574
}
7675
}
7776

7877
const fn is_size_align_valid(size: usize, align: usize) -> bool {
79-
if !align.is_power_of_two() {
80-
return false;
81-
}
82-
// SAFETY: Precondition checked directly above.
83-
let align = unsafe { Alignment::new_unchecked(align) };
78+
let Some(align) = Alignment::new(align) else { return false };
8479
if size > Self::max_size_for_align(align) {
8580
return false;
8681
}
@@ -139,7 +134,7 @@ impl Layout {
139134
) => Layout::is_size_align_valid(size, align)
140135
);
141136
// SAFETY: the caller is required to uphold the preconditions.
142-
unsafe { Layout { size, align: Alignment::new_unchecked(align) } }
137+
unsafe { Layout { size, align: mem::transmute(align) } }
143138
}
144139

145140
/// The minimum size in bytes for a memory block of this layout.

0 commit comments

Comments
 (0)