Trait path_slash::PathBufExt

source ·
pub trait PathBufExt {
    // Required methods
    fn from_slash<S: AsRef<str>>(s: S) -> Self;
    fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self;
    fn from_backslash<S: AsRef<str>>(s: S) -> Self;
    fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self;
    fn to_slash(&self) -> Option<Cow<'_, str>>;
    fn to_slash_lossy(&self) -> Cow<'_, str>;
}
Expand description

Trait to extend PathBuf.

use path_slash::PathBufExt as _;

assert_eq!(
    PathBuf::from_slash("foo/bar/piyo.txt").to_slash().unwrap(),
    "foo/bar/piyo.txt",
);

Required Methods§

source

fn from_slash<S: AsRef<str>>(s: S) -> Self

Convert the slash path (path separated with ‘/’) to PathBuf.

Any ‘/’ in the slash path is replaced with the file path separator. The replacements only happen on Windows since the file path separators on Unix-like OS are the same as ‘/’.

On non-Windows OS, it is simply equivalent to PathBuf::from.

use path_slash::PathBufExt as _;

let p = PathBuf::from_slash("foo/bar/piyo.txt");

#[cfg(target_os = "windows")]
assert_eq!(p, PathBuf::from(r"foo\bar\piyo.txt"));

#[cfg(not(target_os = "windows"))]
assert_eq!(p, PathBuf::from("foo/bar/piyo.txt"));
source

fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self

Convert the OsStr slash path (path separated with ‘/’) to PathBuf.

Any ‘/’ in the slash path is replaced with the file path separator. The replacements 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, it is simply equivalent to PathBuf::from and there is no loss while conversion.

use path_slash::PathBufExt as _;

let s: &OsStr = "foo/bar/piyo.txt".as_ref();
let p = PathBuf::from_slash_lossy(s);

#[cfg(target_os = "windows")]
assert_eq!(p, PathBuf::from(r"foo\bar\piyo.txt"));

#[cfg(not(target_os = "windows"))]
assert_eq!(p, PathBuf::from("foo/bar/piyo.txt"));
source

fn from_backslash<S: AsRef<str>>(s: S) -> Self

Convert the backslash path (path separated with ‘\’) to PathBuf.

Any ‘\’ in the slash path is replaced with the file path separator. The replacements only happen on non-Windows.

source

fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self

Convert the OsStr backslash path (path separated with ‘\’) to PathBuf.

Any ‘\’ in the slash path is replaced with the file path separator.

source

fn to_slash(&self) -> Option<Cow<'_, str>>

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

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

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

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

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

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

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl PathBufExt for PathBuf

source§

fn from_slash<S: AsRef<str>>(s: S) -> Self

source§

fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self

source§

fn from_backslash<S: AsRef<str>>(s: S) -> Self

source§

fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self

source§

fn to_slash(&self) -> Option<Cow<'_, str>>

source§

fn to_slash_lossy(&self) -> Cow<'_, str>

Implementors§