Implement 'Eq' for 'MediaType', 'ContentType'.

This also fixes the 'Hash' implementation to match the docs.

Resolves #2132.
This commit is contained in:
Sergio Benitez 2022-04-19 13:58:30 -07:00
parent 38bd5663c2
commit 4d83f73f86
2 changed files with 10 additions and 11 deletions

View File

@ -40,7 +40,7 @@ use crate::ext::IntoCollection;
///
/// let response = Response::build().header(ContentType::HTML).finalize();
/// ```
#[derive(Debug, Clone, PartialEq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ContentType(pub MediaType);
macro_rules! content_types {

View File

@ -467,13 +467,15 @@ impl MediaType {
/// Compares `self` with `other` and returns `true` if `self` and `other`
/// are exactly equal to each other, including with respect to their
/// parameters.
/// parameters and their order.
///
/// This is different from the `PartialEq` implementation in that it
/// considers parameters. If `PartialEq` returns false, this function is
/// guaranteed to return false. Similarly, if this function returns `true`,
/// `PartialEq` is guaranteed to return true. However, if `PartialEq`
/// returns `true`, this function may or may not return `true`.
/// considers parameters. In particular, `Eq` implies `PartialEq` but
/// `PartialEq` does not imply `Eq`. That is, if `PartialEq` returns false,
/// this function is guaranteed to return false. Similarly, if `exact_eq`
/// returns `true`, `PartialEq` is guaranteed to return true. However, if
/// `PartialEq` returns `true`, `exact_eq` function may or may not return
/// `true`.
///
/// # Example
///
@ -572,16 +574,13 @@ impl PartialEq for MediaType {
}
}
impl Eq for MediaType { }
impl Hash for MediaType {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.top().hash(state);
self.sub().hash(state);
for (key, val) in self.params() {
key.hash(state);
val.hash(state);
}
}
}