This repository contains the source code for camino, an extension of the std::path module that adds new Utf8PathBuf and Utf8Path types.
camino‘s Utf8PathBuf and Utf8Path types are like the standard library’s PathBuf and Path types, except they are guaranteed to only contain UTF-8 encoded data. Therefore, they expose the ability to get their contents as strings, they implement Display, etc.
The std::path types are not guaranteed to be valid UTF-8. This is the right decision for the standard library, since it must be as general as possible. However, on all platforms, non-Unicode paths are vanishingly uncommon for a number of reasons:
Cargo.toml) that lists the names of other files, how should the names in the Makefile be matched with the ones on disk? This has no general, cross-platform solution in systems that support non-UTF-8 paths. However, restricting paths to UTF-8 eliminates this problem.Therefore, many programs that want to manipulate paths do assume they contain UTF-8 data, and convert them to strs as necessary. However, because this invariant is not encoded in the Path type, conversions such as path.to_str().unwrap() need to be repeated again and again, creating a frustrating experience.
Instead, camino allows you to check that your paths are UTF-8 once, and then manipulate them as valid UTF-8 from there on, avoiding repeated lossy and confusing conversions.
The documentation for Utf8PathBuf and Utf8Path contains several examples.
For examples of how to use camino with other libraries like serde and clap, see the camino-examples directory.
camino is a very thin wrapper around std::path. Utf8Path and Utf8PathBuf are drop-in replacements for Path and PathBuf.
Most APIs are the same, but those at the boundary with str are different. Some examples:
Path::to_str() -> Option<&str> has been renamed to Utf8Path::as_str() -> &str.Utf8Path implements Display, and Path::display() has been removed.Utf8Path returns &str, not &OsStr.Every Utf8Path is a valid Path, so Utf8Path implements AsRef<Path>. Any APIs that accept impl AsRef<Path> will continue to work with Utf8Path instances.
camino trades off some utility for a great deal of simplicity. Whether camino is appropriate for a project or not is ultimately a case-by-case decision. Here are some general guidelines that may help.
You should consider using camino if...
In general, using camino is the right choice for most projects.
You should NOT use camino, if...
mv or cat replacement, you should not use camino. Instead, use std::path::Path and add extensive tests for non-UTF-8 paths.Vec<u8>.std::path::Path supports arbitrary bytestrings on Unix but not on Windows.By default, camino has no dependencies other than std. There are some optional features that enable dependencies:
serde1 adds serde Serialize and Deserialize impls for Utf8PathBuf and Utf8Path (zero-copy).proptest1 adds proptest Arbitrary implementations for Utf8PathBuf and Box<Utf8Path>.NOTE: Enabling the
serdeorproptestfeatures will not do anything. You must enable theserde1andproptest1features, respectively.
The minimum supported Rust version (MSRV) for camino with default features is 1.34. This project is tested in CI against the latest stable version of Rust and the MSRV.
build.rs, or through backfills that also work on older versions.camino is designed to be a core library and has a conservative MSRV policy. MSRV increases will only happen for a compelling enough reason, and will involve at least a minor version bump.
Optional features may pull in dependencies that require a newer version of Rust.
This project is available under the terms of either the Apache 2.0 license or the MIT license.
This project's documentation is adapted from The Rust Programming Language, which is available under the terms of either the Apache 2.0 license or the MIT license.