-
-
Notifications
You must be signed in to change notification settings - Fork 16.9k
Dedicated Display type for CStr::display #162837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -651,8 +651,8 @@ impl CStr { | |
| #[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()) | ||
| pub fn display(&self) -> Display<'_> { | ||
| Display { c_str: self } | ||
| } | ||
|
|
||
| /// Returns the same string as a string slice `&CStr`. | ||
|
|
@@ -847,3 +847,41 @@ impl Iterator for Bytes<'_> { | |
|
|
||
| #[unstable(feature = "cstr_bytes", issue = "112115")] | ||
| impl FusedIterator for Bytes<'_> {} | ||
|
|
||
| /// Helper struct for safely printing a [`CStr`] with [`format!`] and `{}`. | ||
| /// | ||
| /// A [`CStr`] might contain non-Unicode data. This `struct` implements the | ||
| /// [`Display`] trait in a way that mitigates that. It is created by the | ||
| /// [`display`](CStr::display) method on [`CStr`]. This may perform lossy | ||
| /// conversion, depending on the platform. If you would like an implementation | ||
| /// which escapes the [`CStr`] please use [`Debug`] instead. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// #![feature(cstr_display)] | ||
| /// | ||
| /// let s = c"Hello, world!"; | ||
| /// println!("{}", s.display()); | ||
| /// ``` | ||
| /// | ||
| /// [`Display`]: fmt::Display | ||
| /// [`format!`]: ../../../std/macro.format.html | ||
| #[unstable(feature = "cstr_display", issue = "139984")] | ||
| pub struct Display<'a> { | ||
|
clarfonthey marked this conversation as resolved.
|
||
| c_str: &'a CStr, | ||
| } | ||
|
|
||
| #[unstable(feature = "cstr_display", issue = "139984")] | ||
| impl fmt::Debug for Display<'_> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The debug impl on the return value of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No equivalent tests for |
||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Debug::fmt(&self.c_str, f) | ||
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "cstr_display", issue = "139984")] | ||
| impl fmt::Display for Display<'_> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| fmt::Display::fmt(crate::bstr::ByteStr::from_bytes(self.c_str.to_bytes()), f) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.