Invert <CString as Deref>::deref and CString::as_c_str

This is consistent with the style of `ByteString`.
This commit is contained in:
Tamir Duberstein 2025-04-18 10:15:11 -04:00
parent a15cce2690
commit f727b3622b
No known key found for this signature in database

View File

@ -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)
}
}