Skip to main content

Utf8Encoder

Trait Utf8Encoder 

Source
pub trait Utf8Encoder {
    // Required method
    fn to_utf8_string(&self) -> String;

    // Provided method
    fn to_utf8_path(&self) -> PathBuf { ... }
}

Required Methods§

Provided Methods§

Source

fn to_utf8_path(&self) -> PathBuf

Converts a byte sequence into a PathBuf using the same decoding rules as Self::to_utf8_string.

Valid UTF-8 runs pass through unchanged; other bytes are mapped through the CHARS fallback table. This is lossy for paths containing non-UTF-8 bytes.

To preserve the original bytes exactly (e.g. when opening an existing file with a non-UTF-8 name on Unix), do not use this method. Convert the bytes directly into an OsString via a platform-specific API such as std::os::unix::ffi::OsStringExt::from_vec, then into a PathBuf.

§Example
use std::path::PathBuf;
use ps_str::Utf8Encoder;

let bytes = b"hello.txt";

assert_eq!(bytes.to_utf8_path(), PathBuf::from("hello.txt"));

Implementors§

Source§

impl<T: AsRef<[u8]>> Utf8Encoder for T