Skip to content

Commit a342c0f

Browse files
committed
Stabilize alloc_layout_extra
1 parent 1851f08 commit a342c0f

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

library/alloc/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
//
8888
// Library features:
8989
#![cfg_attr(not(no_global_oom_handling), feature(alloc_c_string))]
90-
#![feature(alloc_layout_extra)]
9190
#![feature(allocator_api)]
9291
#![feature(array_chunks)]
9392
#![feature(array_methods)]

library/alloc/tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![feature(allocator_api)]
2-
#![feature(alloc_layout_extra)]
32
#![feature(assert_matches)]
43
#![feature(box_syntax)]
54
#![feature(cow_is_borrowed)]
5+
#![feature(const_alloc_layout)]
66
#![feature(const_box)]
77
#![feature(const_convert)]
88
#![feature(const_cow_is_borrowed)]

library/core/src/alloc/layout.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ impl Layout {
187187
/// which means this must not be used as a "not yet initialized"
188188
/// sentinel value. Types that lazily allocate must track initialization by
189189
/// 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")]
192192
#[must_use]
193193
#[inline]
194194
pub const fn dangling(&self) -> NonNull<u8> {
@@ -225,14 +225,11 @@ impl Layout {
225225
/// padding required to get a 4-aligned address (assuming that the
226226
/// corresponding memory block starts at a 4-aligned address).
227227
///
228-
/// The return value of this function has no meaning if `align` is
229-
/// not a power-of-two.
230-
///
231228
/// Note that the utility of the returned value requires `align`
232229
/// to be less than or equal to the alignment of the starting
233230
/// address for the whole allocated block of memory. One way to
234231
/// 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")]
236233
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
237234
#[must_use = "this returns the padding needed, \
238235
without modifying the `Layout`"]
@@ -288,24 +285,30 @@ impl Layout {
288285
/// Creates a layout describing the record for `n` instances of
289286
/// `self`, with a suitable amount of padding between each to
290287
/// 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.
294292
///
295293
/// On arithmetic overflow, returns `LayoutError`.
296-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
294+
#[stable(feature = "alloc_layout_extra", since = "1.63.0")]
297295
#[inline]
298296
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> {
297+
let padding = self.padding_needed_for(self.align());
298+
299299
// This cannot overflow. Quoting from the invariant of Layout:
300300
// > `size`, when rounded up to the nearest multiple of `align`,
301301
// > must not overflow (i.e., the rounded value must be less than
302302
// > `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+
))
309312
}
310313

311314
/// Creates a layout describing the record for `self` followed by
@@ -378,7 +381,7 @@ impl Layout {
378381
/// aligned.
379382
///
380383
/// On arithmetic overflow, returns `LayoutError`.
381-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
384+
#[stable(feature = "alloc_layout_extra", since = "1.63.0")]
382385
#[inline]
383386
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
384387
let size = self.size().checked_mul(n).ok_or(LayoutError)?;
@@ -391,7 +394,7 @@ impl Layout {
391394
/// and is not incorporated *at all* into the resulting layout.
392395
///
393396
/// On arithmetic overflow, returns `LayoutError`.
394-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
397+
#[stable(feature = "alloc_layout_extra", since = "1.63.0")]
395398
#[inline]
396399
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
397400
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError)?;

library/core/tests/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#![feature(alloc_layout_extra)]
21
#![feature(array_chunks)]
32
#![feature(array_methods)]
43
#![feature(array_windows)]
54
#![feature(bench_black_box)]
65
#![feature(box_syntax)]
76
#![feature(cell_update)]
7+
#![feature(const_alloc_layout)]
88
#![feature(const_assume)]
99
#![feature(const_black_box)]
1010
#![feature(const_bool_to_option)]

library/std/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@
291291
#![feature(strict_provenance)]
292292
//
293293
// Library features (alloc):
294-
#![feature(alloc_layout_extra)]
295294
#![feature(alloc_c_string)]
296295
#![feature(allocator_api)]
297296
#![feature(get_mut_unchecked)]

0 commit comments

Comments
 (0)