Skip to content

Commit a17b017

Browse files
committed
Implemented Path::to_string_lossy
1 parent ea43d7f commit a17b017

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/path/path.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,31 @@ impl Path {
684684
pub fn to_str(&self) -> Option<&str> {
685685
self.inner.to_str()
686686
}
687+
688+
/// Converts a `Path` to a [`Cow<str>`].
689+
///
690+
/// Any non-Unicode sequences are replaced with
691+
/// [`U+FFFD REPLACEMENT CHARACTER`][U+FFFD].
692+
///
693+
/// [`Cow<str>`]: https://doc.rust-lang.org/std/borrow/enum.Cow.html
694+
/// [U+FFFD]: https://doc.rust-lang.org/std/char/constant.REPLACEMENT_CHARACTER.html
695+
///
696+
/// # Examples
697+
///
698+
/// Calling `to_string_lossy` on a `Path` with valid unicode:
699+
///
700+
/// ```
701+
/// use async_std::path::Path;
702+
///
703+
/// let path = Path::new("foo.txt");
704+
/// assert_eq!(path.to_string_lossy(), "foo.txt");
705+
/// ```
706+
///
707+
/// Had `path` contained invalid unicode, the `to_string_lossy` call might
708+
/// have returned `"fo�.txt"`.
709+
pub fn to_string_lossy(&self) -> std::borrow::Cow<'_, str> {
710+
self.inner.to_string_lossy()
711+
}
687712
}
688713

689714
impl<'a> From<&'a std::path::Path> for &'a Path {

0 commit comments

Comments
 (0)