Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cost-free conversion from paths to &str #93

Merged
merged 10 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
- 1.63
- nightly-2022-12-14
- 1.68
- 1.74
- stable
exclude:
# These versions started failing with "archive member 'lib.rmeta' with length 26456 is not
Expand Down
6 changes: 6 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn main() {
println!("cargo:rustc-check-cfg=cfg(path_buf_capacity)");
println!("cargo:rustc-check-cfg=cfg(shrink_to)");
println!("cargo:rustc-check-cfg=cfg(try_reserve_2)");
println!("cargo:rustc-check-cfg=cfg(os_str_bytes)");

let compiler = match rustc_version() {
Some(compiler) => compiler,
Expand Down Expand Up @@ -49,6 +50,11 @@ fn main() {
{
println!("cargo:rustc-cfg=path_buf_deref_mut");
}
// os_str_bytes was added in a 1.74 stable.
if (compiler.minor >= 74 && compiler.channel == ReleaseChannel::Stable) || compiler.minor >= 75
{
println!("cargo:rustc-cfg=os_str_bytes");
}

// Catch situations where the actual features aren't enabled. Currently, they're only shown with
// `-vv` output, but maybe that will be noticed.
Expand Down
25 changes: 19 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,10 @@ impl Utf8Path {
/// the current directory.
///
/// * On Unix, a path is absolute if it starts with the root, so
/// `is_absolute` and [`has_root`] are equivalent.
/// `is_absolute` and [`has_root`] are equivalent.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy complains if I don't comply to the newly added rule in 1.80.0: https://github.com/camino-rs/camino/actions/runs/10400509135/job/28801185038?pr=93#step:4:150

///
/// * On Windows, a path is absolute if it has a prefix and starts with the
/// root: `c:\windows` is absolute, while `c:temp` and `\temp` are not.
/// root: `c:\windows` is absolute, while `c:temp` and `\temp` are not.
///
/// # Examples
///
Expand Down Expand Up @@ -3067,9 +3067,22 @@ impl_cmp_os_str!(&'a Utf8Path, OsString);
// invariant: OsStr must be guaranteed to be utf8 data
#[inline]
unsafe fn str_assume_utf8(string: &OsStr) -> &str {
// Adapted from the source code for Option::unwrap_unchecked.
match string.to_str() {
Some(val) => val,
None => std::hint::unreachable_unchecked(),
#[cfg(os_str_bytes)]
{
// SAFETY: OsStr is guaranteed to be utf8 data from the invariant
unsafe {
std::str::from_utf8_unchecked(
#[allow(clippy::incompatible_msrv)]
string.as_encoded_bytes(),
)
}
}
#[cfg(not(os_str_bytes))]
{
// Adapted from the source code for Option::unwrap_unchecked.
match string.to_str() {
Some(val) => val,
None => std::hint::unreachable_unchecked(),
}
}
}
Loading