path_slash

Trait CowExt

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

Trait to extend Cow.

use path_slash::CowExt as _;

assert_eq!(
    Cow::from_slash("foo/bar/piyo.txt").to_slash_lossy(),
    "foo/bar/piyo.txt",
);

Required Methods§

Source

fn from_slash(s: &'a str) -> Self

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

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

use path_slash::CowExt as _;

#[cfg(not(target_os = "windows"))]
assert_eq!(
    Cow::from_slash("foo/bar/piyo.txt"),
    Path::new("foo/bar/piyo.txt"),
);

#[cfg(target_os = "windows")]
assert_eq!(
    Cow::from_slash("foo/bar/piyo.txt"),
    Path::new(r"foo\\bar\\piyo.txt"),
);
Source

fn from_slash_lossy(s: &'a OsStr) -> Self

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

Any ‘/’ in the slash path is replaced with the file path separator. Heap allocation may 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, there is no loss while conversion.

Source

fn from_backslash(s: &'a str) -> Self

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

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

use path_slash::CowExt as _;

#[cfg(not(target_os = "windows"))]
assert_eq!(
    Cow::from_backslash(r"foo\\bar\\piyo.txt"),
    Path::new("foo/bar/piyo.txt"),
);

#[cfg(target_os = "windows")]
assert_eq!(
    Cow::from_backslash(r"foo\\bar\\piyo.txt"),
    Path::new(r"foo\\bar\\piyo.txt"),
);
Source

fn from_backslash_lossy(s: &'a OsStr) -> Self

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

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

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

use path_slash::CowExt as _;

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

#[cfg(not(target_os = "windows"))]
let s = Cow::Borrowed(Path::new("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::CowExt as _;

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

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

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a> CowExt<'a> for Cow<'a, Path>

Source§

fn from_slash(s: &'a str) -> Self

Source§

fn from_slash_lossy(s: &'a OsStr) -> Self

Source§

fn from_backslash(s: &'a str) -> Self

Source§

fn from_backslash_lossy(s: &'a OsStr) -> Self

Source§

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

Source§

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

Implementors§