Trait path_slash::PathBufExt
source · [−]pub trait PathBufExt {
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<String>;
fn to_slash_lossy(&self) -> String;
}
Expand description
Trait to extend std::path::PathBuf
.
use path_slash::PathBufExt;
assert_eq!(
std::path::PathBuf::from_slash("foo/bar/piyo.txt").to_slash(),
Some("foo/bar/piyo.txt".to_string()),
);
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_lossy(&self) -> String
Implementations on Foreign Types
sourceimpl PathBufExt for PathBuf
impl PathBufExt for PathBuf
sourcefn from_slash<S: AsRef<str>>(s: S) -> Self
fn from_slash<S: AsRef<str>>(s: S) -> Self
Convert the slash path (path separated with ‘/’) to std::path::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 other OSes are the same as ‘/’.
On non-Windows OS, it is simply equivalent to std::path::PathBuf::from
.
use std::path::PathBuf;
use path_slash::PathBufExt;
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"));
sourcefn from_backslash<S: AsRef<str>>(s: S) -> Self
fn from_backslash<S: AsRef<str>>(s: S) -> Self
Convert the backslash path (path separated with ‘\’) to std::path::PathBuf
.
Any ‘\’ in the slash path is replaced with the file path separator. The replacements only happen on non-Windows.
sourcefn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self
fn from_backslash_lossy<S: AsRef<OsStr>>(s: S) -> Self
Convert the backslash path (path separated with ‘\’) to std::path::PathBuf
.
Any ‘\’ in the slash path is replaced with the file path separator.
sourcefn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self
fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self
Convert the slash path (path separated with ‘/’) to std::path::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 other OSes 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 std::path::PathBuf::from
and there is no
loss while conversion.
use std::ffi::OsStr;
use std::path::PathBuf;
use path_slash::PathBufExt;
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"));
sourcefn to_slash_lossy(&self) -> String
fn to_slash_lossy(&self) -> String
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 path_slash::PathBufExt;
#[cfg(target_os = "windows")]
let s = std::path::PathBuf::from(r"foo\bar\piyo.txt");
#[cfg(not(target_os = "windows"))]
let s = std::path::PathBuf::from("foo/bar/piyo.txt");
assert_eq!(s.to_slash_lossy(), "foo/bar/piyo.txt".to_string());
sourcefn to_slash(&self) -> Option<String>
fn to_slash(&self) -> Option<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(std::to_string())
use path_slash::PathBufExt;
#[cfg(target_os = "windows")]
let s = std::path::PathBuf::from(r"foo\bar\piyo.txt");
#[cfg(not(target_os = "windows"))]
let s = std::path::PathBuf::from("foo/bar/piyo.txt");
assert_eq!(s.to_slash(), Some("foo/bar/piyo.txt".to_string()));