-
Notifications
You must be signed in to change notification settings - Fork 13.4k
make CStr::from_bytes_with_nul_unchecked()
a const fn
#54745
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
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. @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 commentThe 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 commentThe 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) | ||
} | ||
|
||
|
@@ -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!"); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.