-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #42859 - eddyb:const-size-and-align-of, r=nikomatsakis
Implement const fn {size,align}_of. Fixes #34078. r? @nikomatsakis
- Loading branch information
Showing
8 changed files
with
195 additions
and
15 deletions.
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
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
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
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,18 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(const_fn)] | ||
|
||
struct Foo { | ||
bytes: [u8; std::mem::size_of::<Foo>()] | ||
//~^ ERROR unsupported cyclic reference between types/traits detected | ||
} | ||
|
||
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,60 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(const_fn)] | ||
|
||
use std::mem; | ||
|
||
// Get around the limitations of CTFE in today's Rust. | ||
const fn choice_u64(c: bool, a: u64, b: u64) -> u64 { | ||
(-(c as i64) as u64) & a | (-(!c as i64) as u64) & b | ||
} | ||
|
||
const fn max_usize(a: usize, b: usize) -> usize { | ||
choice_u64(a > b, a as u64, b as u64) as usize | ||
} | ||
|
||
const fn align_to(size: usize, align: usize) -> usize { | ||
(size + (align - 1)) & !(align - 1) | ||
} | ||
|
||
const fn packed_union_size_of<A, B>() -> usize { | ||
max_usize(mem::size_of::<A>(), mem::size_of::<B>()) | ||
} | ||
|
||
const fn union_align_of<A, B>() -> usize { | ||
max_usize(mem::align_of::<A>(), mem::align_of::<B>()) | ||
} | ||
|
||
const fn union_size_of<A, B>() -> usize { | ||
align_to(packed_union_size_of::<A, B>(), union_align_of::<A, B>()) | ||
} | ||
|
||
macro_rules! fake_union { | ||
($name:ident { $a:ty, $b:ty }) => ( | ||
struct $name { | ||
_align: ([$a; 0], [$b; 0]), | ||
_bytes: [u8; union_size_of::<$a, $b>()] | ||
} | ||
) | ||
} | ||
|
||
// Check that we can (poorly) emulate unions by | ||
// calling size_of and align_of at compile-time. | ||
fake_union!(U { u16, [u8; 3] }); | ||
|
||
fn test(u: U) { | ||
assert_eq!(mem::size_of_val(&u._bytes), 4); | ||
} | ||
|
||
fn main() { | ||
assert_eq!(mem::size_of::<U>(), 4); | ||
assert_eq!(mem::align_of::<U>(), 2); | ||
} |