Skip to content

Commit 660e4db

Browse files
authored
Merge pull request #80 from http-rs/headers-equality
Add equality checks between headers and strings
2 parents cae80cd + 0f266d1 commit 660e4db

File tree

3 files changed

+63
-20
lines changed

3 files changed

+63
-20
lines changed

src/headers/header_name.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ impl<'a> std::convert::TryFrom<&'a str> for HeaderName {
7171
}
7272
}
7373

74+
impl PartialEq<str> for HeaderName {
75+
fn eq(&self, other: &str) -> bool {
76+
self.0 == other
77+
}
78+
}
79+
80+
impl<'a> PartialEq<&'a str> for HeaderName {
81+
fn eq(&self, other: &&'a str) -> bool {
82+
&self.0 == other
83+
}
84+
}
85+
86+
impl PartialEq<String> for HeaderName {
87+
fn eq(&self, other: &String) -> bool {
88+
&self.0 == other
89+
}
90+
}
91+
92+
impl<'a> PartialEq<&String> for HeaderName {
93+
fn eq(&self, other: &&String) -> bool {
94+
&&self.0 == other
95+
}
96+
}
97+
7498
#[cfg(test)]
7599
mod tests {
76100
use super::*;
@@ -88,4 +112,13 @@ mod tests {
88112
assert_eq!(static_header, static_header);
89113
assert_eq!(non_static_header, non_static_header);
90114
}
115+
116+
#[test]
117+
fn equality() {
118+
let static_header = HeaderName::from_lowercase_str("hello");
119+
assert_eq!(static_header, "hello");
120+
assert_eq!(&static_header, "hello");
121+
assert_eq!(static_header, String::from("hello"));
122+
assert_eq!(static_header, &String::from("hello"));
123+
}
91124
}

src/headers/header_value.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,27 @@ impl Display for HeaderValue {
8787
write!(f, "{}", self.inner)
8888
}
8989
}
90+
91+
impl PartialEq<str> for HeaderValue {
92+
fn eq(&self, other: &str) -> bool {
93+
self.inner == other
94+
}
95+
}
96+
97+
impl<'a> PartialEq<&'a str> for HeaderValue {
98+
fn eq(&self, other: &&'a str) -> bool {
99+
&self.inner == other
100+
}
101+
}
102+
103+
impl PartialEq<String> for HeaderValue {
104+
fn eq(&self, other: &String) -> bool {
105+
&self.inner == other
106+
}
107+
}
108+
109+
impl<'a> PartialEq<&String> for HeaderValue {
110+
fn eq(&self, other: &&String) -> bool {
111+
&&self.inner == other
112+
}
113+
}

src/headers/mod.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,41 +164,27 @@ mod tests {
164164
let non_static_header = HeaderName::from_str("hello").unwrap();
165165

166166
let mut headers = Headers::new();
167+
headers.append(STATIC_HEADER, &["foo0"][..]).unwrap();
167168
headers
168-
.append(STATIC_HEADER, &["foo0".parse().unwrap()][..])
169+
.append(static_header.clone(), &["foo1"][..])
169170
.unwrap();
170171
headers
171-
.append(static_header.clone(), &["foo1".parse().unwrap()][..])
172-
.unwrap();
173-
headers
174-
.append(non_static_header.clone(), &["foo2".parse().unwrap()][..])
172+
.append(non_static_header.clone(), &["foo2"][..])
175173
.unwrap();
176174

177175
assert_eq!(
178176
&headers.get(&STATIC_HEADER).unwrap()[..],
179-
&[
180-
"foo0".parse().unwrap(),
181-
"foo1".parse().unwrap(),
182-
"foo2".parse().unwrap()
183-
][..]
177+
&["foo0", "foo1", "foo2",][..]
184178
);
185179

186180
assert_eq!(
187181
&headers.get(&static_header).unwrap()[..],
188-
&[
189-
"foo0".parse().unwrap(),
190-
"foo1".parse().unwrap(),
191-
"foo2".parse().unwrap()
192-
][..]
182+
&["foo0", "foo1", "foo2",][..]
193183
);
194184

195185
assert_eq!(
196186
&headers.get(&non_static_header).unwrap()[..],
197-
&[
198-
"foo0".parse().unwrap(),
199-
"foo1".parse().unwrap(),
200-
"foo2".parse().unwrap()
201-
][..]
187+
&["foo0", "foo1", "foo2",][..]
202188
);
203189
}
204190
}

0 commit comments

Comments
 (0)