forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#60370 - Richard-W:const-layout-construction…
…, r=sfackler Mark core::alloc::Layout::from_size_align_unchecked const Makes it possible (pending stabilization of rust-lang#57563 (`const_fn`)) to rewrite code like ```rust const BUFFER_SIZE: usize = 0x2000; const BUFFER_ALIGN: usize = 0x1000; fn foo() { let layout = std::alloc::Layout::from_size_align(BUFFER_SIZE, BUFFER_ALIGN) .unwrap(); let buffer = std::alloc::alloc(layout); } ``` to ```rust const BUFFER_LAYOUT: std::alloc::Layout = unsafe { std::alloc::Layout::from_size_align_unchecked(0x2000, 0x1000) }; fn foo() { let buffer = std::alloc::alloc(BUFFER_LAYOUT); } ``` which (although `unsafe` is used) looks somewhat cleaner and is easier to read.
- Loading branch information
Showing
5 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use core::alloc::Layout; | ||
|
||
#[test] | ||
fn const_unchecked_layout() { | ||
const SIZE: usize = 0x2000; | ||
const ALIGN: usize = 0x1000; | ||
const LAYOUT: Layout = unsafe { Layout::from_size_align_unchecked(SIZE, ALIGN) }; | ||
assert_eq!(LAYOUT.size(), SIZE); | ||
assert_eq!(LAYOUT.align(), ALIGN); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use std::alloc::Layout; | ||
|
||
// ok | ||
const LAYOUT_VALID: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x08) }; | ||
|
||
// not ok, since alignment needs to be non-zero. | ||
const LAYOUT_INVALID: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x00) }; | ||
//~^ ERROR it is undefined behavior to use this value | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0080]: it is undefined behavior to use this value | ||
--> $DIR/alloc.rs:7:1 | ||
| | ||
LL | const LAYOUT_INVALID: Layout = unsafe { Layout::from_size_align_unchecked(0x1000, 0x00) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0 at .align_, but expected something greater or equal to 1 | ||
| | ||
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |