Skip to content

Commit 1c56a53

Browse files
committed
constify remaining layout methods
Remove bad impl for Eq Update Cargo.lock and fix last ValidAlign
1 parent 6284998 commit 1c56a53

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

library/core/src/alloc/layout.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@ impl Layout {
157157
/// allocate backing structure for `T` (which could be a trait
158158
/// or other unsized type like a slice).
159159
#[stable(feature = "alloc_layout", since = "1.28.0")]
160+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
160161
#[must_use]
161162
#[inline]
162-
pub fn for_value<T: ?Sized>(t: &T) -> Self {
163+
pub const fn for_value<T: ?Sized>(t: &T) -> Self {
163164
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
164165
// SAFETY: see rationale in `new` for why this is using the unsafe variant
165166
unsafe { Layout::from_size_align_unchecked(size, align) }
@@ -191,8 +192,9 @@ impl Layout {
191192
/// [trait object]: ../../book/ch17-02-trait-objects.html
192193
/// [extern type]: ../../unstable-book/language-features/extern-types.html
193194
#[unstable(feature = "layout_for_ptr", issue = "69835")]
195+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
194196
#[must_use]
195-
pub unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self {
197+
pub const unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self {
196198
// SAFETY: we pass along the prerequisites of these functions to the caller
197199
let (size, align) = unsafe { (mem::size_of_val_raw(t), mem::align_of_val_raw(t)) };
198200
// SAFETY: see rationale in `new` for why this is using the unsafe variant
@@ -229,8 +231,9 @@ impl Layout {
229231
/// Returns an error if the combination of `self.size()` and the given
230232
/// `align` violates the conditions listed in [`Layout::from_size_align`].
231233
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
234+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
232235
#[inline]
233-
pub fn align_to(&self, align: usize) -> Result<Self, LayoutError> {
236+
pub const fn align_to(&self, align: usize) -> Result<Self, LayoutError> {
234237
Layout::from_size_align(self.size(), cmp::max(self.align(), align))
235238
}
236239

@@ -287,10 +290,11 @@ impl Layout {
287290
/// This is equivalent to adding the result of `padding_needed_for`
288291
/// to the layout's current size.
289292
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
293+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
290294
#[must_use = "this returns a new `Layout`, \
291295
without modifying the original"]
292296
#[inline]
293-
pub fn pad_to_align(&self) -> Layout {
297+
pub const fn pad_to_align(&self) -> Layout {
294298
let pad = self.padding_needed_for(self.align());
295299
// This cannot overflow. Quoting from the invariant of Layout:
296300
// > `size`, when rounded up to the nearest multiple of `align`,
@@ -311,8 +315,9 @@ impl Layout {
311315
///
312316
/// On arithmetic overflow, returns `LayoutError`.
313317
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
318+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
314319
#[inline]
315-
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> {
320+
pub const fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> {
316321
// This cannot overflow. Quoting from the invariant of Layout:
317322
// > `size`, when rounded up to the nearest multiple of `align`,
318323
// > must not overflow isize (i.e., the rounded value must be
@@ -370,8 +375,9 @@ impl Layout {
370375
/// # assert_eq!(repr_c(&[u64, u32, u16, u32]), Ok((s, vec![0, 8, 12, 16])));
371376
/// ```
372377
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
378+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
373379
#[inline]
374-
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutError> {
380+
pub const fn extend(&self, next: Self) -> Result<(Self, usize), LayoutError> {
375381
let new_align = cmp::max(self.align, next.align);
376382
let pad = self.padding_needed_for(next.align());
377383

@@ -396,8 +402,9 @@ impl Layout {
396402
///
397403
/// On arithmetic overflow, returns `LayoutError`.
398404
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
405+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
399406
#[inline]
400-
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
407+
pub const fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
401408
let size = self.size().checked_mul(n).ok_or(LayoutError)?;
402409
// The safe constructor is called here to enforce the isize size limit.
403410
Layout::from_size_alignment(size, self.align)
@@ -410,8 +417,9 @@ impl Layout {
410417
///
411418
/// On arithmetic overflow, returns `LayoutError`.
412419
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
420+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
413421
#[inline]
414-
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
422+
pub const fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
415423
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError)?;
416424
// The safe constructor is called here to enforce the isize size limit.
417425
Layout::from_size_alignment(new_size, self.align)
@@ -422,13 +430,18 @@ impl Layout {
422430
/// On arithmetic overflow or when the total size would exceed
423431
/// `isize::MAX`, returns `LayoutError`.
424432
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
433+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
425434
#[inline]
426-
pub fn array<T>(n: usize) -> Result<Self, LayoutError> {
435+
pub const fn array<T>(n: usize) -> Result<Self, LayoutError> {
427436
// Reduce the amount of code we need to monomorphize per `T`.
428437
return inner(mem::size_of::<T>(), Alignment::of::<T>(), n);
429438

430439
#[inline]
431-
fn inner(element_size: usize, align: Alignment, n: usize) -> Result<Layout, LayoutError> {
440+
const fn inner(
441+
element_size: usize,
442+
align: Alignment,
443+
n: usize,
444+
) -> Result<Layout, LayoutError> {
432445
// We need to check two things about the size:
433446
// - That the total size won't overflow a `usize`, and
434447
// - That the total size still fits in an `isize`.

library/core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
// Library features:
9999
#![feature(const_align_offset)]
100100
#![feature(const_align_of_val)]
101+
#![feature(const_align_of_val_raw)]
102+
#![feature(const_alloc_layout)]
101103
#![feature(const_arguments_as_str)]
102104
#![feature(const_array_into_iter_constructors)]
103105
#![feature(const_bigint_helper_methods)]
@@ -137,6 +139,7 @@
137139
#![feature(const_ptr_write)]
138140
#![feature(const_raw_ptr_comparison)]
139141
#![feature(const_size_of_val)]
142+
#![feature(const_size_of_val_raw)]
140143
#![feature(const_slice_from_raw_parts_mut)]
141144
#![feature(const_slice_ptr_len)]
142145
#![feature(const_slice_split_at_mut)]

library/core/src/ptr/alignment.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{cmp, fmt, hash, mem, num};
99
/// Note that particularly large alignments, while representable in this type,
1010
/// are likely not to be supported by actual allocators and linkers.
1111
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
12-
#[derive(Copy, Clone, Eq, PartialEq)]
12+
#[derive(Copy, Clone, Eq)]
1313
#[repr(transparent)]
1414
pub struct Alignment(AlignmentEnum);
1515

@@ -167,16 +167,25 @@ impl From<Alignment> for usize {
167167
}
168168
}
169169

170-
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
171-
impl cmp::Ord for Alignment {
170+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
171+
impl const cmp::PartialEq for Alignment {
172+
#[inline]
173+
fn eq(&self, other: &Self) -> bool {
174+
self.as_nonzero().get() == other.as_nonzero().get()
175+
}
176+
}
177+
178+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
179+
impl const cmp::Ord for Alignment {
172180
#[inline]
173181
fn cmp(&self, other: &Self) -> cmp::Ordering {
174-
self.as_nonzero().cmp(&other.as_nonzero())
182+
self.as_nonzero().get().cmp(&other.as_nonzero().get())
175183
}
176184
}
177185

186+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "87864")]
178187
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
179-
impl cmp::PartialOrd for Alignment {
188+
impl const cmp::PartialOrd for Alignment {
180189
#[inline]
181190
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
182191
Some(self.cmp(other))

0 commit comments

Comments
 (0)