@@ -157,9 +157,10 @@ impl Layout {
157157 /// allocate backing structure for `T` (which could be a trait
158158 /// or other unsized type like a slice).
159159 #[ stable( feature = "alloc_layout" , since = "1.28.0" ) ]
160+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
160161 #[ must_use]
161162 #[ inline]
162- pub fn for_value < T : ?Sized > ( t : & T ) -> Self {
163+ pub const fn for_value < T : ?Sized > ( t : & T ) -> Self {
163164 let ( size, align) = ( mem:: size_of_val ( t) , mem:: align_of_val ( t) ) ;
164165 // SAFETY: see rationale in `new` for why this is using the unsafe variant
165166 unsafe { Layout :: from_size_align_unchecked ( size, align) }
@@ -191,8 +192,9 @@ impl Layout {
191192 /// [trait object]: ../../book/ch17-02-trait-objects.html
192193 /// [extern type]: ../../unstable-book/language-features/extern-types.html
193194 #[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
195+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
194196 #[ must_use]
195- pub unsafe fn for_value_raw < T : ?Sized > ( t : * const T ) -> Self {
197+ pub const unsafe fn for_value_raw < T : ?Sized > ( t : * const T ) -> Self {
196198 // SAFETY: we pass along the prerequisites of these functions to the caller
197199 let ( size, align) = unsafe { ( mem:: size_of_val_raw ( t) , mem:: align_of_val_raw ( t) ) } ;
198200 // SAFETY: see rationale in `new` for why this is using the unsafe variant
@@ -229,8 +231,9 @@ impl Layout {
229231 /// Returns an error if the combination of `self.size()` and the given
230232 /// `align` violates the conditions listed in [`Layout::from_size_align`].
231233 #[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
234+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
232235 #[ inline]
233- pub fn align_to ( & self , align : usize ) -> Result < Self , LayoutError > {
236+ pub const fn align_to ( & self , align : usize ) -> Result < Self , LayoutError > {
234237 Layout :: from_size_align ( self . size ( ) , cmp:: max ( self . align ( ) , align) )
235238 }
236239
@@ -287,10 +290,11 @@ impl Layout {
287290 /// This is equivalent to adding the result of `padding_needed_for`
288291 /// to the layout's current size.
289292 #[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
293+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
290294 #[ must_use = "this returns a new `Layout`, \
291295 without modifying the original"]
292296 #[ inline]
293- pub fn pad_to_align ( & self ) -> Layout {
297+ pub const fn pad_to_align ( & self ) -> Layout {
294298 let pad = self . padding_needed_for ( self . align ( ) ) ;
295299 // This cannot overflow. Quoting from the invariant of Layout:
296300 // > `size`, when rounded up to the nearest multiple of `align`,
@@ -311,8 +315,9 @@ impl Layout {
311315 ///
312316 /// On arithmetic overflow, returns `LayoutError`.
313317 #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
318+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
314319 #[ inline]
315- pub fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutError > {
320+ pub const fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutError > {
316321 // This cannot overflow. Quoting from the invariant of Layout:
317322 // > `size`, when rounded up to the nearest multiple of `align`,
318323 // > must not overflow isize (i.e., the rounded value must be
@@ -321,7 +326,8 @@ impl Layout {
321326 let alloc_size = padded_size. checked_mul ( n) . ok_or ( LayoutError ) ?;
322327
323328 // The safe constructor is called here to enforce the isize size limit.
324- Layout :: from_size_alignment ( alloc_size, self . align ) . map ( |layout| ( layout, padded_size) )
329+ let layout = Layout :: from_size_alignment ( alloc_size, self . align ) ?;
330+ Ok ( ( layout, padded_size) )
325331 }
326332
327333 /// Creates a layout describing the record for `self` followed by
@@ -370,8 +376,9 @@ impl Layout {
370376 /// # assert_eq!(repr_c(&[u64, u32, u16, u32]), Ok((s, vec![0, 8, 12, 16])));
371377 /// ```
372378 #[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
379+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
373380 #[ inline]
374- pub fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutError > {
381+ pub const fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutError > {
375382 let new_align = cmp:: max ( self . align , next. align ) ;
376383 let pad = self . padding_needed_for ( next. align ( ) ) ;
377384
@@ -396,8 +403,9 @@ impl Layout {
396403 ///
397404 /// On arithmetic overflow, returns `LayoutError`.
398405 #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
406+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
399407 #[ inline]
400- pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
408+ pub const fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
401409 let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
402410 // The safe constructor is called here to enforce the isize size limit.
403411 Layout :: from_size_alignment ( size, self . align )
@@ -410,8 +418,9 @@ impl Layout {
410418 ///
411419 /// On arithmetic overflow, returns `LayoutError`.
412420 #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
421+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
413422 #[ inline]
414- pub fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
423+ pub const fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
415424 let new_size = self . size ( ) . checked_add ( next. size ( ) ) . ok_or ( LayoutError ) ?;
416425 // The safe constructor is called here to enforce the isize size limit.
417426 Layout :: from_size_alignment ( new_size, self . align )
@@ -422,13 +431,18 @@ impl Layout {
422431 /// On arithmetic overflow or when the total size would exceed
423432 /// `isize::MAX`, returns `LayoutError`.
424433 #[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
434+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
425435 #[ inline]
426- pub fn array < T > ( n : usize ) -> Result < Self , LayoutError > {
436+ pub const fn array < T > ( n : usize ) -> Result < Self , LayoutError > {
427437 // Reduce the amount of code we need to monomorphize per `T`.
428438 return inner ( mem:: size_of :: < T > ( ) , Alignment :: of :: < T > ( ) , n) ;
429439
430440 #[ inline]
431- fn inner ( element_size : usize , align : Alignment , n : usize ) -> Result < Layout , LayoutError > {
441+ const fn inner (
442+ element_size : usize ,
443+ align : Alignment ,
444+ n : usize ,
445+ ) -> Result < Layout , LayoutError > {
432446 // We need to check two things about the size:
433447 // - That the total size won't overflow a `usize`, and
434448 // - That the total size still fits in an `isize`.
0 commit comments