-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 CStr::from_bytes_with_nul_unchecked()
a const fn
#54745
Conversation
r? @dtolnay (rust_highfive has picked a reviewer for you, use r? to override) |
Not sure if this needs a test to confirm the ability to use this in a |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@oli-obk little snag with |
src/libstd/ffi/c_str.rs
Outdated
@@ -1040,8 +1040,8 @@ impl CStr { | |||
/// ``` | |||
#[inline] | |||
#[stable(feature = "cstr_from_bytes", since = "1.10.0")] | |||
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr { | |||
&*(bytes as *const [u8] as *const CStr) |
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.
Oh. I should have checked the original source :D You should be able to just add #![feature(const_raw_ptr_deref)]
and keep the original source ;)
A test would be great, yes. |
r? @oli-obk |
@@ -1040,7 +1040,8 @@ impl CStr { | |||
/// ``` | |||
#[inline] | |||
#[stable(feature = "cstr_from_bytes", since = "1.10.0")] | |||
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr { | |||
#[rustc_const_unstable] | |||
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr { |
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 preferred ordering of the attributes? shortest to longest or doesn't really matter?
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.
I don't think it matters. I ususally put the const unstable attribute below the stable attribute
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.
Done, and added it to the crate feature flags as well.
src/libstd/ffi/c_str.rs
Outdated
@@ -1040,7 +1040,8 @@ impl CStr { | |||
/// ``` | |||
#[inline] | |||
#[stable(feature = "cstr_from_bytes", since = "1.10.0")] | |||
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr { | |||
#[rustc_const_unstable] |
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.
you need to specify a feature name like in
#[rustc_const_unstable(feature="fzzzzzt")] //~ERROR internal feature |
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.
Should I just make one up or what?
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.
jup :D that's how feature names work
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@bors r+ |
📌 Commit e0caaec has been approved by |
make `CStr::from_bytes_with_nul_unchecked()` a const fn closes rust-lang#54678
make `CStr::from_bytes_with_nul_unchecked()` a const fn closes rust-lang#54678
Rollup of 10 pull requests Successful merges: - #54269 (#53840: Consolidate pattern check errors) - #54458 (Allow both explicit and elided lifetimes in the same impl header) - #54603 (Add `crate::` to trait suggestions in Rust 2018.) - #54648 (Update Cargo's submodule) - #54680 (make run-pass tests with empty main just compile-pass tests) - #54687 (Use impl_header_lifetime_elision in libcore) - #54699 (Re-export `getopts` so custom drivers can reference it.) - #54702 (do not promote comparing function pointers) - #54728 (Renumber `proc_macro` tracking issues) - #54745 (make `CStr::from_bytes_with_nul_unchecked()` a const fn) Failed merges: r? @ghost
closes #54678