| 
 | 1 | +//! The [`OsStr`] and [`OsString`] types and associated utilities.  | 
 | 2 | +
  | 
1 | 3 | #[cfg(test)]  | 
2 | 4 | mod tests;  | 
3 | 5 | 
 
  | 
@@ -1173,6 +1175,32 @@ impl OsStr {  | 
1173 | 1175 |     pub fn eq_ignore_ascii_case<S: AsRef<OsStr>>(&self, other: S) -> bool {  | 
1174 | 1176 |         self.inner.eq_ignore_ascii_case(&other.as_ref().inner)  | 
1175 | 1177 |     }  | 
 | 1178 | + | 
 | 1179 | +    /// Returns an object that implements [`Display`] for safely printing an  | 
 | 1180 | +    /// [`OsStr`] that may contain non-Unicode data. This may perform lossy  | 
 | 1181 | +    /// conversion, depending on the platform.  If you would like an  | 
 | 1182 | +    /// implementation which escapes the [`OsStr`] please use [`Debug`]  | 
 | 1183 | +    /// instead.  | 
 | 1184 | +    ///  | 
 | 1185 | +    /// [`Display`]: fmt::Display  | 
 | 1186 | +    /// [`Debug`]: fmt::Debug  | 
 | 1187 | +    ///  | 
 | 1188 | +    /// # Examples  | 
 | 1189 | +    ///  | 
 | 1190 | +    /// ```  | 
 | 1191 | +    /// use std::ffi::OsStr;  | 
 | 1192 | +    ///  | 
 | 1193 | +    /// let s = OsStr::new("Hello, world!");  | 
 | 1194 | +    ///  | 
 | 1195 | +    /// println!("{}", s.display());  | 
 | 1196 | +    /// ```  | 
 | 1197 | +    #[unstable(feature = "os_str_display", issue = "120048")]  | 
 | 1198 | +    #[must_use = "this does not display the osstr, \  | 
 | 1199 | +                  it returns an object that can be displayed"]  | 
 | 1200 | +    #[inline]  | 
 | 1201 | +    pub fn display(&self) -> Display<'_> {  | 
 | 1202 | +        Display { os_str: self }  | 
 | 1203 | +    }  | 
1176 | 1204 | }  | 
1177 | 1205 | 
 
  | 
1178 | 1206 | #[stable(feature = "box_from_os_str", since = "1.17.0")]  | 
@@ -1467,9 +1495,42 @@ impl fmt::Debug for OsStr {  | 
1467 | 1495 |     }  | 
1468 | 1496 | }  | 
1469 | 1497 | 
 
  | 
1470 |  | -impl OsStr {  | 
1471 |  | -    pub(crate) fn display(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {  | 
1472 |  | -        fmt::Display::fmt(&self.inner, formatter)  | 
 | 1498 | +/// Helper struct for safely printing an [`OsStr`] with [`format!`] and `{}`.  | 
 | 1499 | +///  | 
 | 1500 | +/// An [`OsStr`] might contain non-Unicode data. This `struct` implements the  | 
 | 1501 | +/// [`Display`] trait in a way that mitigates that. It is created by the  | 
 | 1502 | +/// [`display`](OsStr::display) method on [`OsStr`]. This may perform lossy  | 
 | 1503 | +/// conversion, depending on the platform. If you would like an implementation  | 
 | 1504 | +/// which escapes the [`OsStr`] please use [`Debug`] instead.  | 
 | 1505 | +///  | 
 | 1506 | +/// # Examples  | 
 | 1507 | +///  | 
 | 1508 | +/// ```  | 
 | 1509 | +/// use std::ffi::OsStr;  | 
 | 1510 | +///  | 
 | 1511 | +/// let s = OsStr::new("Hello, world!");  | 
 | 1512 | +///  | 
 | 1513 | +/// println!("{}", s.display());  | 
 | 1514 | +/// ```  | 
 | 1515 | +///  | 
 | 1516 | +/// [`Display`]: fmt::Display  | 
 | 1517 | +/// [`format!`]: crate::format  | 
 | 1518 | +#[unstable(feature = "os_str_display", issue = "120048")]  | 
 | 1519 | +pub struct Display<'a> {  | 
 | 1520 | +    os_str: &'a OsStr,  | 
 | 1521 | +}  | 
 | 1522 | + | 
 | 1523 | +#[unstable(feature = "os_str_display", issue = "120048")]  | 
 | 1524 | +impl fmt::Debug for Display<'_> {  | 
 | 1525 | +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {  | 
 | 1526 | +        fmt::Debug::fmt(&self.os_str, f)  | 
 | 1527 | +    }  | 
 | 1528 | +}  | 
 | 1529 | + | 
 | 1530 | +#[unstable(feature = "os_str_display", issue = "120048")]  | 
 | 1531 | +impl fmt::Display for Display<'_> {  | 
 | 1532 | +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {  | 
 | 1533 | +        fmt::Display::fmt(&self.os_str.inner, f)  | 
1473 | 1534 |     }  | 
1474 | 1535 | }  | 
1475 | 1536 | 
 
  | 
 | 
0 commit comments