From aa537824c40437d11d32c66fe16d4764b072c094 Mon Sep 17 00:00:00 2001 From: joboet Date: Wed, 24 Sep 2025 00:50:36 +0200 Subject: [PATCH 1/2] core: simplify `CStr::default()` Just use a `CStr`-literal... --- library/core/src/ffi/c_str.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs index d0b53e3a237f6..09d9b160700ca 100644 --- a/library/core/src/ffi/c_str.rs +++ b/library/core/src/ffi/c_str.rs @@ -179,9 +179,7 @@ impl fmt::Debug for CStr { impl Default for &CStr { #[inline] fn default() -> Self { - const SLICE: &[c_char] = &[0]; - // SAFETY: `SLICE` is indeed pointing to a valid nul-terminated string. - unsafe { CStr::from_ptr(SLICE.as_ptr()) } + c"" } } From a875f7779e3c3b9023bff24b6beca08e7be0e0e8 Mon Sep 17 00:00:00 2001 From: joboet Date: Wed, 24 Sep 2025 14:13:34 +0200 Subject: [PATCH 2/2] alloc: simplify `Default` for `Box` and `Rc` --- library/alloc/src/ffi/c_str.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index b0c8c4b1ca4a7..3e78d680ea68a 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -970,17 +970,14 @@ impl Default for Rc { /// This may or may not share an allocation with other Rcs on the same thread. #[inline] fn default() -> Self { - let rc = Rc::<[u8]>::from(*b"\0"); - // `[u8]` has the same layout as `CStr`, and it is `NUL` terminated. - unsafe { Rc::from_raw(Rc::into_raw(rc) as *const CStr) } + Rc::from(c"") } } #[stable(feature = "default_box_extra", since = "1.17.0")] impl Default for Box { fn default() -> Box { - let boxed: Box<[u8]> = Box::from([0]); - unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) } + Box::from(c"") } }