@@ -157,9 +157,10 @@ impl Layout {
157
157
/// allocate backing structure for `T` (which could be a trait
158
158
/// or other unsized type like a slice).
159
159
#[ stable( feature = "alloc_layout" , since = "1.28.0" ) ]
160
+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
160
161
#[ must_use]
161
162
#[ inline]
162
- pub fn for_value < T : ?Sized > ( t : & T ) -> Self {
163
+ pub const fn for_value < T : ?Sized > ( t : & T ) -> Self {
163
164
let ( size, align) = ( mem:: size_of_val ( t) , mem:: align_of_val ( t) ) ;
164
165
// SAFETY: see rationale in `new` for why this is using the unsafe variant
165
166
unsafe { Layout :: from_size_align_unchecked ( size, align) }
@@ -191,8 +192,9 @@ impl Layout {
191
192
/// [trait object]: ../../book/ch17-02-trait-objects.html
192
193
/// [extern type]: ../../unstable-book/language-features/extern-types.html
193
194
#[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
195
+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
194
196
#[ 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 {
196
198
// SAFETY: we pass along the prerequisites of these functions to the caller
197
199
let ( size, align) = unsafe { ( mem:: size_of_val_raw ( t) , mem:: align_of_val_raw ( t) ) } ;
198
200
// SAFETY: see rationale in `new` for why this is using the unsafe variant
@@ -229,8 +231,9 @@ impl Layout {
229
231
/// Returns an error if the combination of `self.size()` and the given
230
232
/// `align` violates the conditions listed in [`Layout::from_size_align`].
231
233
#[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
234
+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
232
235
#[ inline]
233
- pub fn align_to ( & self , align : usize ) -> Result < Self , LayoutError > {
236
+ pub const fn align_to ( & self , align : usize ) -> Result < Self , LayoutError > {
234
237
Layout :: from_size_align ( self . size ( ) , cmp:: max ( self . align ( ) , align) )
235
238
}
236
239
@@ -287,10 +290,11 @@ impl Layout {
287
290
/// This is equivalent to adding the result of `padding_needed_for`
288
291
/// to the layout's current size.
289
292
#[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
293
+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
290
294
#[ must_use = "this returns a new `Layout`, \
291
295
without modifying the original"]
292
296
#[ inline]
293
- pub fn pad_to_align ( & self ) -> Layout {
297
+ pub const fn pad_to_align ( & self ) -> Layout {
294
298
let pad = self . padding_needed_for ( self . align ( ) ) ;
295
299
// This cannot overflow. Quoting from the invariant of Layout:
296
300
// > `size`, when rounded up to the nearest multiple of `align`,
@@ -311,8 +315,9 @@ impl Layout {
311
315
///
312
316
/// On arithmetic overflow, returns `LayoutError`.
313
317
#[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
318
+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
314
319
#[ inline]
315
- pub fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutError > {
320
+ pub const fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutError > {
316
321
// This cannot overflow. Quoting from the invariant of Layout:
317
322
// > `size`, when rounded up to the nearest multiple of `align`,
318
323
// > must not overflow isize (i.e., the rounded value must be
@@ -321,7 +326,8 @@ impl Layout {
321
326
let alloc_size = padded_size. checked_mul ( n) . ok_or ( LayoutError ) ?;
322
327
323
328
// 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) )
325
331
}
326
332
327
333
/// Creates a layout describing the record for `self` followed by
@@ -370,8 +376,9 @@ impl Layout {
370
376
/// # assert_eq!(repr_c(&[u64, u32, u16, u32]), Ok((s, vec![0, 8, 12, 16])));
371
377
/// ```
372
378
#[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
379
+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
373
380
#[ inline]
374
- pub fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutError > {
381
+ pub const fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutError > {
375
382
let new_align = cmp:: max ( self . align , next. align ) ;
376
383
let pad = self . padding_needed_for ( next. align ( ) ) ;
377
384
@@ -396,8 +403,9 @@ impl Layout {
396
403
///
397
404
/// On arithmetic overflow, returns `LayoutError`.
398
405
#[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
406
+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
399
407
#[ inline]
400
- pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
408
+ pub const fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
401
409
let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
402
410
// The safe constructor is called here to enforce the isize size limit.
403
411
Layout :: from_size_alignment ( size, self . align )
@@ -410,8 +418,9 @@ impl Layout {
410
418
///
411
419
/// On arithmetic overflow, returns `LayoutError`.
412
420
#[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
421
+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
413
422
#[ inline]
414
- pub fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
423
+ pub const fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
415
424
let new_size = self . size ( ) . checked_add ( next. size ( ) ) . ok_or ( LayoutError ) ?;
416
425
// The safe constructor is called here to enforce the isize size limit.
417
426
Layout :: from_size_alignment ( new_size, self . align )
@@ -422,13 +431,18 @@ impl Layout {
422
431
/// On arithmetic overflow or when the total size would exceed
423
432
/// `isize::MAX`, returns `LayoutError`.
424
433
#[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
434
+ #[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
425
435
#[ inline]
426
- pub fn array < T > ( n : usize ) -> Result < Self , LayoutError > {
436
+ pub const fn array < T > ( n : usize ) -> Result < Self , LayoutError > {
427
437
// Reduce the amount of code we need to monomorphize per `T`.
428
438
return inner ( mem:: size_of :: < T > ( ) , Alignment :: of :: < T > ( ) , n) ;
429
439
430
440
#[ 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 > {
432
446
// We need to check two things about the size:
433
447
// - That the total size won't overflow a `usize`, and
434
448
// - That the total size still fits in an `isize`.
0 commit comments