http/header/
map.rs

1use std::collections::hash_map::RandomState;
2use std::collections::HashMap;
3use std::convert::TryFrom;
4use std::hash::{BuildHasher, Hash, Hasher};
5use std::iter::{FromIterator, FusedIterator};
6use std::marker::PhantomData;
7use std::{fmt, mem, ops, ptr, vec};
8
9use crate::Error;
10
11use super::name::{HdrName, HeaderName, InvalidHeaderName};
12use super::HeaderValue;
13
14pub use self::as_header_name::AsHeaderName;
15pub use self::into_header_name::IntoHeaderName;
16
17/// A specialized [multimap](<https://en.wikipedia.org/wiki/Multimap>) for
18/// header names and values.
19///
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.
55///
56/// [`HeaderName`]: struct.HeaderName.html
57/// [`HeaderMap`]: struct.HeaderMap.html
58///
59/// # Examples
60///
61/// Basic usage
62///
63/// ```
64/// # use http::HeaderMap;
65/// # use http::header::{CONTENT_LENGTH, HOST, LOCATION};
66/// let mut headers = HeaderMap::new();
67///
68/// headers.insert(HOST, "example.com".parse().unwrap());
69/// headers.insert(CONTENT_LENGTH, "123".parse().unwrap());
70///
71/// assert!(headers.contains_key(HOST));
72/// assert!(!headers.contains_key(LOCATION));
73///
74/// assert_eq!(headers[HOST], "example.com");
75///
76/// headers.remove(HOST);
77///
78/// assert!(!headers.contains_key(HOST));
79/// ```
80#[derive(Clone)]
81pub struct HeaderMap<T = HeaderValue> {
82    // Used to mask values to get an index
83    mask: Size,
84    indices: Box<[Pos]>,
85    entries: Vec<Bucket<T>>,
86    extra_values: Vec<ExtraValue<T>>,
87    danger: Danger,
88}
89
90// # Implementation notes
91//
92// Below, you will find a fairly large amount of code. Most of this is to
93// provide the necessary functions to efficiently manipulate the header
94// multimap. The core hashing table is based on robin hood hashing [1]. While
95// this is the same hashing algorithm used as part of Rust's `HashMap` in
96// stdlib, many implementation details are different. The two primary reasons
97// for this divergence are that `HeaderMap` is a multimap and the structure has
98// been optimized to take advantage of the characteristics of HTTP headers.
99//
100// ## Structure Layout
101//
102// Most of the data contained by `HeaderMap` is *not* stored in the hash table.
103// Instead, pairs of header name and *first* associated header value are stored
104// in the `entries` vector. If the header name has more than one associated
105// header value, then additional values are stored in `extra_values`. The actual
106// hash table (`indices`) only maps hash codes to indices in `entries`. This
107// means that, when an eviction happens, the actual header name and value stay
108// put and only a tiny amount of memory has to be copied.
109//
110// Extra values associated with a header name are tracked using a linked list.
111// Links are formed with offsets into `extra_values` and not pointers.
112//
113// [1]: https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing
114
115/// `HeaderMap` entry iterator.
116///
117/// Yields `(&HeaderName, &value)` tuples. The same header name may be yielded
118/// more than once if it has more than one associated value.
119#[derive(Debug)]
120pub struct Iter<'a, T> {
121    map: &'a HeaderMap<T>,
122    entry: usize,
123    cursor: Option<Cursor>,
124}
125
126/// `HeaderMap` mutable entry iterator
127///
128/// Yields `(&HeaderName, &mut value)` tuples. The same header name may be
129/// yielded more than once if it has more than one associated value.
130#[derive(Debug)]
131pub struct IterMut<'a, T> {
132    map: *mut HeaderMap<T>,
133    entry: usize,
134    cursor: Option<Cursor>,
135    lt: PhantomData<&'a mut HeaderMap<T>>,
136}
137
138/// An owning iterator over the entries of a `HeaderMap`.
139///
140/// This struct is created by the `into_iter` method on `HeaderMap`.
141#[derive(Debug)]
142pub struct IntoIter<T> {
143    // If None, pull from `entries`
144    next: Option<usize>,
145    entries: vec::IntoIter<Bucket<T>>,
146    extra_values: Vec<ExtraValue<T>>,
147}
148
149/// An iterator over `HeaderMap` keys.
150///
151/// Each header name is yielded only once, even if it has more than one
152/// associated value.
153#[derive(Debug)]
154pub struct Keys<'a, T> {
155    inner: ::std::slice::Iter<'a, Bucket<T>>,
156}
157
158/// `HeaderMap` value iterator.
159///
160/// Each value contained in the `HeaderMap` will be yielded.
161#[derive(Debug)]
162pub struct Values<'a, T> {
163    inner: Iter<'a, T>,
164}
165
166/// `HeaderMap` mutable value iterator
167#[derive(Debug)]
168pub struct ValuesMut<'a, T> {
169    inner: IterMut<'a, T>,
170}
171
172/// A drain iterator for `HeaderMap`.
173#[derive(Debug)]
174pub struct Drain<'a, T> {
175    idx: usize,
176    len: usize,
177    entries: *mut [Bucket<T>],
178    // If None, pull from `entries`
179    next: Option<usize>,
180    extra_values: *mut Vec<ExtraValue<T>>,
181    lt: PhantomData<&'a mut HeaderMap<T>>,
182}
183
184/// A view to all values stored in a single entry.
185///
186/// This struct is returned by `HeaderMap::get_all`.
187#[derive(Debug)]
188pub struct GetAll<'a, T> {
189    map: &'a HeaderMap<T>,
190    index: Option<usize>,
191}
192
193/// A view into a single location in a `HeaderMap`, which may be vacant or occupied.
194#[derive(Debug)]
195pub enum Entry<'a, T: 'a> {
196    /// An occupied entry
197    Occupied(OccupiedEntry<'a, T>),
198
199    /// A vacant entry
200    Vacant(VacantEntry<'a, T>),
201}
202
203/// A view into a single empty location in a `HeaderMap`.
204///
205/// This struct is returned as part of the `Entry` enum.
206#[derive(Debug)]
207pub struct VacantEntry<'a, T> {
208    map: &'a mut HeaderMap<T>,
209    key: HeaderName,
210    hash: HashValue,
211    probe: usize,
212    danger: bool,
213}
214
215/// A view into a single occupied location in a `HeaderMap`.
216///
217/// This struct is returned as part of the `Entry` enum.
218#[derive(Debug)]
219pub struct OccupiedEntry<'a, T> {
220    map: &'a mut HeaderMap<T>,
221    probe: usize,
222    index: usize,
223}
224
225/// An iterator of all values associated with a single header name.
226#[derive(Debug)]
227pub struct ValueIter<'a, T> {
228    map: &'a HeaderMap<T>,
229    index: usize,
230    front: Option<Cursor>,
231    back: Option<Cursor>,
232}
233
234/// A mutable iterator of all values associated with a single header name.
235#[derive(Debug)]
236pub struct ValueIterMut<'a, T> {
237    map: *mut HeaderMap<T>,
238    index: usize,
239    front: Option<Cursor>,
240    back: Option<Cursor>,
241    lt: PhantomData<&'a mut HeaderMap<T>>,
242}
243
244/// An drain iterator of all values associated with a single header name.
245#[derive(Debug)]
246pub struct ValueDrain<'a, T> {
247    first: Option<T>,
248    next: Option<::std::vec::IntoIter<T>>,
249    lt: PhantomData<&'a mut HeaderMap<T>>,
250}
251
252/// Error returned when max capacity of `HeaderMap` is exceeded
253pub struct MaxSizeReached {
254    _priv: (),
255}
256
257/// Tracks the value iterator state
258#[derive(Debug, Copy, Clone, Eq, PartialEq)]
259enum Cursor {
260    Head,
261    Values(usize),
262}
263
264/// Type used for representing the size of a HeaderMap value.
265///
266/// 32,768 is more than enough entries for a single header map. Setting this
267/// limit enables using `u16` to represent all offsets, which takes 2 bytes
268/// instead of 8 on 64 bit processors.
269///
270/// Setting this limit is especially beneficial for `indices`, making it more
271/// cache friendly. More hash codes can fit in a cache line.
272///
273/// You may notice that `u16` may represent more than 32,768 values. This is
274/// true, but 32,768 should be plenty and it allows us to reserve the top bit
275/// for future usage.
276type Size = u16;
277
278/// This limit falls out from above.
279const MAX_SIZE: usize = 1 << 15;
280
281/// An entry in the hash table. This represents the full hash code for an entry
282/// as well as the position of the entry in the `entries` vector.
283#[derive(Copy, Clone)]
284struct Pos {
285    // Index in the `entries` vec
286    index: Size,
287    // Full hash value for the entry.
288    hash: HashValue,
289}
290
291/// Hash values are limited to u16 as well. While `fast_hash` and `Hasher`
292/// return `usize` hash codes, limiting the effective hash code to the lower 16
293/// bits is fine since we know that the `indices` vector will never grow beyond
294/// that size.
295#[derive(Debug, Copy, Clone, Eq, PartialEq)]
296struct HashValue(u16);
297
298/// Stores the data associated with a `HeaderMap` entry. Only the first value is
299/// included in this struct. If a header name has more than one associated
300/// value, all extra values are stored in the `extra_values` vector. A doubly
301/// linked list of entries is maintained. The doubly linked list is used so that
302/// removing a value is constant time. This also has the nice property of
303/// enabling double ended iteration.
304#[derive(Debug, Clone)]
305struct Bucket<T> {
306    hash: HashValue,
307    key: HeaderName,
308    value: T,
309    links: Option<Links>,
310}
311
312/// The head and tail of the value linked list.
313#[derive(Debug, Copy, Clone)]
314struct Links {
315    next: usize,
316    tail: usize,
317}
318
319/// Access to the `links` value in a slice of buckets.
320///
321/// It's important that no other field is accessed, since it may have been
322/// freed in a `Drain` iterator.
323#[derive(Debug)]
324struct RawLinks<T>(*mut [Bucket<T>]);
325
326/// Node in doubly-linked list of header value entries
327#[derive(Debug, Clone)]
328struct ExtraValue<T> {
329    value: T,
330    prev: Link,
331    next: Link,
332}
333
334/// A header value node is either linked to another node in the `extra_values`
335/// list or it points to an entry in `entries`. The entry in `entries` is the
336/// start of the list and holds the associated header name.
337#[derive(Debug, Copy, Clone, Eq, PartialEq)]
338enum Link {
339    Entry(usize),
340    Extra(usize),
341}
342
343/// Tracks the header map danger level! This relates to the adaptive hashing
344/// algorithm. A HeaderMap starts in the "green" state, when a large number of
345/// collisions are detected, it transitions to the yellow state. At this point,
346/// the header map will either grow and switch back to the green state OR it
347/// will transition to the red state.
348///
349/// When in the red state, a safe hashing algorithm is used and all values in
350/// the header map have to be rehashed.
351#[derive(Clone)]
352enum Danger {
353    Green,
354    Yellow,
355    Red(RandomState),
356}
357
358// Constants related to detecting DOS attacks.
359//
360// Displacement is the number of entries that get shifted when inserting a new
361// value. Forward shift is how far the entry gets stored from the ideal
362// position.
363//
364// The current constant values were picked from another implementation. It could
365// be that there are different values better suited to the header map case.
366const DISPLACEMENT_THRESHOLD: usize = 128;
367const FORWARD_SHIFT_THRESHOLD: usize = 512;
368
369// The default strategy for handling the yellow danger state is to increase the
370// header map capacity in order to (hopefully) reduce the number of collisions.
371// If growing the hash map would cause the load factor to drop bellow this
372// threshold, then instead of growing, the headermap is switched to the red
373// danger state and safe hashing is used instead.
374const LOAD_FACTOR_THRESHOLD: f32 = 0.2;
375
376// Macro used to iterate the hash table starting at a given point, looping when
377// the end is hit.
378macro_rules! probe_loop {
379    ($label:tt: $probe_var: ident < $len: expr, $body: expr) => {
380        debug_assert!($len > 0);
381        $label:
382        loop {
383            if $probe_var < $len {
384                $body
385                $probe_var += 1;
386            } else {
387                $probe_var = 0;
388            }
389        }
390    };
391    ($probe_var: ident < $len: expr, $body: expr) => {
392        debug_assert!($len > 0);
393        loop {
394            if $probe_var < $len {
395                $body
396                $probe_var += 1;
397            } else {
398                $probe_var = 0;
399            }
400        }
401    };
402}
403
404// First part of the robinhood algorithm. Given a key, find the slot in which it
405// will be inserted. This is done by starting at the "ideal" spot. Then scanning
406// until the destination slot is found. A destination slot is either the next
407// empty slot or the next slot that is occupied by an entry that has a lower
408// displacement (displacement is the distance from the ideal spot).
409//
410// This is implemented as a macro instead of a function that takes a closure in
411// order to guarantee that it is "inlined". There is no way to annotate closures
412// to guarantee inlining.
413macro_rules! insert_phase_one {
414    ($map:ident,
415     $key:expr,
416     $probe:ident,
417     $pos:ident,
418     $hash:ident,
419     $danger:ident,
420     $vacant:expr,
421     $occupied:expr,
422     $robinhood:expr) =>
423    {{
424        let $hash = hash_elem_using(&$map.danger, &$key);
425        let mut $probe = desired_pos($map.mask, $hash);
426        let mut dist = 0;
427        let ret;
428
429        // Start at the ideal position, checking all slots
430        probe_loop!('probe: $probe < $map.indices.len(), {
431            if let Some(($pos, entry_hash)) = $map.indices[$probe].resolve() {
432                // The slot is already occupied, but check if it has a lower
433                // displacement.
434                let their_dist = probe_distance($map.mask, entry_hash, $probe);
435
436                if their_dist < dist {
437                    // The new key's distance is larger, so claim this spot and
438                    // displace the current entry.
439                    //
440                    // Check if this insertion is above the danger threshold.
441                    let $danger =
442                        dist >= FORWARD_SHIFT_THRESHOLD && !$map.danger.is_red();
443
444                    ret = $robinhood;
445                    break 'probe;
446                } else if entry_hash == $hash && $map.entries[$pos].key == $key {
447                    // There already is an entry with the same key.
448                    ret = $occupied;
449                    break 'probe;
450                }
451            } else {
452                // The entry is vacant, use it for this key.
453                let $danger =
454                    dist >= FORWARD_SHIFT_THRESHOLD && !$map.danger.is_red();
455
456                ret = $vacant;
457                break 'probe;
458            }
459
460            dist += 1;
461        });
462
463        ret
464    }}
465}
466
467// ===== impl HeaderMap =====
468
469impl HeaderMap {
470    /// Create an empty `HeaderMap`.
471    ///
472    /// The map will be created without any capacity. This function will not
473    /// allocate.
474    ///
475    /// # Examples
476    ///
477    /// ```
478    /// # use http::HeaderMap;
479    /// let map = HeaderMap::new();
480    ///
481    /// assert!(map.is_empty());
482    /// assert_eq!(0, map.capacity());
483    /// ```
484    #[inline]
485    pub fn new() -> Self {
486        Self::default()
487    }
488}
489
490impl<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        }
499    }
500}
501
502impl<T> HeaderMap<T> {
503    /// Create an empty `HeaderMap` with the specified capacity.
504    ///
505    /// The returned map will allocate internal storage in order to hold about
506    /// `capacity` elements without reallocating. However, this is a "best
507    /// effort" as there are usage patterns that could cause additional
508    /// allocations before `capacity` headers are stored in the map.
509    ///
510    /// More capacity than requested may be allocated.
511    ///
512    /// # Panics
513    ///
514    /// This method panics if capacity exceeds max `HeaderMap` capacity.
515    ///
516    /// # Examples
517    ///
518    /// ```
519    /// # use http::HeaderMap;
520    /// let map: HeaderMap<u32> = HeaderMap::with_capacity(10);
521    ///
522    /// assert!(map.is_empty());
523    /// assert_eq!(12, map.capacity());
524    /// ```
525    pub fn with_capacity(capacity: usize) -> HeaderMap<T> {
526        Self::try_with_capacity(capacity).expect("size overflows MAX_SIZE")
527    }
528
529    /// Create an empty `HeaderMap` with the specified capacity.
530    ///
531    /// The returned map will allocate internal storage in order to hold about
532    /// `capacity` elements without reallocating. However, this is a "best
533    /// effort" as there are usage patterns that could cause additional
534    /// allocations before `capacity` headers are stored in the map.
535    ///
536    /// More capacity than requested may be allocated.
537    ///
538    /// # Errors
539    ///
540    /// This function may return an error if `HeaderMap` exceeds max capacity
541    ///
542    /// # Examples
543    ///
544    /// ```
545    /// # use http::HeaderMap;
546    /// let map: HeaderMap<u32> = HeaderMap::try_with_capacity(10).unwrap();
547    ///
548    /// assert!(map.is_empty());
549    /// assert_eq!(12, map.capacity());
550    /// ```
551    pub fn try_with_capacity(capacity: usize) -> Result<HeaderMap<T>, MaxSizeReached> {
552        if capacity == 0 {
553            Ok(Self::default())
554        } else {
555            let raw_cap = to_raw_capacity(capacity)?;
556            let raw_cap = match raw_cap.checked_next_power_of_two() {
557                Some(c) => c,
558                None => return Err(MaxSizeReached { _priv: () }),
559            };
560            if raw_cap > MAX_SIZE {
561                return Err(MaxSizeReached { _priv: () });
562            }
563            debug_assert!(raw_cap > 0);
564
565            Ok(HeaderMap {
566                mask: (raw_cap - 1) as Size,
567                indices: vec![Pos::none(); raw_cap].into_boxed_slice(),
568                entries: Vec::with_capacity(usable_capacity(raw_cap)),
569                extra_values: Vec::new(),
570                danger: Danger::Green,
571            })
572        }
573    }
574
575    /// Returns the number of headers stored in the map.
576    ///
577    /// This number represents the total number of **values** stored in the map.
578    /// This number can be greater than or equal to the number of **keys**
579    /// stored given that a single key may have more than one associated value.
580    ///
581    /// # Examples
582    ///
583    /// ```
584    /// # use http::HeaderMap;
585    /// # use http::header::{ACCEPT, HOST};
586    /// let mut map = HeaderMap::new();
587    ///
588    /// assert_eq!(0, map.len());
589    ///
590    /// map.insert(ACCEPT, "text/plain".parse().unwrap());
591    /// map.insert(HOST, "localhost".parse().unwrap());
592    ///
593    /// assert_eq!(2, map.len());
594    ///
595    /// map.append(ACCEPT, "text/html".parse().unwrap());
596    ///
597    /// assert_eq!(3, map.len());
598    /// ```
599    pub fn len(&self) -> usize {
600        self.entries.len() + self.extra_values.len()
601    }
602
603    /// Returns the number of keys stored in the map.
604    ///
605    /// This number will be less than or equal to `len()` as each key may have
606    /// more than one associated value.
607    ///
608    /// # Examples
609    ///
610    /// ```
611    /// # use http::HeaderMap;
612    /// # use http::header::{ACCEPT, HOST};
613    /// let mut map = HeaderMap::new();
614    ///
615    /// assert_eq!(0, map.keys_len());
616    ///
617    /// map.insert(ACCEPT, "text/plain".parse().unwrap());
618    /// map.insert(HOST, "localhost".parse().unwrap());
619    ///
620    /// assert_eq!(2, map.keys_len());
621    ///
622    /// map.insert(ACCEPT, "text/html".parse().unwrap());
623    ///
624    /// assert_eq!(2, map.keys_len());
625    /// ```
626    pub fn keys_len(&self) -> usize {
627        self.entries.len()
628    }
629
630    /// Returns true if the map contains no elements.
631    ///
632    /// # Examples
633    ///
634    /// ```
635    /// # use http::HeaderMap;
636    /// # use http::header::HOST;
637    /// let mut map = HeaderMap::new();
638    ///
639    /// assert!(map.is_empty());
640    ///
641    /// map.insert(HOST, "hello.world".parse().unwrap());
642    ///
643    /// assert!(!map.is_empty());
644    /// ```
645    pub fn is_empty(&self) -> bool {
646        self.entries.len() == 0
647    }
648
649    /// Clears the map, removing all key-value pairs. Keeps the allocated memory
650    /// for reuse.
651    ///
652    /// # Examples
653    ///
654    /// ```
655    /// # use http::HeaderMap;
656    /// # use http::header::HOST;
657    /// let mut map = HeaderMap::new();
658    /// map.insert(HOST, "hello.world".parse().unwrap());
659    ///
660    /// map.clear();
661    /// assert!(map.is_empty());
662    /// assert!(map.capacity() > 0);
663    /// ```
664    pub fn clear(&mut self) {
665        self.entries.clear();
666        self.extra_values.clear();
667        self.danger = Danger::Green;
668
669        for e in self.indices.iter_mut() {
670            *e = Pos::none();
671        }
672    }
673
674    /// Returns the number of headers the map can hold without reallocating.
675    ///
676    /// This number is an approximation as certain usage patterns could cause
677    /// additional allocations before the returned capacity is filled.
678    ///
679    /// # Examples
680    ///
681    /// ```
682    /// # use http::HeaderMap;
683    /// # use http::header::HOST;
684    /// let mut map = HeaderMap::new();
685    ///
686    /// assert_eq!(0, map.capacity());
687    ///
688    /// map.insert(HOST, "hello.world".parse().unwrap());
689    /// assert_eq!(6, map.capacity());
690    /// ```
691    pub fn capacity(&self) -> usize {
692        usable_capacity(self.indices.len())
693    }
694
695    /// Reserves capacity for at least `additional` more headers to be inserted
696    /// into the `HeaderMap`.
697    ///
698    /// The header map may reserve more space to avoid frequent reallocations.
699    /// Like with `with_capacity`, this will be a "best effort" to avoid
700    /// allocations until `additional` more headers are inserted. Certain usage
701    /// patterns could cause additional allocations before the number is
702    /// reached.
703    ///
704    /// # Panics
705    ///
706    /// Panics if the new allocation size overflows `HeaderMap` `MAX_SIZE`.
707    ///
708    /// # Examples
709    ///
710    /// ```
711    /// # use http::HeaderMap;
712    /// # use http::header::HOST;
713    /// let mut map = HeaderMap::new();
714    /// map.reserve(10);
715    /// # map.insert(HOST, "bar".parse().unwrap());
716    /// ```
717    pub fn reserve(&mut self, additional: usize) {
718        self.try_reserve(additional)
719            .expect("size overflows MAX_SIZE")
720    }
721
722    /// Reserves capacity for at least `additional` more headers to be inserted
723    /// into the `HeaderMap`.
724    ///
725    /// The header map may reserve more space to avoid frequent reallocations.
726    /// Like with `with_capacity`, this will be a "best effort" to avoid
727    /// allocations until `additional` more headers are inserted. Certain usage
728    /// patterns could cause additional allocations before the number is
729    /// reached.
730    ///
731    /// # Errors
732    ///
733    /// This method differs from `reserve` by returning an error instead of
734    /// panicking if the value is too large.
735    ///
736    /// # Examples
737    ///
738    /// ```
739    /// # use http::HeaderMap;
740    /// # use http::header::HOST;
741    /// let mut map = HeaderMap::new();
742    /// map.try_reserve(10).unwrap();
743    /// # map.try_insert(HOST, "bar".parse().unwrap()).unwrap();
744    /// ```
745    pub fn try_reserve(&mut self, additional: usize) -> Result<(), MaxSizeReached> {
746        // TODO: This can't overflow if done properly... since the max # of
747        // elements is u16::MAX.
748        let cap = self
749            .entries
750            .len()
751            .checked_add(additional)
752            .ok_or_else(MaxSizeReached::new)?;
753
754        let raw_cap = to_raw_capacity(cap)?;
755
756        if raw_cap > self.indices.len() {
757            let raw_cap = raw_cap
758                .checked_next_power_of_two()
759                .ok_or_else(MaxSizeReached::new)?;
760            if raw_cap > MAX_SIZE {
761                return Err(MaxSizeReached::new());
762            }
763
764            if self.entries.is_empty() {
765                self.mask = raw_cap as Size - 1;
766                self.indices = vec![Pos::none(); raw_cap].into_boxed_slice();
767                self.entries = Vec::with_capacity(usable_capacity(raw_cap));
768            } else {
769                self.try_grow(raw_cap)?;
770            }
771        }
772
773        Ok(())
774    }
775
776    /// Returns a reference to the value associated with the key.
777    ///
778    /// If there are multiple values associated with the key, then the first one
779    /// is returned. Use `get_all` to get all values associated with a given
780    /// key. Returns `None` if there are no values associated with the key.
781    ///
782    /// # Examples
783    ///
784    /// ```
785    /// # use http::HeaderMap;
786    /// # use http::header::HOST;
787    /// let mut map = HeaderMap::new();
788    /// assert!(map.get("host").is_none());
789    ///
790    /// map.insert(HOST, "hello".parse().unwrap());
791    /// assert_eq!(map.get(HOST).unwrap(), &"hello");
792    /// assert_eq!(map.get("host").unwrap(), &"hello");
793    ///
794    /// map.append(HOST, "world".parse().unwrap());
795    /// assert_eq!(map.get("host").unwrap(), &"hello");
796    /// ```
797    pub fn get<K>(&self, key: K) -> Option<&T>
798    where
799        K: AsHeaderName,
800    {
801        self.get2(&key)
802    }
803
804    fn get2<K>(&self, key: &K) -> Option<&T>
805    where
806        K: AsHeaderName,
807    {
808        match key.find(self) {
809            Some((_, found)) => {
810                let entry = &self.entries[found];
811                Some(&entry.value)
812            }
813            None => None,
814        }
815    }
816
817    /// Returns a mutable reference to the value associated with the key.
818    ///
819    /// If there are multiple values associated with the key, then the first one
820    /// is returned. Use `entry` to get all values associated with a given
821    /// key. Returns `None` if there are no values associated with the key.
822    ///
823    /// # Examples
824    ///
825    /// ```
826    /// # use http::HeaderMap;
827    /// # use http::header::HOST;
828    /// let mut map = HeaderMap::default();
829    /// map.insert(HOST, "hello".to_string());
830    /// map.get_mut("host").unwrap().push_str("-world");
831    ///
832    /// assert_eq!(map.get(HOST).unwrap(), &"hello-world");
833    /// ```
834    pub fn get_mut<K>(&mut self, key: K) -> Option<&mut T>
835    where
836        K: AsHeaderName,
837    {
838        match key.find(self) {
839            Some((_, found)) => {
840                let entry = &mut self.entries[found];
841                Some(&mut entry.value)
842            }
843            None => None,
844        }
845    }
846
847    /// Returns a view of all values associated with a key.
848    ///
849    /// The returned view does not incur any allocations and allows iterating
850    /// the values associated with the key.  See [`GetAll`] for more details.
851    /// Returns `None` if there are no values associated with the key.
852    ///
853    /// [`GetAll`]: struct.GetAll.html
854    ///
855    /// # Examples
856    ///
857    /// ```
858    /// # use http::HeaderMap;
859    /// # use http::header::HOST;
860    /// let mut map = HeaderMap::new();
861    ///
862    /// map.insert(HOST, "hello".parse().unwrap());
863    /// map.append(HOST, "goodbye".parse().unwrap());
864    ///
865    /// let view = map.get_all("host");
866    ///
867    /// let mut iter = view.iter();
868    /// assert_eq!(&"hello", iter.next().unwrap());
869    /// assert_eq!(&"goodbye", iter.next().unwrap());
870    /// assert!(iter.next().is_none());
871    /// ```
872    pub fn get_all<K>(&self, key: K) -> GetAll<'_, T>
873    where
874        K: AsHeaderName,
875    {
876        GetAll {
877            map: self,
878            index: key.find(self).map(|(_, i)| i),
879        }
880    }
881
882    /// Returns true if the map contains a value for the specified key.
883    ///
884    /// # Examples
885    ///
886    /// ```
887    /// # use http::HeaderMap;
888    /// # use http::header::HOST;
889    /// let mut map = HeaderMap::new();
890    /// assert!(!map.contains_key(HOST));
891    ///
892    /// map.insert(HOST, "world".parse().unwrap());
893    /// assert!(map.contains_key("host"));
894    /// ```
895    pub fn contains_key<K>(&self, key: K) -> bool
896    where
897        K: AsHeaderName,
898    {
899        key.find(self).is_some()
900    }
901
902    /// An iterator visiting all key-value pairs.
903    ///
904    /// The iteration order is arbitrary, but consistent across platforms for
905    /// the same crate version. Each key will be yielded once per associated
906    /// value. So, if a key has 3 associated values, it will be yielded 3 times.
907    ///
908    /// # Examples
909    ///
910    /// ```
911    /// # use http::HeaderMap;
912    /// # use http::header::{CONTENT_LENGTH, HOST};
913    /// let mut map = HeaderMap::new();
914    ///
915    /// map.insert(HOST, "hello".parse().unwrap());
916    /// map.append(HOST, "goodbye".parse().unwrap());
917    /// map.insert(CONTENT_LENGTH, "123".parse().unwrap());
918    ///
919    /// for (key, value) in map.iter() {
920    ///     println!("{:?}: {:?}", key, value);
921    /// }
922    /// ```
923    pub fn iter(&self) -> Iter<'_, T> {
924        Iter {
925            map: self,
926            entry: 0,
927            cursor: self.entries.first().map(|_| Cursor::Head),
928        }
929    }
930
931    /// An iterator visiting all key-value pairs, with mutable value references.
932    ///
933    /// The iterator order is arbitrary, but consistent across platforms for the
934    /// same crate version. Each key will be yielded once per associated value,
935    /// so if a key has 3 associated values, it will be yielded 3 times.
936    ///
937    /// # Examples
938    ///
939    /// ```
940    /// # use http::HeaderMap;
941    /// # use http::header::{CONTENT_LENGTH, HOST};
942    /// let mut map = HeaderMap::default();
943    ///
944    /// map.insert(HOST, "hello".to_string());
945    /// map.append(HOST, "goodbye".to_string());
946    /// map.insert(CONTENT_LENGTH, "123".to_string());
947    ///
948    /// for (key, value) in map.iter_mut() {
949    ///     value.push_str("-boop");
950    /// }
951    /// ```
952    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
953        IterMut {
954            map: self as *mut _,
955            entry: 0,
956            cursor: self.entries.first().map(|_| Cursor::Head),
957            lt: PhantomData,
958        }
959    }
960
961    /// An iterator visiting all keys.
962    ///
963    /// The iteration order is arbitrary, but consistent across platforms for
964    /// the same crate version. Each key will be yielded only once even if it
965    /// has multiple associated values.
966    ///
967    /// # Examples
968    ///
969    /// ```
970    /// # use http::HeaderMap;
971    /// # use http::header::{CONTENT_LENGTH, HOST};
972    /// let mut map = HeaderMap::new();
973    ///
974    /// map.insert(HOST, "hello".parse().unwrap());
975    /// map.append(HOST, "goodbye".parse().unwrap());
976    /// map.insert(CONTENT_LENGTH, "123".parse().unwrap());
977    ///
978    /// for key in map.keys() {
979    ///     println!("{:?}", key);
980    /// }
981    /// ```
982    pub fn keys(&self) -> Keys<'_, T> {
983        Keys {
984            inner: self.entries.iter(),
985        }
986    }
987
988    /// An iterator visiting all values.
989    ///
990    /// The iteration order is arbitrary, but consistent across platforms for
991    /// the same crate version.
992    ///
993    /// # Examples
994    ///
995    /// ```
996    /// # use http::HeaderMap;
997    /// # use http::header::{CONTENT_LENGTH, HOST};
998    /// let mut map = HeaderMap::new();
999    ///
1000    /// map.insert(HOST, "hello".parse().unwrap());
1001    /// map.append(HOST, "goodbye".parse().unwrap());
1002    /// map.insert(CONTENT_LENGTH, "123".parse().unwrap());
1003    ///
1004    /// for value in map.values() {
1005    ///     println!("{:?}", value);
1006    /// }
1007    /// ```
1008    pub fn values(&self) -> Values<'_, T> {
1009        Values { inner: self.iter() }
1010    }
1011
1012    /// An iterator visiting all values mutably.
1013    ///
1014    /// The iteration order is arbitrary, but consistent across platforms for
1015    /// the same crate version.
1016    ///
1017    /// # Examples
1018    ///
1019    /// ```
1020    /// # use http::HeaderMap;
1021    /// # use http::header::{CONTENT_LENGTH, HOST};
1022    /// let mut map = HeaderMap::default();
1023    ///
1024    /// map.insert(HOST, "hello".to_string());
1025    /// map.append(HOST, "goodbye".to_string());
1026    /// map.insert(CONTENT_LENGTH, "123".to_string());
1027    ///
1028    /// for value in map.values_mut() {
1029    ///     value.push_str("-boop");
1030    /// }
1031    /// ```
1032    pub fn values_mut(&mut self) -> ValuesMut<'_, T> {
1033        ValuesMut {
1034            inner: self.iter_mut(),
1035        }
1036    }
1037
1038    /// Clears the map, returning all entries as an iterator.
1039    ///
1040    /// The internal memory is kept for reuse.
1041    ///
1042    /// For each yielded item that has `None` provided for the `HeaderName`,
1043    /// then the associated header name is the same as that of the previously
1044    /// yielded item. The first yielded item will have `HeaderName` set.
1045    ///
1046    /// # Examples
1047    ///
1048    /// ```
1049    /// # use http::HeaderMap;
1050    /// # use http::header::{CONTENT_LENGTH, HOST};
1051    /// let mut map = HeaderMap::new();
1052    ///
1053    /// map.insert(HOST, "hello".parse().unwrap());
1054    /// map.append(HOST, "goodbye".parse().unwrap());
1055    /// map.insert(CONTENT_LENGTH, "123".parse().unwrap());
1056    ///
1057    /// let mut drain = map.drain();
1058    ///
1059    ///
1060    /// assert_eq!(drain.next(), Some((Some(HOST), "hello".parse().unwrap())));
1061    /// assert_eq!(drain.next(), Some((None, "goodbye".parse().unwrap())));
1062    ///
1063    /// assert_eq!(drain.next(), Some((Some(CONTENT_LENGTH), "123".parse().unwrap())));
1064    ///
1065    /// assert_eq!(drain.next(), None);
1066    /// ```
1067    pub fn drain(&mut self) -> Drain<'_, T> {
1068        for i in self.indices.iter_mut() {
1069            *i = Pos::none();
1070        }
1071
1072        // Memory safety
1073        //
1074        // When the Drain is first created, it shortens the length of
1075        // the source vector to make sure no uninitialized or moved-from
1076        // elements are accessible at all if the Drain's destructor never
1077        // gets to run.
1078
1079        let entries = &mut self.entries[..] as *mut _;
1080        let extra_values = &mut self.extra_values as *mut _;
1081        let len = self.entries.len();
1082        unsafe {
1083            self.entries.set_len(0);
1084        }
1085
1086        Drain {
1087            idx: 0,
1088            len,
1089            entries,
1090            extra_values,
1091            next: None,
1092            lt: PhantomData,
1093        }
1094    }
1095
1096    fn value_iter(&self, idx: Option<usize>) -> ValueIter<'_, T> {
1097        use self::Cursor::*;
1098
1099        if let Some(idx) = idx {
1100            let back = {
1101                let entry = &self.entries[idx];
1102
1103                entry.links.map(|l| Values(l.tail)).unwrap_or(Head)
1104            };
1105
1106            ValueIter {
1107                map: self,
1108                index: idx,
1109                front: Some(Head),
1110                back: Some(back),
1111            }
1112        } else {
1113            ValueIter {
1114                map: self,
1115                index: usize::MAX,
1116                front: None,
1117                back: None,
1118            }
1119        }
1120    }
1121
1122    fn value_iter_mut(&mut self, idx: usize) -> ValueIterMut<'_, T> {
1123        use self::Cursor::*;
1124
1125        let back = {
1126            let entry = &self.entries[idx];
1127
1128            entry.links.map(|l| Values(l.tail)).unwrap_or(Head)
1129        };
1130
1131        ValueIterMut {
1132            map: self as *mut _,
1133            index: idx,
1134            front: Some(Head),
1135            back: Some(back),
1136            lt: PhantomData,
1137        }
1138    }
1139
1140    /// Gets the given key's corresponding entry in the map for in-place
1141    /// manipulation.
1142    ///
1143    /// # Panics
1144    ///
1145    /// This method panics if capacity exceeds max `HeaderMap` capacity
1146    ///
1147    /// # Examples
1148    ///
1149    /// ```
1150    /// # use http::HeaderMap;
1151    /// let mut map: HeaderMap<u32> = HeaderMap::default();
1152    ///
1153    /// let headers = &[
1154    ///     "content-length",
1155    ///     "x-hello",
1156    ///     "Content-Length",
1157    ///     "x-world",
1158    /// ];
1159    ///
1160    /// for &header in headers {
1161    ///     let counter = map.entry(header).or_insert(0);
1162    ///     *counter += 1;
1163    /// }
1164    ///
1165    /// assert_eq!(map["content-length"], 2);
1166    /// assert_eq!(map["x-hello"], 1);
1167    /// ```
1168    pub fn entry<K>(&mut self, key: K) -> Entry<'_, T>
1169    where
1170        K: IntoHeaderName,
1171    {
1172        key.try_entry(self).expect("size overflows MAX_SIZE")
1173    }
1174
1175    /// Gets the given key's corresponding entry in the map for in-place
1176    /// manipulation.
1177    ///
1178    /// # Errors
1179    ///
1180    /// This method differs from `entry` by allowing types that may not be
1181    /// valid `HeaderName`s to passed as the key (such as `String`). If they
1182    /// do not parse as a valid `HeaderName`, this returns an
1183    /// `InvalidHeaderName` error.
1184    ///
1185    /// If reserving space goes over the maximum, this will also return an
1186    /// error. However, to prevent breaking changes to the return type, the
1187    /// error will still say `InvalidHeaderName`, unlike other `try_*` methods
1188    /// which return a `MaxSizeReached` error.
1189    pub fn try_entry<K>(&mut self, key: K) -> Result<Entry<'_, T>, InvalidHeaderName>
1190    where
1191        K: AsHeaderName,
1192    {
1193        key.try_entry(self).map_err(|err| match err {
1194            as_header_name::TryEntryError::InvalidHeaderName(e) => e,
1195            as_header_name::TryEntryError::MaxSizeReached(_e) => {
1196                // Unfortunately, we cannot change the return type of this
1197                // method, so the max size reached error needs to be converted
1198                // into an InvalidHeaderName. Yay.
1199                InvalidHeaderName::new()
1200            }
1201        })
1202    }
1203
1204    fn try_entry2<K>(&mut self, key: K) -> Result<Entry<'_, T>, MaxSizeReached>
1205    where
1206        K: Hash + Into<HeaderName>,
1207        HeaderName: PartialEq<K>,
1208    {
1209        // Ensure that there is space in the map
1210        self.try_reserve_one()?;
1211
1212        Ok(insert_phase_one!(
1213            self,
1214            key,
1215            probe,
1216            pos,
1217            hash,
1218            danger,
1219            Entry::Vacant(VacantEntry {
1220                map: self,
1221                hash,
1222                key: key.into(),
1223                probe,
1224                danger,
1225            }),
1226            Entry::Occupied(OccupiedEntry {
1227                map: self,
1228                index: pos,
1229                probe,
1230            }),
1231            Entry::Vacant(VacantEntry {
1232                map: self,
1233                hash,
1234                key: key.into(),
1235                probe,
1236                danger,
1237            })
1238        ))
1239    }
1240
1241    /// Inserts a key-value pair into the map.
1242    ///
1243    /// If the map did not previously have this key present, then `None` is
1244    /// returned.
1245    ///
1246    /// If the map did have this key present, the new value is associated with
1247    /// the key and all previous values are removed. **Note** that only a single
1248    /// one of the previous values is returned. If there are multiple values
1249    /// that have been previously associated with the key, then the first one is
1250    /// returned. See `insert_mult` on `OccupiedEntry` for an API that returns
1251    /// all values.
1252    ///
1253    /// The key is not updated, though; this matters for types that can be `==`
1254    /// without being identical.
1255    ///
1256    /// # Panics
1257    ///
1258    /// This method panics if capacity exceeds max `HeaderMap` capacity
1259    ///
1260    /// # Examples
1261    ///
1262    /// ```
1263    /// # use http::HeaderMap;
1264    /// # use http::header::HOST;
1265    /// let mut map = HeaderMap::new();
1266    /// assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
1267    /// assert!(!map.is_empty());
1268    ///
1269    /// let mut prev = map.insert(HOST, "earth".parse().unwrap()).unwrap();
1270    /// assert_eq!("world", prev);
1271    /// ```
1272    pub fn insert<K>(&mut self, key: K, val: T) -> Option<T>
1273    where
1274        K: IntoHeaderName,
1275    {
1276        self.try_insert(key, val).expect("size overflows MAX_SIZE")
1277    }
1278
1279    /// Inserts a key-value pair into the map.
1280    ///
1281    /// If the map did not previously have this key present, then `None` is
1282    /// returned.
1283    ///
1284    /// If the map did have this key present, the new value is associated with
1285    /// the key and all previous values are removed. **Note** that only a single
1286    /// one of the previous values is returned. If there are multiple values
1287    /// that have been previously associated with the key, then the first one is
1288    /// returned. See `insert_mult` on `OccupiedEntry` for an API that returns
1289    /// all values.
1290    ///
1291    /// The key is not updated, though; this matters for types that can be `==`
1292    /// without being identical.
1293    ///
1294    /// # Errors
1295    ///
1296    /// This function may return an error if `HeaderMap` exceeds max capacity
1297    ///
1298    /// # Examples
1299    ///
1300    /// ```
1301    /// # use http::HeaderMap;
1302    /// # use http::header::HOST;
1303    /// let mut map = HeaderMap::new();
1304    /// assert!(map.try_insert(HOST, "world".parse().unwrap()).unwrap().is_none());
1305    /// assert!(!map.is_empty());
1306    ///
1307    /// let mut prev = map.try_insert(HOST, "earth".parse().unwrap()).unwrap().unwrap();
1308    /// assert_eq!("world", prev);
1309    /// ```
1310    pub fn try_insert<K>(&mut self, key: K, val: T) -> Result<Option<T>, MaxSizeReached>
1311    where
1312        K: IntoHeaderName,
1313    {
1314        key.try_insert(self, val)
1315    }
1316
1317    #[inline]
1318    fn try_insert2<K>(&mut self, key: K, value: T) -> Result<Option<T>, MaxSizeReached>
1319    where
1320        K: Hash + Into<HeaderName>,
1321        HeaderName: PartialEq<K>,
1322    {
1323        self.try_reserve_one()?;
1324
1325        Ok(insert_phase_one!(
1326            self,
1327            key,
1328            probe,
1329            pos,
1330            hash,
1331            danger,
1332            // Vacant
1333            {
1334                let _ = danger; // Make lint happy
1335                let index = self.entries.len();
1336                self.try_insert_entry(hash, key.into(), value)?;
1337                self.indices[probe] = Pos::new(index, hash);
1338                None
1339            },
1340            // Occupied
1341            Some(self.insert_occupied(pos, value)),
1342            // Robinhood
1343            {
1344                self.try_insert_phase_two(key.into(), value, hash, probe, danger)?;
1345                None
1346            }
1347        ))
1348    }
1349
1350    /// Set an occupied bucket to the given value
1351    #[inline]
1352    fn insert_occupied(&mut self, index: usize, value: T) -> T {
1353        if let Some(links) = self.entries[index].links {
1354            self.remove_all_extra_values(links.next);
1355        }
1356
1357        let entry = &mut self.entries[index];
1358        mem::replace(&mut entry.value, value)
1359    }
1360
1361    fn insert_occupied_mult(&mut self, index: usize, value: T) -> ValueDrain<'_, T> {
1362        let old;
1363        let links;
1364
1365        {
1366            let entry = &mut self.entries[index];
1367
1368            old = mem::replace(&mut entry.value, value);
1369            links = entry.links.take();
1370        }
1371
1372        let raw_links = self.raw_links();
1373        let extra_values = &mut self.extra_values;
1374
1375        let next =
1376            links.map(|l| drain_all_extra_values(raw_links, extra_values, l.next).into_iter());
1377
1378        ValueDrain {
1379            first: Some(old),
1380            next,
1381            lt: PhantomData,
1382        }
1383    }
1384
1385    /// Inserts a key-value pair into the map.
1386    ///
1387    /// If the map did not previously have this key present, then `false` is
1388    /// returned.
1389    ///
1390    /// If the map did have this key present, the new value is pushed to the end
1391    /// of the list of values currently associated with the key. The key is not
1392    /// updated, though; this matters for types that can be `==` without being
1393    /// identical.
1394    ///
1395    /// # Panics
1396    ///
1397    /// This method panics if capacity exceeds max `HeaderMap` capacity
1398    ///
1399    /// # Examples
1400    ///
1401    /// ```
1402    /// # use http::HeaderMap;
1403    /// # use http::header::HOST;
1404    /// let mut map = HeaderMap::new();
1405    /// assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
1406    /// assert!(!map.is_empty());
1407    ///
1408    /// map.append(HOST, "earth".parse().unwrap());
1409    ///
1410    /// let values = map.get_all("host");
1411    /// let mut i = values.iter();
1412    /// assert_eq!("world", *i.next().unwrap());
1413    /// assert_eq!("earth", *i.next().unwrap());
1414    /// ```
1415    pub fn append<K>(&mut self, key: K, value: T) -> bool
1416    where
1417        K: IntoHeaderName,
1418    {
1419        self.try_append(key, value)
1420            .expect("size overflows MAX_SIZE")
1421    }
1422
1423    /// Inserts a key-value pair into the map.
1424    ///
1425    /// If the map did not previously have this key present, then `false` is
1426    /// returned.
1427    ///
1428    /// If the map did have this key present, the new value is pushed to the end
1429    /// of the list of values currently associated with the key. The key is not
1430    /// updated, though; this matters for types that can be `==` without being
1431    /// identical.
1432    ///
1433    /// # Errors
1434    ///
1435    /// This function may return an error if `HeaderMap` exceeds max capacity
1436    ///
1437    /// # Examples
1438    ///
1439    /// ```
1440    /// # use http::HeaderMap;
1441    /// # use http::header::HOST;
1442    /// let mut map = HeaderMap::new();
1443    /// assert!(map.try_insert(HOST, "world".parse().unwrap()).unwrap().is_none());
1444    /// assert!(!map.is_empty());
1445    ///
1446    /// map.try_append(HOST, "earth".parse().unwrap()).unwrap();
1447    ///
1448    /// let values = map.get_all("host");
1449    /// let mut i = values.iter();
1450    /// assert_eq!("world", *i.next().unwrap());
1451    /// assert_eq!("earth", *i.next().unwrap());
1452    /// ```
1453    pub fn try_append<K>(&mut self, key: K, value: T) -> Result<bool, MaxSizeReached>
1454    where
1455        K: IntoHeaderName,
1456    {
1457        key.try_append(self, value)
1458    }
1459
1460    #[inline]
1461    fn try_append2<K>(&mut self, key: K, value: T) -> Result<bool, MaxSizeReached>
1462    where
1463        K: Hash + Into<HeaderName>,
1464        HeaderName: PartialEq<K>,
1465    {
1466        self.try_reserve_one()?;
1467
1468        Ok(insert_phase_one!(
1469            self,
1470            key,
1471            probe,
1472            pos,
1473            hash,
1474            danger,
1475            // Vacant
1476            {
1477                let _ = danger;
1478                let index = self.entries.len();
1479                self.try_insert_entry(hash, key.into(), value)?;
1480                self.indices[probe] = Pos::new(index, hash);
1481                false
1482            },
1483            // Occupied
1484            {
1485                append_value(pos, &mut self.entries[pos], &mut self.extra_values, value);
1486                true
1487            },
1488            // Robinhood
1489            {
1490                self.try_insert_phase_two(key.into(), value, hash, probe, danger)?;
1491
1492                false
1493            }
1494        ))
1495    }
1496
1497    #[inline]
1498    fn find<K>(&self, key: &K) -> Option<(usize, usize)>
1499    where
1500        K: Hash + Into<HeaderName> + ?Sized,
1501        HeaderName: PartialEq<K>,
1502    {
1503        if self.entries.is_empty() {
1504            return None;
1505        }
1506
1507        let hash = hash_elem_using(&self.danger, key);
1508        let mask = self.mask;
1509        let mut probe = desired_pos(mask, hash);
1510        let mut dist = 0;
1511
1512        probe_loop!(probe < self.indices.len(), {
1513            if let Some((i, entry_hash)) = self.indices[probe].resolve() {
1514                if dist > probe_distance(mask, entry_hash, probe) {
1515                    // give up when probe distance is too long
1516                    return None;
1517                } else if entry_hash == hash && self.entries[i].key == *key {
1518                    return Some((probe, i));
1519                }
1520            } else {
1521                return None;
1522            }
1523
1524            dist += 1;
1525        });
1526    }
1527
1528    /// phase 2 is post-insert where we forward-shift `Pos` in the indices.
1529    #[inline]
1530    fn try_insert_phase_two(
1531        &mut self,
1532        key: HeaderName,
1533        value: T,
1534        hash: HashValue,
1535        probe: usize,
1536        danger: bool,
1537    ) -> Result<usize, MaxSizeReached> {
1538        // Push the value and get the index
1539        let index = self.entries.len();
1540        self.try_insert_entry(hash, key, value)?;
1541
1542        let num_displaced = do_insert_phase_two(&mut self.indices, probe, Pos::new(index, hash));
1543
1544        if danger || num_displaced >= DISPLACEMENT_THRESHOLD {
1545            // Increase danger level
1546            self.danger.set_yellow();
1547        }
1548
1549        Ok(index)
1550    }
1551
1552    /// Removes a key from the map, returning the value associated with the key.
1553    ///
1554    /// Returns `None` if the map does not contain the key. If there are
1555    /// multiple values associated with the key, then the first one is returned.
1556    /// See `remove_entry_mult` on `OccupiedEntry` for an API that yields all
1557    /// values.
1558    ///
1559    /// # Examples
1560    ///
1561    /// ```
1562    /// # use http::HeaderMap;
1563    /// # use http::header::HOST;
1564    /// let mut map = HeaderMap::new();
1565    /// map.insert(HOST, "hello.world".parse().unwrap());
1566    ///
1567    /// let prev = map.remove(HOST).unwrap();
1568    /// assert_eq!("hello.world", prev);
1569    ///
1570    /// assert!(map.remove(HOST).is_none());
1571    /// ```
1572    pub fn remove<K>(&mut self, key: K) -> Option<T>
1573    where
1574        K: AsHeaderName,
1575    {
1576        match key.find(self) {
1577            Some((probe, idx)) => {
1578                if let Some(links) = self.entries[idx].links {
1579                    self.remove_all_extra_values(links.next);
1580                }
1581
1582                let entry = self.remove_found(probe, idx);
1583
1584                Some(entry.value)
1585            }
1586            None => None,
1587        }
1588    }
1589
1590    /// Remove an entry from the map.
1591    ///
1592    /// Warning: To avoid inconsistent state, extra values _must_ be removed
1593    /// for the `found` index (via `remove_all_extra_values` or similar)
1594    /// _before_ this method is called.
1595    #[inline]
1596    fn remove_found(&mut self, probe: usize, found: usize) -> Bucket<T> {
1597        // index `probe` and entry `found` is to be removed
1598        // use swap_remove, but then we need to update the index that points
1599        // to the other entry that has to move
1600        self.indices[probe] = Pos::none();
1601        let entry = self.entries.swap_remove(found);
1602
1603        // correct index that points to the entry that had to swap places
1604        if let Some(entry) = self.entries.get(found) {
1605            // was not last element
1606            // examine new element in `found` and find it in indices
1607            let mut probe = desired_pos(self.mask, entry.hash);
1608
1609            probe_loop!(probe < self.indices.len(), {
1610                if let Some((i, _)) = self.indices[probe].resolve() {
1611                    if i >= self.entries.len() {
1612                        // found it
1613                        self.indices[probe] = Pos::new(found, entry.hash);
1614                        break;
1615                    }
1616                }
1617            });
1618
1619            // Update links
1620            if let Some(links) = entry.links {
1621                self.extra_values[links.next].prev = Link::Entry(found);
1622                self.extra_values[links.tail].next = Link::Entry(found);
1623            }
1624        }
1625
1626        // backward shift deletion in self.indices
1627        // after probe, shift all non-ideally placed indices backward
1628        if !self.entries.is_empty() {
1629            let mut last_probe = probe;
1630            let mut probe = probe + 1;
1631
1632            probe_loop!(probe < self.indices.len(), {
1633                if let Some((_, entry_hash)) = self.indices[probe].resolve() {
1634                    if probe_distance(self.mask, entry_hash, probe) > 0 {
1635                        self.indices[last_probe] = self.indices[probe];
1636                        self.indices[probe] = Pos::none();
1637                    } else {
1638                        break;
1639                    }
1640                } else {
1641                    break;
1642                }
1643
1644                last_probe = probe;
1645            });
1646        }
1647
1648        entry
1649    }
1650
1651    /// Removes the `ExtraValue` at the given index.
1652    #[inline]
1653    fn remove_extra_value(&mut self, idx: usize) -> ExtraValue<T> {
1654        let raw_links = self.raw_links();
1655        remove_extra_value(raw_links, &mut self.extra_values, idx)
1656    }
1657
1658    fn remove_all_extra_values(&mut self, mut head: usize) {
1659        loop {
1660            let extra = self.remove_extra_value(head);
1661
1662            if let Link::Extra(idx) = extra.next {
1663                head = idx;
1664            } else {
1665                break;
1666            }
1667        }
1668    }
1669
1670    #[inline]
1671    fn try_insert_entry(
1672        &mut self,
1673        hash: HashValue,
1674        key: HeaderName,
1675        value: T,
1676    ) -> Result<(), MaxSizeReached> {
1677        if self.entries.len() >= MAX_SIZE {
1678            return Err(MaxSizeReached::new());
1679        }
1680
1681        self.entries.push(Bucket {
1682            hash,
1683            key,
1684            value,
1685            links: None,
1686        });
1687
1688        Ok(())
1689    }
1690
1691    fn rebuild(&mut self) {
1692        // Loop over all entries and re-insert them into the map
1693        'outer: for (index, entry) in self.entries.iter_mut().enumerate() {
1694            let hash = hash_elem_using(&self.danger, &entry.key);
1695            let mut probe = desired_pos(self.mask, hash);
1696            let mut dist = 0;
1697
1698            // Update the entry's hash code
1699            entry.hash = hash;
1700
1701            probe_loop!(probe < self.indices.len(), {
1702                if let Some((_, entry_hash)) = self.indices[probe].resolve() {
1703                    // if existing element probed less than us, swap
1704                    let their_dist = probe_distance(self.mask, entry_hash, probe);
1705
1706                    if their_dist < dist {
1707                        // Robinhood
1708                        break;
1709                    }
1710                } else {
1711                    // Vacant slot
1712                    self.indices[probe] = Pos::new(index, hash);
1713                    continue 'outer;
1714                }
1715
1716                dist += 1;
1717            });
1718
1719            do_insert_phase_two(&mut self.indices, probe, Pos::new(index, hash));
1720        }
1721    }
1722
1723    fn reinsert_entry_in_order(&mut self, pos: Pos) {
1724        if let Some((_, entry_hash)) = pos.resolve() {
1725            // Find first empty bucket and insert there
1726            let mut probe = desired_pos(self.mask, entry_hash);
1727
1728            probe_loop!(probe < self.indices.len(), {
1729                if self.indices[probe].resolve().is_none() {
1730                    // empty bucket, insert here
1731                    self.indices[probe] = pos;
1732                    return;
1733                }
1734            });
1735        }
1736    }
1737
1738    fn try_reserve_one(&mut self) -> Result<(), MaxSizeReached> {
1739        let len = self.entries.len();
1740
1741        if self.danger.is_yellow() {
1742            let load_factor = self.entries.len() as f32 / self.indices.len() as f32;
1743
1744            if load_factor >= LOAD_FACTOR_THRESHOLD {
1745                // Transition back to green danger level
1746                self.danger.set_green();
1747
1748                // Double the capacity
1749                let new_cap = self.indices.len() * 2;
1750
1751                // Grow the capacity
1752                self.try_grow(new_cap)?;
1753            } else {
1754                self.danger.set_red();
1755
1756                // Rebuild hash table
1757                for index in self.indices.iter_mut() {
1758                    *index = Pos::none();
1759                }
1760
1761                self.rebuild();
1762            }
1763        } else if len == self.capacity() {
1764            if len == 0 {
1765                let new_raw_cap = 8;
1766                self.mask = 8 - 1;
1767                self.indices = vec![Pos::none(); new_raw_cap].into_boxed_slice();
1768                self.entries = Vec::with_capacity(usable_capacity(new_raw_cap));
1769            } else {
1770                let raw_cap = self.indices.len();
1771                self.try_grow(raw_cap << 1)?;
1772            }
1773        }
1774
1775        Ok(())
1776    }
1777
1778    #[inline]
1779    fn try_grow(&mut self, new_raw_cap: usize) -> Result<(), MaxSizeReached> {
1780        if new_raw_cap > MAX_SIZE {
1781            return Err(MaxSizeReached::new());
1782        }
1783
1784        // find first ideally placed element -- start of cluster
1785        let mut first_ideal = 0;
1786
1787        for (i, pos) in self.indices.iter().enumerate() {
1788            if let Some((_, entry_hash)) = pos.resolve() {
1789                if 0 == probe_distance(self.mask, entry_hash, i) {
1790                    first_ideal = i;
1791                    break;
1792                }
1793            }
1794        }
1795
1796        // visit the entries in an order where we can simply reinsert them
1797        // into self.indices without any bucket stealing.
1798        let old_indices = mem::replace(
1799            &mut self.indices,
1800            vec![Pos::none(); new_raw_cap].into_boxed_slice(),
1801        );
1802        self.mask = new_raw_cap.wrapping_sub(1) as Size;
1803
1804        for &pos in &old_indices[first_ideal..] {
1805            self.reinsert_entry_in_order(pos);
1806        }
1807
1808        for &pos in &old_indices[..first_ideal] {
1809            self.reinsert_entry_in_order(pos);
1810        }
1811
1812        // Reserve additional entry slots
1813        let more = self.capacity() - self.entries.len();
1814        self.entries.reserve_exact(more);
1815        Ok(())
1816    }
1817
1818    #[inline]
1819    fn raw_links(&mut self) -> RawLinks<T> {
1820        RawLinks(&mut self.entries[..] as *mut _)
1821    }
1822}
1823
1824/// Removes the `ExtraValue` at the given index.
1825#[inline]
1826fn remove_extra_value<T>(
1827    mut raw_links: RawLinks<T>,
1828    extra_values: &mut Vec<ExtraValue<T>>,
1829    idx: usize,
1830) -> ExtraValue<T> {
1831    let prev;
1832    let next;
1833
1834    {
1835        debug_assert!(extra_values.len() > idx);
1836        let extra = &extra_values[idx];
1837        prev = extra.prev;
1838        next = extra.next;
1839    }
1840
1841    // First unlink the extra value
1842    match (prev, next) {
1843        (Link::Entry(prev), Link::Entry(next)) => {
1844            debug_assert_eq!(prev, next);
1845
1846            raw_links[prev] = None;
1847        }
1848        (Link::Entry(prev), Link::Extra(next)) => {
1849            debug_assert!(raw_links[prev].is_some());
1850
1851            raw_links[prev].as_mut().unwrap().next = next;
1852
1853            debug_assert!(extra_values.len() > next);
1854            extra_values[next].prev = Link::Entry(prev);
1855        }
1856        (Link::Extra(prev), Link::Entry(next)) => {
1857            debug_assert!(raw_links[next].is_some());
1858
1859            raw_links[next].as_mut().unwrap().tail = prev;
1860
1861            debug_assert!(extra_values.len() > prev);
1862            extra_values[prev].next = Link::Entry(next);
1863        }
1864        (Link::Extra(prev), Link::Extra(next)) => {
1865            debug_assert!(extra_values.len() > next);
1866            debug_assert!(extra_values.len() > prev);
1867
1868            extra_values[prev].next = Link::Extra(next);
1869            extra_values[next].prev = Link::Extra(prev);
1870        }
1871    }
1872
1873    // Remove the extra value
1874    let mut extra = extra_values.swap_remove(idx);
1875
1876    // This is the index of the value that was moved (possibly `extra`)
1877    let old_idx = extra_values.len();
1878
1879    // Update the links
1880    if extra.prev == Link::Extra(old_idx) {
1881        extra.prev = Link::Extra(idx);
1882    }
1883
1884    if extra.next == Link::Extra(old_idx) {
1885        extra.next = Link::Extra(idx);
1886    }
1887
1888    // Check if another entry was displaced. If it was, then the links
1889    // need to be fixed.
1890    if idx != old_idx {
1891        let next;
1892        let prev;
1893
1894        {
1895            debug_assert!(extra_values.len() > idx);
1896            let moved = &extra_values[idx];
1897            next = moved.next;
1898            prev = moved.prev;
1899        }
1900
1901        // An entry was moved, we have to the links
1902        match prev {
1903            Link::Entry(entry_idx) => {
1904                // It is critical that we do not attempt to read the
1905                // header name or value as that memory may have been
1906                // "released" already.
1907                debug_assert!(raw_links[entry_idx].is_some());
1908
1909                let links = raw_links[entry_idx].as_mut().unwrap();
1910                links.next = idx;
1911            }
1912            Link::Extra(extra_idx) => {
1913                debug_assert!(extra_values.len() > extra_idx);
1914                extra_values[extra_idx].next = Link::Extra(idx);
1915            }
1916        }
1917
1918        match next {
1919            Link::Entry(entry_idx) => {
1920                debug_assert!(raw_links[entry_idx].is_some());
1921
1922                let links = raw_links[entry_idx].as_mut().unwrap();
1923                links.tail = idx;
1924            }
1925            Link::Extra(extra_idx) => {
1926                debug_assert!(extra_values.len() > extra_idx);
1927                extra_values[extra_idx].prev = Link::Extra(idx);
1928            }
1929        }
1930    }
1931
1932    debug_assert!({
1933        for v in &*extra_values {
1934            assert!(v.next != Link::Extra(old_idx));
1935            assert!(v.prev != Link::Extra(old_idx));
1936        }
1937
1938        true
1939    });
1940
1941    extra
1942}
1943
1944fn drain_all_extra_values<T>(
1945    raw_links: RawLinks<T>,
1946    extra_values: &mut Vec<ExtraValue<T>>,
1947    mut head: usize,
1948) -> Vec<T> {
1949    let mut vec = Vec::new();
1950    loop {
1951        let extra = remove_extra_value(raw_links, extra_values, head);
1952        vec.push(extra.value);
1953
1954        if let Link::Extra(idx) = extra.next {
1955            head = idx;
1956        } else {
1957            break;
1958        }
1959    }
1960    vec
1961}
1962
1963impl<'a, T> IntoIterator for &'a HeaderMap<T> {
1964    type Item = (&'a HeaderName, &'a T);
1965    type IntoIter = Iter<'a, T>;
1966
1967    fn into_iter(self) -> Iter<'a, T> {
1968        self.iter()
1969    }
1970}
1971
1972impl<'a, T> IntoIterator for &'a mut HeaderMap<T> {
1973    type Item = (&'a HeaderName, &'a mut T);
1974    type IntoIter = IterMut<'a, T>;
1975
1976    fn into_iter(self) -> IterMut<'a, T> {
1977        self.iter_mut()
1978    }
1979}
1980
1981impl<T> IntoIterator for HeaderMap<T> {
1982    type Item = (Option<HeaderName>, T);
1983    type IntoIter = IntoIter<T>;
1984
1985    /// Creates a consuming iterator, that is, one that moves keys and values
1986    /// out of the map in arbitrary order. The map cannot be used after calling
1987    /// this.
1988    ///
1989    /// For each yielded item that has `None` provided for the `HeaderName`,
1990    /// then the associated header name is the same as that of the previously
1991    /// yielded item. The first yielded item will have `HeaderName` set.
1992    ///
1993    /// # Examples
1994    ///
1995    /// Basic usage.
1996    ///
1997    /// ```
1998    /// # use http::header;
1999    /// # use http::header::*;
2000    /// let mut map = HeaderMap::new();
2001    /// map.insert(header::CONTENT_LENGTH, "123".parse().unwrap());
2002    /// map.insert(header::CONTENT_TYPE, "json".parse().unwrap());
2003    ///
2004    /// let mut iter = map.into_iter();
2005    /// assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap())));
2006    /// assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap())));
2007    /// assert!(iter.next().is_none());
2008    /// ```
2009    ///
2010    /// Multiple values per key.
2011    ///
2012    /// ```
2013    /// # use http::header;
2014    /// # use http::header::*;
2015    /// let mut map = HeaderMap::new();
2016    ///
2017    /// map.append(header::CONTENT_LENGTH, "123".parse().unwrap());
2018    /// map.append(header::CONTENT_LENGTH, "456".parse().unwrap());
2019    ///
2020    /// map.append(header::CONTENT_TYPE, "json".parse().unwrap());
2021    /// map.append(header::CONTENT_TYPE, "html".parse().unwrap());
2022    /// map.append(header::CONTENT_TYPE, "xml".parse().unwrap());
2023    ///
2024    /// let mut iter = map.into_iter();
2025    ///
2026    /// assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap())));
2027    /// assert_eq!(iter.next(), Some((None, "456".parse().unwrap())));
2028    ///
2029    /// assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap())));
2030    /// assert_eq!(iter.next(), Some((None, "html".parse().unwrap())));
2031    /// assert_eq!(iter.next(), Some((None, "xml".parse().unwrap())));
2032    /// assert!(iter.next().is_none());
2033    /// ```
2034    fn into_iter(self) -> IntoIter<T> {
2035        IntoIter {
2036            next: None,
2037            entries: self.entries.into_iter(),
2038            extra_values: self.extra_values,
2039        }
2040    }
2041}
2042
2043impl<T> FromIterator<(HeaderName, T)> for HeaderMap<T> {
2044    fn from_iter<I>(iter: I) -> Self
2045    where
2046        I: IntoIterator<Item = (HeaderName, T)>,
2047    {
2048        let mut map = HeaderMap::default();
2049        map.extend(iter);
2050        map
2051    }
2052}
2053
2054/// Try to convert a `HashMap` into a `HeaderMap`.
2055///
2056/// # Examples
2057///
2058/// ```
2059/// use std::collections::HashMap;
2060/// use std::convert::TryInto;
2061/// use http::HeaderMap;
2062///
2063/// let mut map = HashMap::new();
2064/// map.insert("X-Custom-Header".to_string(), "my value".to_string());
2065///
2066/// let headers: HeaderMap = (&map).try_into().expect("valid headers");
2067/// assert_eq!(headers["X-Custom-Header"], "my value");
2068/// ```
2069impl<'a, K, V, S, T> TryFrom<&'a HashMap<K, V, S>> for HeaderMap<T>
2070where
2071    K: Eq + Hash,
2072    HeaderName: TryFrom<&'a K>,
2073    <HeaderName as TryFrom<&'a K>>::Error: Into<crate::Error>,
2074    T: TryFrom<&'a V>,
2075    T::Error: Into<crate::Error>,
2076{
2077    type Error = Error;
2078
2079    fn try_from(c: &'a HashMap<K, V, S>) -> Result<Self, Self::Error> {
2080        c.iter()
2081            .map(|(k, v)| -> crate::Result<(HeaderName, T)> {
2082                let name = TryFrom::try_from(k).map_err(Into::into)?;
2083                let value = TryFrom::try_from(v).map_err(Into::into)?;
2084                Ok((name, value))
2085            })
2086            .collect()
2087    }
2088}
2089
2090impl<T> Extend<(Option<HeaderName>, T)> for HeaderMap<T> {
2091    /// Extend a `HeaderMap` with the contents of another `HeaderMap`.
2092    ///
2093    /// This function expects the yielded items to follow the same structure as
2094    /// `IntoIter`.
2095    ///
2096    /// # Panics
2097    ///
2098    /// This panics if the first yielded item does not have a `HeaderName`.
2099    ///
2100    /// # Examples
2101    ///
2102    /// ```
2103    /// # use http::header::*;
2104    /// let mut map = HeaderMap::new();
2105    ///
2106    /// map.insert(ACCEPT, "text/plain".parse().unwrap());
2107    /// map.insert(HOST, "hello.world".parse().unwrap());
2108    ///
2109    /// let mut extra = HeaderMap::new();
2110    ///
2111    /// extra.insert(HOST, "foo.bar".parse().unwrap());
2112    /// extra.insert(COOKIE, "hello".parse().unwrap());
2113    /// extra.append(COOKIE, "world".parse().unwrap());
2114    ///
2115    /// map.extend(extra);
2116    ///
2117    /// assert_eq!(map["host"], "foo.bar");
2118    /// assert_eq!(map["accept"], "text/plain");
2119    /// assert_eq!(map["cookie"], "hello");
2120    ///
2121    /// let v = map.get_all("host");
2122    /// assert_eq!(1, v.iter().count());
2123    ///
2124    /// let v = map.get_all("cookie");
2125    /// assert_eq!(2, v.iter().count());
2126    /// ```
2127    fn extend<I: IntoIterator<Item = (Option<HeaderName>, T)>>(&mut self, iter: I) {
2128        let mut iter = iter.into_iter();
2129
2130        // Reserve capacity similar to the (HeaderName, T) impl.
2131        // Keys may be already present or show multiple times in the iterator.
2132        // Reserve the entire hint lower bound if the map is empty.
2133        // Otherwise reserve half the hint (rounded up), so the map
2134        // will only resize twice in the worst case.
2135        let reserve = if self.is_empty() {
2136            iter.size_hint().0
2137        } else {
2138            (iter.size_hint().0 + 1) / 2
2139        };
2140
2141        self.reserve(reserve);
2142
2143        // The structure of this is a bit weird, but it is mostly to make the
2144        // borrow checker happy.
2145        let (mut key, mut val) = match iter.next() {
2146            Some((Some(key), val)) => (key, val),
2147            Some((None, _)) => panic!("expected a header name, but got None"),
2148            None => return,
2149        };
2150
2151        'outer: loop {
2152            let mut entry = match self.try_entry2(key).expect("size overflows MAX_SIZE") {
2153                Entry::Occupied(mut e) => {
2154                    // Replace all previous values while maintaining a handle to
2155                    // the entry.
2156                    e.insert(val);
2157                    e
2158                }
2159                Entry::Vacant(e) => e.insert_entry(val),
2160            };
2161
2162            // As long as `HeaderName` is none, keep inserting the value into
2163            // the current entry
2164            loop {
2165                match iter.next() {
2166                    Some((Some(k), v)) => {
2167                        key = k;
2168                        val = v;
2169                        continue 'outer;
2170                    }
2171                    Some((None, v)) => {
2172                        entry.append(v);
2173                    }
2174                    None => {
2175                        return;
2176                    }
2177                }
2178            }
2179        }
2180    }
2181}
2182
2183impl<T> Extend<(HeaderName, T)> for HeaderMap<T> {
2184    fn extend<I: IntoIterator<Item = (HeaderName, T)>>(&mut self, iter: I) {
2185        // Keys may be already present or show multiple times in the iterator.
2186        // Reserve the entire hint lower bound if the map is empty.
2187        // Otherwise reserve half the hint (rounded up), so the map
2188        // will only resize twice in the worst case.
2189        let iter = iter.into_iter();
2190
2191        let reserve = if self.is_empty() {
2192            iter.size_hint().0
2193        } else {
2194            (iter.size_hint().0 + 1) / 2
2195        };
2196
2197        self.reserve(reserve);
2198
2199        for (k, v) in iter {
2200            self.append(k, v);
2201        }
2202    }
2203}
2204
2205impl<T: PartialEq> PartialEq for HeaderMap<T> {
2206    fn eq(&self, other: &HeaderMap<T>) -> bool {
2207        if self.len() != other.len() {
2208            return false;
2209        }
2210
2211        self.keys()
2212            .all(|key| self.get_all(key) == other.get_all(key))
2213    }
2214}
2215
2216impl<T: Eq> Eq for HeaderMap<T> {}
2217
2218impl<T: fmt::Debug> fmt::Debug for HeaderMap<T> {
2219    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2220        f.debug_map().entries(self.iter()).finish()
2221    }
2222}
2223
2224impl<K, T> ops::Index<K> for HeaderMap<T>
2225where
2226    K: AsHeaderName,
2227{
2228    type Output = T;
2229
2230    /// # Panics
2231    /// Using the index operator will cause a panic if the header you're querying isn't set.
2232    #[inline]
2233    fn index(&self, index: K) -> &T {
2234        match self.get2(&index) {
2235            Some(val) => val,
2236            None => panic!("no entry found for key {:?}", index.as_str()),
2237        }
2238    }
2239}
2240
2241/// phase 2 is post-insert where we forward-shift `Pos` in the indices.
2242///
2243/// returns the number of displaced elements
2244#[inline]
2245fn do_insert_phase_two(indices: &mut [Pos], mut probe: usize, mut old_pos: Pos) -> usize {
2246    let mut num_displaced = 0;
2247
2248    probe_loop!(probe < indices.len(), {
2249        let pos = &mut indices[probe];
2250
2251        if pos.is_none() {
2252            *pos = old_pos;
2253            break;
2254        } else {
2255            num_displaced += 1;
2256            old_pos = mem::replace(pos, old_pos);
2257        }
2258    });
2259
2260    num_displaced
2261}
2262
2263#[inline]
2264fn append_value<T>(
2265    entry_idx: usize,
2266    entry: &mut Bucket<T>,
2267    extra: &mut Vec<ExtraValue<T>>,
2268    value: T,
2269) {
2270    match entry.links {
2271        Some(links) => {
2272            let idx = extra.len();
2273            extra.push(ExtraValue {
2274                value,
2275                prev: Link::Extra(links.tail),
2276                next: Link::Entry(entry_idx),
2277            });
2278
2279            extra[links.tail].next = Link::Extra(idx);
2280
2281            entry.links = Some(Links { tail: idx, ..links });
2282        }
2283        None => {
2284            let idx = extra.len();
2285            extra.push(ExtraValue {
2286                value,
2287                prev: Link::Entry(entry_idx),
2288                next: Link::Entry(entry_idx),
2289            });
2290
2291            entry.links = Some(Links {
2292                next: idx,
2293                tail: idx,
2294            });
2295        }
2296    }
2297}
2298
2299// ===== impl Iter =====
2300
2301impl<'a, T> Iterator for Iter<'a, T> {
2302    type Item = (&'a HeaderName, &'a T);
2303
2304    fn next(&mut self) -> Option<Self::Item> {
2305        use self::Cursor::*;
2306
2307        if self.cursor.is_none() {
2308            if (self.entry + 1) >= self.map.entries.len() {
2309                return None;
2310            }
2311
2312            self.entry += 1;
2313            self.cursor = Some(Cursor::Head);
2314        }
2315
2316        let entry = &self.map.entries[self.entry];
2317
2318        match self.cursor.unwrap() {
2319            Head => {
2320                self.cursor = entry.links.map(|l| Values(l.next));
2321                Some((&entry.key, &entry.value))
2322            }
2323            Values(idx) => {
2324                let extra = &self.map.extra_values[idx];
2325
2326                match extra.next {
2327                    Link::Entry(_) => self.cursor = None,
2328                    Link::Extra(i) => self.cursor = Some(Values(i)),
2329                }
2330
2331                Some((&entry.key, &extra.value))
2332            }
2333        }
2334    }
2335
2336    fn size_hint(&self) -> (usize, Option<usize>) {
2337        let map = self.map;
2338        debug_assert!(map.entries.len() >= self.entry);
2339
2340        let lower = map.entries.len() - self.entry;
2341        // We could pessimistically guess at the upper bound, saying
2342        // that its lower + map.extra_values.len(). That could be
2343        // way over though, such as if we're near the end, and have
2344        // already gone through several extra values...
2345        (lower, None)
2346    }
2347}
2348
2349impl<'a, T> FusedIterator for Iter<'a, T> {}
2350
2351unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {}
2352unsafe impl<'a, T: Sync> Send for Iter<'a, T> {}
2353
2354// ===== impl IterMut =====
2355
2356impl<'a, T> IterMut<'a, T> {
2357    fn next_unsafe(&mut self) -> Option<(&'a HeaderName, *mut T)> {
2358        use self::Cursor::*;
2359
2360        if self.cursor.is_none() {
2361            if (self.entry + 1) >= unsafe { &*self.map }.entries.len() {
2362                return None;
2363            }
2364
2365            self.entry += 1;
2366            self.cursor = Some(Cursor::Head);
2367        }
2368
2369        let entry = &mut unsafe { &mut *self.map }.entries[self.entry];
2370
2371        match self.cursor.unwrap() {
2372            Head => {
2373                self.cursor = entry.links.map(|l| Values(l.next));
2374                Some((&entry.key, &mut entry.value as *mut _))
2375            }
2376            Values(idx) => {
2377                let extra = &mut unsafe { &mut (*self.map) }.extra_values[idx];
2378
2379                match extra.next {
2380                    Link::Entry(_) => self.cursor = None,
2381                    Link::Extra(i) => self.cursor = Some(Values(i)),
2382                }
2383
2384                Some((&entry.key, &mut extra.value as *mut _))
2385            }
2386        }
2387    }
2388}
2389
2390impl<'a, T> Iterator for IterMut<'a, T> {
2391    type Item = (&'a HeaderName, &'a mut T);
2392
2393    fn next(&mut self) -> Option<Self::Item> {
2394        self.next_unsafe()
2395            .map(|(key, ptr)| (key, unsafe { &mut *ptr }))
2396    }
2397
2398    fn size_hint(&self) -> (usize, Option<usize>) {
2399        let map = unsafe { &*self.map };
2400        debug_assert!(map.entries.len() >= self.entry);
2401
2402        let lower = map.entries.len() - self.entry;
2403        // We could pessimistically guess at the upper bound, saying
2404        // that its lower + map.extra_values.len(). That could be
2405        // way over though, such as if we're near the end, and have
2406        // already gone through several extra values...
2407        (lower, None)
2408    }
2409}
2410
2411impl<'a, T> FusedIterator for IterMut<'a, T> {}
2412
2413unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {}
2414unsafe impl<'a, T: Send> Send for IterMut<'a, T> {}
2415
2416// ===== impl Keys =====
2417
2418impl<'a, T> Iterator for Keys<'a, T> {
2419    type Item = &'a HeaderName;
2420
2421    fn next(&mut self) -> Option<Self::Item> {
2422        self.inner.next().map(|b| &b.key)
2423    }
2424
2425    fn size_hint(&self) -> (usize, Option<usize>) {
2426        self.inner.size_hint()
2427    }
2428
2429    fn nth(&mut self, n: usize) -> Option<Self::Item> {
2430        self.inner.nth(n).map(|b| &b.key)
2431    }
2432
2433    fn count(self) -> usize {
2434        self.inner.count()
2435    }
2436
2437    fn last(self) -> Option<Self::Item> {
2438        self.inner.last().map(|b| &b.key)
2439    }
2440}
2441
2442impl<'a, T> ExactSizeIterator for Keys<'a, T> {}
2443impl<'a, T> FusedIterator for Keys<'a, T> {}
2444
2445// ===== impl Values ====
2446
2447impl<'a, T> Iterator for Values<'a, T> {
2448    type Item = &'a T;
2449
2450    fn next(&mut self) -> Option<Self::Item> {
2451        self.inner.next().map(|(_, v)| v)
2452    }
2453
2454    fn size_hint(&self) -> (usize, Option<usize>) {
2455        self.inner.size_hint()
2456    }
2457}
2458
2459impl<'a, T> FusedIterator for Values<'a, T> {}
2460
2461// ===== impl ValuesMut ====
2462
2463impl<'a, T> Iterator for ValuesMut<'a, T> {
2464    type Item = &'a mut T;
2465
2466    fn next(&mut self) -> Option<Self::Item> {
2467        self.inner.next().map(|(_, v)| v)
2468    }
2469
2470    fn size_hint(&self) -> (usize, Option<usize>) {
2471        self.inner.size_hint()
2472    }
2473}
2474
2475impl<'a, T> FusedIterator for ValuesMut<'a, T> {}
2476
2477// ===== impl Drain =====
2478
2479impl<'a, T> Iterator for Drain<'a, T> {
2480    type Item = (Option<HeaderName>, T);
2481
2482    fn next(&mut self) -> Option<Self::Item> {
2483        if let Some(next) = self.next {
2484            // Remove the extra value
2485
2486            let raw_links = RawLinks(self.entries);
2487            let extra = unsafe { remove_extra_value(raw_links, &mut *self.extra_values, next) };
2488
2489            match extra.next {
2490                Link::Extra(idx) => self.next = Some(idx),
2491                Link::Entry(_) => self.next = None,
2492            }
2493
2494            return Some((None, extra.value));
2495        }
2496
2497        let idx = self.idx;
2498
2499        if idx == self.len {
2500            return None;
2501        }
2502
2503        self.idx += 1;
2504
2505        unsafe {
2506            let entry = &(*self.entries)[idx];
2507
2508            // Read the header name
2509            let key = ptr::read(&entry.key as *const _);
2510            let value = ptr::read(&entry.value as *const _);
2511            self.next = entry.links.map(|l| l.next);
2512
2513            Some((Some(key), value))
2514        }
2515    }
2516
2517    fn size_hint(&self) -> (usize, Option<usize>) {
2518        // At least this many names... It's unknown if the user wants
2519        // to count the extra_values on top.
2520        //
2521        // For instance, extending a new `HeaderMap` wouldn't need to
2522        // reserve the upper-bound in `entries`, only the lower-bound.
2523        let lower = self.len - self.idx;
2524        let upper = unsafe { (*self.extra_values).len() } + lower;
2525        (lower, Some(upper))
2526    }
2527}
2528
2529impl<'a, T> FusedIterator for Drain<'a, T> {}
2530
2531impl<'a, T> Drop for Drain<'a, T> {
2532    fn drop(&mut self) {
2533        for _ in self {}
2534    }
2535}
2536
2537unsafe impl<'a, T: Sync> Sync for Drain<'a, T> {}
2538unsafe impl<'a, T: Send> Send for Drain<'a, T> {}
2539
2540// ===== impl Entry =====
2541
2542impl<'a, T> Entry<'a, T> {
2543    /// Ensures a value is in the entry by inserting the default if empty.
2544    ///
2545    /// Returns a mutable reference to the **first** value in the entry.
2546    ///
2547    /// # Panics
2548    ///
2549    /// This method panics if capacity exceeds max `HeaderMap` capacity
2550    ///
2551    /// # Examples
2552    ///
2553    /// ```
2554    /// # use http::HeaderMap;
2555    /// let mut map: HeaderMap<u32> = HeaderMap::default();
2556    ///
2557    /// let headers = &[
2558    ///     "content-length",
2559    ///     "x-hello",
2560    ///     "Content-Length",
2561    ///     "x-world",
2562    /// ];
2563    ///
2564    /// for &header in headers {
2565    ///     let counter = map.entry(header)
2566    ///         .or_insert(0);
2567    ///     *counter += 1;
2568    /// }
2569    ///
2570    /// assert_eq!(map["content-length"], 2);
2571    /// assert_eq!(map["x-hello"], 1);
2572    /// ```
2573    pub fn or_insert(self, default: T) -> &'a mut T {
2574        self.or_try_insert(default)
2575            .expect("size overflows MAX_SIZE")
2576    }
2577
2578    /// Ensures a value is in the entry by inserting the default if empty.
2579    ///
2580    /// Returns a mutable reference to the **first** value in the entry.
2581    ///
2582    /// # Errors
2583    ///
2584    /// This function may return an error if `HeaderMap` exceeds max capacity
2585    ///
2586    /// # Examples
2587    ///
2588    /// ```
2589    /// # use http::HeaderMap;
2590    /// let mut map: HeaderMap<u32> = HeaderMap::default();
2591    ///
2592    /// let headers = &[
2593    ///     "content-length",
2594    ///     "x-hello",
2595    ///     "Content-Length",
2596    ///     "x-world",
2597    /// ];
2598    ///
2599    /// for &header in headers {
2600    ///     let counter = map.entry(header)
2601    ///         .or_try_insert(0)
2602    ///         .unwrap();
2603    ///     *counter += 1;
2604    /// }
2605    ///
2606    /// assert_eq!(map["content-length"], 2);
2607    /// assert_eq!(map["x-hello"], 1);
2608    /// ```
2609    pub fn or_try_insert(self, default: T) -> Result<&'a mut T, MaxSizeReached> {
2610        use self::Entry::*;
2611
2612        match self {
2613            Occupied(e) => Ok(e.into_mut()),
2614            Vacant(e) => e.try_insert(default),
2615        }
2616    }
2617
2618    /// Ensures a value is in the entry by inserting the result of the default
2619    /// function if empty.
2620    ///
2621    /// The default function is not called if the entry exists in the map.
2622    /// Returns a mutable reference to the **first** value in the entry.
2623    ///
2624    /// # Examples
2625    ///
2626    /// Basic usage.
2627    ///
2628    /// ```
2629    /// # use http::HeaderMap;
2630    /// let mut map = HeaderMap::new();
2631    ///
2632    /// let res = map.entry("x-hello")
2633    ///     .or_insert_with(|| "world".parse().unwrap());
2634    ///
2635    /// assert_eq!(res, "world");
2636    /// ```
2637    ///
2638    /// The default function is not called if the entry exists in the map.
2639    ///
2640    /// ```
2641    /// # use http::HeaderMap;
2642    /// # use http::header::HOST;
2643    /// let mut map = HeaderMap::new();
2644    /// map.try_insert(HOST, "world".parse().unwrap()).unwrap();
2645    ///
2646    /// let res = map.try_entry("host")
2647    ///     .unwrap()
2648    ///     .or_try_insert_with(|| unreachable!())
2649    ///     .unwrap();
2650    ///
2651    ///
2652    /// assert_eq!(res, "world");
2653    /// ```
2654    pub fn or_insert_with<F: FnOnce() -> T>(self, default: F) -> &'a mut T {
2655        self.or_try_insert_with(default)
2656            .expect("size overflows MAX_SIZE")
2657    }
2658
2659    /// Ensures a value is in the entry by inserting the result of the default
2660    /// function if empty.
2661    ///
2662    /// The default function is not called if the entry exists in the map.
2663    /// Returns a mutable reference to the **first** value in the entry.
2664    ///
2665    /// # Examples
2666    ///
2667    /// Basic usage.
2668    ///
2669    /// ```
2670    /// # use http::HeaderMap;
2671    /// let mut map = HeaderMap::new();
2672    ///
2673    /// let res = map.entry("x-hello")
2674    ///     .or_insert_with(|| "world".parse().unwrap());
2675    ///
2676    /// assert_eq!(res, "world");
2677    /// ```
2678    ///
2679    /// The default function is not called if the entry exists in the map.
2680    ///
2681    /// ```
2682    /// # use http::HeaderMap;
2683    /// # use http::header::HOST;
2684    /// let mut map = HeaderMap::new();
2685    /// map.try_insert(HOST, "world".parse().unwrap()).unwrap();
2686    ///
2687    /// let res = map.try_entry("host")
2688    ///     .unwrap()
2689    ///     .or_try_insert_with(|| unreachable!())
2690    ///     .unwrap();
2691    ///
2692    ///
2693    /// assert_eq!(res, "world");
2694    /// ```
2695    pub fn or_try_insert_with<F: FnOnce() -> T>(
2696        self,
2697        default: F,
2698    ) -> Result<&'a mut T, MaxSizeReached> {
2699        use self::Entry::*;
2700
2701        match self {
2702            Occupied(e) => Ok(e.into_mut()),
2703            Vacant(e) => e.try_insert(default()),
2704        }
2705    }
2706
2707    /// Returns a reference to the entry's key
2708    ///
2709    /// # Examples
2710    ///
2711    /// ```
2712    /// # use http::HeaderMap;
2713    /// let mut map = HeaderMap::new();
2714    ///
2715    /// assert_eq!(map.entry("x-hello").key(), "x-hello");
2716    /// ```
2717    pub fn key(&self) -> &HeaderName {
2718        use self::Entry::*;
2719
2720        match *self {
2721            Vacant(ref e) => e.key(),
2722            Occupied(ref e) => e.key(),
2723        }
2724    }
2725}
2726
2727// ===== impl VacantEntry =====
2728
2729impl<'a, T> VacantEntry<'a, T> {
2730    /// Returns a reference to the entry's key
2731    ///
2732    /// # Examples
2733    ///
2734    /// ```
2735    /// # use http::HeaderMap;
2736    /// let mut map = HeaderMap::new();
2737    ///
2738    /// assert_eq!(map.entry("x-hello").key().as_str(), "x-hello");
2739    /// ```
2740    pub fn key(&self) -> &HeaderName {
2741        &self.key
2742    }
2743
2744    /// Take ownership of the key
2745    ///
2746    /// # Examples
2747    ///
2748    /// ```
2749    /// # use http::header::{HeaderMap, Entry};
2750    /// let mut map = HeaderMap::new();
2751    ///
2752    /// if let Entry::Vacant(v) = map.entry("x-hello") {
2753    ///     assert_eq!(v.into_key().as_str(), "x-hello");
2754    /// }
2755    /// ```
2756    pub fn into_key(self) -> HeaderName {
2757        self.key
2758    }
2759
2760    /// Insert the value into the entry.
2761    ///
2762    /// The value will be associated with this entry's key. A mutable reference
2763    /// to the inserted value will be returned.
2764    ///
2765    /// # Examples
2766    ///
2767    /// ```
2768    /// # use http::header::{HeaderMap, Entry};
2769    /// let mut map = HeaderMap::new();
2770    ///
2771    /// if let Entry::Vacant(v) = map.entry("x-hello") {
2772    ///     v.insert("world".parse().unwrap());
2773    /// }
2774    ///
2775    /// assert_eq!(map["x-hello"], "world");
2776    /// ```
2777    pub fn insert(self, value: T) -> &'a mut T {
2778        self.try_insert(value).expect("size overflows MAX_SIZE")
2779    }
2780
2781    /// Insert the value into the entry.
2782    ///
2783    /// The value will be associated with this entry's key. A mutable reference
2784    /// to the inserted value will be returned.
2785    ///
2786    /// # Examples
2787    ///
2788    /// ```
2789    /// # use http::header::{HeaderMap, Entry};
2790    /// let mut map = HeaderMap::new();
2791    ///
2792    /// if let Entry::Vacant(v) = map.entry("x-hello") {
2793    ///     v.insert("world".parse().unwrap());
2794    /// }
2795    ///
2796    /// assert_eq!(map["x-hello"], "world");
2797    /// ```
2798    pub fn try_insert(self, value: T) -> Result<&'a mut T, MaxSizeReached> {
2799        // Ensure that there is space in the map
2800        let index =
2801            self.map
2802                .try_insert_phase_two(self.key, value, self.hash, self.probe, self.danger)?;
2803
2804        Ok(&mut self.map.entries[index].value)
2805    }
2806
2807    /// Insert the value into the entry.
2808    ///
2809    /// The value will be associated with this entry's key. The new
2810    /// `OccupiedEntry` is returned, allowing for further manipulation.
2811    ///
2812    /// # Examples
2813    ///
2814    /// ```
2815    /// # use http::header::*;
2816    /// let mut map = HeaderMap::new();
2817    ///
2818    /// if let Entry::Vacant(v) = map.try_entry("x-hello").unwrap() {
2819    ///     let mut e = v.try_insert_entry("world".parse().unwrap()).unwrap();
2820    ///     e.insert("world2".parse().unwrap());
2821    /// }
2822    ///
2823    /// assert_eq!(map["x-hello"], "world2");
2824    /// ```
2825    pub fn insert_entry(self, value: T) -> OccupiedEntry<'a, T> {
2826        self.try_insert_entry(value)
2827            .expect("size overflows MAX_SIZE")
2828    }
2829
2830    /// Insert the value into the entry.
2831    ///
2832    /// The value will be associated with this entry's key. The new
2833    /// `OccupiedEntry` is returned, allowing for further manipulation.
2834    ///
2835    /// # Examples
2836    ///
2837    /// ```
2838    /// # use http::header::*;
2839    /// let mut map = HeaderMap::new();
2840    ///
2841    /// if let Entry::Vacant(v) = map.try_entry("x-hello").unwrap() {
2842    ///     let mut e = v.try_insert_entry("world".parse().unwrap()).unwrap();
2843    ///     e.insert("world2".parse().unwrap());
2844    /// }
2845    ///
2846    /// assert_eq!(map["x-hello"], "world2");
2847    /// ```
2848    pub fn try_insert_entry(self, value: T) -> Result<OccupiedEntry<'a, T>, MaxSizeReached> {
2849        // Ensure that there is space in the map
2850        let index =
2851            self.map
2852                .try_insert_phase_two(self.key, value, self.hash, self.probe, self.danger)?;
2853
2854        Ok(OccupiedEntry {
2855            map: self.map,
2856            index,
2857            probe: self.probe,
2858        })
2859    }
2860}
2861
2862// ===== impl GetAll =====
2863
2864impl<'a, T: 'a> GetAll<'a, T> {
2865    /// Returns an iterator visiting all values associated with the entry.
2866    ///
2867    /// Values are iterated in insertion order.
2868    ///
2869    /// # Examples
2870    ///
2871    /// ```
2872    /// # use http::HeaderMap;
2873    /// # use http::header::HOST;
2874    /// let mut map = HeaderMap::new();
2875    /// map.insert(HOST, "hello.world".parse().unwrap());
2876    /// map.append(HOST, "hello.earth".parse().unwrap());
2877    ///
2878    /// let values = map.get_all("host");
2879    /// let mut iter = values.iter();
2880    /// assert_eq!(&"hello.world", iter.next().unwrap());
2881    /// assert_eq!(&"hello.earth", iter.next().unwrap());
2882    /// assert!(iter.next().is_none());
2883    /// ```
2884    pub fn iter(&self) -> ValueIter<'a, T> {
2885        // This creates a new GetAll struct so that the lifetime
2886        // isn't bound to &self.
2887        GetAll {
2888            map: self.map,
2889            index: self.index,
2890        }
2891        .into_iter()
2892    }
2893}
2894
2895impl<'a, T: PartialEq> PartialEq for GetAll<'a, T> {
2896    fn eq(&self, other: &Self) -> bool {
2897        self.iter().eq(other.iter())
2898    }
2899}
2900
2901impl<'a, T> IntoIterator for GetAll<'a, T> {
2902    type Item = &'a T;
2903    type IntoIter = ValueIter<'a, T>;
2904
2905    fn into_iter(self) -> ValueIter<'a, T> {
2906        self.map.value_iter(self.index)
2907    }
2908}
2909
2910impl<'a, 'b: 'a, T> IntoIterator for &'b GetAll<'a, T> {
2911    type Item = &'a T;
2912    type IntoIter = ValueIter<'a, T>;
2913
2914    fn into_iter(self) -> ValueIter<'a, T> {
2915        self.map.value_iter(self.index)
2916    }
2917}
2918
2919// ===== impl ValueIter =====
2920
2921impl<'a, T: 'a> Iterator for ValueIter<'a, T> {
2922    type Item = &'a T;
2923
2924    fn next(&mut self) -> Option<Self::Item> {
2925        use self::Cursor::*;
2926
2927        match self.front {
2928            Some(Head) => {
2929                let entry = &self.map.entries[self.index];
2930
2931                if self.back == Some(Head) {
2932                    self.front = None;
2933                    self.back = None;
2934                } else {
2935                    // Update the iterator state
2936                    match entry.links {
2937                        Some(links) => {
2938                            self.front = Some(Values(links.next));
2939                        }
2940                        None => unreachable!(),
2941                    }
2942                }
2943
2944                Some(&entry.value)
2945            }
2946            Some(Values(idx)) => {
2947                let extra = &self.map.extra_values[idx];
2948
2949                if self.front == self.back {
2950                    self.front = None;
2951                    self.back = None;
2952                } else {
2953                    match extra.next {
2954                        Link::Entry(_) => self.front = None,
2955                        Link::Extra(i) => self.front = Some(Values(i)),
2956                    }
2957                }
2958
2959                Some(&extra.value)
2960            }
2961            None => None,
2962        }
2963    }
2964
2965    fn size_hint(&self) -> (usize, Option<usize>) {
2966        match (self.front, self.back) {
2967            // Exactly 1 value...
2968            (Some(Cursor::Head), Some(Cursor::Head)) => (1, Some(1)),
2969            // At least 1...
2970            (Some(_), _) => (1, None),
2971            // No more values...
2972            (None, _) => (0, Some(0)),
2973        }
2974    }
2975}
2976
2977impl<'a, T: 'a> DoubleEndedIterator for ValueIter<'a, T> {
2978    fn next_back(&mut self) -> Option<Self::Item> {
2979        use self::Cursor::*;
2980
2981        match self.back {
2982            Some(Head) => {
2983                self.front = None;
2984                self.back = None;
2985                Some(&self.map.entries[self.index].value)
2986            }
2987            Some(Values(idx)) => {
2988                let extra = &self.map.extra_values[idx];
2989
2990                if self.front == self.back {
2991                    self.front = None;
2992                    self.back = None;
2993                } else {
2994                    match extra.prev {
2995                        Link::Entry(_) => self.back = Some(Head),
2996                        Link::Extra(idx) => self.back = Some(Values(idx)),
2997                    }
2998                }
2999
3000                Some(&extra.value)
3001            }
3002            None => None,
3003        }
3004    }
3005}
3006
3007impl<'a, T> FusedIterator for ValueIter<'a, T> {}
3008
3009// ===== impl ValueIterMut =====
3010
3011impl<'a, T: 'a> Iterator for ValueIterMut<'a, T> {
3012    type Item = &'a mut T;
3013
3014    fn next(&mut self) -> Option<Self::Item> {
3015        use self::Cursor::*;
3016
3017        let entry = &mut unsafe { &mut *self.map }.entries[self.index];
3018
3019        match self.front {
3020            Some(Head) => {
3021                if self.back == Some(Head) {
3022                    self.front = None;
3023                    self.back = None;
3024                } else {
3025                    // Update the iterator state
3026                    match entry.links {
3027                        Some(links) => {
3028                            self.front = Some(Values(links.next));
3029                        }
3030                        None => unreachable!(),
3031                    }
3032                }
3033
3034                Some(&mut entry.value)
3035            }
3036            Some(Values(idx)) => {
3037                let extra = &mut unsafe { &mut *self.map }.extra_values[idx];
3038
3039                if self.front == self.back {
3040                    self.front = None;
3041                    self.back = None;
3042                } else {
3043                    match extra.next {
3044                        Link::Entry(_) => self.front = None,
3045                        Link::Extra(i) => self.front = Some(Values(i)),
3046                    }
3047                }
3048
3049                Some(&mut extra.value)
3050            }
3051            None => None,
3052        }
3053    }
3054}
3055
3056impl<'a, T: 'a> DoubleEndedIterator for ValueIterMut<'a, T> {
3057    fn next_back(&mut self) -> Option<Self::Item> {
3058        use self::Cursor::*;
3059
3060        let entry = &mut unsafe { &mut *self.map }.entries[self.index];
3061
3062        match self.back {
3063            Some(Head) => {
3064                self.front = None;
3065                self.back = None;
3066                Some(&mut entry.value)
3067            }
3068            Some(Values(idx)) => {
3069                let extra = &mut unsafe { &mut *self.map }.extra_values[idx];
3070
3071                if self.front == self.back {
3072                    self.front = None;
3073                    self.back = None;
3074                } else {
3075                    match extra.prev {
3076                        Link::Entry(_) => self.back = Some(Head),
3077                        Link::Extra(idx) => self.back = Some(Values(idx)),
3078                    }
3079                }
3080
3081                Some(&mut extra.value)
3082            }
3083            None => None,
3084        }
3085    }
3086}
3087
3088impl<'a, T> FusedIterator for ValueIterMut<'a, T> {}
3089
3090unsafe impl<'a, T: Sync> Sync for ValueIterMut<'a, T> {}
3091unsafe impl<'a, T: Send> Send for ValueIterMut<'a, T> {}
3092
3093// ===== impl IntoIter =====
3094
3095impl<T> Iterator for IntoIter<T> {
3096    type Item = (Option<HeaderName>, T);
3097
3098    fn next(&mut self) -> Option<Self::Item> {
3099        if let Some(next) = self.next {
3100            self.next = match self.extra_values[next].next {
3101                Link::Entry(_) => None,
3102                Link::Extra(v) => Some(v),
3103            };
3104
3105            let value = unsafe { ptr::read(&self.extra_values[next].value) };
3106
3107            return Some((None, value));
3108        }
3109
3110        if let Some(bucket) = self.entries.next() {
3111            self.next = bucket.links.map(|l| l.next);
3112            let name = Some(bucket.key);
3113            let value = bucket.value;
3114
3115            return Some((name, value));
3116        }
3117
3118        None
3119    }
3120
3121    fn size_hint(&self) -> (usize, Option<usize>) {
3122        let (lower, _) = self.entries.size_hint();
3123        // There could be more than just the entries upper, as there
3124        // could be items in the `extra_values`. We could guess, saying
3125        // `upper + extra_values.len()`, but that could overestimate by a lot.
3126        (lower, None)
3127    }
3128}
3129
3130impl<T> FusedIterator for IntoIter<T> {}
3131
3132impl<T> Drop for IntoIter<T> {
3133    fn drop(&mut self) {
3134        // Ensure the iterator is consumed
3135        for _ in self.by_ref() {}
3136
3137        // All the values have already been yielded out.
3138        unsafe {
3139            self.extra_values.set_len(0);
3140        }
3141    }
3142}
3143
3144// ===== impl OccupiedEntry =====
3145
3146impl<'a, T> OccupiedEntry<'a, T> {
3147    /// Returns a reference to the entry's key.
3148    ///
3149    /// # Examples
3150    ///
3151    /// ```
3152    /// # use http::header::{HeaderMap, Entry, HOST};
3153    /// let mut map = HeaderMap::new();
3154    /// map.insert(HOST, "world".parse().unwrap());
3155    ///
3156    /// if let Entry::Occupied(e) = map.entry("host") {
3157    ///     assert_eq!("host", e.key());
3158    /// }
3159    /// ```
3160    pub fn key(&self) -> &HeaderName {
3161        &self.map.entries[self.index].key
3162    }
3163
3164    /// Get a reference to the first value in the entry.
3165    ///
3166    /// Values are stored in insertion order.
3167    ///
3168    /// # Panics
3169    ///
3170    /// `get` panics if there are no values associated with the entry.
3171    ///
3172    /// # Examples
3173    ///
3174    /// ```
3175    /// # use http::header::{HeaderMap, Entry, HOST};
3176    /// let mut map = HeaderMap::new();
3177    /// map.insert(HOST, "hello.world".parse().unwrap());
3178    ///
3179    /// if let Entry::Occupied(mut e) = map.entry("host") {
3180    ///     assert_eq!(e.get(), &"hello.world");
3181    ///
3182    ///     e.append("hello.earth".parse().unwrap());
3183    ///
3184    ///     assert_eq!(e.get(), &"hello.world");
3185    /// }
3186    /// ```
3187    pub fn get(&self) -> &T {
3188        &self.map.entries[self.index].value
3189    }
3190
3191    /// Get a mutable reference to the first value in the entry.
3192    ///
3193    /// Values are stored in insertion order.
3194    ///
3195    /// # Panics
3196    ///
3197    /// `get_mut` panics if there are no values associated with the entry.
3198    ///
3199    /// # Examples
3200    ///
3201    /// ```
3202    /// # use http::header::{HeaderMap, Entry, HOST};
3203    /// let mut map = HeaderMap::default();
3204    /// map.insert(HOST, "hello.world".to_string());
3205    ///
3206    /// if let Entry::Occupied(mut e) = map.entry("host") {
3207    ///     e.get_mut().push_str("-2");
3208    ///     assert_eq!(e.get(), &"hello.world-2");
3209    /// }
3210    /// ```
3211    pub fn get_mut(&mut self) -> &mut T {
3212        &mut self.map.entries[self.index].value
3213    }
3214
3215    /// Converts the `OccupiedEntry` into a mutable reference to the **first**
3216    /// value.
3217    ///
3218    /// The lifetime of the returned reference is bound to the original map.
3219    ///
3220    /// # Panics
3221    ///
3222    /// `into_mut` panics if there are no values associated with the entry.
3223    ///
3224    /// # Examples
3225    ///
3226    /// ```
3227    /// # use http::header::{HeaderMap, Entry, HOST};
3228    /// let mut map = HeaderMap::default();
3229    /// map.insert(HOST, "hello.world".to_string());
3230    /// map.append(HOST, "hello.earth".to_string());
3231    ///
3232    /// if let Entry::Occupied(e) = map.entry("host") {
3233    ///     e.into_mut().push_str("-2");
3234    /// }
3235    ///
3236    /// assert_eq!("hello.world-2", map["host"]);
3237    /// ```
3238    pub fn into_mut(self) -> &'a mut T {
3239        &mut self.map.entries[self.index].value
3240    }
3241
3242    /// Sets the value of the entry.
3243    ///
3244    /// All previous values associated with the entry are removed and the first
3245    /// one is returned. See `insert_mult` for an API that returns all values.
3246    ///
3247    /// # Examples
3248    ///
3249    /// ```
3250    /// # use http::header::{HeaderMap, Entry, HOST};
3251    /// let mut map = HeaderMap::new();
3252    /// map.insert(HOST, "hello.world".parse().unwrap());
3253    ///
3254    /// if let Entry::Occupied(mut e) = map.entry("host") {
3255    ///     let mut prev = e.insert("earth".parse().unwrap());
3256    ///     assert_eq!("hello.world", prev);
3257    /// }
3258    ///
3259    /// assert_eq!("earth", map["host"]);
3260    /// ```
3261    pub fn insert(&mut self, value: T) -> T {
3262        self.map.insert_occupied(self.index, value)
3263    }
3264
3265    /// Sets the value of the entry.
3266    ///
3267    /// This function does the same as `insert` except it returns an iterator
3268    /// that yields all values previously associated with the key.
3269    ///
3270    /// # Examples
3271    ///
3272    /// ```
3273    /// # use http::header::{HeaderMap, Entry, HOST};
3274    /// let mut map = HeaderMap::new();
3275    /// map.insert(HOST, "world".parse().unwrap());
3276    /// map.append(HOST, "world2".parse().unwrap());
3277    ///
3278    /// if let Entry::Occupied(mut e) = map.entry("host") {
3279    ///     let mut prev = e.insert_mult("earth".parse().unwrap());
3280    ///     assert_eq!("world", prev.next().unwrap());
3281    ///     assert_eq!("world2", prev.next().unwrap());
3282    ///     assert!(prev.next().is_none());
3283    /// }
3284    ///
3285    /// assert_eq!("earth", map["host"]);
3286    /// ```
3287    pub fn insert_mult(&mut self, value: T) -> ValueDrain<'_, T> {
3288        self.map.insert_occupied_mult(self.index, value)
3289    }
3290
3291    /// Insert the value into the entry.
3292    ///
3293    /// The new value is appended to the end of the entry's value list. All
3294    /// previous values associated with the entry are retained.
3295    ///
3296    /// # Examples
3297    ///
3298    /// ```
3299    /// # use http::header::{HeaderMap, Entry, HOST};
3300    /// let mut map = HeaderMap::new();
3301    /// map.insert(HOST, "world".parse().unwrap());
3302    ///
3303    /// if let Entry::Occupied(mut e) = map.entry("host") {
3304    ///     e.append("earth".parse().unwrap());
3305    /// }
3306    ///
3307    /// let values = map.get_all("host");
3308    /// let mut i = values.iter();
3309    /// assert_eq!("world", *i.next().unwrap());
3310    /// assert_eq!("earth", *i.next().unwrap());
3311    /// ```
3312    pub fn append(&mut self, value: T) {
3313        let idx = self.index;
3314        let entry = &mut self.map.entries[idx];
3315        append_value(idx, entry, &mut self.map.extra_values, value);
3316    }
3317
3318    /// Remove the entry from the map.
3319    ///
3320    /// All values associated with the entry are removed and the first one is
3321    /// returned. See `remove_entry_mult` for an API that returns all values.
3322    ///
3323    /// # Examples
3324    ///
3325    /// ```
3326    /// # use http::header::{HeaderMap, Entry, HOST};
3327    /// let mut map = HeaderMap::new();
3328    /// map.insert(HOST, "world".parse().unwrap());
3329    ///
3330    /// if let Entry::Occupied(e) = map.entry("host") {
3331    ///     let mut prev = e.remove();
3332    ///     assert_eq!("world", prev);
3333    /// }
3334    ///
3335    /// assert!(!map.contains_key("host"));
3336    /// ```
3337    pub fn remove(self) -> T {
3338        self.remove_entry().1
3339    }
3340
3341    /// Remove the entry from the map.
3342    ///
3343    /// The key and all values associated with the entry are removed and the
3344    /// first one is returned. See `remove_entry_mult` for an API that returns
3345    /// all values.
3346    ///
3347    /// # Examples
3348    ///
3349    /// ```
3350    /// # use http::header::{HeaderMap, Entry, HOST};
3351    /// let mut map = HeaderMap::new();
3352    /// map.insert(HOST, "world".parse().unwrap());
3353    ///
3354    /// if let Entry::Occupied(e) = map.entry("host") {
3355    ///     let (key, mut prev) = e.remove_entry();
3356    ///     assert_eq!("host", key.as_str());
3357    ///     assert_eq!("world", prev);
3358    /// }
3359    ///
3360    /// assert!(!map.contains_key("host"));
3361    /// ```
3362    pub fn remove_entry(self) -> (HeaderName, T) {
3363        if let Some(links) = self.map.entries[self.index].links {
3364            self.map.remove_all_extra_values(links.next);
3365        }
3366
3367        let entry = self.map.remove_found(self.probe, self.index);
3368
3369        (entry.key, entry.value)
3370    }
3371
3372    /// Remove the entry from the map.
3373    ///
3374    /// The key and all values associated with the entry are removed and
3375    /// returned.
3376    pub fn remove_entry_mult(self) -> (HeaderName, ValueDrain<'a, T>) {
3377        let raw_links = self.map.raw_links();
3378        let extra_values = &mut self.map.extra_values;
3379
3380        let next = self.map.entries[self.index]
3381            .links
3382            .map(|l| drain_all_extra_values(raw_links, extra_values, l.next).into_iter());
3383
3384        let entry = self.map.remove_found(self.probe, self.index);
3385
3386        let drain = ValueDrain {
3387            first: Some(entry.value),
3388            next,
3389            lt: PhantomData,
3390        };
3391        (entry.key, drain)
3392    }
3393
3394    /// Returns an iterator visiting all values associated with the entry.
3395    ///
3396    /// Values are iterated in insertion order.
3397    ///
3398    /// # Examples
3399    ///
3400    /// ```
3401    /// # use http::header::{HeaderMap, Entry, HOST};
3402    /// let mut map = HeaderMap::new();
3403    /// map.insert(HOST, "world".parse().unwrap());
3404    /// map.append(HOST, "earth".parse().unwrap());
3405    ///
3406    /// if let Entry::Occupied(e) = map.entry("host") {
3407    ///     let mut iter = e.iter();
3408    ///     assert_eq!(&"world", iter.next().unwrap());
3409    ///     assert_eq!(&"earth", iter.next().unwrap());
3410    ///     assert!(iter.next().is_none());
3411    /// }
3412    /// ```
3413    pub fn iter(&self) -> ValueIter<'_, T> {
3414        self.map.value_iter(Some(self.index))
3415    }
3416
3417    /// Returns an iterator mutably visiting all values associated with the
3418    /// entry.
3419    ///
3420    /// Values are iterated in insertion order.
3421    ///
3422    /// # Examples
3423    ///
3424    /// ```
3425    /// # use http::header::{HeaderMap, Entry, HOST};
3426    /// let mut map = HeaderMap::default();
3427    /// map.insert(HOST, "world".to_string());
3428    /// map.append(HOST, "earth".to_string());
3429    ///
3430    /// if let Entry::Occupied(mut e) = map.entry("host") {
3431    ///     for e in e.iter_mut() {
3432    ///         e.push_str("-boop");
3433    ///     }
3434    /// }
3435    ///
3436    /// let mut values = map.get_all("host");
3437    /// let mut i = values.iter();
3438    /// assert_eq!(&"world-boop", i.next().unwrap());
3439    /// assert_eq!(&"earth-boop", i.next().unwrap());
3440    /// ```
3441    pub fn iter_mut(&mut self) -> ValueIterMut<'_, T> {
3442        self.map.value_iter_mut(self.index)
3443    }
3444}
3445
3446impl<'a, T> IntoIterator for OccupiedEntry<'a, T> {
3447    type Item = &'a mut T;
3448    type IntoIter = ValueIterMut<'a, T>;
3449
3450    fn into_iter(self) -> ValueIterMut<'a, T> {
3451        self.map.value_iter_mut(self.index)
3452    }
3453}
3454
3455impl<'a, 'b: 'a, T> IntoIterator for &'b OccupiedEntry<'a, T> {
3456    type Item = &'a T;
3457    type IntoIter = ValueIter<'a, T>;
3458
3459    fn into_iter(self) -> ValueIter<'a, T> {
3460        self.iter()
3461    }
3462}
3463
3464impl<'a, 'b: 'a, T> IntoIterator for &'b mut OccupiedEntry<'a, T> {
3465    type Item = &'a mut T;
3466    type IntoIter = ValueIterMut<'a, T>;
3467
3468    fn into_iter(self) -> ValueIterMut<'a, T> {
3469        self.iter_mut()
3470    }
3471}
3472
3473// ===== impl ValueDrain =====
3474
3475impl<'a, T> Iterator for ValueDrain<'a, T> {
3476    type Item = T;
3477
3478    fn next(&mut self) -> Option<T> {
3479        if self.first.is_some() {
3480            self.first.take()
3481        } else if let Some(ref mut extras) = self.next {
3482            extras.next()
3483        } else {
3484            None
3485        }
3486    }
3487
3488    fn size_hint(&self) -> (usize, Option<usize>) {
3489        match (&self.first, &self.next) {
3490            // Exactly 1
3491            (&Some(_), &None) => (1, Some(1)),
3492            // 1 + extras
3493            (&Some(_), Some(extras)) => {
3494                let (l, u) = extras.size_hint();
3495                (l + 1, u.map(|u| u + 1))
3496            }
3497            // Extras only
3498            (&None, Some(extras)) => extras.size_hint(),
3499            // No more
3500            (&None, &None) => (0, Some(0)),
3501        }
3502    }
3503}
3504
3505impl<'a, T> FusedIterator for ValueDrain<'a, T> {}
3506
3507impl<'a, T> Drop for ValueDrain<'a, T> {
3508    fn drop(&mut self) {
3509        for _ in self.by_ref() {}
3510    }
3511}
3512
3513unsafe impl<'a, T: Sync> Sync for ValueDrain<'a, T> {}
3514unsafe impl<'a, T: Send> Send for ValueDrain<'a, T> {}
3515
3516// ===== impl RawLinks =====
3517
3518impl<T> Clone for RawLinks<T> {
3519    fn clone(&self) -> RawLinks<T> {
3520        *self
3521    }
3522}
3523
3524impl<T> Copy for RawLinks<T> {}
3525
3526impl<T> ops::Index<usize> for RawLinks<T> {
3527    type Output = Option<Links>;
3528
3529    fn index(&self, idx: usize) -> &Self::Output {
3530        unsafe { &(*self.0)[idx].links }
3531    }
3532}
3533
3534impl<T> ops::IndexMut<usize> for RawLinks<T> {
3535    fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
3536        unsafe { &mut (*self.0)[idx].links }
3537    }
3538}
3539
3540// ===== impl Pos =====
3541
3542impl Pos {
3543    #[inline]
3544    fn new(index: usize, hash: HashValue) -> Self {
3545        debug_assert!(index < MAX_SIZE);
3546        Pos {
3547            index: index as Size,
3548            hash,
3549        }
3550    }
3551
3552    #[inline]
3553    fn none() -> Self {
3554        Pos {
3555            index: !0,
3556            hash: HashValue(0),
3557        }
3558    }
3559
3560    #[inline]
3561    fn is_some(&self) -> bool {
3562        !self.is_none()
3563    }
3564
3565    #[inline]
3566    fn is_none(&self) -> bool {
3567        self.index == !0
3568    }
3569
3570    #[inline]
3571    fn resolve(&self) -> Option<(usize, HashValue)> {
3572        if self.is_some() {
3573            Some((self.index as usize, self.hash))
3574        } else {
3575            None
3576        }
3577    }
3578}
3579
3580impl Danger {
3581    fn is_red(&self) -> bool {
3582        matches!(*self, Danger::Red(_))
3583    }
3584
3585    fn set_red(&mut self) {
3586        debug_assert!(self.is_yellow());
3587        *self = Danger::Red(RandomState::new());
3588    }
3589
3590    fn is_yellow(&self) -> bool {
3591        matches!(*self, Danger::Yellow)
3592    }
3593
3594    fn set_yellow(&mut self) {
3595        if let Danger::Green = *self {
3596            *self = Danger::Yellow;
3597        }
3598    }
3599
3600    fn set_green(&mut self) {
3601        debug_assert!(self.is_yellow());
3602        *self = Danger::Green;
3603    }
3604}
3605
3606// ===== impl MaxSizeReached =====
3607
3608impl MaxSizeReached {
3609    fn new() -> Self {
3610        MaxSizeReached { _priv: () }
3611    }
3612}
3613
3614impl fmt::Debug for MaxSizeReached {
3615    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3616        f.debug_struct("MaxSizeReached")
3617            // skip _priv noise
3618            .finish()
3619    }
3620}
3621
3622impl fmt::Display for MaxSizeReached {
3623    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3624        f.write_str("max size reached")
3625    }
3626}
3627
3628impl std::error::Error for MaxSizeReached {}
3629
3630// ===== impl Utils =====
3631
3632#[inline]
3633fn usable_capacity(cap: usize) -> usize {
3634    cap - cap / 4
3635}
3636
3637#[inline]
3638fn to_raw_capacity(n: usize) -> Result<usize, MaxSizeReached> {
3639    n.checked_add(n / 3).ok_or_else(MaxSizeReached::new)
3640}
3641
3642#[inline]
3643fn desired_pos(mask: Size, hash: HashValue) -> usize {
3644    (hash.0 & mask) as usize
3645}
3646
3647/// The number of steps that `current` is forward of the desired position for hash
3648#[inline]
3649fn probe_distance(mask: Size, hash: HashValue, current: usize) -> usize {
3650    current.wrapping_sub(desired_pos(mask, hash)) & mask as usize
3651}
3652
3653fn hash_elem_using<K>(danger: &Danger, k: &K) -> HashValue
3654where
3655    K: Hash + ?Sized,
3656{
3657    const MASK: u64 = (MAX_SIZE as u64) - 1;
3658
3659    let hash = match *danger {
3660        // Safe hash
3661        Danger::Red(ref hasher) => {
3662            let mut h = hasher.build_hasher();
3663            k.hash(&mut h);
3664            h.finish()
3665        }
3666        // Fast hash
3667        _ => {
3668            let mut h = FnvHasher::new();
3669            k.hash(&mut h);
3670            h.finish()
3671        }
3672    };
3673
3674    HashValue((hash & MASK) as u16)
3675}
3676
3677struct FnvHasher(u64);
3678
3679impl FnvHasher {
3680    #[inline]
3681    fn new() -> Self {
3682        FnvHasher(0xcbf29ce484222325)
3683    }
3684}
3685
3686impl std::hash::Hasher for FnvHasher {
3687    #[inline]
3688    fn finish(&self) -> u64 {
3689        self.0
3690    }
3691
3692    #[inline]
3693    fn write(&mut self, bytes: &[u8]) {
3694        let mut hash = self.0;
3695        for &b in bytes {
3696            hash = hash ^ (b as u64);
3697            hash = hash.wrapping_mul(0x100000001b3);
3698        }
3699        self.0 = hash;
3700    }
3701}
3702
3703/*
3704 *
3705 * ===== impl IntoHeaderName / AsHeaderName =====
3706 *
3707 */
3708
3709mod into_header_name {
3710    use super::{Entry, HdrName, HeaderMap, HeaderName, MaxSizeReached};
3711
3712    /// A marker trait used to identify values that can be used as insert keys
3713    /// to a `HeaderMap`.
3714    pub trait IntoHeaderName: Sealed {}
3715
3716    // All methods are on this pub(super) trait, instead of `IntoHeaderName`,
3717    // so that they aren't publicly exposed to the world.
3718    //
3719    // Being on the `IntoHeaderName` trait would mean users could call
3720    // `"host".insert(&mut map, "localhost")`.
3721    //
3722    // Ultimately, this allows us to adjust the signatures of these methods
3723    // without breaking any external crate.
3724    pub trait Sealed {
3725        #[doc(hidden)]
3726        fn try_insert<T>(self, map: &mut HeaderMap<T>, val: T)
3727            -> Result<Option<T>, MaxSizeReached>;
3728
3729        #[doc(hidden)]
3730        fn try_append<T>(self, map: &mut HeaderMap<T>, val: T) -> Result<bool, MaxSizeReached>;
3731
3732        #[doc(hidden)]
3733        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, MaxSizeReached>;
3734    }
3735
3736    // ==== impls ====
3737
3738    impl Sealed for HeaderName {
3739        #[inline]
3740        fn try_insert<T>(
3741            self,
3742            map: &mut HeaderMap<T>,
3743            val: T,
3744        ) -> Result<Option<T>, MaxSizeReached> {
3745            map.try_insert2(self, val)
3746        }
3747
3748        #[inline]
3749        fn try_append<T>(self, map: &mut HeaderMap<T>, val: T) -> Result<bool, MaxSizeReached> {
3750            map.try_append2(self, val)
3751        }
3752
3753        #[inline]
3754        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, MaxSizeReached> {
3755            map.try_entry2(self)
3756        }
3757    }
3758
3759    impl IntoHeaderName for HeaderName {}
3760
3761    impl<'a> Sealed for &'a HeaderName {
3762        #[inline]
3763        fn try_insert<T>(
3764            self,
3765            map: &mut HeaderMap<T>,
3766            val: T,
3767        ) -> Result<Option<T>, MaxSizeReached> {
3768            map.try_insert2(self, val)
3769        }
3770        #[inline]
3771        fn try_append<T>(self, map: &mut HeaderMap<T>, val: T) -> Result<bool, MaxSizeReached> {
3772            map.try_append2(self, val)
3773        }
3774
3775        #[inline]
3776        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, MaxSizeReached> {
3777            map.try_entry2(self)
3778        }
3779    }
3780
3781    impl<'a> IntoHeaderName for &'a HeaderName {}
3782
3783    impl Sealed for &'static str {
3784        #[inline]
3785        fn try_insert<T>(
3786            self,
3787            map: &mut HeaderMap<T>,
3788            val: T,
3789        ) -> Result<Option<T>, MaxSizeReached> {
3790            HdrName::from_static(self, move |hdr| map.try_insert2(hdr, val))
3791        }
3792        #[inline]
3793        fn try_append<T>(self, map: &mut HeaderMap<T>, val: T) -> Result<bool, MaxSizeReached> {
3794            HdrName::from_static(self, move |hdr| map.try_append2(hdr, val))
3795        }
3796
3797        #[inline]
3798        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, MaxSizeReached> {
3799            HdrName::from_static(self, move |hdr| map.try_entry2(hdr))
3800        }
3801    }
3802
3803    impl IntoHeaderName for &'static str {}
3804}
3805
3806mod as_header_name {
3807    use super::{Entry, HdrName, HeaderMap, HeaderName, InvalidHeaderName, MaxSizeReached};
3808
3809    /// A marker trait used to identify values that can be used as search keys
3810    /// to a `HeaderMap`.
3811    pub trait AsHeaderName: Sealed {}
3812
3813    // Debug not currently needed, save on compiling it
3814    #[allow(missing_debug_implementations)]
3815    pub enum TryEntryError {
3816        InvalidHeaderName(InvalidHeaderName),
3817        MaxSizeReached(MaxSizeReached),
3818    }
3819
3820    impl From<InvalidHeaderName> for TryEntryError {
3821        fn from(e: InvalidHeaderName) -> TryEntryError {
3822            TryEntryError::InvalidHeaderName(e)
3823        }
3824    }
3825
3826    impl From<MaxSizeReached> for TryEntryError {
3827        fn from(e: MaxSizeReached) -> TryEntryError {
3828            TryEntryError::MaxSizeReached(e)
3829        }
3830    }
3831
3832    // All methods are on this pub(super) trait, instead of `AsHeaderName`,
3833    // so that they aren't publicly exposed to the world.
3834    //
3835    // Being on the `AsHeaderName` trait would mean users could call
3836    // `"host".find(&map)`.
3837    //
3838    // Ultimately, this allows us to adjust the signatures of these methods
3839    // without breaking any external crate.
3840    pub trait Sealed {
3841        #[doc(hidden)]
3842        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError>;
3843
3844        #[doc(hidden)]
3845        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)>;
3846
3847        #[doc(hidden)]
3848        fn as_str(&self) -> &str;
3849    }
3850
3851    // ==== impls ====
3852
3853    impl Sealed for HeaderName {
3854        #[inline]
3855        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
3856            Ok(map.try_entry2(self)?)
3857        }
3858
3859        #[inline]
3860        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
3861            map.find(self)
3862        }
3863
3864        fn as_str(&self) -> &str {
3865            <HeaderName>::as_str(self)
3866        }
3867    }
3868
3869    impl AsHeaderName for HeaderName {}
3870
3871    impl<'a> Sealed for &'a HeaderName {
3872        #[inline]
3873        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
3874            Ok(map.try_entry2(self)?)
3875        }
3876
3877        #[inline]
3878        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
3879            map.find(*self)
3880        }
3881
3882        fn as_str(&self) -> &str {
3883            <HeaderName>::as_str(self)
3884        }
3885    }
3886
3887    impl<'a> AsHeaderName for &'a HeaderName {}
3888
3889    impl<'a> Sealed for &'a str {
3890        #[inline]
3891        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
3892            Ok(HdrName::from_bytes(self.as_bytes(), move |hdr| {
3893                map.try_entry2(hdr)
3894            })??)
3895        }
3896
3897        #[inline]
3898        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
3899            HdrName::from_bytes(self.as_bytes(), move |hdr| map.find(&hdr)).unwrap_or(None)
3900        }
3901
3902        fn as_str(&self) -> &str {
3903            self
3904        }
3905    }
3906
3907    impl<'a> AsHeaderName for &'a str {}
3908
3909    impl Sealed for String {
3910        #[inline]
3911        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
3912            self.as_str().try_entry(map)
3913        }
3914
3915        #[inline]
3916        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
3917            Sealed::find(&self.as_str(), map)
3918        }
3919
3920        fn as_str(&self) -> &str {
3921            self
3922        }
3923    }
3924
3925    impl AsHeaderName for String {}
3926
3927    impl<'a> Sealed for &'a String {
3928        #[inline]
3929        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
3930            self.as_str().try_entry(map)
3931        }
3932
3933        #[inline]
3934        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
3935            Sealed::find(*self, map)
3936        }
3937
3938        fn as_str(&self) -> &str {
3939            self
3940        }
3941    }
3942
3943    impl<'a> AsHeaderName for &'a String {}
3944}
3945
3946#[test]
3947fn test_bounds() {
3948    fn check_bounds<T: Send + Send>() {}
3949
3950    check_bounds::<HeaderMap<()>>();
3951    check_bounds::<Iter<'static, ()>>();
3952    check_bounds::<IterMut<'static, ()>>();
3953    check_bounds::<Keys<'static, ()>>();
3954    check_bounds::<Values<'static, ()>>();
3955    check_bounds::<ValuesMut<'static, ()>>();
3956    check_bounds::<Drain<'static, ()>>();
3957    check_bounds::<GetAll<'static, ()>>();
3958    check_bounds::<Entry<'static, ()>>();
3959    check_bounds::<VacantEntry<'static, ()>>();
3960    check_bounds::<OccupiedEntry<'static, ()>>();
3961    check_bounds::<ValueIter<'static, ()>>();
3962    check_bounds::<ValueIterMut<'static, ()>>();
3963    check_bounds::<ValueDrain<'static, ()>>();
3964}
3965
3966#[test]
3967fn skip_duplicates_during_key_iteration() {
3968    let mut map = HeaderMap::new();
3969    map.try_append("a", HeaderValue::from_static("a")).unwrap();
3970    map.try_append("a", HeaderValue::from_static("b")).unwrap();
3971    assert_eq!(map.keys().count(), map.keys_len());
3972}