docs

你正在阅读 v0.1.2 的文档。Nabi 0.1.2 已不再提供支持。 最新版本:v0.1.3 →

本页尚未翻译,以下为英文原文。

#3.7. path:: - UTF-8 Path (Python-style)

本页内容

The std::path module defines path representation. The key point is that it puts a UTF-8 newtype first, to avoid the ergonomic burden of Rust's split between Path and OsStr while adopting the intuitiveness of Python's pathlib.Path.

#3.7.1. Two types - Path / RawPath

Nabi's path representation is split into two types.

TypeMeaningFrequency of use
PathUTF-8 path (95%+)Default for ordinary code
RawPathOS native bytes (Unix bytes / Windows WTF-16)1% (non-UTF-8 file names)

Most code needs only Path. RawPath is needed only where legacy bytes are truly required, such as when dealing with old unix systems.

#3.7.2. Path - UTF-8

Path is an immutable value. join, / and with_* all return a new Path; there is no PathBuf / push that modifies a path in place. p /= q is p = p / q under the ordinary compound assignment rule.

The Path interface is as follows.

#3.7.3. The / operator - Python pathlib style

The / operator is introduced to make path joining more readable.

nabi
impl Div<str> for Path {
    type Output = Path;
    #[infix("/")]
    fn div(self, rhs: str) -> Path { self.join(Path::new(rhs)) }
}

impl Div<Path> for Path { ... }

impl From<str> for Path { ... }   // Path::new
impl Display for Path { ... }     // the text: /etc/nginx
impl Debug for Path { ... }       // the quoted text: "/etc/nginx"

p /= "x" and p /= q (q: Path) are also allowed and mean p = p / ...

The effect is clear from the following example.

nabi
let p = Path::new("/etc") / "nginx" / "nginx.conf";
                                          //→ "/etc/nginx/nginx.conf"

let home = env::home_dir()? / ".config" / "myapp" / "settings.toml";

This provides the same ergonomics as Python's Path("/etc") / "nginx" / "nginx.conf", and avoids the verbosity of Rust's PathBuf::join and push.

#3.7.3a. AsPath - path arguments

nabi
trait AsPath {
    fn as_path(self) -> Path;
}
impl AsPath for str { ... }
impl AsPath for Path { ... }

Path arguments of fs are taken as AsPath, so both str and Path can be passed (fs::read("a.txt"), fs::read(dir / "a.txt")).

#3.7.4. UTF-8 invariant

Path is always UTF-8. Path::new(s) takes a str, which is already UTF-8, so it converts and validates nothing.

The OS can hold names that are not UTF-8: arbitrary bytes on Unix, unpaired surrogates in Windows' UTF-16. fs and env never convert such a name lossily: a call that would return one as a Path throws IoError with InvalidData. A RawPath (3.7.5), built from OS bytes the code got elsewhere (through FFI, for one), converts explicitly:

nabi
let raw: RawPath = ...;
let p: Path = try Path::try_from(raw);    // throws Utf8Error if not UTF-8
let q: Path = try raw.try_into();          // same

This conversion is impl TryFrom<RawPath> for Path { type Error = Utf8Error; ... } (2.11.4).

#3.7.5. RawPath - bytes / WTF-16

RawPath exists for places that need the OS's native representation as is.

nabi
struct RawPath { /* windows: List<u16>, elsewhere: List<u8> */ }   // + Clone, PartialEq, Eq, Hash, Debug

impl RawPath {
    fn from_bytes<B: AsBytes>(bytes: B) -> RawPath;    // every target but windows
    fn from_wide(wide: Slice<u16>) -> RawPath;         // windows

    fn as_bytes(self) -> Slice<u8>;                    // every target but windows
    fn as_wide(self) -> Slice<u16>;                    // windows only

    fn to_path(self) throws (Utf8Error) -> Path;       // as RawStr::to_str
    fn to_path_lossy(self) -> Path;
}

to_path throws Utf8Error (2.15.6), the error of str::from_utf8 and RawStr::to_str; on Windows its valid_up_to() counts UTF-16 units. to_path_lossy replaces each maximal ill-formed subsequence (Unix) / each lone surrogate (Windows) with one U+FFFD.

Most code never encounters this type. It appears only where code exchanges syscalls directly with the OS.

#3.7.6. canonicalize vs. normalize

These two functions look similar but differ clearly in behavior and cost.

FunctionMeaningFile system access
normalize()Purely lexical cleanup (resolves "..")No
canonicalize()Resolves symlinks + converts to an absolute pathYes (realpath)
nabi
Path::new("/a/b/../c").normalize();              //→ "/a/c" (lexical)
try Path::new("/a/b/../c").canonicalize();       //→ absolute path with symlinks resolved

normalize is fast and makes no syscalls, so it does the job in 90% of places.

#3.7.7. WASI compatibility

Path also integrates naturally with WASI Preview 2's capability paths.

nabi
let dir: Dir = try Dir::root().open_dir("data");
let f = try dir.open(Path::new("config.toml"));
                                          // Path is relative to dir; escape rejected

The capability model is covered in detail in 3.1 fs.

#3.7.8. Comparison with other languages

To pin down the design decision, compare with neighboring languages.

AspectRustPython pathlibGo filepathNabi
TypePath / PathBufPathstringPath
owned vs viewborrowed &Path / ownedimmutableimmutableimmutable (/ gives a new Path)
/ operatornone (explicit .join())/none/
non-UTF-8OsStr (mixed)bytes (separate)string (confusing)separate RawPath
canonicalize.canonicalize().resolve()filepath.Abs.canonicalize()
Method chainingOK (mostly)excellentOKexcellent

In summary, Nabi's biggest decision is the combination of a UTF-8 default and the / operator, which brings Python pathlib's ergonomics together with Rust-level performance.

Found a mistake? Edit this page on GitHub. Last updated