about summary refs log tree commit diff
diff options
context:
space:
mode:
authorjoboet <jonasboettiger@icloud.com>2025-09-24 14:13:34 +0200
committerjoboet <jonasboettiger@icloud.com>2025-09-24 14:13:34 +0200
commita875f7779e3c3b9023bff24b6beca08e7be0e0e8 (patch)
tree4454c90844736b26530d05d98918646a66c41fcb
parentaa537824c40437d11d32c66fe16d4764b072c094 (diff)
downloadrust-a875f7779e3c3b9023bff24b6beca08e7be0e0e8.tar.gz
rust-a875f7779e3c3b9023bff24b6beca08e7be0e0e8.zip
alloc: simplify `Default` for `Box<CStr>` and `Rc<CStr>`
-rw-r--r--library/alloc/src/ffi/c_str.rs7
1 files changed, 2 insertions, 5 deletions
diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs
index b0c8c4b1ca4..3e78d680ea6 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<CStr> {
     /// 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<CStr> {
     fn default() -> Box<CStr> {
-        let boxed: Box<[u8]> = Box::from([0]);
-        unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
+        Box::from(c"")
     }
 }