You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::{
ffi::OsStr,
path::Path,
collections::hash_map::DefaultHasher,
hash::{Hash,Hasher},};fnhash<T: ?Sized + Hash>(v:&T) -> u64{letmut sh = DefaultHasher::new();
v.hash(&mut sh);
sh.finish()}fnmain(){let s = OsStr::new("/dev/null");let p = Path::new("/dev/null");assert_eq!(s, p);assert!(
hash(s) == hash(p),"Hashes differ for values that compare as equal");}
The problem is that the provided impls of PartialEq, while convenient, cross the data domains and break the intuition on matching behavior of Hash and ==. This does not affect HashMap due to lack of a cross-type Borrow impl, but the inconsistency can bite the unwary.
The text was updated successfully, but these errors were encountered:
Enselic
added
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
and removed
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
labels
Nov 20, 2023
Enselic
changed the title
impl PartialEq<OsStr> for Path and similar impls violate Hash consistencyEq + Hash rule is broken when mixing OsStr and PathNov 20, 2023
Triage: Maybe all that's needed to close this issue is adding some clarification in the docs that the rule only applies to one type, and not across different (but related) types?
Uh oh!
There was an error while loading. Please reload this page.
This program surprisingly fails:
The problem is that the provided impls of
PartialEq
, while convenient, cross the data domains and break the intuition on matching behavior ofHash
and==
. This does not affectHashMap
due to lack of a cross-typeBorrow
impl, but the inconsistency can bite the unwary.The text was updated successfully, but these errors were encountered: