Skip to content

Mark core::alloc::Layout::from_size_align_unchecked const#60370

Merged
bors merged 3 commits intorust-lang:masterfrom
richard-w:const-layout-construction
May 19, 2019
Merged

Mark core::alloc::Layout::from_size_align_unchecked const#60370
bors merged 3 commits intorust-lang:masterfrom
richard-w:const-layout-construction

Conversation

@richard-w
Copy link
Copy Markdown
Contributor

Makes it possible (pending stabilization of #57563 (const_fn)) to rewrite code like

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants