Skip to content
Open
Changes from all 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
39 changes: 37 additions & 2 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl fmt::Display for PathAndQuery {

impl hash::Hash for PathAndQuery {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.data.hash(state);
self.as_str().hash(state);
}
}

Expand All @@ -301,7 +301,7 @@ impl hash::Hash for PathAndQuery {
impl PartialEq for PathAndQuery {
#[inline]
fn eq(&self, other: &PathAndQuery) -> bool {
self.data == other.data
self.as_str() == other.as_str()
}
}

Expand Down Expand Up @@ -703,6 +703,41 @@ mod tests {
);
}

#[test]
fn empty_and_slash_eq_hash_ord_consistent() {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

fn hash_of(pq: &PathAndQuery) -> u64 {
let mut hasher = DefaultHasher::new();
pq.hash(&mut hasher);
hasher.finish()
}

let slash = PathAndQuery::from_static("/");
let empty = PathAndQuery::empty();
let frag_only = pq("#frag");
let built = crate::Uri::builder()
.scheme("http")
.authority("example.com")
.path_and_query("")
.build()
.unwrap();
let parsed = crate::Uri::from_static("http://example.com/");

for other in [&empty, &frag_only, built.path_and_query().unwrap()] {
assert_eq!(other.as_str(), "/");
assert_eq!(other.partial_cmp(&slash), Some(cmp::Ordering::Equal));
assert_eq!(slash.partial_cmp(other), Some(cmp::Ordering::Equal));
assert_eq!(other, &slash);
assert_eq!(&slash, other);
assert_eq!(hash_of(other), hash_of(&slash));
}

assert_eq!(built, parsed);
assert_eq!(built.path_and_query(), parsed.path_and_query());
}

fn pq(s: &str) -> PathAndQuery {
s.parse().expect(&format!("parsing {}", s))
}
Expand Down