Skip to content

Commit f727b36

Browse files
committed
Invert <CString as Deref>::deref and CString::as_c_str
This is consistent with the style of `ByteString`.
1 parent a15cce2 commit f727b36

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

library/alloc/src/ffi/c_str.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl CString {
574574
#[stable(feature = "as_c_str", since = "1.20.0")]
575575
#[rustc_diagnostic_item = "cstring_as_c_str"]
576576
pub fn as_c_str(&self) -> &CStr {
577-
&*self
577+
unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
578578
}
579579

580580
/// Converts this `CString` into a boxed [`CStr`].
@@ -705,14 +705,14 @@ impl ops::Deref for CString {
705705

706706
#[inline]
707707
fn deref(&self) -> &CStr {
708-
unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
708+
self.as_c_str()
709709
}
710710
}
711711

712712
#[stable(feature = "rust1", since = "1.0.0")]
713713
impl fmt::Debug for CString {
714714
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
715-
fmt::Debug::fmt(&**self, f)
715+
fmt::Debug::fmt(self.as_c_str(), f)
716716
}
717717
}
718718

0 commit comments

Comments
 (0)