Skip to content

Commit 00e4b27

Browse files
authored
Rollup merge of #54745 - abonander:cstr-const, r=oli-obk
make `CStr::from_bytes_with_nul_unchecked()` a const fn closes #54678
2 parents 1826970 + e0caaec commit 00e4b27

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/libstd/ffi/c_str.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,8 @@ impl CStr {
10401040
/// ```
10411041
#[inline]
10421042
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
1043-
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
1043+
#[rustc_const_unstable(feature = "const_cstr_unchecked")]
1044+
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
10441045
&*(bytes as *const [u8] as *const CStr)
10451046
}
10461047

@@ -1471,4 +1472,13 @@ mod tests {
14711472
assert_eq!(&*rc2, cstr);
14721473
assert_eq!(&*arc2, cstr);
14731474
}
1475+
1476+
#[test]
1477+
fn cstr_const_constructor() {
1478+
const CSTR: &'static CStr = unsafe {
1479+
CStr::from_bytes_with_nul_unchecked(b"Hello, world!\0")
1480+
};
1481+
1482+
assert_eq!(CSTR.to_str().unwrap(), "Hello, world!");
1483+
}
14741484
}

src/libstd/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@
253253
#![feature(min_const_fn)]
254254
#![feature(const_int_ops)]
255255
#![feature(const_ip)]
256+
#![feature(const_raw_ptr_deref)]
257+
#![feature(const_cstr_unchecked)]
256258
#![feature(core_intrinsics)]
257259
#![feature(dropck_eyepatch)]
258260
#![feature(exact_size_is_empty)]

0 commit comments

Comments
 (0)