about summary refs log tree commit diff
path: root/library/alloc/src
diff options
context:
space:
mode:
authorChris Denton <chris@chrisdenton.dev>2025-04-21 18:53:21 +0000
committerGitHub <noreply@github.com>2025-04-21 18:53:21 +0000
commit1cb9a0d450c935cda211bb970446d7e8cf504311 (patch)
tree8e66cbc32513531d2c17c31908aff98fd26d8792 /library/alloc/src
parent8ecaf148e793c93cdd4b14855eb55e04fd15768a (diff)
parentaedbd2d1ec91339954573b691022f89d169f9b84 (diff)
Rollup merge of #140118 - tamird:cstr-cleanup, r=joboet
{B,C}Str: minor cleanup

(hopefully) uncontroversial bits extracted from #139994.
Diffstat (limited to 'library/alloc/src')
-rw-r--r--library/alloc/src/ffi/c_str.rs6
1 files changed, 3 insertions, 3 deletions
diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs
index ef8548d2429..d5b19470e31 100644
--- a/library/alloc/src/ffi/c_str.rs
+++ b/library/alloc/src/ffi/c_str.rs
@@ -574,7 +574,7 @@ impl CString {
     #[stable(feature = "as_c_str", since = "1.20.0")]
     #[rustc_diagnostic_item = "cstring_as_c_str"]
     pub fn as_c_str(&self) -> &CStr {
-        &*self
+        unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
     }
 
     /// Converts this `CString` into a boxed [`CStr`].
@@ -705,14 +705,14 @@ impl ops::Deref for CString {
 
     #[inline]
     fn deref(&self) -> &CStr {
-        unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
+        self.as_c_str()
     }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl fmt::Debug for CString {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        fmt::Debug::fmt(&**self, f)
+        fmt::Debug::fmt(self.as_c_str(), f)
     }
 }