Skip to content
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

Merged
merged 2 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(feature = "const_cstr_unchecked")]
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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.

&*(bytes as *const [u8] as *const CStr)
}

Expand Down Expand Up @@ -1471,4 +1472,13 @@ mod tests {
assert_eq!(&*rc2, cstr);
assert_eq!(&*arc2, cstr);
}

#[test]
fn cstr_const_constructor() {
const CSTR: &'static CStr = unsafe {
CStr::from_bytes_with_nul_unchecked(b"Hello, world!\0")
};

assert_eq!(CSTR.to_str().unwrap(), "Hello, world!");
}
}
2 changes: 2 additions & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@
#![feature(min_const_fn)]
#![feature(const_int_ops)]
#![feature(const_ip)]
#![feature(const_raw_ptr_deref)]
#![feature(const_cstr_unchecked)]
#![feature(core_intrinsics)]
#![feature(dropck_eyepatch)]
#![feature(exact_size_is_empty)]
Expand Down