|
| 1 | +//! The [`OsStr`] and [`OsString`] types and associated utilities. |
| 2 | +
|
1 | 3 | #[cfg(test)]
|
2 | 4 | mod tests;
|
3 | 5 |
|
@@ -1147,6 +1149,32 @@ impl OsStr {
|
1147 | 1149 | pub fn eq_ignore_ascii_case<S: AsRef<OsStr>>(&self, other: S) -> bool {
|
1148 | 1150 | self.inner.eq_ignore_ascii_case(&other.as_ref().inner)
|
1149 | 1151 | }
|
| 1152 | + |
| 1153 | + /// Returns an object that implements [`Display`] for safely printing an |
| 1154 | + /// [`OsStr`] that may contain non-Unicode data. This may perform lossy |
| 1155 | + /// conversion, depending on the platform. If you would like an |
| 1156 | + /// implementation which escapes the [`OsStr`] please use [`Debug`] |
| 1157 | + /// instead. |
| 1158 | + /// |
| 1159 | + /// [`Display`]: fmt::Display |
| 1160 | + /// [`Debug`]: fmt::Debug |
| 1161 | + /// |
| 1162 | + /// # Examples |
| 1163 | + /// |
| 1164 | + /// ``` |
| 1165 | + /// #![feature(os_str_display)] |
| 1166 | + /// use std::ffi::OsStr; |
| 1167 | + /// |
| 1168 | + /// let s = OsStr::new("Hello, world!"); |
| 1169 | + /// println!("{}", s.display()); |
| 1170 | + /// ``` |
| 1171 | + #[unstable(feature = "os_str_display", issue = "120048")] |
| 1172 | + #[must_use = "this does not display the `OsStr`; \ |
| 1173 | + it returns an object that can be displayed"] |
| 1174 | + #[inline] |
| 1175 | + pub fn display(&self) -> Display<'_> { |
| 1176 | + Display { os_str: self } |
| 1177 | + } |
1150 | 1178 | }
|
1151 | 1179 |
|
1152 | 1180 | #[stable(feature = "box_from_os_str", since = "1.17.0")]
|
@@ -1441,9 +1469,42 @@ impl fmt::Debug for OsStr {
|
1441 | 1469 | }
|
1442 | 1470 | }
|
1443 | 1471 |
|
1444 |
| -impl OsStr { |
1445 |
| - pub(crate) fn display(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
1446 |
| - fmt::Display::fmt(&self.inner, formatter) |
| 1472 | +/// Helper struct for safely printing an [`OsStr`] with [`format!`] and `{}`. |
| 1473 | +/// |
| 1474 | +/// An [`OsStr`] might contain non-Unicode data. This `struct` implements the |
| 1475 | +/// [`Display`] trait in a way that mitigates that. It is created by the |
| 1476 | +/// [`display`](OsStr::display) method on [`OsStr`]. This may perform lossy |
| 1477 | +/// conversion, depending on the platform. If you would like an implementation |
| 1478 | +/// which escapes the [`OsStr`] please use [`Debug`] instead. |
| 1479 | +/// |
| 1480 | +/// # Examples |
| 1481 | +/// |
| 1482 | +/// ``` |
| 1483 | +/// #![feature(os_str_display)] |
| 1484 | +/// use std::ffi::OsStr; |
| 1485 | +/// |
| 1486 | +/// let s = OsStr::new("Hello, world!"); |
| 1487 | +/// println!("{}", s.display()); |
| 1488 | +/// ``` |
| 1489 | +/// |
| 1490 | +/// [`Display`]: fmt::Display |
| 1491 | +/// [`format!`]: crate::format |
| 1492 | +#[unstable(feature = "os_str_display", issue = "120048")] |
| 1493 | +pub struct Display<'a> { |
| 1494 | + os_str: &'a OsStr, |
| 1495 | +} |
| 1496 | + |
| 1497 | +#[unstable(feature = "os_str_display", issue = "120048")] |
| 1498 | +impl fmt::Debug for Display<'_> { |
| 1499 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1500 | + fmt::Debug::fmt(&self.os_str, f) |
| 1501 | + } |
| 1502 | +} |
| 1503 | + |
| 1504 | +#[unstable(feature = "os_str_display", issue = "120048")] |
| 1505 | +impl fmt::Display for Display<'_> { |
| 1506 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1507 | + fmt::Display::fmt(&self.os_str.inner, f) |
1447 | 1508 | }
|
1448 | 1509 | }
|
1449 | 1510 |
|
|
0 commit comments