Trait path_slash::CowExt
source · [−]pub trait CowExt<'a> {
fn from_slash(s: &'a str) -> Self;
fn from_slash_lossy(s: &'a OsStr) -> Self;
fn from_backslash(s: &'a str) -> Self;
fn from_backslash_lossy(s: &'a OsStr) -> Self;
fn to_slash(&self) -> Option<Cow<'_, str>>;
fn to_slash_lossy(&self) -> Cow<'_, str>;
}
Expand description
Trait to extend Cow
.
use path_slash::CowExt as _;
assert_eq!(
Cow::from_slash("foo/bar/piyo.txt").to_slash_lossy(),
"foo/bar/piyo.txt",
);
Required Methods
fn from_slash(s: &'a str) -> Self
fn from_slash(s: &'a str) -> Self
Convert the slash path (path separated with ‘/’) to Cow
.
Any ‘/’ in the slash path is replaced with the file path separator. Heap allocation may only happen on Windows since the file path separators on Unix-like OS are the same as ‘/’.
use path_slash::CowExt as _;
#[cfg(not(target_os = "windows"))]
assert_eq!(
Cow::from_slash("foo/bar/piyo.txt"),
Path::new("foo/bar/piyo.txt"),
);
#[cfg(target_os = "windows")]
assert_eq!(
Cow::from_slash("foo/bar/piyo.txt"),
Path::new(r"foo\\bar\\piyo.txt"),
);
fn from_slash_lossy(s: &'a OsStr) -> Self
fn from_slash_lossy(s: &'a OsStr) -> Self
Convert the OsStr
slash path (path separated with ‘/’) to Cow
.
Any ‘/’ in the slash path is replaced with the file path separator. Heap allocation may only happen on Windows since the file path separators on Unix-like OS are the same as ‘/’.
On Windows, any non-Unicode sequences are replaced with U+FFFD while the conversion. On non-Windows OS, there is no loss while conversion.
fn from_backslash(s: &'a str) -> Self
fn from_backslash(s: &'a str) -> Self
Convert the backslash path (path separated with ‘\’) to Cow
.
Any ‘\’ in the slash path is replaced with the file path separator. Heap allocation may only happen on non-Windows.
use path_slash::CowExt as _;
#[cfg(not(target_os = "windows"))]
assert_eq!(
Cow::from_backslash(r"foo\\bar\\piyo.txt"),
Path::new("foo/bar/piyo.txt"),
);
#[cfg(target_os = "windows")]
assert_eq!(
Cow::from_backslash(r"foo\\bar\\piyo.txt"),
Path::new(r"foo\\bar\\piyo.txt"),
);
fn from_backslash_lossy(s: &'a OsStr) -> Self
fn from_backslash_lossy(s: &'a OsStr) -> Self
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 ‘/’.
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 sequences, this method returns None
.
use path_slash::CowExt as _;
#[cfg(target_os = "windows")]
let s = Cow::Borrowed(Path::new(r"foo\bar\piyo.txt"));
#[cfg(not(target_os = "windows"))]
let s = Cow::Borrowed(Path::new("foo/bar/piyo.txt"));
assert_eq!(s.to_slash(), Some(Cow::Borrowed("foo/bar/piyo.txt")));
fn to_slash_lossy(&self) -> Cow<'_, str>
fn to_slash_lossy(&self) -> Cow<'_, str>
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 ‘/’.
Any file path separators in the file path are replaced with ‘/’. Any non-Unicode sequences are replaced with U+FFFD.
use path_slash::CowExt as _;
#[cfg(target_os = "windows")]
let s = Cow::Borrowed(Path::new(r"foo\bar\piyo.txt"));
#[cfg(not(target_os = "windows"))]
let s = Cow::Borrowed(Path::new("foo/bar/piyo.txt"));
assert_eq!(s.to_slash_lossy(), "foo/bar/piyo.txt");