From c98e5ff9123f83a58d069f5e0219482549f69427 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 6 Aug 2022 18:20:14 +0900 Subject: [PATCH] fix `PathBufExt` document and use `as _` to import extension traits --- src/lib.rs | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6f48bf1..5a4aac3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,7 +72,7 @@ use std::path::{Path, PathBuf, MAIN_SEPARATOR}; #[cfg(target_os = "windows")] mod windows { use super::*; - use std::os::windows::ffi::OsStrExt; + use std::os::windows::ffi::OsStrExt as _; // Workaround for Windows. There is no way to extract raw byte sequence from `OsStr` (in `Path`). // And `OsStr::to_string_lossy` may cause extra heap allocation. @@ -237,7 +237,7 @@ impl PathExt for Path { /// /// ``` /// # use std::path::PathBuf; -/// use path_slash::PathBufExt; +/// use path_slash::PathBufExt as _; /// /// assert_eq!( /// PathBuf::from_slash("foo/bar/piyo.txt").to_slash().unwrap(), @@ -255,7 +255,7 @@ pub trait PathBufExt { /// /// ``` /// # use std::path::PathBuf; - /// use path_slash::PathBufExt; + /// use path_slash::PathBufExt as _; /// /// let p = PathBuf::from_slash("foo/bar/piyo.txt"); /// @@ -279,7 +279,7 @@ pub trait PathBufExt { /// ``` /// # use std::path::PathBuf; /// # use std::ffi::OsStr; - /// use path_slash::PathBufExt; + /// use path_slash::PathBufExt as _; /// /// let s: &OsStr = "foo/bar/piyo.txt".as_ref(); /// let p = PathBuf::from_slash_lossy(s); @@ -301,14 +301,16 @@ pub trait PathBufExt { /// Any '\\' in the slash path is replaced with the file path separator. fn from_backslash_lossy>(s: S) -> Self; /// Convert the file path into slash path as UTF-8 string. This method is similar to - /// [`Path::to_string_lossy`], but the path separator is fixed to '/'. + /// [`Path::to_str`], but the path separator is fixed to '/'. /// - /// Any file path separators in the file path are replaced with '/'. - /// Any non-Unicode sequences are replaced with U+FFFD. + /// Any file path separators in the file path are replaced with '/'. Only when the replacement + /// happens, heap allocation happens and `Cow::Owned` is returned. + /// When the path contains non-Unicode sequence, this method returns None. /// /// ``` /// # use std::path::PathBuf; - /// use path_slash::PathBufExt; + /// # use std::borrow::Cow; + /// use path_slash::PathBufExt as _; /// /// #[cfg(target_os = "windows")] /// let s = PathBuf::from(r"foo\bar\piyo.txt"); @@ -316,20 +318,18 @@ pub trait PathBufExt { /// #[cfg(not(target_os = "windows"))] /// let s = PathBuf::from("foo/bar/piyo.txt"); /// - /// assert_eq!(s.to_slash_lossy(), "foo/bar/piyo.txt"); + /// assert_eq!(s.to_slash(), Some(Cow::Borrowed("foo/bar/piyo.txt"))); /// ``` fn to_slash(&self) -> Option>; /// Convert the file path into slash path as UTF-8 string. This method is similar to - /// [`Path::to_str`], but the path separator is fixed to '/'. + /// [`Path::to_string_lossy`], but the path separator is fixed to '/'. /// - /// Any file path separators in the file path are replaced with '/'. Only when the replacement - /// happens, heap allocation happens and `Cow::Owned` is returned. - /// When the path contains non-Unicode sequence, this method returns None. + /// Any file path separators in the file path are replaced with '/'. + /// Any non-Unicode sequences are replaced with U+FFFD. /// /// ``` /// # use std::path::PathBuf; - /// # use std::borrow::Cow; - /// use path_slash::PathBufExt; + /// use path_slash::PathBufExt as _; /// /// #[cfg(target_os = "windows")] /// let s = PathBuf::from(r"foo\bar\piyo.txt"); @@ -337,7 +337,7 @@ pub trait PathBufExt { /// #[cfg(not(target_os = "windows"))] /// let s = PathBuf::from("foo/bar/piyo.txt"); /// - /// assert_eq!(s.to_slash(), Some(Cow::Borrowed("foo/bar/piyo.txt"))); + /// assert_eq!(s.to_slash_lossy(), "foo/bar/piyo.txt"); /// ``` fn to_slash_lossy(&self) -> Cow<'_, str>; } @@ -379,13 +379,13 @@ impl PathBufExt for PathBuf { PathBuf::from(s.as_ref()) } - fn to_slash_lossy(&self) -> Cow<'_, str> { - self.as_path().to_slash_lossy() - } - fn to_slash(&self) -> Option> { self.as_path().to_slash() } + + fn to_slash_lossy(&self) -> Cow<'_, str> { + self.as_path().to_slash_lossy() + } } /// Trait to extend [`Cow`]. @@ -409,7 +409,7 @@ pub trait CowExt<'a> { /// ``` /// # use std::borrow::Cow; /// # use std::path::Path; - /// use path_slash::CowExt; + /// use path_slash::CowExt as _; /// /// #[cfg(not(target_os = "windows"))] /// assert_eq!( @@ -441,7 +441,7 @@ pub trait CowExt<'a> { /// ``` /// # use std::borrow::Cow; /// # use std::path::Path; - /// use path_slash::CowExt; + /// use path_slash::CowExt as _; /// /// #[cfg(not(target_os = "windows"))] /// assert_eq!( @@ -471,7 +471,7 @@ pub trait CowExt<'a> { /// ``` /// # use std::path::Path; /// # use std::borrow::Cow; - /// use path_slash::CowExt; + /// use path_slash::CowExt as _; /// /// #[cfg(target_os = "windows")] /// let s = Cow::Borrowed(Path::new(r"foo\bar\piyo.txt")); @@ -491,7 +491,7 @@ pub trait CowExt<'a> { /// ``` /// # use std::path::Path; /// # use std::borrow::Cow; - /// use path_slash::CowExt; + /// use path_slash::CowExt as _; /// /// #[cfg(target_os = "windows")] /// let s = Cow::Borrowed(Path::new(r"foo\bar\piyo.txt"));