Skip to content

Commit b8c488e

Browse files
authored
Unrolled build for #146950
Rollup merge of #146950 - joboet:cstr_default, r=tgross35 core: simplify `CStr::default()` Just use a `CStr`-literal...
2 parents bbcbc78 + a875f77 commit b8c488e

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

library/alloc/src/ffi/c_str.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -970,17 +970,14 @@ impl Default for Rc<CStr> {
970970
/// This may or may not share an allocation with other Rcs on the same thread.
971971
#[inline]
972972
fn default() -> Self {
973-
let rc = Rc::<[u8]>::from(*b"\0");
974-
// `[u8]` has the same layout as `CStr`, and it is `NUL` terminated.
975-
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) }
973+
Rc::from(c"")
976974
}
977975
}
978976

979977
#[stable(feature = "default_box_extra", since = "1.17.0")]
980978
impl Default for Box<CStr> {
981979
fn default() -> Box<CStr> {
982-
let boxed: Box<[u8]> = Box::from([0]);
983-
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
980+
Box::from(c"")
984981
}
985982
}
986983

library/core/src/ffi/c_str.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ impl fmt::Debug for CStr {
179179
impl Default for &CStr {
180180
#[inline]
181181
fn default() -> Self {
182-
const SLICE: &[c_char] = &[0];
183-
// SAFETY: `SLICE` is indeed pointing to a valid nul-terminated string.
184-
unsafe { CStr::from_ptr(SLICE.as_ptr()) }
182+
c""
185183
}
186184
}
187185

0 commit comments

Comments
 (0)