@@ -14,11 +14,47 @@ use super::HeaderValue;
1414pub use self :: as_header_name:: AsHeaderName ;
1515pub use self :: into_header_name:: IntoHeaderName ;
1616
17- /// A set of HTTP headers
17+ /// A specialized [multimap](<https://en.wikipedia.org/wiki/Multimap>) for
18+ /// header names and values.
1819///
19- /// `HeaderMap` is a multimap of [`HeaderName`] to values.
20+ /// # Overview
21+ ///
22+ /// `HeaderMap` is designed specifically for efficient manipulation of HTTP
23+ /// headers. It supports multiple values per header name and provides
24+ /// specialized APIs for insertion, retrieval, and iteration.
25+ ///
26+ /// The internal implementation is optimized for common usage patterns in HTTP,
27+ /// and may change across versions. For example, the current implementation uses
28+ /// [Robin Hood
29+ /// hashing](<https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing>) to
30+ /// store entries compactly and enable high load factors with good performance.
31+ /// However, the collision resolution strategy and storage mechanism are not
32+ /// part of the public API and may be altered in future releases.
33+ ///
34+ /// # Iteration order
35+ ///
36+ /// Unless otherwise specified, the order in which items are returned by
37+ /// iterators from `HeaderMap` methods is arbitrary; there is no guaranteed
38+ /// ordering among the elements yielded by such an iterator. Changes to the
39+ /// iteration order are not considered breaking changes, so users must not rely
40+ /// on any incidental order produced by such an iterator. However, for a given
41+ /// crate version, the iteration order will be consistent across all platforms.
42+ ///
43+ /// # Adaptive hashing
44+ ///
45+ /// `HeaderMap` uses an adaptive strategy for hashing to maintain fast lookups
46+ /// while resisting hash collision attacks. The default hash function
47+ /// prioritizes performance. In scenarios where high collision rates are
48+ /// detected—typically indicative of denial-of-service attacks—the
49+ /// implementation switches to a more secure, collision-resistant hash function.
50+ ///
51+ /// # Limitations
52+ ///
53+ /// A `HeaderMap` can store at most 32,768 entries \(header name/value pairs\).
54+ /// Attempting to exceed this limit will result in a panic.
2055///
2156/// [`HeaderName`]: struct.HeaderName.html
57+ /// [`HeaderMap`]: struct.HeaderMap.html
2258///
2359/// # Examples
2460///
@@ -445,8 +481,21 @@ impl HeaderMap {
445481 /// assert!(map.is_empty());
446482 /// assert_eq!(0, map.capacity());
447483 /// ```
484+ #[ inline]
448485 pub fn new ( ) -> Self {
449- HeaderMap :: try_with_capacity ( 0 ) . unwrap ( )
486+ Self :: default ( )
487+ }
488+ }
489+
490+ impl < T > Default for HeaderMap < T > {
491+ fn default ( ) -> Self {
492+ HeaderMap {
493+ mask : 0 ,
494+ indices : Box :: new ( [ ] ) , // as a ZST, this doesn't actually allocate anything
495+ entries : Vec :: new ( ) ,
496+ extra_values : Vec :: new ( ) ,
497+ danger : Danger :: Green ,
498+ }
450499 }
451500}
452501
@@ -501,13 +550,7 @@ impl<T> HeaderMap<T> {
501550 /// ```
502551 pub fn try_with_capacity ( capacity : usize ) -> Result < HeaderMap < T > , MaxSizeReached > {
503552 if capacity == 0 {
504- Ok ( HeaderMap {
505- mask : 0 ,
506- indices : Box :: new ( [ ] ) , // as a ZST, this doesn't actually allocate anything
507- entries : Vec :: new ( ) ,
508- extra_values : Vec :: new ( ) ,
509- danger : Danger :: Green ,
510- } )
553+ Ok ( Self :: default ( ) )
511554 } else {
512555 let raw_cap = match to_raw_capacity ( capacity) . checked_next_power_of_two ( ) {
513556 Some ( c) => c,
@@ -2164,12 +2207,6 @@ impl<T: fmt::Debug> fmt::Debug for HeaderMap<T> {
21642207 }
21652208}
21662209
2167- impl < T > Default for HeaderMap < T > {
2168- fn default ( ) -> Self {
2169- HeaderMap :: try_with_capacity ( 0 ) . expect ( "zero capacity should never fail" )
2170- }
2171- }
2172-
21732210impl < K , T > ops:: Index < K > for HeaderMap < T >
21742211where
21752212 K : AsHeaderName ,
@@ -2315,15 +2352,15 @@ impl<'a, T> IterMut<'a, T> {
23152352 self . cursor = Some ( Cursor :: Head ) ;
23162353 }
23172354
2318- let entry = unsafe { & mut ( * self . map ) . entries [ self . entry ] } ;
2355+ let entry = & mut unsafe { & mut * self . map } . entries [ self . entry ] ;
23192356
23202357 match self . cursor . unwrap ( ) {
23212358 Head => {
23222359 self . cursor = entry. links . map ( |l| Values ( l. next ) ) ;
23232360 Some ( ( & entry. key , & mut entry. value as * mut _ ) )
23242361 }
23252362 Values ( idx) => {
2326- let extra = unsafe { & mut ( * self . map ) . extra_values [ idx] } ;
2363+ let extra = & mut unsafe { & mut ( * self . map ) } . extra_values [ idx] ;
23272364
23282365 match extra. next {
23292366 Link :: Entry ( _) => self . cursor = None ,
@@ -2963,7 +3000,7 @@ impl<'a, T: 'a> Iterator for ValueIterMut<'a, T> {
29633000 fn next ( & mut self ) -> Option < Self :: Item > {
29643001 use self :: Cursor :: * ;
29653002
2966- let entry = unsafe { & mut ( * self . map ) . entries [ self . index ] } ;
3003+ let entry = & mut unsafe { & mut * self . map } . entries [ self . index ] ;
29673004
29683005 match self . front {
29693006 Some ( Head ) => {
@@ -2983,7 +3020,7 @@ impl<'a, T: 'a> Iterator for ValueIterMut<'a, T> {
29833020 Some ( & mut entry. value )
29843021 }
29853022 Some ( Values ( idx) ) => {
2986- let extra = unsafe { & mut ( * self . map ) . extra_values [ idx] } ;
3023+ let extra = & mut unsafe { & mut * self . map } . extra_values [ idx] ;
29873024
29883025 if self . front == self . back {
29893026 self . front = None ;
@@ -3006,7 +3043,7 @@ impl<'a, T: 'a> DoubleEndedIterator for ValueIterMut<'a, T> {
30063043 fn next_back ( & mut self ) -> Option < Self :: Item > {
30073044 use self :: Cursor :: * ;
30083045
3009- let entry = unsafe { & mut ( * self . map ) . entries [ self . index ] } ;
3046+ let entry = & mut unsafe { & mut * self . map } . entries [ self . index ] ;
30103047
30113048 match self . back {
30123049 Some ( Head ) => {
@@ -3015,7 +3052,7 @@ impl<'a, T: 'a> DoubleEndedIterator for ValueIterMut<'a, T> {
30153052 Some ( & mut entry. value )
30163053 }
30173054 Some ( Values ( idx) ) => {
3018- let extra = unsafe { & mut ( * self . map ) . extra_values [ idx] } ;
3055+ let extra = & mut unsafe { & mut * self . map } . extra_values [ idx] ;
30193056
30203057 if self . front == self . back {
30213058 self . front = None ;
0 commit comments