Skip to content

Commit 8509362

Browse files
committed
TryInto<HeaderName> for header insert/append
1 parent 4f1fa87 commit 8509362

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

src/headers/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use async_std::io;
44

5+
use std::convert::TryInto;
56
use std::collections::HashMap;
67
use std::iter::IntoIterator;
78

@@ -44,9 +45,10 @@ impl Headers {
4445
/// Insert a header into the headers.
4546
pub fn insert(
4647
&mut self,
47-
name: HeaderName,
48+
name: impl TryInto<HeaderName>,
4849
values: impl ToHeaderValues,
4950
) -> io::Result<Option<Vec<HeaderValue>>> {
51+
let name = name.try_into().map_err(|_| io::Error::new(io::ErrorKind::Other, "Could not convert into header name"))?;
5052
let values: Vec<HeaderValue> = values.to_header_values()?.collect();
5153
Ok(self.headers.insert(name, values))
5254
}
@@ -55,7 +57,8 @@ impl Headers {
5557
///
5658
/// Unlike `insert` this function will not override the contents of a header, but insert a
5759
/// header if there aren't any. Or else append to the existing list of headers.
58-
pub fn append(&mut self, name: HeaderName, values: impl ToHeaderValues) -> io::Result<()> {
60+
pub fn append(&mut self, name: impl TryInto<HeaderName>, values: impl ToHeaderValues) -> io::Result<()> {
61+
let name = name.try_into().map_err(|_| io::Error::new(io::ErrorKind::Other, "Could not convert into header name"))?;
5962
match self.get_mut(&name) {
6063
Some(headers) => {
6164
let mut values: Vec<HeaderValue> = values.to_header_values()?.collect();

src/request.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use async_std::sync;
44
use std::mem;
55
use std::pin::Pin;
66
use std::task::{Context, Poll};
7+
use std::convert::TryInto;
78

89
use crate::headers::{
910
self, HeaderName, HeaderValue, Headers, Names, ToHeaderValues, Values, CONTENT_TYPE,
@@ -214,7 +215,7 @@ impl Request {
214215
/// Set an HTTP header.
215216
pub fn insert_header(
216217
&mut self,
217-
name: HeaderName,
218+
name: impl TryInto<HeaderName>,
218219
values: impl ToHeaderValues,
219220
) -> io::Result<Option<Vec<HeaderValue>>> {
220221
self.headers.insert(name, values)
@@ -226,7 +227,7 @@ impl Request {
226227
/// header if there aren't any. Or else append to the existing list of headers.
227228
pub fn append_header(
228229
&mut self,
229-
name: HeaderName,
230+
name: impl TryInto<HeaderName>,
230231
values: impl ToHeaderValues,
231232
) -> io::Result<()> {
232233
self.headers.append(name, values)

src/response.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use async_std::sync;
44
use std::mem;
55
use std::pin::Pin;
66
use std::task::{Context, Poll};
7+
use std::convert::TryInto;
78

89
use crate::headers::{
910
self, HeaderName, HeaderValue, Headers, Names, ToHeaderValues, Values, CONTENT_TYPE,
@@ -76,7 +77,7 @@ impl Response {
7677
/// Set an HTTP header.
7778
pub fn insert_header(
7879
&mut self,
79-
name: HeaderName,
80+
name: impl TryInto<HeaderName>,
8081
values: impl ToHeaderValues,
8182
) -> io::Result<Option<Vec<HeaderValue>>> {
8283
self.headers.insert(name, values)
@@ -88,7 +89,7 @@ impl Response {
8889
/// header if there aren't any. Or else append to the existing list of headers.
8990
pub fn append_header(
9091
&mut self,
91-
name: HeaderName,
92+
name: impl TryInto<HeaderName>,
9293
values: impl ToHeaderValues,
9394
) -> io::Result<()> {
9495
self.headers.append(name, values)

src/trailers.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@
3131
//!
3232
//! let sender = req.send_trailers();
3333
//! let mut trailers = Trailers::new();
34-
//! trailers.insert(
35-
//! HeaderName::from_str("Content-Type")?,
36-
//! "text/plain",
37-
//! );
34+
//! trailers.insert("Content-Type", "text/plain")?;
3835
//!
3936
//! task::spawn(async move {
4037
//! let _trailers = req.recv_trailers().await;
@@ -54,6 +51,7 @@ use crate::headers::{
5451
};
5552
use async_std::sync::Sender;
5653

54+
use std::convert::TryInto;
5755
use std::io;
5856
use std::ops::{Deref, DerefMut};
5957

@@ -74,7 +72,7 @@ impl Trailers {
7472
/// Insert a header into the headers.
7573
pub fn insert(
7674
&mut self,
77-
name: HeaderName,
75+
name: impl TryInto<HeaderName>,
7876
values: impl ToHeaderValues,
7977
) -> io::Result<Option<Vec<HeaderValue>>> {
8078
self.headers.insert(name, values)
@@ -84,7 +82,7 @@ impl Trailers {
8482
///
8583
/// Unlike `insert` this function will not override the contents of a header, but insert a
8684
/// header if there aren't any. Or else append to the existing list of headers.
87-
pub fn append(&mut self, name: HeaderName, values: impl ToHeaderValues) -> io::Result<()> {
85+
pub fn append(&mut self, name: impl TryInto<HeaderName>, values: impl ToHeaderValues) -> io::Result<()> {
8886
self.headers.append(name, values)
8987
}
9088

0 commit comments

Comments
 (0)