-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make size_of_val and align_of_val const fns #50559
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -47,6 +47,36 @@ fn size_of_val_basic() { | |
assert_eq!(size_of_val(&1u64), 8); | ||
} | ||
|
||
#[test] | ||
fn size_of_val_const() { | ||
macro_rules! ez_byte_string { | ||
($name: ident, $string: expr) => { | ||
static $name: [u8; size_of_val($string)] = *$string; | ||
} | ||
} | ||
|
||
ez_byte_string!(OWO, b"what's this?"); | ||
ez_byte_string!(EMPTY, b""); | ||
|
||
assert_eq!(&OWO, b"what's this?"); | ||
assert_eq!(&EMPTY, b""); | ||
|
||
// ~ one day ~ | ||
// static SIZE_OF_OWO_SLICE: usize = size_of_val(&OWO[..]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should be able to write static FOO: &[u8] = &OWO;
static BAR: usize = size_of_val(FOO); because coercions don't need method calls like slice indexing does |
||
// static SIZE_OF_EMPTY_SLICE: usize = size_of_val(&EMPTY[..]); | ||
|
||
// assert_eq!(SIZE_OF_OWO_SLICE, OWO.len()); | ||
// assert_eq!(SIZE_OF_EMPTY_SLICE, EMPTY.len()); | ||
|
||
const SIZE_OF_U8: usize = size_of_val(&0u8); | ||
const SIZE_OF_U16: usize = size_of_val(&1u16); | ||
static SIZE_OF_U32: usize = size_of_val(&9u32); | ||
|
||
assert_eq!(SIZE_OF_U8, 1); | ||
assert_eq!(SIZE_OF_U16, 2); | ||
assert_eq!(SIZE_OF_U32, 4); | ||
} | ||
|
||
#[test] | ||
fn align_of_basic() { | ||
assert_eq!(align_of::<u8>(), 1); | ||
|
@@ -82,6 +112,17 @@ fn align_of_val_basic() { | |
assert_eq!(align_of_val(&1u32), 4); | ||
} | ||
|
||
#[test] | ||
fn align_of_val_const() { | ||
const ALIGN_OF_U8: usize = align_of_val(&0u8); | ||
const ALIGN_OF_U16: usize = align_of_val(&1u16); | ||
static ALIGN_OF_U32: usize = align_of_val(&9u32); | ||
|
||
assert_eq!(ALIGN_OF_U8, 1); | ||
assert_eq!(ALIGN_OF_U16, 2); | ||
assert_eq!(ALIGN_OF_U32, 4); | ||
} | ||
|
||
#[test] | ||
fn test_swap() { | ||
let mut x = 31337; | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want this PR to go through fast, you should be marking the function with
#[rustc_const_unstable(feature = "const_size_of_val")]
(and create a tracking issue)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oli-obk const fn features have no tracking issues for now, unfortunately. See #44644.