@@ -187,8 +187,8 @@ impl Layout {
187
187
/// which means this must not be used as a "not yet initialized"
188
188
/// sentinel value. Types that lazily allocate must track initialization by
189
189
/// some other means.
190
- #[ unstable ( feature = "alloc_layout_extra" , issue = "55724 " ) ]
191
- #[ rustc_const_unstable( feature = "alloc_layout_extra " , issue = "55724 " ) ]
190
+ #[ stable ( feature = "alloc_layout_extra" , since = "1.63.0 " ) ]
191
+ #[ rustc_const_unstable( feature = "const_alloc_layout " , issue = "67521 " ) ]
192
192
#[ must_use]
193
193
#[ inline]
194
194
pub const fn dangling ( & self ) -> NonNull < u8 > {
@@ -225,14 +225,11 @@ impl Layout {
225
225
/// padding required to get a 4-aligned address (assuming that the
226
226
/// corresponding memory block starts at a 4-aligned address).
227
227
///
228
- /// The return value of this function has no meaning if `align` is
229
- /// not a power-of-two.
230
- ///
231
228
/// Note that the utility of the returned value requires `align`
232
229
/// to be less than or equal to the alignment of the starting
233
230
/// address for the whole allocated block of memory. One way to
234
231
/// satisfy this constraint is to ensure `align <= self.align()`.
235
- #[ unstable ( feature = "alloc_layout_extra" , issue = "55724 " ) ]
232
+ #[ stable ( feature = "alloc_layout_extra" , since = "1.63.0 " ) ]
236
233
#[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
237
234
#[ must_use = "this returns the padding needed, \
238
235
without modifying the `Layout`"]
@@ -288,24 +285,30 @@ impl Layout {
288
285
/// Creates a layout describing the record for `n` instances of
289
286
/// `self`, with a suitable amount of padding between each to
290
287
/// ensure that each instance is given its requested size and
291
- /// alignment. On success, returns `(k, offs)` where `k` is the
292
- /// layout of the array and `offs` is the distance between the start
293
- /// of each element in the array.
288
+ /// alignment, but *no trailing padding*. On success, returns `(k, offs)`
289
+ /// where `k` is the layout of the repetition and `offs` is the distance
290
+ /// between the start of each element in the repetition. This is equivalent
291
+ /// to calling `extend` multiple times.
294
292
///
295
293
/// On arithmetic overflow, returns `LayoutError`.
296
- #[ unstable ( feature = "alloc_layout_extra" , issue = "55724 " ) ]
294
+ #[ stable ( feature = "alloc_layout_extra" , since = "1.63.0 " ) ]
297
295
#[ inline]
298
296
pub fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutError > {
297
+ let padding = self . padding_needed_for ( self . align ( ) ) ;
298
+
299
299
// This cannot overflow. Quoting from the invariant of Layout:
300
300
// > `size`, when rounded up to the nearest multiple of `align`,
301
301
// > must not overflow (i.e., the rounded value must be less than
302
302
// > `usize::MAX`)
303
- let padded_size = self . size ( ) + self . padding_needed_for ( self . align ( ) ) ;
304
- let alloc_size = padded_size. checked_mul ( n) . ok_or ( LayoutError ) ?;
305
-
306
- // SAFETY: self.align is already known to be valid and alloc_size has been
307
- // padded already.
308
- unsafe { Ok ( ( Layout :: from_size_align_unchecked ( alloc_size, self . align ( ) ) , padded_size) ) }
303
+ let padded_size = self . size ( ) + padding;
304
+ let array_size = padded_size. checked_mul ( n) . ok_or ( LayoutError ) ?;
305
+ let repetition_size = array_size - padding;
306
+
307
+ Ok ( (
308
+ // SAFETY: self.align is already known to be valid and array_size doesn't overflow.
309
+ unsafe { Layout :: from_size_align_unchecked ( repetition_size, self . align ( ) ) } ,
310
+ padded_size,
311
+ ) )
309
312
}
310
313
311
314
/// Creates a layout describing the record for `self` followed by
@@ -378,7 +381,7 @@ impl Layout {
378
381
/// aligned.
379
382
///
380
383
/// On arithmetic overflow, returns `LayoutError`.
381
- #[ unstable ( feature = "alloc_layout_extra" , issue = "55724 " ) ]
384
+ #[ stable ( feature = "alloc_layout_extra" , since = "1.63.0 " ) ]
382
385
#[ inline]
383
386
pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
384
387
let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
@@ -391,7 +394,7 @@ impl Layout {
391
394
/// and is not incorporated *at all* into the resulting layout.
392
395
///
393
396
/// On arithmetic overflow, returns `LayoutError`.
394
- #[ unstable ( feature = "alloc_layout_extra" , issue = "55724 " ) ]
397
+ #[ stable ( feature = "alloc_layout_extra" , since = "1.63.0 " ) ]
395
398
#[ inline]
396
399
pub fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
397
400
let new_size = self . size ( ) . checked_add ( next. size ( ) ) . ok_or ( LayoutError ) ?;
0 commit comments