pub trait PathExt {
    fn to_slash(&self) -> Option<Cow<'_, str>>;
    fn to_slash_lossy(&self) -> Cow<'_, str>;
}
Expand description

Trait to extend Path.

use path_slash::PathExt as _;

assert_eq!(
    Path::new("foo").to_slash(),
    Some(Cow::Borrowed("foo")),
);

Required Methods

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 sequence, this method returns None.

use path_slash::PathExt as _;

#[cfg(target_os = "windows")]
let s = Path::new(r"foo\bar\piyo.txt");

#[cfg(not(target_os = "windows"))]
let s = Path::new("foo/bar/piyo.txt");

assert_eq!(s.to_slash(), Some(Cow::Borrowed("foo/bar/piyo.txt")));

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::PathExt as _;

#[cfg(target_os = "windows")]
let s = Path::new(r"foo\bar\piyo.txt");

#[cfg(not(target_os = "windows"))]
let s = Path::new("foo/bar/piyo.txt");

assert_eq!(s.to_slash_lossy(), "foo/bar/piyo.txt");

Implementations on Foreign Types

Implementors