pub trait PathExt {
    fn to_slash(&self) -> Option<String>;
    fn to_slash_lossy(&self) -> String;
}
Expand description

Trait to extend std::path::Path.

use path_slash::PathExt;

assert_eq!(
    std::path::Path::new("foo").to_slash(),
    Some("foo".to_string()),
);

Required Methods

Implementations on Foreign Types

Convert the file path into slash path as UTF-8 string.

Any file path separators in the file path is replaced with ‘/’. Any non-Unicode sequences are replaced with U+FFFD.

On non-Windows OS, it is equivalent to to_string_lossy().to_string()

use std::path::Path;
use path_slash::PathExt;

#[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".to_string());

Convert the file path into slash path as UTF-8 string.

Any file path separators in the file path is replaced with ‘/’. When the path contains non-Unicode sequence, this method returns None.

On non-Windows OS, it is equivalent to .to_str().map(str::to_string)

use std::path::Path;
use path_slash::PathExt;

#[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("foo/bar/piyo.txt".to_string()));

Implementors