@@ -218,6 +218,23 @@ impl Layout {
218
218
len_rounded_up. wrapping_sub ( len)
219
219
}
220
220
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
+
221
238
/// Creates a layout describing the record for `n` instances of
222
239
/// `self`, with a suitable amount of padding between each to
223
240
/// ensure that each instance is given its requested size and
@@ -506,7 +523,7 @@ pub unsafe trait GlobalAlloc {
506
523
ptr
507
524
}
508
525
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`.
510
527
/// The block is described by the given `ptr` pointer and `layout`.
511
528
///
512
529
/// If this returns a non-null pointer, then ownership of the memory block
@@ -757,7 +774,7 @@ pub unsafe trait Alloc {
757
774
// realloc. alloc_excess, realloc_excess
758
775
759
776
/// 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
761
778
/// by `new_size`. To
762
779
/// accomplish this, this may extend or shrink the allocation
763
780
/// referenced by `ptr` to fit the new layout.
0 commit comments