Skip to content

Commit db616d5

Browse files
committed
replace TryInto with Into
1 parent 0a33fc7 commit db616d5

File tree

11 files changed

+100
-137
lines changed

11 files changed

+100
-137
lines changed

src/headers/header_name.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,9 @@ impl FromStr for HeaderName {
6363
}
6464
}
6565

66-
impl<'a> std::convert::TryFrom<&'a str> for HeaderName {
67-
type Error = Error;
68-
69-
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
70-
Self::from_str(value)
66+
impl<'a> From<&'a str> for HeaderName {
67+
fn from(value: &'a str) -> Self {
68+
Self::from_str(value).unwrap()
7169
}
7270
}
7371

src/headers/header_values.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ impl<'a> PartialEq<&'a str> for HeaderValues {
9595
}
9696
}
9797

98+
impl<'a> PartialEq<[&'a str]> for HeaderValues {
99+
fn eq(&self, other: &[&'a str]) -> bool {
100+
self.inner.iter().eq(other.iter())
101+
}
102+
}
103+
98104
impl PartialEq<String> for HeaderValues {
99105
fn eq(&self, other: &String) -> bool {
100106
self.inner.len() == 1 && &self.inner[0] == other

src/headers/headers.rs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! HTTP headers.
22
33
use std::collections::HashMap;
4-
use std::convert::TryInto;
4+
use std::convert::Into;
55
use std::iter::IntoIterator;
66
use std::ops::Index;
77
use std::str::FromStr;
@@ -23,7 +23,7 @@ use crate::headers::{
2323
/// use http_types::{Response, StatusCode};
2424
///
2525
/// let mut res = Response::new(StatusCode::Ok);
26-
/// res.insert_header("hello", "foo0").unwrap();
26+
/// res.insert_header("hello", "foo0");
2727
/// assert_eq!(res["hello"], "foo0");
2828
/// ```
2929
#[derive(Debug, Clone)]
@@ -46,14 +46,12 @@ impl Headers {
4646
/// use `Headers::append`
4747
pub fn insert(
4848
&mut self,
49-
name: impl TryInto<HeaderName>,
49+
name: impl Into<HeaderName>,
5050
values: impl ToHeaderValues,
51-
) -> crate::Result<Option<HeaderValues>> {
52-
let name = name
53-
.try_into()
54-
.map_err(|_| crate::format_err!("Could not convert into header name"))?;
55-
let values: HeaderValues = values.to_header_values()?.collect();
56-
Ok(self.headers.insert(name, values))
51+
) -> Option<HeaderValues> {
52+
let name = name.into();
53+
let values: HeaderValues = values.to_header_values().unwrap().collect();
54+
self.headers.insert(name, values)
5755
}
5856

5957
/// Append a header to the headers.
@@ -62,27 +60,25 @@ impl Headers {
6260
/// header if there aren't any. Or else append to the existing list of headers.
6361
pub fn append(
6462
&mut self,
65-
name: impl TryInto<HeaderName>,
63+
name: impl Into<HeaderName>,
6664
values: impl ToHeaderValues,
6765
) -> crate::Result<()> {
68-
let name = name
69-
.try_into()
70-
.map_err(|_| crate::format_err!("Could not convert into header name"))?;
66+
let name = name.into();
7167
match self.get_mut(&name) {
7268
Some(headers) => {
7369
let mut values: HeaderValues = values.to_header_values()?.collect();
7470
headers.append(&mut values);
7571
}
7672
None => {
77-
self.insert(name, values)?;
73+
self.insert(name, values);
7874
}
7975
}
8076
Ok(())
8177
}
8278

8379
/// Get a reference to a header.
84-
pub fn get(&self, name: &HeaderName) -> Option<&HeaderValues> {
85-
self.headers.get(name)
80+
pub fn get(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues> {
81+
self.headers.get(&name.into())
8682
}
8783

8884
/// Get a mutable reference to a header.
@@ -123,7 +119,7 @@ impl Headers {
123119
}
124120
}
125121

126-
impl Index<&HeaderName> for Headers {
122+
impl Index<HeaderName> for Headers {
127123
type Output = HeaderValues;
128124

129125
/// Returns a reference to the value corresponding to the supplied name.
@@ -132,7 +128,7 @@ impl Index<&HeaderName> for Headers {
132128
///
133129
/// Panics if the name is not present in `Headers`.
134130
#[inline]
135-
fn index(&self, name: &HeaderName) -> &HeaderValues {
131+
fn index(&self, name: HeaderName) -> &HeaderValues {
136132
self.get(name).expect("no entry found for name")
137133
}
138134
}
@@ -148,7 +144,7 @@ impl Index<&str> for Headers {
148144
#[inline]
149145
fn index(&self, name: &str) -> &HeaderValues {
150146
let name = HeaderName::from_str(name).expect("string slice needs to be valid ASCII");
151-
self.get(&name).expect("no entry found for name")
147+
self.get(name).expect("no entry found for name")
152148
}
153149
}
154150

@@ -202,28 +198,18 @@ mod tests {
202198
headers.append(static_header.clone(), "foo1")?;
203199
headers.append(non_static_header.clone(), "foo2")?;
204200

205-
assert_eq!(
206-
&headers.get(&STATIC_HEADER).unwrap()[..],
207-
&["foo0", "foo1", "foo2",][..]
208-
);
209-
210-
assert_eq!(
211-
&headers.get(&static_header).unwrap()[..],
212-
&["foo0", "foo1", "foo2",][..]
213-
);
214-
215-
assert_eq!(
216-
&headers.get(&non_static_header).unwrap()[..],
217-
&["foo0", "foo1", "foo2",][..]
218-
);
201+
assert_eq!(headers[STATIC_HEADER], ["foo0", "foo1", "foo2",][..]);
202+
assert_eq!(headers[static_header], ["foo0", "foo1", "foo2",][..]);
203+
assert_eq!(headers[non_static_header], ["foo0", "foo1", "foo2",][..]);
219204

220205
Ok(())
221206
}
222207

223208
#[test]
224209
fn index_into_headers() {
225210
let mut headers = Headers::new();
226-
headers.insert("hello", "foo0").unwrap();
211+
headers.insert("hello", "foo0");
227212
assert_eq!(headers["hello"], "foo0");
213+
assert_eq!(headers.get("hello").unwrap(), "foo0");
228214
}
229215
}

src/request.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use async_std::io::{self, BufRead, Read};
22
use async_std::sync;
33

4-
use std::convert::TryInto;
4+
use std::convert::Into;
55
use std::mem;
66
use std::ops::Index;
77
use std::pin::Pin;
@@ -93,7 +93,7 @@ impl Request {
9393
}
9494

9595
fn forwarded_for(&self) -> Option<&str> {
96-
if let Some(header) = self.header(&"Forwarded".parse().unwrap()) {
96+
if let Some(header) = self.header("Forwarded") {
9797
header.as_str().split(";").find_map(|key_equals_value| {
9898
let parts = key_equals_value.split("=").collect::<Vec<_>>();
9999
if parts.len() == 2 && parts[0].eq_ignore_ascii_case("for") {
@@ -102,7 +102,7 @@ impl Request {
102102
None
103103
}
104104
})
105-
} else if let Some(header) = self.header(&"X-Forwarded-For".parse().unwrap()) {
105+
} else if let Some(header) = self.header("X-Forwarded-For") {
106106
header.as_str().split(",").next()
107107
} else {
108108
None
@@ -364,7 +364,7 @@ impl Request {
364364
}
365365

366366
/// Get an HTTP header.
367-
pub fn header(&self, name: &HeaderName) -> Option<&HeaderValues> {
367+
pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues> {
368368
self.headers.get(name)
369369
}
370370

@@ -388,15 +388,15 @@ impl Request {
388388
/// use http_types::{Url, Method, Request};
389389
///
390390
/// let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
391-
/// req.insert_header("Content-Type", "text/plain")?;
391+
/// req.insert_header("Content-Type", "text/plain");
392392
/// #
393393
/// # Ok(()) }
394394
/// ```
395395
pub fn insert_header(
396396
&mut self,
397-
name: impl TryInto<HeaderName>,
397+
name: impl Into<HeaderName>,
398398
values: impl ToHeaderValues,
399-
) -> crate::Result<Option<HeaderValues>> {
399+
) -> Option<HeaderValues> {
400400
self.headers.insert(name, values)
401401
}
402402

@@ -419,7 +419,7 @@ impl Request {
419419
/// ```
420420
pub fn append_header(
421421
&mut self,
422-
name: impl TryInto<HeaderName>,
422+
name: impl Into<HeaderName>,
423423
values: impl ToHeaderValues,
424424
) -> crate::Result<()> {
425425
self.headers.append(name, values)
@@ -431,19 +431,19 @@ impl Request {
431431
let value: HeaderValue = mime.into();
432432

433433
// A Mime instance is guaranteed to be valid header name.
434-
self.insert_header(CONTENT_TYPE, value).unwrap()
434+
self.insert_header(CONTENT_TYPE, value)
435435
}
436436

437437
/// Copy MIME data from the body.
438438
fn copy_content_type_from_body(&mut self) {
439-
if self.header(&CONTENT_TYPE).is_none() {
439+
if self.header(CONTENT_TYPE).is_none() {
440440
self.set_content_type(self.body.mime().clone());
441441
}
442442
}
443443

444444
/// Get the current content type
445445
pub fn content_type(&self) -> Option<Mime> {
446-
self.header(&CONTENT_TYPE)?.last().as_str().parse().ok()
446+
self.header(CONTENT_TYPE)?.last().as_str().parse().ok()
447447
}
448448

449449
/// Get the length of the body stream, if it has been set.
@@ -623,7 +623,7 @@ impl From<Request> for Body {
623623
}
624624
}
625625

626-
impl Index<&HeaderName> for Request {
626+
impl Index<HeaderName> for Request {
627627
type Output = HeaderValues;
628628

629629
/// Returns a reference to the value corresponding to the supplied name.
@@ -632,7 +632,7 @@ impl Index<&HeaderName> for Request {
632632
///
633633
/// Panics if the name is not present in `Request`.
634634
#[inline]
635-
fn index(&self, name: &HeaderName) -> &HeaderValues {
635+
fn index(&self, name: HeaderName) -> &HeaderValues {
636636
self.headers.index(name)
637637
}
638638
}
@@ -691,21 +691,17 @@ mod tests {
691691
}
692692

693693
fn set_x_forwarded_for(request: &mut Request, client: &'static str) {
694-
request
695-
.insert_header(
696-
"x-forwarded-for",
697-
format!("{},proxy.com,other-proxy.com", client),
698-
)
699-
.unwrap();
694+
request.insert_header(
695+
"x-forwarded-for",
696+
format!("{},proxy.com,other-proxy.com", client),
697+
);
700698
}
701699

702700
fn set_forwarded(request: &mut Request, client: &'static str) {
703-
request
704-
.insert_header(
705-
"Forwarded",
706-
format!("by=something.com;for={};host=host.com;proto=http", client),
707-
)
708-
.unwrap();
701+
request.insert_header(
702+
"Forwarded",
703+
format!("by=something.com;for={};host=host.com;proto=http", client),
704+
);
709705
}
710706

711707
#[test]
@@ -725,9 +721,7 @@ mod tests {
725721
"127.0.0.1:8000".parse::<std::net::SocketAddr>().unwrap(),
726722
));
727723

728-
request
729-
.insert_header("Forwarded", "this is an improperly ;;; formatted header")
730-
.unwrap();
724+
request.insert_header("Forwarded", "this is an improperly ;;; formatted header");
731725

732726
assert_eq!(request.forwarded_for(), None);
733727
assert_eq!(request.remote(), Some("127.0.0.1:8000"));

src/response.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use async_std::io::{self, BufRead, Read};
22
use async_std::sync;
33

4-
use std::convert::TryInto;
4+
use std::convert::Into;
55
use std::mem;
66
use std::ops::Index;
77
use std::pin::Pin;
@@ -74,7 +74,7 @@ impl Response {
7474
}
7575

7676
/// Get an HTTP header.
77-
pub fn header(&self, name: &HeaderName) -> Option<&HeaderValues> {
77+
pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues> {
7878
self.headers.get(name)
7979
}
8080

@@ -93,15 +93,15 @@ impl Response {
9393
/// use http_types::{Url, Method, Response, StatusCode};
9494
///
9595
/// let mut req = Response::new(StatusCode::Ok);
96-
/// req.insert_header("Content-Type", "text/plain")?;
96+
/// req.insert_header("Content-Type", "text/plain");
9797
/// #
9898
/// # Ok(()) }
9999
/// ```
100100
pub fn insert_header(
101101
&mut self,
102-
name: impl TryInto<HeaderName>,
102+
name: impl Into<HeaderName>,
103103
values: impl ToHeaderValues,
104-
) -> crate::Result<Option<HeaderValues>> {
104+
) -> Option<HeaderValues> {
105105
self.headers.insert(name, values)
106106
}
107107

@@ -124,7 +124,7 @@ impl Response {
124124
/// ```
125125
pub fn append_header(
126126
&mut self,
127-
name: impl TryInto<HeaderName>,
127+
name: impl Into<HeaderName>,
128128
values: impl ToHeaderValues,
129129
) -> crate::Result<()> {
130130
self.headers.append(name, values)
@@ -349,12 +349,12 @@ impl Response {
349349
let value: HeaderValue = mime.into();
350350

351351
// A Mime instance is guaranteed to be valid header name.
352-
self.insert_header(CONTENT_TYPE, value).unwrap()
352+
self.insert_header(CONTENT_TYPE, value)
353353
}
354354

355355
/// Copy MIME data from the body.
356356
fn copy_content_type_from_body(&mut self) {
357-
if self.header(&CONTENT_TYPE).is_none() {
357+
if self.header(CONTENT_TYPE).is_none() {
358358
self.set_content_type(self.body.mime().clone());
359359
}
360360
}
@@ -563,7 +563,7 @@ impl From<()> for Response {
563563
Response::new(StatusCode::NoContent)
564564
}
565565
}
566-
impl Index<&HeaderName> for Response {
566+
impl Index<HeaderName> for Response {
567567
type Output = HeaderValues;
568568

569569
/// Returns a reference to the value corresponding to the supplied name.
@@ -572,7 +572,7 @@ impl Index<&HeaderName> for Response {
572572
///
573573
/// Panics if the name is not present in `Response`.
574574
#[inline]
575-
fn index(&self, name: &HeaderName) -> &HeaderValues {
575+
fn index(&self, name: HeaderName) -> &HeaderValues {
576576
self.headers.index(name)
577577
}
578578
}

0 commit comments

Comments
 (0)