camino-rs / camino

Like Rust's std::path::Path, but UTF-8.

Home Page:https://docs.rs/camino

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hash implementation of Utf8PathBuf doesn't match Utf8Path

CryZe opened this issue · comments

Utf8PathBuf implements Borrow<Utf8Path> but the following invariant of Borrow does not hold:

In particular Eq, Ord and Hash must be equivalent for borrowed and owned values: x.borrow() == y.borrow() should give the same result as x == y.

That means that Utf8PathBuf and Utf8Path of the same content (i.e. partial_cmp returning Same) need to Hash the same. This does not currently seem to be true:

[src/main.rs:10] hash(camino::Utf8Path::new("db4")) = 2500477520987573830
[src/main.rs:14] hash(camino::Utf8PathBuf::from("db4")) = 12412650977873828595

with

fn hash(value: impl Hash) -> u64 {
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    value.hash(&mut hasher);
    hasher.finish()
}
commented

Whoops, will fix ASAP!

commented

Fixed in camino 1.0.4, out now.

This was a complete oversight on my end -- I'm familiar with the Borrow contract but neglected to write tests for it. Thank you so much for reporting this @CryZe!