File tree Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -716,6 +716,12 @@ impl fmt::Debug for CString {
716
716
}
717
717
}
718
718
719
+ impl fmt::Display for CString {
720
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
721
+ fmt::Display::fmt(self.as_c_str(), f)
722
+ }
723
+ }
724
+
719
725
#[stable(feature = "cstring_into", since = "1.7.0")]
720
726
impl From<CString> for Vec<u8> {
721
727
/// Converts a [`CString`] into a <code>[Vec]<[u8]></code>.
Original file line number Diff line number Diff line change @@ -168,6 +168,15 @@ impl fmt::Debug for CStr {
168
168
}
169
169
}
170
170
171
+ /// Behaves as if `self` were first lossily converted to a `str`, with
172
+ /// invalid UTF-8 presented as the Unicode replacement character: �.
173
+ #[stable(feature = "cstr_display", since = "CURRENT_RUSTC_VERSION")]
174
+ impl fmt::Display for CStr {
175
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
176
+ fmt::Display::fmt(crate::bstr::ByteStr::from_bytes(self.to_bytes()), f)
177
+ }
178
+ }
179
+
171
180
#[stable(feature = "cstr_default", since = "1.10.0")]
172
181
impl Default for &CStr {
173
182
#[inline]
Original file line number Diff line number Diff line change @@ -19,3 +19,9 @@ fn debug() {
19
19
let s = c"abc\x01\x02\n\xE2\x80\xA6\xFF";
20
20
assert_eq!(format!("{s:?}"), r#""abc\x01\x02\n\xe2\x80\xa6\xff""#);
21
21
}
22
+
23
+ #[test]
24
+ fn display() {
25
+ let s = c"\xf0\x28\x8c\xbc";
26
+ assert_eq!(format!("{s}"), "�(��");
27
+ }
You can’t perform that action at this time.
0 commit comments