diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-01-12 03:27:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-12 03:27:54 +0100 |
| commit | 019790c72a079fd9f695290322985bd693e06037 (patch) | |
| tree | e923e00968dba3ec09e9998242bf15ee76b30cd9 | |
| parent | f363745872f9b45cfec575f3c2cac42f0c242c03 (diff) | |
| parent | c5a9a14c9f793ce20d34cffac09f4ee21d541565 (diff) | |
| download | rust-019790c72a079fd9f695290322985bd693e06037.tar.gz rust-019790c72a079fd9f695290322985bd693e06037.zip | |
Rollup merge of #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.
| -rw-r--r-- | src/libcore/alloc.rs | 12 | ||||
| -rw-r--r-- | src/libcore/lib.rs | 1 |
2 files changed, 9 insertions, 4 deletions
diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 163f9170b8b..4354e1c7b5f 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -64,8 +64,9 @@ impl Layout { /// must not overflow (i.e., the rounded value must be less than /// `usize::MAX`). #[stable(feature = "alloc_layout", since = "1.28.0")] + #[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[inline] - pub fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutErr> { + pub const fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutErr> { if !align.is_power_of_two() { return Err(LayoutErr { private: () }); } @@ -106,15 +107,17 @@ impl Layout { /// The minimum size in bytes for a memory block of this layout. #[stable(feature = "alloc_layout", since = "1.28.0")] + #[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[inline] - pub fn size(&self) -> usize { + pub const fn size(&self) -> usize { self.size_ } /// The minimum byte alignment for a memory block of this layout. #[stable(feature = "alloc_layout", since = "1.28.0")] + #[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[inline] - pub fn align(&self) -> usize { + pub const fn align(&self) -> usize { self.align_.get() } @@ -181,8 +184,9 @@ impl Layout { /// address for the whole allocated block of memory. One way to /// satisfy this constraint is to ensure `align <= self.align()`. #[unstable(feature = "alloc_layout_extra", issue = "55724")] + #[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")] #[inline] - pub fn padding_needed_for(&self, align: usize) -> usize { + pub const fn padding_needed_for(&self, align: usize) -> usize { let len = self.size(); // Rounded up value is: diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 6c495666384..95ffe4f438f 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -70,6 +70,7 @@ #![feature(bound_cloned)] #![feature(cfg_target_has_atomic)] #![feature(concat_idents)] +#![feature(const_alloc_layout)] #![feature(const_if_match)] #![feature(const_panic)] #![feature(const_fn_union)] |
