Skip to main content

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