From b504df7cf82d24ebe1e1e525a71e723a9d59d334 Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Fri, 12 Jul 2019 09:24:15 +0200 Subject: [PATCH] Implement changes suggested by @seanmonstar https://github.com/hyperium/http/pull/326/files#r302696965 https://github.com/hyperium/http/pull/326/files#r302697620 --- src/convert.rs | 3 ++- src/header/map.rs | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/convert.rs b/src/convert.rs index 7f61c8ca..16943f77 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -1,5 +1,5 @@ use Error; -use header::{HeaderName, HeaderValue}; +use header::{HeaderName, HeaderValue, HeaderMap}; use method::Method; use sealed::Sealed; use status::StatusCode; @@ -56,6 +56,7 @@ reflexive! { Uri, Method, StatusCode, + HeaderMap, HeaderName, HeaderValue, Scheme, diff --git a/src/header/map.rs b/src/header/map.rs index 2e738080..e9dbd807 100644 --- a/src/header/map.rs +++ b/src/header/map.rs @@ -1,15 +1,16 @@ -use super::HeaderValue; -use super::name::{HeaderName, HdrName, InvalidHeaderName}; -use convert::{HttpTryFrom, HttpTryInto}; -use Error; -use sealed::Sealed; - use std::{fmt, mem, ops, ptr, vec}; +use std::collections::{BTreeMap, HashMap}; use std::collections::hash_map::RandomState; -use std::hash::{BuildHasher, Hasher, Hash}; +use std::hash::{BuildHasher, Hash, Hasher}; use std::iter::FromIterator; use std::marker::PhantomData; +use convert::{HttpTryFrom, HttpTryInto}; +use Error; + +use super::HeaderValue; +use super::name::{HdrName, HeaderName, InvalidHeaderName}; + pub use self::as_header_name::AsHeaderName; pub use self::into_header_name::IntoHeaderName; @@ -1724,7 +1725,23 @@ impl FromIterator<(HeaderName, T)> for HeaderMap } } -impl Sealed for HeaderMap {} +/// A marker trait that allows conversion from a type to HeaderMap +/// This trait is needed to have both a fast implementation of +/// HttpTryFrom and a generic implementation +/// of HttpTryFrom> for HeaderMap that do not conflict. +pub trait IntoHeaderMapAllowed {} + +impl IntoHeaderMapAllowed for HashMap {} + +impl<'a, K, V, S> IntoHeaderMapAllowed for &'a HashMap {} + +impl IntoHeaderMapAllowed for BTreeMap {} + +impl<'a, K, V> IntoHeaderMapAllowed for &'a BTreeMap {} + +impl<'a, T> IntoHeaderMapAllowed for &'a [T] {} + +impl IntoHeaderMapAllowed for Vec {} /// Convert a collection of tuples into a HeaderMap /// @@ -1746,7 +1763,7 @@ impl Sealed for HeaderMap {} /// ``` impl HttpTryFrom for HeaderMap where - C: IntoIterator, + C: IntoIterator + IntoHeaderMapAllowed, HeaderName: HttpTryFrom, HeaderValue: HttpTryFrom { @@ -3132,7 +3149,7 @@ mod into_header_name { use super::{HdrName, HeaderMap, HeaderName}; /// A marker trait used to identify values that can be used as insert keys - /// to a `HeaderMap`. + /// to a `HeaderMap`. pub trait IntoHeaderName: Sealed {} // All methods are on this pub(super) trait, instead of `IntoHeaderName`, @@ -3204,7 +3221,7 @@ mod as_header_name { use super::{Entry, HdrName, HeaderMap, HeaderName, InvalidHeaderName}; /// A marker trait used to identify values that can be used as search keys - /// to a `HeaderMap`. + /// to a `HeaderMap`. pub trait AsHeaderName: Sealed {} // All methods are on this pub(super) trait, instead of `AsHeaderName`,