@@ -68,6 +68,13 @@ impl Layout {
6868 return Err ( LayoutError ) ;
6969 }
7070
71+ // SAFETY: just checked that align is a power of two.
72+ Layout :: from_size_valid_align ( size, unsafe { ValidAlign :: new_unchecked ( align) } )
73+ }
74+
75+ /// Internal helper constructor to skip revalidating alignment validity.
76+ #[ inline]
77+ const fn from_size_valid_align ( size : usize , align : ValidAlign ) -> Result < Self , LayoutError > {
7178 // (power-of-two implies align != 0.)
7279
7380 // Rounded up size is:
@@ -82,23 +89,11 @@ impl Layout {
8289 //
8390 // Above implies that checking for summation overflow is both
8491 // necessary and sufficient.
85- if size > isize:: MAX as usize - ( align - 1 ) {
86- return Err ( LayoutError ) ;
87- }
88-
89- // SAFETY: the conditions for `from_size_align_unchecked` have been
90- // checked above.
91- unsafe { Ok ( Layout :: from_size_align_unchecked ( size, align) ) }
92- }
93-
94- /// Internal helper constructor to skip revalidating alignment validity.
95- #[ inline]
96- const fn from_size_valid_align ( size : usize , align : ValidAlign ) -> Result < Self , LayoutError > {
97- // See above for the correctness of this check.
9892 if size > isize:: MAX as usize - ( align. as_nonzero ( ) . get ( ) - 1 ) {
9993 return Err ( LayoutError ) ;
10094 }
101- // SAFTEY: as above, this check is sufficient.
95+
96+ // SAFETY: Layout::size invariants checked above.
10297 Ok ( Layout { size, align } )
10398 }
10499
@@ -113,8 +108,8 @@ impl Layout {
113108 #[ must_use]
114109 #[ inline]
115110 pub const unsafe fn from_size_align_unchecked ( size : usize , align : usize ) -> Self {
116- // SAFETY: the caller must ensure that `align` is a power of two .
117- Layout { size, align : unsafe { ValidAlign :: new_unchecked ( align) } }
111+ // SAFETY: the caller is required to uphold the preconditions .
112+ unsafe { Layout { size, align : ValidAlign :: new_unchecked ( align) } }
118113 }
119114
120115 /// The minimum size in bytes for a memory block of this layout.
0 commit comments