@@ -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
@@ -370,8 +375,9 @@ impl Layout {
370375 /// # assert_eq!(repr_c(&[u64, u32, u16, u32]), Ok((s, vec![0, 8, 12, 16])));
371376 /// ```
372377 #[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
378+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
373379 #[ inline]
374- pub fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutError > {
380+ pub const fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutError > {
375381 let new_align = cmp:: max ( self . align , next. align ) ;
376382 let pad = self . padding_needed_for ( next. align ( ) ) ;
377383
@@ -396,8 +402,9 @@ impl Layout {
396402 ///
397403 /// On arithmetic overflow, returns `LayoutError`.
398404 #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
405+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
399406 #[ inline]
400- pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
407+ pub const fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
401408 let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
402409 // The safe constructor is called here to enforce the isize size limit.
403410 Layout :: from_size_alignment ( size, self . align )
@@ -410,8 +417,9 @@ impl Layout {
410417 ///
411418 /// On arithmetic overflow, returns `LayoutError`.
412419 #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
420+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
413421 #[ inline]
414- pub fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
422+ pub const fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
415423 let new_size = self . size ( ) . checked_add ( next. size ( ) ) . ok_or ( LayoutError ) ?;
416424 // The safe constructor is called here to enforce the isize size limit.
417425 Layout :: from_size_alignment ( new_size, self . align )
@@ -422,13 +430,18 @@ impl Layout {
422430 /// On arithmetic overflow or when the total size would exceed
423431 /// `isize::MAX`, returns `LayoutError`.
424432 #[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
433+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
425434 #[ inline]
426- pub fn array < T > ( n : usize ) -> Result < Self , LayoutError > {
435+ pub const fn array < T > ( n : usize ) -> Result < Self , LayoutError > {
427436 // Reduce the amount of code we need to monomorphize per `T`.
428437 return inner ( mem:: size_of :: < T > ( ) , Alignment :: of :: < T > ( ) , n) ;
429438
430439 #[ inline]
431- fn inner ( element_size : usize , align : Alignment , n : usize ) -> Result < Layout , LayoutError > {
440+ const fn inner (
441+ element_size : usize ,
442+ align : Alignment ,
443+ n : usize ,
444+ ) -> Result < Layout , LayoutError > {
432445 // We need to check two things about the size:
433446 // - That the total size won't overflow a `usize`, and
434447 // - That the total size still fits in an `isize`.
0 commit comments