Skip to content

Commit

Permalink
Implement conversion from HashMaps of any type, not just str
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Jul 3, 2019
1 parent b76d702 commit 660c4be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/header/map.rs
@@ -1,6 +1,6 @@
use super::HeaderValue;
use super::name::{HeaderName, HdrName, InvalidHeaderName};
use convert::HttpTryFrom;
use convert::{HttpTryFrom, HttpTryInto};
use Error;
use sealed::Sealed;

Expand Down Expand Up @@ -1747,16 +1747,16 @@ impl<T> Sealed for HeaderMap<T> {}
impl<COLLECTION, K, V> HttpTryFrom<COLLECTION> for HeaderMap<HeaderValue>
where
COLLECTION: IntoIterator<Item=(K, V)>,
K: AsRef<str>,
V: AsRef<str>
HeaderName: HttpTryFrom<K>,
HeaderValue: HttpTryFrom<V>
{
type Error = Error;

fn try_from(c: COLLECTION) -> Result<Self, Self::Error> {
c.into_iter()
.map(|(k, v)| -> ::Result<(HeaderName, HeaderValue)> {
let name = k.as_ref().parse()?;
let value = v.as_ref().parse()?;
let name : HeaderName = k.http_try_into()?;
let value : HeaderValue = v.http_try_into()?;
Ok((name, value))
})
.collect()
Expand Down
8 changes: 8 additions & 0 deletions src/header/name.rs
Expand Up @@ -1772,6 +1772,14 @@ impl<'a> HttpTryFrom<&'a str> for HeaderName {
}
}

impl<'a> HttpTryFrom<&'a String> for HeaderName {
type Error = InvalidHeaderName;
#[inline]
fn try_from(s: &'a String) -> Result<Self, Self::Error> {
Self::from_bytes(s.as_bytes())
}
}

impl<'a> HttpTryFrom<&'a [u8]> for HeaderName {
type Error = InvalidHeaderName;
#[inline]
Expand Down
8 changes: 8 additions & 0 deletions src/header/value.rs
Expand Up @@ -521,6 +521,14 @@ impl<'a> HttpTryFrom<&'a str> for HeaderValue {
}
}

impl<'a> HttpTryFrom<&'a String> for HeaderValue {
type Error = InvalidHeaderValue;
#[inline]
fn try_from(s: &'a String) -> Result<Self, Self::Error> {
Self::from_bytes(s.as_bytes())
}
}

impl<'a> HttpTryFrom<&'a [u8]> for HeaderValue {
type Error = InvalidHeaderValue;

Expand Down

0 comments on commit 660c4be

Please sign in to comment.