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;
}
Expand description

Trait to extend std::borrow::Cow.

use path_slash::{CowExt as _, PathExt as _};

assert_eq!(
    Cow::from_slash("foo/bar/piyo.txt").to_slash_lossy(),
    "foo/bar/piyo.txt",
);

Required Methods

Implementations on Foreign Types

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;

#[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"),
);

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.

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;

#[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"),
);

Convert the OsStr 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.

Implementors