@@ -218,6 +218,23 @@ impl Layout {
218218 len_rounded_up. wrapping_sub ( len)
219219 }
220220
221+ /// Creates a layout by rounding the size of this layout up to a multiple
222+ /// of the layout's alignment.
223+ ///
224+ /// Returns `Err` if the padded size would overflow.
225+ ///
226+ /// This is equivalent to adding the result of `padding_needed_for`
227+ /// to the layout's current size.
228+ #[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
229+ #[ inline]
230+ pub fn pad_to_align ( & self ) -> Result < Layout , LayoutErr > {
231+ let pad = self . padding_needed_for ( self . align ( ) ) ;
232+ let new_size = self . size ( ) . checked_add ( pad)
233+ . ok_or ( LayoutErr { private : ( ) } ) ?;
234+
235+ Layout :: from_size_align ( new_size, self . align ( ) )
236+ }
237+
221238 /// Creates a layout describing the record for `n` instances of
222239 /// `self`, with a suitable amount of padding between each to
223240 /// ensure that each instance is given its requested size and
@@ -506,7 +523,7 @@ pub unsafe trait GlobalAlloc {
506523 ptr
507524 }
508525
509- /// Shink or grow a block of memory to the given `new_size`.
526+ /// Shrink or grow a block of memory to the given `new_size`.
510527 /// The block is described by the given `ptr` pointer and `layout`.
511528 ///
512529 /// If this returns a non-null pointer, then ownership of the memory block
@@ -757,7 +774,7 @@ pub unsafe trait Alloc {
757774 // realloc. alloc_excess, realloc_excess
758775
759776 /// Returns a pointer suitable for holding data described by
760- /// a new layout with `layout`’s alginment and a size given
777+ /// a new layout with `layout`’s alignment and a size given
761778 /// by `new_size`. To
762779 /// accomplish this, this may extend or shrink the allocation
763780 /// referenced by `ptr` to fit the new layout.
0 commit comments