[][src]Trait path_slash::PathBufExt

pub trait PathBufExt {
    pub fn from_slash<S: AsRef<str>>(s: S) -> Self;
pub fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self;
pub fn to_slash(&self) -> Option<String>;
pub fn to_slash_lossy(&self) -> String; }

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

pub fn from_slash<S: AsRef<str>>(s: S) -> Self[src]

pub fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self[src]

pub fn to_slash(&self) -> Option<String>[src]

pub fn to_slash_lossy(&self) -> String[src]

Loading content...

Implementations on Foreign Types

impl PathBufExt for PathBuf[src]

pub fn from_slash<S: AsRef<str>>(s: S) -> Self[src]

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"));

pub fn from_slash_lossy<S: AsRef<OsStr>>(s: S) -> Self[src]

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"));

pub fn to_slash_lossy(&self) -> String[src]

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());

pub fn to_slash(&self) -> Option<String>[src]

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()));
Loading content...

Implementors

Loading content...