about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTamir Duberstein <tamird@gmail.com>2025-04-17 19:43:56 -0400
committerTamir Duberstein <tamird@gmail.com>2025-05-24 08:24:52 -0400
commit947be5f4316088f44cf2db69d137fd9afbfd7723 (patch)
treef337a975305c47ed274485847817d0fa49de206b
parent5af801b687e6e8b860ae970e725c8b9a3820d0ce (diff)
downloadrust-947be5f4316088f44cf2db69d137fd9afbfd7723.tar.gz
rust-947be5f4316088f44cf2db69d137fd9afbfd7723.zip
add CStr::display
The implementation delegates to `<ByteStr as Display>::fmt`.

Link: https://github.com/rust-lang/libs-team/issues/550
Link: https://github.com/rust-lang/rust/issues/139984.
-rw-r--r--library/core/src/ffi/c_str.rs24
-rw-r--r--library/coretests/tests/ffi/cstr.rs6
-rw-r--r--library/coretests/tests/lib.rs1
3 files changed, 31 insertions, 0 deletions
diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs
index ac07c645c01..c2799fa0b5c 100644
--- a/library/core/src/ffi/c_str.rs
+++ b/library/core/src/ffi/c_str.rs
@@ -659,6 +659,30 @@ impl CStr {
         // instead of doing it afterwards.
         str::from_utf8(self.to_bytes())
     }
+
+    /// Returns an object that implements [`Display`] for safely printing a [`CStr`] that may
+    /// contain non-Unicode data.
+    ///
+    /// Behaves as if `self` were first lossily converted to a `str`, with invalid UTF-8 presented
+    /// as the Unicode replacement character: �.
+    ///
+    /// [`Display`]: fmt::Display
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(cstr_display)]
+    ///
+    /// let cstr = c"Hello, world!";
+    /// println!("{}", cstr.display());
+    /// ```
+    #[unstable(feature = "cstr_display", issue = "139984")]
+    #[must_use = "this does not display the `CStr`; \
+                  it returns an object that can be displayed"]
+    #[inline]
+    pub fn display(&self) -> impl fmt::Display {
+        crate::bstr::ByteStr::from_bytes(self.to_bytes())
+    }
 }
 
 // `.to_bytes()` representations are compared instead of the inner `[c_char]`s,
diff --git a/library/coretests/tests/ffi/cstr.rs b/library/coretests/tests/ffi/cstr.rs
index 0d85b22c585..dc34240cd99 100644
--- a/library/coretests/tests/ffi/cstr.rs
+++ b/library/coretests/tests/ffi/cstr.rs
@@ -19,3 +19,9 @@ fn debug() {
     let s = c"abc\x01\x02\n\xE2\x80\xA6\xFF";
     assert_eq!(format!("{s:?}"), r#""abc\x01\x02\n\xe2\x80\xa6\xff""#);
 }
+
+#[test]
+fn display() {
+    let s = c"\xf0\x28\x8c\xbc";
+    assert_eq!(format!("{}", s.display()), "�(��");
+}
diff --git a/library/coretests/tests/lib.rs b/library/coretests/tests/lib.rs
index b1301200981..693b14ef762 100644
--- a/library/coretests/tests/lib.rs
+++ b/library/coretests/tests/lib.rs
@@ -23,6 +23,7 @@
 #![feature(core_io_borrowed_buf)]
 #![feature(core_private_bignum)]
 #![feature(core_private_diy_float)]
+#![feature(cstr_display)]
 #![feature(dec2flt)]
 #![feature(duration_constants)]
 #![feature(duration_constructors)]