@@ -230,10 +230,11 @@ impl Layout {
230230 /// be that of a valid pointer, which means this must not be used
231231 /// as a "not yet initialized" sentinel value.
232232 /// Types that lazily allocate must track initialization by some other means.
233- #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
233+ #[ stable( feature = "alloc_layout_extra" , since = "CURRENT_RUSTC_VERSION" ) ]
234+ #[ rustc_const_stable( feature = "alloc_layout_extra" , since = "CURRENT_RUSTC_VERSION" ) ]
234235 #[ must_use]
235236 #[ inline]
236- pub const fn dangling ( & self ) -> NonNull < u8 > {
237+ pub const fn dangling_ptr ( & self ) -> NonNull < u8 > {
237238 NonNull :: without_provenance ( self . align . as_nonzero ( ) )
238239 }
239240
@@ -263,29 +264,23 @@ impl Layout {
263264 }
264265
265266 /// Returns the amount of padding we must insert after `self`
266- /// to ensure that the following address will satisfy `align`
267- /// (measured in bytes).
267+ /// to ensure that the following address will satisfy `alignment`.
268268 ///
269- /// e.g., if `self.size()` is 9, then `self.padding_needed_for(4)`
269+ /// e.g., if `self.size()` is 9, then `self.padding_needed_for(alignment4)`
270+ /// (where `alignment4.as_usize() == 4`)
270271 /// returns 3, because that is the minimum number of bytes of
271272 /// padding required to get a 4-aligned address (assuming that the
272273 /// corresponding memory block starts at a 4-aligned address).
273274 ///
274- /// The return value of this function has no meaning if `align` is
275- /// not a power-of-two.
276- ///
277- /// Note that the utility of the returned value requires `align`
275+ /// Note that the utility of the returned value requires `alignment`
278276 /// to be less than or equal to the alignment of the starting
279277 /// address for the whole allocated block of memory. One way to
280- /// satisfy this constraint is to ensure `align <= self.align()`.
281- #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
282- #[ must_use = "this returns the padding needed, \
283- without modifying the `Layout`"]
278+ /// satisfy this constraint is to ensure `alignment.as_usize() <= self.align()`.
279+ #[ unstable( feature = "ptr_alignment_type" , issue = "102070" ) ]
280+ #[ must_use = "this returns the padding needed, without modifying the `Layout`" ]
284281 #[ inline]
285- pub const fn padding_needed_for ( & self , align : usize ) -> usize {
286- // FIXME: Can we just change the type on this to `Alignment`?
287- let Some ( align) = Alignment :: new ( align) else { return usize:: MAX } ;
288- let len_rounded_up = self . size_rounded_up_to_custom_align ( align) ;
282+ pub const fn padding_needed_for ( & self , alignment : Alignment ) -> usize {
283+ let len_rounded_up = self . size_rounded_up_to_custom_align ( alignment) ;
289284 // SAFETY: Cannot overflow because the rounded-up value is never less
290285 unsafe { unchecked_sub ( len_rounded_up, self . size ) }
291286 }
@@ -348,14 +343,15 @@ impl Layout {
348343 /// layout of the array and `offs` is the distance between the start
349344 /// of each element in the array.
350345 ///
346+ /// Does not include padding after the trailing element.
347+ ///
351348 /// (That distance between elements is sometimes known as "stride".)
352349 ///
353350 /// On arithmetic overflow, returns `LayoutError`.
354351 ///
355352 /// # Examples
356353 ///
357354 /// ```
358- /// #![feature(alloc_layout_extra)]
359355 /// use std::alloc::Layout;
360356 ///
361357 /// // All rust types have a size that's a multiple of their alignment.
@@ -366,17 +362,26 @@ impl Layout {
366362 /// // But you can manually make layouts which don't meet that rule.
367363 /// let padding_needed = Layout::from_size_align(6, 4).unwrap();
368364 /// let repeated = padding_needed.repeat(3).unwrap();
369- /// assert_eq!(repeated, (Layout::from_size_align(24 , 4).unwrap(), 8));
365+ /// assert_eq!(repeated, (Layout::from_size_align(22 , 4).unwrap(), 8));
370366 /// ```
371- #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
367+ #[ stable( feature = "alloc_layout_extra" , since = "CURRENT_RUSTC_VERSION" ) ]
368+ #[ rustc_const_stable( feature = "alloc_layout_extra" , since = "CURRENT_RUSTC_VERSION" ) ]
372369 #[ inline]
373370 pub const fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutError > {
371+ // FIXME(const-hack): the following could be way shorter with `?`
374372 let padded = self . pad_to_align ( ) ;
375- if let Ok ( repeated) = padded. repeat_packed ( n) {
376- Ok ( ( repeated, padded. size ( ) ) )
373+ let Ok ( result) = ( if let Some ( k) = n. checked_sub ( 1 ) {
374+ let Ok ( repeated) = padded. repeat_packed ( k) else {
375+ return Err ( LayoutError ) ;
376+ } ;
377+ repeated. extend_packed ( * self )
377378 } else {
378- Err ( LayoutError )
379- }
379+ debug_assert ! ( n == 0 ) ;
380+ self . repeat_packed ( 0 )
381+ } ) else {
382+ return Err ( LayoutError ) ;
383+ } ;
384+ Ok ( ( result, padded. size ( ) ) )
380385 }
381386
382387 /// Creates a layout describing the record for `self` followed by
@@ -456,7 +461,8 @@ impl Layout {
456461 /// aligned.
457462 ///
458463 /// On arithmetic overflow, returns `LayoutError`.
459- #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
464+ #[ stable( feature = "alloc_layout_extra" , since = "CURRENT_RUSTC_VERSION" ) ]
465+ #[ rustc_const_stable( feature = "alloc_layout_extra" , since = "CURRENT_RUSTC_VERSION" ) ]
460466 #[ inline]
461467 pub const fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
462468 if let Some ( size) = self . size . checked_mul ( n) {
@@ -473,7 +479,8 @@ impl Layout {
473479 /// and is not incorporated *at all* into the resulting layout.
474480 ///
475481 /// On arithmetic overflow, returns `LayoutError`.
476- #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
482+ #[ stable( feature = "alloc_layout_extra" , since = "CURRENT_RUSTC_VERSION" ) ]
483+ #[ rustc_const_stable( feature = "alloc_layout_extra" , since = "CURRENT_RUSTC_VERSION" ) ]
477484 #[ inline]
478485 pub const fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
479486 // SAFETY: each `size` is at most `isize::MAX == usize::MAX/2`, so the
0 commit comments