Skip to content

Commit 019790c

Browse files
authored
Rollup merge of rust-lang#67494 - lukaslueg:const_alloc, r=oli-obk
Constify more of alloc::Layout Making more methods of `alloc::Layout` const would allow us to compute alignment/size information for arbitrary (sized) types at compile-time, including placing the information in associated constants. While `mem::size_of` and `mem::align_of` are already const and `Layout` is solely based on those, there is no guarantee in the implementation that a const derived from these functions will be exactly the same as what `Layout` uses and is subsequently used in a call to `alloc::alloc`. Constifying `Layout` makes this possible. First contribution to core, excuse my ignorance.
2 parents f363745 + c5a9a14 commit 019790c

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/libcore/alloc.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ impl Layout {
6464
/// must not overflow (i.e., the rounded value must be less than
6565
/// `usize::MAX`).
6666
#[stable(feature = "alloc_layout", since = "1.28.0")]
67+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
6768
#[inline]
68-
pub fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutErr> {
69+
pub const fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutErr> {
6970
if !align.is_power_of_two() {
7071
return Err(LayoutErr { private: () });
7172
}
@@ -106,15 +107,17 @@ impl Layout {
106107

107108
/// The minimum size in bytes for a memory block of this layout.
108109
#[stable(feature = "alloc_layout", since = "1.28.0")]
110+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
109111
#[inline]
110-
pub fn size(&self) -> usize {
112+
pub const fn size(&self) -> usize {
111113
self.size_
112114
}
113115

114116
/// The minimum byte alignment for a memory block of this layout.
115117
#[stable(feature = "alloc_layout", since = "1.28.0")]
118+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
116119
#[inline]
117-
pub fn align(&self) -> usize {
120+
pub const fn align(&self) -> usize {
118121
self.align_.get()
119122
}
120123

@@ -181,8 +184,9 @@ impl Layout {
181184
/// address for the whole allocated block of memory. One way to
182185
/// satisfy this constraint is to ensure `align <= self.align()`.
183186
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
187+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
184188
#[inline]
185-
pub fn padding_needed_for(&self, align: usize) -> usize {
189+
pub const fn padding_needed_for(&self, align: usize) -> usize {
186190
let len = self.size();
187191

188192
// Rounded up value is:

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#![feature(bound_cloned)]
7171
#![feature(cfg_target_has_atomic)]
7272
#![feature(concat_idents)]
73+
#![feature(const_alloc_layout)]
7374
#![feature(const_if_match)]
7475
#![feature(const_panic)]
7576
#![feature(const_fn_union)]

0 commit comments

Comments
 (0)