Skip to main content

rama_http_types/header/
map.rs

1use std::collections::HashMap;
2use std::collections::hash_map::RandomState;
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::HeaderValue;
12use super::name::{HdrName, HeaderName, InvalidHeaderName};
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 rama_http_types::HeaderMap;
65/// # use rama_http_types::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    order: Vec<Option<Link>>,
88    order_holes: usize,
89    danger: Danger,
90}
91
92// # Implementation notes
93//
94// Below, you will find a fairly large amount of code. Most of this is to
95// provide the necessary functions to efficiently manipulate the header
96// multimap. The core hashing table is based on robin hood hashing [1]. While
97// this is the same hashing algorithm used as part of Rust's `HashMap` in
98// stdlib, many implementation details are different. The two primary reasons
99// for this divergence are that `HeaderMap` is a multimap and the structure has
100// been optimized to take advantage of the characteristics of HTTP headers.
101//
102// ## Structure Layout
103//
104// Most of the data contained by `HeaderMap` is *not* stored in the hash table.
105// Instead, pairs of header name and *first* associated header value are stored
106// in the `entries` vector. If the header name has more than one associated
107// header value, then additional values are stored in `extra_values`. The actual
108// hash table (`indices`) only maps hash codes to indices in `entries`. This
109// means that, when an eviction happens, the actual header name and value stay
110// put and only a tiny amount of memory has to be copied.
111//
112// Extra values associated with a header name are tracked using a linked list.
113// Links are formed with offsets into `extra_values` and not pointers.
114//
115// [1]: https://en.wikipedia.org/wiki/Hash_table#Robin_Hood_hashing
116
117/// `HeaderMap` entry iterator.
118///
119/// Yields `(&HeaderName, &value)` tuples. The same header name may be yielded
120/// more than once if it has more than one associated value.
121#[derive(Debug)]
122pub struct Iter<'a, T> {
123    map: &'a HeaderMap<T>,
124    entry: usize,
125    cursor: Option<Cursor>,
126}
127
128/// `HeaderMap` mutable entry iterator
129///
130/// Yields `(&HeaderName, &mut value)` tuples. The same header name may be
131/// yielded more than once if it has more than one associated value.
132#[derive(Debug)]
133pub struct IterMut<'a, T> {
134    // Raw access avoids reborrowing the whole `HeaderMap` on every `next()`,
135    // which would invalidate previously yielded `&mut T`s.
136    entries: *mut Bucket<T>,
137    entries_len: usize,
138    // This points at the original `HeaderMap::extra_values` allocation for the
139    // lifetime of the iterator.
140    extra_values: *mut ExtraValue<T>,
141    entry: usize,
142    cursor: Option<Cursor>,
143    lt: PhantomData<&'a mut HeaderMap<T>>,
144}
145
146/// An owning iterator over the entries of a `HeaderMap`.
147///
148/// This struct is created by the `into_iter` method on `HeaderMap`.
149#[derive(Debug)]
150pub struct IntoIter<T> {
151    // If None, pull from `entries`
152    next: Option<usize>,
153    entries: vec::IntoIter<Bucket<T>>,
154    extra_values: Vec<ExtraValue<T>>,
155}
156
157/// An iterator over `HeaderMap` entries in their original insertion order.
158///
159/// Yields `(&HeaderName, &value)` tuples. The same semantic header name may be
160/// yielded more than once and each yielded name preserves the casing that was
161/// used when that field line was inserted.
162#[derive(Debug)]
163pub struct OrderedIter<'a, T> {
164    map: &'a HeaderMap<T>,
165    index: usize,
166}
167
168/// A consuming iterator over `HeaderMap` entries in original insertion order.
169///
170/// Each yielded `HeaderName` preserves the casing that was used when that field
171/// line was inserted.
172#[derive(Debug)]
173pub struct IntoOrderedIter<T> {
174    index: usize,
175    order: Vec<Option<Link>>,
176    entries: Vec<Bucket<T>>,
177    extra_values: Vec<ExtraValue<T>>,
178}
179
180/// An iterator over `HeaderMap` keys.
181///
182/// Each header name is yielded only once, even if it has more than one
183/// associated value.
184#[derive(Debug)]
185pub struct Keys<'a, T> {
186    inner: ::std::slice::Iter<'a, Bucket<T>>,
187}
188
189/// `HeaderMap` value iterator.
190///
191/// Each value contained in the `HeaderMap` will be yielded.
192#[derive(Debug)]
193pub struct Values<'a, T> {
194    inner: Iter<'a, T>,
195}
196
197/// `HeaderMap` mutable value iterator
198#[derive(Debug)]
199pub struct ValuesMut<'a, T> {
200    inner: IterMut<'a, T>,
201}
202
203/// A drain iterator for `HeaderMap`.
204#[derive(Debug)]
205pub struct Drain<'a, T> {
206    idx: usize,
207    len: usize,
208    entries: *mut [Bucket<T>],
209    // If None, pull from `entries`
210    next: Option<usize>,
211    extra_values: *mut Vec<ExtraValue<T>>,
212    lt: PhantomData<&'a mut HeaderMap<T>>,
213}
214
215/// A view to all values stored in a single entry.
216///
217/// This struct is returned by `HeaderMap::get_all`.
218#[derive(Debug)]
219pub struct GetAll<'a, T> {
220    map: &'a HeaderMap<T>,
221    index: Option<usize>,
222}
223
224/// A view into a single location in a `HeaderMap`, which may be vacant or occupied.
225#[derive(Debug)]
226pub enum Entry<'a, T: 'a> {
227    /// An occupied entry
228    Occupied(OccupiedEntry<'a, T>),
229
230    /// A vacant entry
231    Vacant(VacantEntry<'a, T>),
232}
233
234/// A view into a single empty location in a `HeaderMap`.
235///
236/// This struct is returned as part of the `Entry` enum.
237#[derive(Debug)]
238pub struct VacantEntry<'a, T> {
239    map: &'a mut HeaderMap<T>,
240    key: HeaderName,
241    hash: HashValue,
242    probe: usize,
243    danger: bool,
244}
245
246/// A view into a single occupied location in a `HeaderMap`.
247///
248/// This struct is returned as part of the `Entry` enum.
249#[derive(Debug)]
250pub struct OccupiedEntry<'a, T> {
251    map: &'a mut HeaderMap<T>,
252    probe: usize,
253    index: usize,
254}
255
256/// An iterator of all values associated with a single header name.
257#[derive(Debug)]
258pub struct ValueIter<'a, T> {
259    map: &'a HeaderMap<T>,
260    index: usize,
261    front: Option<Cursor>,
262    back: Option<Cursor>,
263}
264
265/// A mutable iterator of all values associated with a single header name.
266#[derive(Debug)]
267pub struct ValueIterMut<'a, T> {
268    // Raw access avoids reborrowing the whole `HeaderMap` on every step.
269    entries: *mut Bucket<T>,
270    // This points at the original `HeaderMap::extra_values` allocation for the
271    // lifetime of the iterator.
272    extra_values: *mut ExtraValue<T>,
273    index: usize,
274    front: Option<Cursor>,
275    back: Option<Cursor>,
276    lt: PhantomData<&'a mut HeaderMap<T>>,
277}
278
279/// An drain iterator of all values associated with a single header name.
280#[derive(Debug)]
281pub struct ValueDrain<'a, T> {
282    first: Option<T>,
283    next: Option<::std::vec::IntoIter<T>>,
284    lt: PhantomData<&'a mut HeaderMap<T>>,
285}
286
287/// Error returned when max capacity of `HeaderMap` is exceeded
288pub struct MaxSizeReached {
289    _priv: (),
290}
291
292/// Tracks the value iterator state
293#[derive(Debug, Copy, Clone, Eq, PartialEq)]
294enum Cursor {
295    Head,
296    Values(usize),
297}
298
299/// Type used for representing the size of a HeaderMap value.
300///
301/// 32,768 is more than enough entries for a single header map. Setting this
302/// limit enables using `u16` to represent all offsets, which takes 2 bytes
303/// instead of 8 on 64 bit processors.
304///
305/// Setting this limit is especially beneficial for `indices`, making it more
306/// cache friendly. More hash codes can fit in a cache line.
307///
308/// You may notice that `u16` may represent more than 32,768 values. This is
309/// true, but 32,768 should be plenty and it allows us to reserve the top bit
310/// for future usage.
311type Size = u16;
312
313/// This limit falls out from above.
314const MAX_SIZE: usize = 1 << 15;
315
316/// An entry in the hash table. This represents the full hash code for an entry
317/// as well as the position of the entry in the `entries` vector.
318#[derive(Copy, Clone)]
319struct Pos {
320    // Index in the `entries` vec
321    index: Size,
322    // Full hash value for the entry.
323    hash: HashValue,
324}
325
326/// Hash values are limited to u16 as well. While `fast_hash` and `Hasher`
327/// return `usize` hash codes, limiting the effective hash code to the lower 16
328/// bits is fine since we know that the `indices` vector will never grow beyond
329/// that size.
330#[derive(Debug, Copy, Clone, Eq, PartialEq)]
331struct HashValue(u16);
332
333/// Stores the data associated with a `HeaderMap` entry. Only the first value is
334/// included in this struct. If a header name has more than one associated
335/// value, all extra values are stored in the `extra_values` vector. A doubly
336/// linked list of entries is maintained. The doubly linked list is used so that
337/// removing a value is constant time. This also has the nice property of
338/// enabling double ended iteration.
339#[derive(Debug, Clone)]
340struct Bucket<T> {
341    hash: HashValue,
342    key: HeaderName,
343    value: T,
344    links: Option<Links>,
345    order: usize,
346}
347
348/// The head and tail of the value linked list.
349#[derive(Debug, Copy, Clone)]
350struct Links {
351    next: usize,
352    tail: usize,
353}
354
355/// Access to the `links` value in a slice of buckets.
356///
357/// It's important that no other field is accessed, since it may have been
358/// freed in a `Drain` iterator.
359#[derive(Debug)]
360struct RawLinks<T>(*mut [Bucket<T>]);
361
362/// Node in doubly-linked list of header value entries
363#[derive(Debug, Clone)]
364struct ExtraValue<T> {
365    key: HeaderName,
366    value: T,
367    prev: Link,
368    next: Link,
369    order: usize,
370}
371
372/// A header value node is either linked to another node in the `extra_values`
373/// list or it points to an entry in `entries`. The entry in `entries` is the
374/// start of the list and holds the associated header name.
375#[derive(Debug, Copy, Clone, Eq, PartialEq)]
376enum Link {
377    Entry(usize),
378    Extra(usize),
379}
380
381/// Tracks the header map danger level! This relates to the adaptive hashing
382/// algorithm. A HeaderMap starts in the "green" state, when a large number of
383/// collisions are detected, it transitions to the yellow state. At this point,
384/// the header map will either grow and switch back to the green state OR it
385/// will transition to the red state.
386///
387/// When in the red state, a safe hashing algorithm is used and all values in
388/// the header map have to be rehashed.
389#[derive(Clone)]
390enum Danger {
391    Green,
392    Yellow,
393    Red(RandomState),
394}
395
396// Constants related to detecting DOS attacks.
397//
398// Displacement is the number of entries that get shifted when inserting a new
399// value. Forward shift is how far the entry gets stored from the ideal
400// position.
401//
402// The current constant values were picked from another implementation. It could
403// be that there are different values better suited to the header map case.
404const DISPLACEMENT_THRESHOLD: usize = 128;
405const FORWARD_SHIFT_THRESHOLD: usize = 512;
406
407// The default strategy for handling the yellow danger state is to increase the
408// header map capacity in order to (hopefully) reduce the number of collisions.
409// If growing the hash map would cause the load factor to drop bellow this
410// threshold, then instead of growing, the headermap is switched to the red
411// danger state and safe hashing is used instead.
412const LOAD_FACTOR_THRESHOLD: usize = 5;
413
414// Macro used to iterate the hash table starting at a given point, looping when
415// the end is hit.
416macro_rules! probe_loop {
417    ($label:tt: $probe_var: ident < $len: expr, $body: expr) => {
418        debug_assert!($len > 0);
419        $label:
420        loop {
421            if $probe_var < $len {
422                $body
423                $probe_var += 1;
424            } else {
425                $probe_var = 0;
426            }
427        }
428    };
429    ($probe_var: ident < $len: expr, $body: expr) => {
430        debug_assert!($len > 0);
431        loop {
432            if $probe_var < $len {
433                $body
434                $probe_var += 1;
435            } else {
436                $probe_var = 0;
437            }
438        }
439    };
440}
441
442// First part of the robinhood algorithm. Given a key, find the slot in which it
443// will be inserted. This is done by starting at the "ideal" spot. Then scanning
444// until the destination slot is found. A destination slot is either the next
445// empty slot or the next slot that is occupied by an entry that has a lower
446// displacement (displacement is the distance from the ideal spot).
447//
448// This is implemented as a macro instead of a function that takes a closure in
449// order to guarantee that it is "inlined". There is no way to annotate closures
450// to guarantee inlining.
451macro_rules! insert_phase_one {
452    ($map:ident,
453     $key:expr,
454     $probe:ident,
455     $pos:ident,
456     $hash:ident,
457     $danger:ident,
458     $vacant:expr,
459     $occupied:expr,
460     $robinhood:expr) =>
461    {{
462        let $hash = hash_elem_using(&$map.danger, &$key);
463        let mut $probe = desired_pos($map.mask, $hash);
464        let mut dist = 0;
465        let ret;
466
467        // Start at the ideal position, checking all slots
468        probe_loop!('probe: $probe < $map.indices.len(), {
469            if let Some(($pos, entry_hash)) = $map.indices[$probe].resolve() {
470                // The slot is already occupied, but check if it has a lower
471                // displacement.
472                let their_dist = probe_distance($map.mask, entry_hash, $probe);
473
474                if their_dist < dist {
475                    // The new key's distance is larger, so claim this spot and
476                    // displace the current entry.
477                    //
478                    // Check if this insertion is above the danger threshold.
479                    let $danger =
480                        dist >= FORWARD_SHIFT_THRESHOLD && !$map.danger.is_red();
481
482                    ret = $robinhood;
483                    break 'probe;
484                } else if entry_hash == $hash && $map.entries[$pos].key == $key {
485                    // There already is an entry with the same key.
486                    ret = $occupied;
487                    break 'probe;
488                }
489            } else {
490                // The entry is vacant, use it for this key.
491                let $danger =
492                    dist >= FORWARD_SHIFT_THRESHOLD && !$map.danger.is_red();
493
494                ret = $vacant;
495                break 'probe;
496            }
497
498            dist += 1;
499        });
500
501        ret
502    }}
503}
504
505// ===== impl HeaderMap =====
506
507impl HeaderMap {
508    /// Create an empty `HeaderMap`.
509    ///
510    /// The map will be created without any capacity. This function will not
511    /// allocate.
512    ///
513    /// # Examples
514    ///
515    /// ```
516    /// # use rama_http_types::HeaderMap;
517    /// let map = HeaderMap::new();
518    ///
519    /// assert!(map.is_empty());
520    /// assert_eq!(0, map.capacity());
521    /// ```
522    #[inline]
523    pub fn new() -> Self {
524        Self::default()
525    }
526}
527
528impl<T> Default for HeaderMap<T> {
529    fn default() -> Self {
530        HeaderMap {
531            mask: 0,
532            indices: Box::new([]), // as a ZST, this doesn't actually allocate anything
533            entries: Vec::new(),
534            extra_values: Vec::new(),
535            order: Vec::new(),
536            order_holes: 0,
537            danger: Danger::Green,
538        }
539    }
540}
541
542impl<T> HeaderMap<T> {
543    /// Create an empty `HeaderMap` with the specified capacity.
544    ///
545    /// The returned map will allocate internal storage in order to hold about
546    /// `capacity` elements without reallocating. However, this is a "best
547    /// effort" as there are usage patterns that could cause additional
548    /// allocations before `capacity` headers are stored in the map.
549    ///
550    /// More capacity than requested may be allocated.
551    ///
552    /// # Panics
553    ///
554    /// This method panics if capacity exceeds max `HeaderMap` capacity.
555    ///
556    /// # Examples
557    ///
558    /// ```
559    /// # use rama_http_types::HeaderMap;
560    /// let map: HeaderMap<u32> = HeaderMap::with_capacity(10);
561    ///
562    /// assert!(map.is_empty());
563    /// assert_eq!(12, map.capacity());
564    /// ```
565    pub fn with_capacity(capacity: usize) -> HeaderMap<T> {
566        Self::try_with_capacity(capacity).expect("size overflows MAX_SIZE")
567    }
568
569    /// Create an empty `HeaderMap` with the specified capacity.
570    ///
571    /// The returned map will allocate internal storage in order to hold about
572    /// `capacity` elements without reallocating. However, this is a "best
573    /// effort" as there are usage patterns that could cause additional
574    /// allocations before `capacity` headers are stored in the map.
575    ///
576    /// More capacity than requested may be allocated.
577    ///
578    /// # Errors
579    ///
580    /// This function may return an error if `HeaderMap` exceeds max capacity
581    ///
582    /// # Examples
583    ///
584    /// ```
585    /// # use rama_http_types::HeaderMap;
586    /// let map: HeaderMap<u32> = HeaderMap::try_with_capacity(10).unwrap();
587    ///
588    /// assert!(map.is_empty());
589    /// assert_eq!(12, map.capacity());
590    /// ```
591    pub fn try_with_capacity(capacity: usize) -> Result<HeaderMap<T>, MaxSizeReached> {
592        if capacity == 0 {
593            Ok(Self::default())
594        } else {
595            let raw_cap = to_raw_capacity(capacity)?;
596            let raw_cap = match raw_cap.checked_next_power_of_two() {
597                Some(c) => c,
598                None => return Err(MaxSizeReached { _priv: () }),
599            };
600            if raw_cap > MAX_SIZE {
601                return Err(MaxSizeReached { _priv: () });
602            }
603            debug_assert!(raw_cap > 0);
604
605            Ok(HeaderMap {
606                mask: (raw_cap - 1) as Size,
607                indices: vec![Pos::none(); raw_cap].into_boxed_slice(),
608                entries: Vec::with_capacity(usable_capacity(raw_cap)),
609                extra_values: Vec::new(),
610                order: Vec::with_capacity(capacity),
611                order_holes: 0,
612                danger: Danger::Green,
613            })
614        }
615    }
616
617    /// Returns the number of headers stored in the map.
618    ///
619    /// This number represents the total number of **values** stored in the map.
620    /// This number can be greater than or equal to the number of **keys**
621    /// stored given that a single key may have more than one associated value.
622    ///
623    /// # Examples
624    ///
625    /// ```
626    /// # use rama_http_types::HeaderMap;
627    /// # use rama_http_types::header::{ACCEPT, HOST};
628    /// let mut map = HeaderMap::new();
629    ///
630    /// assert_eq!(0, map.len());
631    ///
632    /// map.insert(ACCEPT, "text/plain".parse().unwrap());
633    /// map.insert(HOST, "localhost".parse().unwrap());
634    ///
635    /// assert_eq!(2, map.len());
636    ///
637    /// map.append(ACCEPT, "text/html".parse().unwrap());
638    ///
639    /// assert_eq!(3, map.len());
640    /// ```
641    pub fn len(&self) -> usize {
642        self.entries.len() + self.extra_values.len()
643    }
644
645    /// Returns the number of keys stored in the map.
646    ///
647    /// This number will be less than or equal to `len()` as each key may have
648    /// more than one associated value.
649    ///
650    /// # Examples
651    ///
652    /// ```
653    /// # use rama_http_types::HeaderMap;
654    /// # use rama_http_types::header::{ACCEPT, HOST};
655    /// let mut map = HeaderMap::new();
656    ///
657    /// assert_eq!(0, map.keys_len());
658    ///
659    /// map.insert(ACCEPT, "text/plain".parse().unwrap());
660    /// map.insert(HOST, "localhost".parse().unwrap());
661    ///
662    /// assert_eq!(2, map.keys_len());
663    ///
664    /// map.insert(ACCEPT, "text/html".parse().unwrap());
665    ///
666    /// assert_eq!(2, map.keys_len());
667    /// ```
668    pub fn keys_len(&self) -> usize {
669        self.entries.len()
670    }
671
672    /// Returns true if the map contains no elements.
673    ///
674    /// # Examples
675    ///
676    /// ```
677    /// # use rama_http_types::HeaderMap;
678    /// # use rama_http_types::header::HOST;
679    /// let mut map = HeaderMap::new();
680    ///
681    /// assert!(map.is_empty());
682    ///
683    /// map.insert(HOST, "hello.world".parse().unwrap());
684    ///
685    /// assert!(!map.is_empty());
686    /// ```
687    pub fn is_empty(&self) -> bool {
688        self.entries.len() == 0
689    }
690
691    /// Clears the map, removing all key-value pairs. Keeps the allocated memory
692    /// for reuse.
693    ///
694    /// # Examples
695    ///
696    /// ```
697    /// # use rama_http_types::HeaderMap;
698    /// # use rama_http_types::header::HOST;
699    /// let mut map = HeaderMap::new();
700    /// map.insert(HOST, "hello.world".parse().unwrap());
701    ///
702    /// map.clear();
703    /// assert!(map.is_empty());
704    /// assert!(map.capacity() > 0);
705    /// ```
706    pub fn clear(&mut self) {
707        self.entries.clear();
708        self.extra_values.clear();
709        self.order.clear();
710        self.order_holes = 0;
711        self.danger = Danger::Green;
712
713        for e in self.indices.iter_mut() {
714            *e = Pos::none();
715        }
716    }
717
718    /// Returns the number of headers the map can hold without reallocating.
719    ///
720    /// This number is an approximation as certain usage patterns could cause
721    /// additional allocations before the returned capacity is filled.
722    ///
723    /// # Examples
724    ///
725    /// ```
726    /// # use rama_http_types::HeaderMap;
727    /// # use rama_http_types::header::HOST;
728    /// let mut map = HeaderMap::new();
729    ///
730    /// assert_eq!(0, map.capacity());
731    ///
732    /// map.insert(HOST, "hello.world".parse().unwrap());
733    /// assert_eq!(6, map.capacity());
734    /// ```
735    pub fn capacity(&self) -> usize {
736        usable_capacity(self.indices.len())
737    }
738
739    /// Reserves capacity for at least `additional` more headers to be inserted
740    /// into the `HeaderMap`.
741    ///
742    /// The header map may reserve more space to avoid frequent reallocations.
743    /// Like with `with_capacity`, this will be a "best effort" to avoid
744    /// allocations until `additional` more headers are inserted. Certain usage
745    /// patterns could cause additional allocations before the number is
746    /// reached.
747    ///
748    /// # Panics
749    ///
750    /// Panics if the new allocation size overflows `HeaderMap` `MAX_SIZE`.
751    ///
752    /// # Examples
753    ///
754    /// ```
755    /// # use rama_http_types::HeaderMap;
756    /// # use rama_http_types::header::HOST;
757    /// let mut map = HeaderMap::new();
758    /// map.reserve(10);
759    /// # map.insert(HOST, "bar".parse().unwrap());
760    /// ```
761    pub fn reserve(&mut self, additional: usize) {
762        self.try_reserve(additional)
763            .expect("size overflows MAX_SIZE")
764    }
765
766    /// Reserves capacity for at least `additional` more headers to be inserted
767    /// into the `HeaderMap`.
768    ///
769    /// The header map may reserve more space to avoid frequent reallocations.
770    /// Like with `with_capacity`, this will be a "best effort" to avoid
771    /// allocations until `additional` more headers are inserted. Certain usage
772    /// patterns could cause additional allocations before the number is
773    /// reached.
774    ///
775    /// # Errors
776    ///
777    /// This method differs from `reserve` by returning an error instead of
778    /// panicking if the value is too large.
779    ///
780    /// # Examples
781    ///
782    /// ```
783    /// # use rama_http_types::HeaderMap;
784    /// # use rama_http_types::header::HOST;
785    /// let mut map = HeaderMap::new();
786    /// map.try_reserve(10).unwrap();
787    /// # map.try_insert(HOST, "bar".parse().unwrap()).unwrap();
788    /// ```
789    pub fn try_reserve(&mut self, additional: usize) -> Result<(), MaxSizeReached> {
790        // TODO: This can't overflow if done properly... since the max # of
791        // elements is u16::MAX.
792        let cap = self
793            .entries
794            .len()
795            .checked_add(additional)
796            .ok_or_else(MaxSizeReached::new)?;
797
798        let raw_cap = to_raw_capacity(cap)?;
799
800        if raw_cap > self.indices.len() {
801            let raw_cap = raw_cap
802                .checked_next_power_of_two()
803                .ok_or_else(MaxSizeReached::new)?;
804            if raw_cap > MAX_SIZE {
805                return Err(MaxSizeReached::new());
806            }
807
808            if self.entries.is_empty() {
809                self.mask = raw_cap as Size - 1;
810                self.indices = vec![Pos::none(); raw_cap].into_boxed_slice();
811                let cap = usable_capacity(raw_cap);
812                self.entries = Vec::with_capacity(cap);
813                self.order.reserve_exact(cap);
814            } else {
815                self.try_grow(raw_cap)?;
816            }
817        }
818
819        self.order.reserve(additional);
820
821        Ok(())
822    }
823
824    /// Returns a reference to the value associated with the key.
825    ///
826    /// If there are multiple values associated with the key, then the first one
827    /// is returned. Use `get_all` to get all values associated with a given
828    /// key. Returns `None` if there are no values associated with the key.
829    ///
830    /// # Examples
831    ///
832    /// ```
833    /// # use rama_http_types::HeaderMap;
834    /// # use rama_http_types::header::HOST;
835    /// let mut map = HeaderMap::new();
836    /// assert!(map.get("host").is_none());
837    ///
838    /// map.insert(HOST, "hello".parse().unwrap());
839    /// assert_eq!(map.get(HOST).unwrap(), &"hello");
840    /// assert_eq!(map.get("host").unwrap(), &"hello");
841    ///
842    /// map.append(HOST, "world".parse().unwrap());
843    /// assert_eq!(map.get("host").unwrap(), &"hello");
844    /// ```
845    pub fn get<K>(&self, key: K) -> Option<&T>
846    where
847        K: AsHeaderName,
848    {
849        self.get2(&key)
850    }
851
852    fn get2<K>(&self, key: &K) -> Option<&T>
853    where
854        K: AsHeaderName,
855    {
856        match key.find(self) {
857            Some((_, found)) => {
858                let entry = &self.entries[found];
859                Some(&entry.value)
860            }
861            None => None,
862        }
863    }
864
865    /// Returns a mutable reference to the value associated with the key.
866    ///
867    /// If there are multiple values associated with the key, then the first one
868    /// is returned. Use `entry` to get all values associated with a given
869    /// key. Returns `None` if there are no values associated with the key.
870    ///
871    /// # Examples
872    ///
873    /// ```
874    /// # use rama_http_types::HeaderMap;
875    /// # use rama_http_types::header::HOST;
876    /// let mut map = HeaderMap::default();
877    /// map.insert(HOST, "hello".to_string());
878    /// map.get_mut("host").unwrap().push_str("-world");
879    ///
880    /// assert_eq!(map.get(HOST).unwrap(), &"hello-world");
881    /// ```
882    pub fn get_mut<K>(&mut self, key: K) -> Option<&mut T>
883    where
884        K: AsHeaderName,
885    {
886        match key.find(self) {
887            Some((_, found)) => {
888                let entry = &mut self.entries[found];
889                Some(&mut entry.value)
890            }
891            None => None,
892        }
893    }
894
895    /// Returns a view of all values associated with a key.
896    ///
897    /// The returned view does not incur any allocations and allows iterating
898    /// the values associated with the key.  See [`GetAll`] for more details.
899    /// Returns `None` if there are no values associated with the key.
900    ///
901    /// [`GetAll`]: struct.GetAll.html
902    ///
903    /// # Examples
904    ///
905    /// ```
906    /// # use rama_http_types::HeaderMap;
907    /// # use rama_http_types::header::HOST;
908    /// let mut map = HeaderMap::new();
909    ///
910    /// map.insert(HOST, "hello".parse().unwrap());
911    /// map.append(HOST, "goodbye".parse().unwrap());
912    ///
913    /// let view = map.get_all("host");
914    ///
915    /// let mut iter = view.iter();
916    /// assert_eq!(&"hello", iter.next().unwrap());
917    /// assert_eq!(&"goodbye", iter.next().unwrap());
918    /// assert!(iter.next().is_none());
919    /// ```
920    pub fn get_all<K>(&self, key: K) -> GetAll<'_, T>
921    where
922        K: AsHeaderName,
923    {
924        GetAll {
925            map: self,
926            index: key.find(self).map(|(_, i)| i),
927        }
928    }
929
930    /// Returns true if the map contains a value for the specified key.
931    ///
932    /// # Examples
933    ///
934    /// ```
935    /// # use rama_http_types::HeaderMap;
936    /// # use rama_http_types::header::HOST;
937    /// let mut map = HeaderMap::new();
938    /// assert!(!map.contains_key(HOST));
939    ///
940    /// map.insert(HOST, "world".parse().unwrap());
941    /// assert!(map.contains_key("host"));
942    /// ```
943    pub fn contains_key<K>(&self, key: K) -> bool
944    where
945        K: AsHeaderName,
946    {
947        key.find(self).is_some()
948    }
949
950    /// An iterator visiting all key-value pairs.
951    ///
952    /// The iteration order is arbitrary, but consistent across platforms for
953    /// the same crate version. Each key will be yielded once per associated
954    /// value. So, if a key has 3 associated values, it will be yielded 3 times.
955    ///
956    /// # Examples
957    ///
958    /// ```
959    /// # use rama_http_types::HeaderMap;
960    /// # use rama_http_types::header::{CONTENT_LENGTH, HOST};
961    /// let mut map = HeaderMap::new();
962    ///
963    /// map.insert(HOST, "hello".parse().unwrap());
964    /// map.append(HOST, "goodbye".parse().unwrap());
965    /// map.insert(CONTENT_LENGTH, "123".parse().unwrap());
966    ///
967    /// for (key, value) in map.iter() {
968    ///     println!("{:?}: {:?}", key, value);
969    /// }
970    /// ```
971    pub fn iter(&self) -> Iter<'_, T> {
972        Iter {
973            map: self,
974            entry: 0,
975            cursor: self.entries.first().map(|_| Cursor::Head),
976        }
977    }
978
979    /// An iterator visiting all key-value pairs in their original insertion
980    /// order.
981    ///
982    /// Each yielded key preserves the casing that was used for that field line
983    /// when it was inserted into the map.
984    pub fn ordered_iter(&self) -> OrderedIter<'_, T> {
985        OrderedIter {
986            map: self,
987            index: 0,
988        }
989    }
990
991    /// An iterator visiting all key-value pairs, with mutable value references.
992    ///
993    /// The iterator order is arbitrary, but consistent across platforms for the
994    /// same crate version. Each key will be yielded once per associated value,
995    /// so if a key has 3 associated values, it will be yielded 3 times.
996    ///
997    /// # Examples
998    ///
999    /// ```
1000    /// # use rama_http_types::HeaderMap;
1001    /// # use rama_http_types::header::{CONTENT_LENGTH, HOST};
1002    /// let mut map = HeaderMap::default();
1003    ///
1004    /// map.insert(HOST, "hello".to_string());
1005    /// map.append(HOST, "goodbye".to_string());
1006    /// map.insert(CONTENT_LENGTH, "123".to_string());
1007    ///
1008    /// for (key, value) in map.iter_mut() {
1009    ///     value.push_str("-boop");
1010    /// }
1011    /// ```
1012    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
1013        IterMut {
1014            entries: self.entries.as_mut_ptr(),
1015            entries_len: self.entries.len(),
1016            extra_values: self.extra_values.as_mut_ptr(),
1017            entry: 0,
1018            cursor: self.entries.first().map(|_| Cursor::Head),
1019            lt: PhantomData,
1020        }
1021    }
1022
1023    /// An iterator visiting all keys.
1024    ///
1025    /// The iteration order is arbitrary, but consistent across platforms for
1026    /// the same crate version. Each key will be yielded only once even if it
1027    /// has multiple associated values.
1028    ///
1029    /// # Examples
1030    ///
1031    /// ```
1032    /// # use rama_http_types::HeaderMap;
1033    /// # use rama_http_types::header::{CONTENT_LENGTH, HOST};
1034    /// let mut map = HeaderMap::new();
1035    ///
1036    /// map.insert(HOST, "hello".parse().unwrap());
1037    /// map.append(HOST, "goodbye".parse().unwrap());
1038    /// map.insert(CONTENT_LENGTH, "123".parse().unwrap());
1039    ///
1040    /// for key in map.keys() {
1041    ///     println!("{:?}", key);
1042    /// }
1043    /// ```
1044    pub fn keys(&self) -> Keys<'_, T> {
1045        Keys {
1046            inner: self.entries.iter(),
1047        }
1048    }
1049
1050    /// An iterator visiting all values.
1051    ///
1052    /// The iteration order is arbitrary, but consistent across platforms for
1053    /// the same crate version.
1054    ///
1055    /// # Examples
1056    ///
1057    /// ```
1058    /// # use rama_http_types::HeaderMap;
1059    /// # use rama_http_types::header::{CONTENT_LENGTH, HOST};
1060    /// let mut map = HeaderMap::new();
1061    ///
1062    /// map.insert(HOST, "hello".parse().unwrap());
1063    /// map.append(HOST, "goodbye".parse().unwrap());
1064    /// map.insert(CONTENT_LENGTH, "123".parse().unwrap());
1065    ///
1066    /// for value in map.values() {
1067    ///     println!("{:?}", value);
1068    /// }
1069    /// ```
1070    pub fn values(&self) -> Values<'_, T> {
1071        Values { inner: self.iter() }
1072    }
1073
1074    /// An iterator visiting all values mutably.
1075    ///
1076    /// The iteration order is arbitrary, but consistent across platforms for
1077    /// the same crate version.
1078    ///
1079    /// # Examples
1080    ///
1081    /// ```
1082    /// # use rama_http_types::HeaderMap;
1083    /// # use rama_http_types::header::{CONTENT_LENGTH, HOST};
1084    /// let mut map = HeaderMap::default();
1085    ///
1086    /// map.insert(HOST, "hello".to_string());
1087    /// map.append(HOST, "goodbye".to_string());
1088    /// map.insert(CONTENT_LENGTH, "123".to_string());
1089    ///
1090    /// for value in map.values_mut() {
1091    ///     value.push_str("-boop");
1092    /// }
1093    /// ```
1094    pub fn values_mut(&mut self) -> ValuesMut<'_, T> {
1095        ValuesMut {
1096            inner: self.iter_mut(),
1097        }
1098    }
1099
1100    /// Clears the map, returning all entries as an iterator.
1101    ///
1102    /// The internal memory is kept for reuse.
1103    ///
1104    /// For each yielded item that has `None` provided for the `HeaderName`,
1105    /// then the associated header name is the same as that of the previously
1106    /// yielded item. The first yielded item will have `HeaderName` set.
1107    ///
1108    /// # Examples
1109    ///
1110    /// ```
1111    /// # use rama_http_types::HeaderMap;
1112    /// # use rama_http_types::header::{CONTENT_LENGTH, HOST};
1113    /// let mut map = HeaderMap::new();
1114    ///
1115    /// map.insert(HOST, "hello".parse().unwrap());
1116    /// map.append(HOST, "goodbye".parse().unwrap());
1117    /// map.insert(CONTENT_LENGTH, "123".parse().unwrap());
1118    ///
1119    /// let mut drain = map.drain();
1120    ///
1121    ///
1122    /// assert_eq!(drain.next(), Some((Some(HOST), "hello".parse().unwrap())));
1123    /// assert_eq!(drain.next(), Some((None, "goodbye".parse().unwrap())));
1124    ///
1125    /// assert_eq!(drain.next(), Some((Some(CONTENT_LENGTH), "123".parse().unwrap())));
1126    ///
1127    /// assert_eq!(drain.next(), None);
1128    /// ```
1129    pub fn drain(&mut self) -> Drain<'_, T> {
1130        for i in self.indices.iter_mut() {
1131            *i = Pos::none();
1132        }
1133
1134        // Memory safety
1135        //
1136        // When the Drain is first created, it shortens the length of
1137        // the source vector to make sure no uninitialized or moved-from
1138        // elements are accessible at all if the Drain's destructor never
1139        // gets to run.
1140
1141        let entries = &mut self.entries[..] as *mut _;
1142        let extra_values = &mut self.extra_values as *mut _;
1143        let len = self.entries.len();
1144        unsafe {
1145            self.entries.set_len(0);
1146        }
1147        self.order.clear();
1148        self.order_holes = 0;
1149
1150        Drain {
1151            idx: 0,
1152            len,
1153            entries,
1154            extra_values,
1155            next: None,
1156            lt: PhantomData,
1157        }
1158    }
1159
1160    fn value_iter(&self, idx: Option<usize>) -> ValueIter<'_, T> {
1161        use self::Cursor::*;
1162
1163        if let Some(idx) = idx {
1164            let back = {
1165                let entry = &self.entries[idx];
1166
1167                entry.links.map(|l| Values(l.tail)).unwrap_or(Head)
1168            };
1169
1170            ValueIter {
1171                map: self,
1172                index: idx,
1173                front: Some(Head),
1174                back: Some(back),
1175            }
1176        } else {
1177            ValueIter {
1178                map: self,
1179                index: usize::MAX,
1180                front: None,
1181                back: None,
1182            }
1183        }
1184    }
1185
1186    fn value_iter_mut(&mut self, idx: usize) -> ValueIterMut<'_, T> {
1187        use self::Cursor::*;
1188
1189        let back = {
1190            let entry = &self.entries[idx];
1191
1192            entry.links.map(|l| Values(l.tail)).unwrap_or(Head)
1193        };
1194
1195        ValueIterMut {
1196            entries: self.entries.as_mut_ptr(),
1197            extra_values: self.extra_values.as_mut_ptr(),
1198            index: idx,
1199            front: Some(Head),
1200            back: Some(back),
1201            lt: PhantomData,
1202        }
1203    }
1204
1205    /// Gets the given key's corresponding entry in the map for in-place
1206    /// manipulation.
1207    ///
1208    /// # Panics
1209    ///
1210    /// This method panics if capacity exceeds max `HeaderMap` capacity
1211    ///
1212    /// # Examples
1213    ///
1214    /// ```
1215    /// # use rama_http_types::HeaderMap;
1216    /// let mut map: HeaderMap<u32> = HeaderMap::default();
1217    ///
1218    /// let headers = &[
1219    ///     "content-length",
1220    ///     "x-hello",
1221    ///     "Content-Length",
1222    ///     "x-world",
1223    /// ];
1224    ///
1225    /// for &header in headers {
1226    ///     let counter = map.entry(header).or_insert(0);
1227    ///     *counter += 1;
1228    /// }
1229    ///
1230    /// assert_eq!(map["content-length"], 2);
1231    /// assert_eq!(map["x-hello"], 1);
1232    /// ```
1233    pub fn entry<K>(&mut self, key: K) -> Entry<'_, T>
1234    where
1235        K: IntoHeaderName,
1236    {
1237        key.try_entry(self).expect("size overflows MAX_SIZE")
1238    }
1239
1240    /// Gets the given key's corresponding entry in the map for in-place
1241    /// manipulation.
1242    ///
1243    /// # Errors
1244    ///
1245    /// This method differs from `entry` by allowing types that may not be
1246    /// valid `HeaderName`s to passed as the key (such as `String`). If they
1247    /// do not parse as a valid `HeaderName`, this returns an
1248    /// `InvalidHeaderName` error.
1249    ///
1250    /// If reserving space goes over the maximum, this will also return an
1251    /// error. However, to prevent breaking changes to the return type, the
1252    /// error will still say `InvalidHeaderName`, unlike other `try_*` methods
1253    /// which return a `MaxSizeReached` error.
1254    pub fn try_entry<K>(&mut self, key: K) -> Result<Entry<'_, T>, InvalidHeaderName>
1255    where
1256        K: AsHeaderName,
1257    {
1258        key.try_entry(self).map_err(|err| match err {
1259            as_header_name::TryEntryError::InvalidHeaderName(e) => e,
1260            as_header_name::TryEntryError::MaxSizeReached(_e) => {
1261                // Unfortunately, we cannot change the return type of this
1262                // method, so the max size reached error needs to be converted
1263                // into an InvalidHeaderName. Yay.
1264                InvalidHeaderName::new()
1265            }
1266        })
1267    }
1268
1269    fn try_entry2<K>(&mut self, key: K) -> Result<Entry<'_, T>, MaxSizeReached>
1270    where
1271        K: Into<HeaderName>,
1272    {
1273        // Ensure that there is space in the map
1274        self.try_reserve_one()?;
1275        let key = key.into();
1276
1277        Ok(insert_phase_one!(
1278            self,
1279            key,
1280            probe,
1281            pos,
1282            hash,
1283            danger,
1284            Entry::Vacant(VacantEntry {
1285                map: self,
1286                hash,
1287                key: key.into(),
1288                probe,
1289                danger,
1290            }),
1291            Entry::Occupied(OccupiedEntry {
1292                map: self,
1293                index: pos,
1294                probe,
1295            }),
1296            Entry::Vacant(VacantEntry {
1297                map: self,
1298                hash,
1299                key: key.into(),
1300                probe,
1301                danger,
1302            })
1303        ))
1304    }
1305
1306    /// Inserts a key-value pair into the map.
1307    ///
1308    /// If the map did not previously have this key present, then `None` is
1309    /// returned.
1310    ///
1311    /// If the map did have this key present, the new value is associated with
1312    /// the key and all previous values are removed. **Note** that only a single
1313    /// one of the previous values is returned. If there are multiple values
1314    /// that have been previously associated with the key, then the first one is
1315    /// returned. See `insert_mult` on `OccupiedEntry` for an API that returns
1316    /// all values.
1317    ///
1318    /// The key is not updated, though; this matters for types that can be `==`
1319    /// without being identical.
1320    ///
1321    /// # Panics
1322    ///
1323    /// This method panics if capacity exceeds max `HeaderMap` capacity
1324    ///
1325    /// # Examples
1326    ///
1327    /// ```
1328    /// # use rama_http_types::HeaderMap;
1329    /// # use rama_http_types::header::HOST;
1330    /// let mut map = HeaderMap::new();
1331    /// assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
1332    /// assert!(!map.is_empty());
1333    ///
1334    /// let mut prev = map.insert(HOST, "earth".parse().unwrap()).unwrap();
1335    /// assert_eq!("world", prev);
1336    /// ```
1337    pub fn insert<K>(&mut self, key: K, val: T) -> Option<T>
1338    where
1339        K: IntoHeaderName,
1340    {
1341        self.try_insert(key, val).expect("size overflows MAX_SIZE")
1342    }
1343
1344    /// Inserts a key-value pair into the map.
1345    ///
1346    /// If the map did not previously have this key present, then `None` is
1347    /// returned.
1348    ///
1349    /// If the map did have this key present, the new value is associated with
1350    /// the key and all previous values are removed. **Note** that only a single
1351    /// one of the previous values is returned. If there are multiple values
1352    /// that have been previously associated with the key, then the first one is
1353    /// returned. See `insert_mult` on `OccupiedEntry` for an API that returns
1354    /// all values.
1355    ///
1356    /// The key is not updated, though; this matters for types that can be `==`
1357    /// without being identical.
1358    ///
1359    /// # Errors
1360    ///
1361    /// This function may return an error if `HeaderMap` exceeds max capacity
1362    ///
1363    /// # Examples
1364    ///
1365    /// ```
1366    /// # use rama_http_types::HeaderMap;
1367    /// # use rama_http_types::header::HOST;
1368    /// let mut map = HeaderMap::new();
1369    /// assert!(map.try_insert(HOST, "world".parse().unwrap()).unwrap().is_none());
1370    /// assert!(!map.is_empty());
1371    ///
1372    /// let mut prev = map.try_insert(HOST, "earth".parse().unwrap()).unwrap().unwrap();
1373    /// assert_eq!("world", prev);
1374    /// ```
1375    pub fn try_insert<K>(&mut self, key: K, val: T) -> Result<Option<T>, MaxSizeReached>
1376    where
1377        K: IntoHeaderName,
1378    {
1379        key.try_insert(self, val)
1380    }
1381
1382    #[inline]
1383    fn try_insert2<K>(&mut self, key: K, value: T) -> Result<Option<T>, MaxSizeReached>
1384    where
1385        K: Into<HeaderName>,
1386    {
1387        self.try_reserve_one()?;
1388        let key = key.into();
1389
1390        Ok(insert_phase_one!(
1391            self,
1392            key,
1393            probe,
1394            pos,
1395            hash,
1396            danger,
1397            // Vacant
1398            {
1399                let _ = danger; // Make lint happy
1400                let index = self.entries.len();
1401                self.try_insert_entry(hash, key.into(), value)?;
1402                self.indices[probe] = Pos::new(index, hash);
1403                None
1404            },
1405            // Occupied
1406            Some(self.insert_occupied(pos, key, value)),
1407            // Robinhood
1408            {
1409                self.try_insert_phase_two(key.into(), value, hash, probe, danger)?;
1410                None
1411            }
1412        ))
1413    }
1414
1415    /// Set an occupied bucket to the given value
1416    #[inline]
1417    fn insert_occupied(&mut self, index: usize, key: HeaderName, value: T) -> T {
1418        let old = self.insert_occupied_same_key(index, value);
1419        self.entries[index].key = key;
1420        old
1421    }
1422
1423    /// Set an occupied bucket to the given value without changing its key.
1424    #[inline]
1425    fn insert_occupied_same_key(&mut self, index: usize, value: T) -> T {
1426        if let Some(links) = self.entries[index].links {
1427            self.remove_all_extra_values(links.next);
1428        }
1429
1430        let entry = &mut self.entries[index];
1431        mem::replace(&mut entry.value, value)
1432    }
1433
1434    fn insert_occupied_mult(
1435        &mut self,
1436        index: usize,
1437        key: HeaderName,
1438        value: T,
1439    ) -> ValueDrain<'_, T> {
1440        self.insert_occupied_mult_inner(index, Some(key), value)
1441    }
1442
1443    fn insert_occupied_mult_same_key(&mut self, index: usize, value: T) -> ValueDrain<'_, T> {
1444        self.insert_occupied_mult_inner(index, None, value)
1445    }
1446
1447    fn insert_occupied_mult_inner(
1448        &mut self,
1449        index: usize,
1450        key: Option<HeaderName>,
1451        value: T,
1452    ) -> ValueDrain<'_, T> {
1453        let old;
1454        let links;
1455
1456        {
1457            let entry = &mut self.entries[index];
1458
1459            old = mem::replace(&mut entry.value, value);
1460            if let Some(key) = key {
1461                entry.key = key;
1462            }
1463            links = entry.links;
1464        }
1465
1466        let next = links.map(|l| self.drain_all_extra_values(l.next).into_iter());
1467
1468        ValueDrain {
1469            first: Some(old),
1470            next,
1471            lt: PhantomData,
1472        }
1473    }
1474
1475    /// Inserts a key-value pair into the map.
1476    ///
1477    /// If the map did not previously have this key present, then `false` is
1478    /// returned.
1479    ///
1480    /// If the map did have this key present, the new value is pushed to the end
1481    /// of the list of values currently associated with the key. The key is not
1482    /// updated, though; this matters for types that can be `==` without being
1483    /// identical.
1484    ///
1485    /// # Panics
1486    ///
1487    /// This method panics if capacity exceeds max `HeaderMap` capacity
1488    ///
1489    /// # Examples
1490    ///
1491    /// ```
1492    /// # use rama_http_types::HeaderMap;
1493    /// # use rama_http_types::header::HOST;
1494    /// let mut map = HeaderMap::new();
1495    /// assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
1496    /// assert!(!map.is_empty());
1497    ///
1498    /// map.append(HOST, "earth".parse().unwrap());
1499    ///
1500    /// let values = map.get_all("host");
1501    /// let mut i = values.iter();
1502    /// assert_eq!("world", *i.next().unwrap());
1503    /// assert_eq!("earth", *i.next().unwrap());
1504    /// ```
1505    pub fn append<K>(&mut self, key: K, value: T) -> bool
1506    where
1507        K: IntoHeaderName,
1508    {
1509        self.try_append(key, value)
1510            .expect("size overflows MAX_SIZE")
1511    }
1512
1513    /// Inserts a key-value pair into the map.
1514    ///
1515    /// If the map did not previously have this key present, then `false` is
1516    /// returned.
1517    ///
1518    /// If the map did have this key present, the new value is pushed to the end
1519    /// of the list of values currently associated with the key. The key is not
1520    /// updated, though; this matters for types that can be `==` without being
1521    /// identical.
1522    ///
1523    /// # Errors
1524    ///
1525    /// This function may return an error if `HeaderMap` exceeds max capacity
1526    ///
1527    /// # Examples
1528    ///
1529    /// ```
1530    /// # use rama_http_types::HeaderMap;
1531    /// # use rama_http_types::header::HOST;
1532    /// let mut map = HeaderMap::new();
1533    /// assert!(map.try_insert(HOST, "world".parse().unwrap()).unwrap().is_none());
1534    /// assert!(!map.is_empty());
1535    ///
1536    /// map.try_append(HOST, "earth".parse().unwrap()).unwrap();
1537    ///
1538    /// let values = map.get_all("host");
1539    /// let mut i = values.iter();
1540    /// assert_eq!("world", *i.next().unwrap());
1541    /// assert_eq!("earth", *i.next().unwrap());
1542    /// ```
1543    pub fn try_append<K>(&mut self, key: K, value: T) -> Result<bool, MaxSizeReached>
1544    where
1545        K: IntoHeaderName,
1546    {
1547        key.try_append(self, value)
1548    }
1549
1550    #[inline]
1551    fn try_append2<K>(&mut self, key: K, value: T) -> Result<bool, MaxSizeReached>
1552    where
1553        K: Into<HeaderName>,
1554    {
1555        self.try_reserve_one()?;
1556        let key = key.into();
1557
1558        Ok(insert_phase_one!(
1559            self,
1560            key,
1561            probe,
1562            pos,
1563            hash,
1564            danger,
1565            // Vacant
1566            {
1567                let _ = danger;
1568                let index = self.entries.len();
1569                self.try_insert_entry(hash, key.into(), value)?;
1570                self.indices[probe] = Pos::new(index, hash);
1571                false
1572            },
1573            // Occupied
1574            {
1575                self.append_value(pos, key, value);
1576                true
1577            },
1578            // Robinhood
1579            {
1580                self.try_insert_phase_two(key.into(), value, hash, probe, danger)?;
1581
1582                false
1583            }
1584        ))
1585    }
1586
1587    #[inline]
1588    fn find<K>(&self, key: &K) -> Option<(usize, usize)>
1589    where
1590        K: Hash + Into<HeaderName> + ?Sized,
1591        HeaderName: PartialEq<K>,
1592    {
1593        if self.entries.is_empty() {
1594            return None;
1595        }
1596
1597        let hash = hash_elem_using(&self.danger, key);
1598        let mask = self.mask;
1599        let mut probe = desired_pos(mask, hash);
1600        let mut dist = 0;
1601
1602        probe_loop!(probe < self.indices.len(), {
1603            if let Some((i, entry_hash)) = self.indices[probe].resolve() {
1604                if dist > probe_distance(mask, entry_hash, probe) {
1605                    // give up when probe distance is too long
1606                    return None;
1607                } else if entry_hash == hash && self.entries[i].key == *key {
1608                    return Some((probe, i));
1609                }
1610            } else {
1611                return None;
1612            }
1613
1614            dist += 1;
1615        });
1616    }
1617
1618    /// phase 2 is post-insert where we forward-shift `Pos` in the indices.
1619    #[inline]
1620    fn try_insert_phase_two(
1621        &mut self,
1622        key: HeaderName,
1623        value: T,
1624        hash: HashValue,
1625        probe: usize,
1626        danger: bool,
1627    ) -> Result<usize, MaxSizeReached> {
1628        // Push the value and get the index
1629        let index = self.entries.len();
1630        self.try_insert_entry(hash, key, value)?;
1631
1632        let num_displaced = do_insert_phase_two(&mut self.indices, probe, Pos::new(index, hash));
1633
1634        if danger || num_displaced >= DISPLACEMENT_THRESHOLD {
1635            // Increase danger level
1636            self.danger.set_yellow();
1637        }
1638
1639        Ok(index)
1640    }
1641
1642    /// Removes a key from the map, returning the value associated with the key.
1643    ///
1644    /// Returns `None` if the map does not contain the key. If there are
1645    /// multiple values associated with the key, then the first one is returned.
1646    /// See `remove_entry_mult` on `OccupiedEntry` for an API that yields all
1647    /// values.
1648    ///
1649    /// # Examples
1650    ///
1651    /// ```
1652    /// # use rama_http_types::HeaderMap;
1653    /// # use rama_http_types::header::HOST;
1654    /// let mut map = HeaderMap::new();
1655    /// map.insert(HOST, "hello.world".parse().unwrap());
1656    ///
1657    /// let prev = map.remove(HOST).unwrap();
1658    /// assert_eq!("hello.world", prev);
1659    ///
1660    /// assert!(map.remove(HOST).is_none());
1661    /// ```
1662    pub fn remove<K>(&mut self, key: K) -> Option<T>
1663    where
1664        K: AsHeaderName,
1665    {
1666        match key.find(self) {
1667            Some((probe, idx)) => {
1668                if let Some(links) = self.entries[idx].links {
1669                    self.remove_all_extra_values(links.next);
1670                }
1671
1672                let entry = self.remove_found(probe, idx);
1673
1674                Some(entry.value)
1675            }
1676            None => None,
1677        }
1678    }
1679
1680    /// Remove an entry from the map.
1681    ///
1682    /// Warning: To avoid inconsistent state, extra values _must_ be removed
1683    /// for the `found` index (via `remove_all_extra_values` or similar)
1684    /// _before_ this method is called.
1685    #[inline]
1686    fn remove_found(&mut self, probe: usize, found: usize) -> Bucket<T> {
1687        self.remove_order(self.entries[found].order);
1688
1689        // index `probe` and entry `found` is to be removed
1690        // use swap_remove, but then we need to update the index that points
1691        // to the other entry that has to move
1692        self.indices[probe] = Pos::none();
1693        let entry = self.entries.swap_remove(found);
1694
1695        // correct index that points to the entry that had to swap places
1696        if found < self.entries.len() {
1697            let order = self.entries[found].order;
1698            self.set_order_link(order, Link::Entry(found));
1699
1700            let entry = &self.entries[found];
1701            // was not last element
1702            // examine new element in `found` and find it in indices
1703            let mut probe = desired_pos(self.mask, entry.hash);
1704
1705            probe_loop!(probe < self.indices.len(), {
1706                if let Some((i, _)) = self.indices[probe].resolve() {
1707                    if i >= self.entries.len() {
1708                        // found it
1709                        self.indices[probe] = Pos::new(found, entry.hash);
1710                        break;
1711                    }
1712                }
1713            });
1714
1715            // Update links
1716            if let Some(links) = entry.links {
1717                self.extra_values[links.next].prev = Link::Entry(found);
1718                self.extra_values[links.tail].next = Link::Entry(found);
1719            }
1720        }
1721
1722        // backward shift deletion in self.indices
1723        // after probe, shift all non-ideally placed indices backward
1724        if !self.entries.is_empty() {
1725            let mut last_probe = probe;
1726            let mut probe = probe + 1;
1727
1728            probe_loop!(probe < self.indices.len(), {
1729                if let Some((_, entry_hash)) = self.indices[probe].resolve() {
1730                    if probe_distance(self.mask, entry_hash, probe) > 0 {
1731                        self.indices[last_probe] = self.indices[probe];
1732                        self.indices[probe] = Pos::none();
1733                    } else {
1734                        break;
1735                    }
1736                } else {
1737                    break;
1738                }
1739
1740                last_probe = probe;
1741            });
1742        }
1743
1744        entry
1745    }
1746
1747    /// Removes the `ExtraValue` at the given index.
1748    #[inline]
1749    fn remove_extra_value(&mut self, idx: usize) -> ExtraValue<T> {
1750        self.remove_order(self.extra_values[idx].order);
1751
1752        let raw_links = self.raw_links();
1753        let extra = remove_extra_value(raw_links, &mut self.extra_values, idx);
1754
1755        if let Some(moved) = self.extra_values.get(idx) {
1756            let order = moved.order;
1757            self.set_order_link(order, Link::Extra(idx));
1758        }
1759
1760        extra
1761    }
1762
1763    fn remove_all_extra_values(&mut self, mut head: usize) {
1764        loop {
1765            let extra = self.remove_extra_value(head);
1766
1767            if let Link::Extra(idx) = extra.next {
1768                head = idx;
1769            } else {
1770                break;
1771            }
1772        }
1773    }
1774
1775    fn drain_all_extra_values(&mut self, mut head: usize) -> Vec<T> {
1776        let mut vec = Vec::new();
1777        loop {
1778            let extra = self.remove_extra_value(head);
1779            vec.push(extra.value);
1780
1781            if let Link::Extra(idx) = extra.next {
1782                head = idx;
1783            } else {
1784                break;
1785            }
1786        }
1787        vec
1788    }
1789
1790    #[inline]
1791    fn try_insert_entry(
1792        &mut self,
1793        hash: HashValue,
1794        key: HeaderName,
1795        value: T,
1796    ) -> Result<(), MaxSizeReached> {
1797        if self.entries.len() >= MAX_SIZE {
1798            return Err(MaxSizeReached::new());
1799        }
1800
1801        let index = self.entries.len();
1802        let order = self.push_order(Link::Entry(index));
1803
1804        self.entries.push(Bucket {
1805            hash,
1806            key,
1807            value,
1808            links: None,
1809            order,
1810        });
1811
1812        Ok(())
1813    }
1814
1815    fn rebuild(&mut self) {
1816        // Loop over all entries and re-insert them into the map
1817        'outer: for (index, entry) in self.entries.iter_mut().enumerate() {
1818            let hash = hash_elem_using(&self.danger, &entry.key);
1819            let mut probe = desired_pos(self.mask, hash);
1820            let mut dist = 0;
1821
1822            // Update the entry's hash code
1823            entry.hash = hash;
1824
1825            probe_loop!(probe < self.indices.len(), {
1826                if let Some((_, entry_hash)) = self.indices[probe].resolve() {
1827                    // if existing element probed less than us, swap
1828                    let their_dist = probe_distance(self.mask, entry_hash, probe);
1829
1830                    if their_dist < dist {
1831                        // Robinhood
1832                        break;
1833                    }
1834                } else {
1835                    // Vacant slot
1836                    self.indices[probe] = Pos::new(index, hash);
1837                    continue 'outer;
1838                }
1839
1840                dist += 1;
1841            });
1842
1843            do_insert_phase_two(&mut self.indices, probe, Pos::new(index, hash));
1844        }
1845    }
1846
1847    fn reinsert_entry_in_order(&mut self, pos: Pos) {
1848        if let Some((_, entry_hash)) = pos.resolve() {
1849            // Find first empty bucket and insert there
1850            let mut probe = desired_pos(self.mask, entry_hash);
1851
1852            probe_loop!(probe < self.indices.len(), {
1853                if self.indices[probe].resolve().is_none() {
1854                    // empty bucket, insert here
1855                    self.indices[probe] = pos;
1856                    return;
1857                }
1858            });
1859        }
1860    }
1861
1862    fn try_reserve_one(&mut self) -> Result<(), MaxSizeReached> {
1863        let len = self.entries.len();
1864
1865        if self.danger.is_yellow() {
1866            // MAX_SIZE (2^15) and LOAD_FACTOR_THRESHOLD is 5, so the product
1867            // cannot overflow.
1868            if self.entries.len() * LOAD_FACTOR_THRESHOLD >= self.indices.len() {
1869                // Transition back to green danger level
1870                self.danger.set_green();
1871
1872                // Double the capacity
1873                let new_cap = self.indices.len() * 2;
1874
1875                // Grow the capacity
1876                self.try_grow(new_cap)?;
1877            } else {
1878                self.danger.set_red();
1879
1880                // Rebuild hash table
1881                for index in self.indices.iter_mut() {
1882                    *index = Pos::none();
1883                }
1884
1885                self.rebuild();
1886            }
1887        } else if len == self.capacity() {
1888            if len == 0 {
1889                let new_raw_cap = 8;
1890                self.mask = 8 - 1;
1891                self.indices = vec![Pos::none(); new_raw_cap].into_boxed_slice();
1892                let cap = usable_capacity(new_raw_cap);
1893                self.entries = Vec::with_capacity(cap);
1894                self.order.reserve_exact(cap);
1895            } else {
1896                let raw_cap = self.indices.len();
1897                self.try_grow(raw_cap << 1)?;
1898            }
1899        }
1900
1901        Ok(())
1902    }
1903
1904    #[inline]
1905    fn try_grow(&mut self, new_raw_cap: usize) -> Result<(), MaxSizeReached> {
1906        if new_raw_cap > MAX_SIZE {
1907            return Err(MaxSizeReached::new());
1908        }
1909
1910        // find first ideally placed element -- start of cluster
1911        let mut first_ideal = 0;
1912
1913        for (i, pos) in self.indices.iter().enumerate() {
1914            if let Some((_, entry_hash)) = pos.resolve() {
1915                if 0 == probe_distance(self.mask, entry_hash, i) {
1916                    first_ideal = i;
1917                    break;
1918                }
1919            }
1920        }
1921
1922        // visit the entries in an order where we can simply reinsert them
1923        // into self.indices without any bucket stealing.
1924        let old_indices = mem::replace(
1925            &mut self.indices,
1926            vec![Pos::none(); new_raw_cap].into_boxed_slice(),
1927        );
1928        self.mask = new_raw_cap.wrapping_sub(1) as Size;
1929
1930        for &pos in &old_indices[first_ideal..] {
1931            self.reinsert_entry_in_order(pos);
1932        }
1933
1934        for &pos in &old_indices[..first_ideal] {
1935            self.reinsert_entry_in_order(pos);
1936        }
1937
1938        // Reserve additional entry slots
1939        let more = self.capacity() - self.entries.len();
1940        self.entries.reserve_exact(more);
1941        self.order.reserve_exact(more);
1942        Ok(())
1943    }
1944
1945    #[inline]
1946    fn raw_links(&mut self) -> RawLinks<T> {
1947        RawLinks(&mut self.entries[..] as *mut _)
1948    }
1949
1950    #[inline]
1951    fn push_order(&mut self, link: Link) -> usize {
1952        let index = self.order.len();
1953        self.order.push(Some(link));
1954        index
1955    }
1956
1957    #[inline]
1958    fn remove_order(&mut self, index: usize) {
1959        if self.order[index].take().is_some() {
1960            self.order_holes += 1;
1961        }
1962    }
1963
1964    #[inline]
1965    fn set_order_link(&mut self, index: usize, link: Link) {
1966        if let Some(slot) = self.order.get_mut(index)
1967            && slot.is_some()
1968        {
1969            *slot = Some(link);
1970        }
1971    }
1972
1973    #[inline]
1974    fn append_value(&mut self, entry_idx: usize, key: HeaderName, value: T) {
1975        let idx = self.extra_values.len();
1976        let order = self.push_order(Link::Extra(idx));
1977        let entry = &mut self.entries[entry_idx];
1978
1979        match entry.links {
1980            Some(links) => {
1981                self.extra_values.push(ExtraValue {
1982                    key,
1983                    value,
1984                    prev: Link::Extra(links.tail),
1985                    next: Link::Entry(entry_idx),
1986                    order,
1987                });
1988
1989                self.extra_values[links.tail].next = Link::Extra(idx);
1990
1991                entry.links = Some(Links { tail: idx, ..links });
1992            }
1993            None => {
1994                self.extra_values.push(ExtraValue {
1995                    key,
1996                    value,
1997                    prev: Link::Entry(entry_idx),
1998                    next: Link::Entry(entry_idx),
1999                    order,
2000                });
2001
2002                entry.links = Some(Links {
2003                    next: idx,
2004                    tail: idx,
2005                });
2006            }
2007        }
2008    }
2009}
2010
2011/// Removes the `ExtraValue` at the given index.
2012#[inline]
2013fn remove_extra_value<T>(
2014    mut raw_links: RawLinks<T>,
2015    extra_values: &mut Vec<ExtraValue<T>>,
2016    idx: usize,
2017) -> ExtraValue<T> {
2018    let prev;
2019    let next;
2020
2021    {
2022        debug_assert!(extra_values.len() > idx);
2023        let extra = &extra_values[idx];
2024        prev = extra.prev;
2025        next = extra.next;
2026    }
2027
2028    // First unlink the extra value
2029    match (prev, next) {
2030        (Link::Entry(prev), Link::Entry(next)) => {
2031            debug_assert_eq!(prev, next);
2032
2033            raw_links[prev] = None;
2034        }
2035        (Link::Entry(prev), Link::Extra(next)) => {
2036            debug_assert!(raw_links[prev].is_some());
2037
2038            raw_links[prev].as_mut().unwrap().next = next;
2039
2040            debug_assert!(extra_values.len() > next);
2041            extra_values[next].prev = Link::Entry(prev);
2042        }
2043        (Link::Extra(prev), Link::Entry(next)) => {
2044            debug_assert!(raw_links[next].is_some());
2045
2046            raw_links[next].as_mut().unwrap().tail = prev;
2047
2048            debug_assert!(extra_values.len() > prev);
2049            extra_values[prev].next = Link::Entry(next);
2050        }
2051        (Link::Extra(prev), Link::Extra(next)) => {
2052            debug_assert!(extra_values.len() > next);
2053            debug_assert!(extra_values.len() > prev);
2054
2055            extra_values[prev].next = Link::Extra(next);
2056            extra_values[next].prev = Link::Extra(prev);
2057        }
2058    }
2059
2060    // Remove the extra value
2061    let mut extra = extra_values.swap_remove(idx);
2062
2063    // This is the index of the value that was moved (possibly `extra`)
2064    let old_idx = extra_values.len();
2065
2066    // Update the links
2067    if extra.prev == Link::Extra(old_idx) {
2068        extra.prev = Link::Extra(idx);
2069    }
2070
2071    if extra.next == Link::Extra(old_idx) {
2072        extra.next = Link::Extra(idx);
2073    }
2074
2075    // Check if another entry was displaced. If it was, then the links
2076    // need to be fixed.
2077    if idx != old_idx {
2078        let next;
2079        let prev;
2080
2081        {
2082            debug_assert!(extra_values.len() > idx);
2083            let moved = &extra_values[idx];
2084            next = moved.next;
2085            prev = moved.prev;
2086        }
2087
2088        // An entry was moved, we have to the links
2089        match prev {
2090            Link::Entry(entry_idx) => {
2091                // It is critical that we do not attempt to read the
2092                // header name or value as that memory may have been
2093                // "released" already.
2094                debug_assert!(raw_links[entry_idx].is_some());
2095
2096                let links = raw_links[entry_idx].as_mut().unwrap();
2097                links.next = idx;
2098            }
2099            Link::Extra(extra_idx) => {
2100                debug_assert!(extra_values.len() > extra_idx);
2101                extra_values[extra_idx].next = Link::Extra(idx);
2102            }
2103        }
2104
2105        match next {
2106            Link::Entry(entry_idx) => {
2107                debug_assert!(raw_links[entry_idx].is_some());
2108
2109                let links = raw_links[entry_idx].as_mut().unwrap();
2110                links.tail = idx;
2111            }
2112            Link::Extra(extra_idx) => {
2113                debug_assert!(extra_values.len() > extra_idx);
2114                extra_values[extra_idx].prev = Link::Extra(idx);
2115            }
2116        }
2117    }
2118
2119    debug_assert!({
2120        for v in &*extra_values {
2121            assert!(v.next != Link::Extra(old_idx));
2122            assert!(v.prev != Link::Extra(old_idx));
2123        }
2124
2125        true
2126    });
2127
2128    extra
2129}
2130
2131impl<'a, T> IntoIterator for &'a HeaderMap<T> {
2132    type Item = (&'a HeaderName, &'a T);
2133    type IntoIter = Iter<'a, T>;
2134
2135    fn into_iter(self) -> Iter<'a, T> {
2136        self.iter()
2137    }
2138}
2139
2140impl<'a, T> IntoIterator for &'a mut HeaderMap<T> {
2141    type Item = (&'a HeaderName, &'a mut T);
2142    type IntoIter = IterMut<'a, T>;
2143
2144    fn into_iter(self) -> IterMut<'a, T> {
2145        self.iter_mut()
2146    }
2147}
2148
2149impl<T> IntoIterator for HeaderMap<T> {
2150    type Item = (Option<HeaderName>, T);
2151    type IntoIter = IntoIter<T>;
2152
2153    /// Creates a consuming iterator, that is, one that moves keys and values
2154    /// out of the map in arbitrary order. The map cannot be used after calling
2155    /// this.
2156    ///
2157    /// For each yielded item that has `None` provided for the `HeaderName`,
2158    /// then the associated header name is the same as that of the previously
2159    /// yielded item. The first yielded item will have `HeaderName` set.
2160    ///
2161    /// # Examples
2162    ///
2163    /// Basic usage.
2164    ///
2165    /// ```
2166    /// # use rama_http_types::header;
2167    /// # use rama_http_types::header::*;
2168    /// let mut map = HeaderMap::new();
2169    /// map.insert(header::CONTENT_LENGTH, "123".parse().unwrap());
2170    /// map.insert(header::CONTENT_TYPE, "json".parse().unwrap());
2171    ///
2172    /// let mut iter = map.into_iter();
2173    /// assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap())));
2174    /// assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap())));
2175    /// assert!(iter.next().is_none());
2176    /// ```
2177    ///
2178    /// Multiple values per key.
2179    ///
2180    /// ```
2181    /// # use rama_http_types::header;
2182    /// # use rama_http_types::header::*;
2183    /// let mut map = HeaderMap::new();
2184    ///
2185    /// map.append(header::CONTENT_LENGTH, "123".parse().unwrap());
2186    /// map.append(header::CONTENT_LENGTH, "456".parse().unwrap());
2187    ///
2188    /// map.append(header::CONTENT_TYPE, "json".parse().unwrap());
2189    /// map.append(header::CONTENT_TYPE, "html".parse().unwrap());
2190    /// map.append(header::CONTENT_TYPE, "xml".parse().unwrap());
2191    ///
2192    /// let mut iter = map.into_iter();
2193    ///
2194    /// assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap())));
2195    /// assert_eq!(iter.next(), Some((None, "456".parse().unwrap())));
2196    ///
2197    /// assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap())));
2198    /// assert_eq!(iter.next(), Some((None, "html".parse().unwrap())));
2199    /// assert_eq!(iter.next(), Some((None, "xml".parse().unwrap())));
2200    /// assert!(iter.next().is_none());
2201    /// ```
2202    fn into_iter(self) -> IntoIter<T> {
2203        IntoIter {
2204            next: None,
2205            entries: self.entries.into_iter(),
2206            extra_values: self.extra_values,
2207        }
2208    }
2209}
2210
2211impl<T> HeaderMap<T> {
2212    /// Creates a consuming iterator over entries in their original insertion
2213    /// order.
2214    ///
2215    /// Each yielded `HeaderName` preserves the casing that was used for that
2216    /// field line when it was inserted into the map.
2217    pub fn into_ordered_iter(self) -> IntoOrderedIter<T> {
2218        IntoOrderedIter {
2219            index: 0,
2220            order: self.order,
2221            entries: self.entries,
2222            extra_values: self.extra_values,
2223        }
2224    }
2225}
2226
2227impl<T> FromIterator<(HeaderName, T)> for HeaderMap<T> {
2228    fn from_iter<I>(iter: I) -> Self
2229    where
2230        I: IntoIterator<Item = (HeaderName, T)>,
2231    {
2232        let mut map = HeaderMap::default();
2233        map.extend(iter);
2234        map
2235    }
2236}
2237
2238/// Try to convert a `HashMap` into a `HeaderMap`.
2239///
2240/// # Examples
2241///
2242/// ```
2243/// use std::collections::HashMap;
2244/// use std::convert::TryInto;
2245/// use rama_http_types::HeaderMap;
2246///
2247/// let mut map = HashMap::new();
2248/// map.insert("X-Custom-Header".to_string(), "my value".to_string());
2249///
2250/// let headers: HeaderMap = (&map).try_into().expect("valid headers");
2251/// assert_eq!(headers["X-Custom-Header"], "my value");
2252/// ```
2253impl<'a, K, V, S, T> TryFrom<&'a HashMap<K, V, S>> for HeaderMap<T>
2254where
2255    K: Eq + Hash,
2256    HeaderName: TryFrom<&'a K>,
2257    <HeaderName as TryFrom<&'a K>>::Error: Into<crate::Error>,
2258    T: TryFrom<&'a V>,
2259    T::Error: Into<crate::Error>,
2260{
2261    type Error = Error;
2262
2263    fn try_from(c: &'a HashMap<K, V, S>) -> Result<Self, Self::Error> {
2264        c.iter()
2265            .map(|(k, v)| -> crate::Result<(HeaderName, T)> {
2266                let name = TryFrom::try_from(k).map_err(Into::into)?;
2267                let value = TryFrom::try_from(v).map_err(Into::into)?;
2268                Ok((name, value))
2269            })
2270            .collect()
2271    }
2272}
2273
2274impl<T> Extend<(Option<HeaderName>, T)> for HeaderMap<T> {
2275    /// Extend a `HeaderMap` with the contents of another `HeaderMap`.
2276    ///
2277    /// This function expects the yielded items to follow the same structure as
2278    /// `IntoIter`.
2279    ///
2280    /// # Panics
2281    ///
2282    /// This panics if the first yielded item does not have a `HeaderName`.
2283    ///
2284    /// # Examples
2285    ///
2286    /// ```
2287    /// # use rama_http_types::header::*;
2288    /// let mut map = HeaderMap::new();
2289    ///
2290    /// map.insert(ACCEPT, "text/plain".parse().unwrap());
2291    /// map.insert(HOST, "hello.world".parse().unwrap());
2292    ///
2293    /// let mut extra = HeaderMap::new();
2294    ///
2295    /// extra.insert(HOST, "foo.bar".parse().unwrap());
2296    /// extra.insert(COOKIE, "hello".parse().unwrap());
2297    /// extra.append(COOKIE, "world".parse().unwrap());
2298    ///
2299    /// map.extend(extra);
2300    ///
2301    /// assert_eq!(map["host"], "foo.bar");
2302    /// assert_eq!(map["accept"], "text/plain");
2303    /// assert_eq!(map["cookie"], "hello");
2304    ///
2305    /// let v = map.get_all("host");
2306    /// assert_eq!(1, v.iter().count());
2307    ///
2308    /// let v = map.get_all("cookie");
2309    /// assert_eq!(2, v.iter().count());
2310    /// ```
2311    fn extend<I: IntoIterator<Item = (Option<HeaderName>, T)>>(&mut self, iter: I) {
2312        let mut iter = iter.into_iter();
2313
2314        // Reserve capacity similar to the (HeaderName, T) impl.
2315        // Keys may be already present or show multiple times in the iterator.
2316        // Reserve the entire hint lower bound if the map is empty.
2317        // Otherwise reserve half the hint (rounded up), so the map
2318        // will only resize twice in the worst case.
2319        let hint = if self.is_empty() {
2320            iter.size_hint().0
2321        } else {
2322            (iter.size_hint().0 + 1) / 2
2323        };
2324
2325        // Clamp the hint so an over-estimate cannot overflow `reserve`.
2326        let max_reserve = usable_capacity(MAX_SIZE).saturating_sub(self.entries.len());
2327        let reserve = hint.min(max_reserve);
2328
2329        self.reserve(reserve);
2330
2331        // The structure of this is a bit weird, but it is mostly to make the
2332        // borrow checker happy.
2333        let (mut key, mut val) = match iter.next() {
2334            Some((Some(key), val)) => (key, val),
2335            Some((None, _)) => panic!("expected a header name, but got None"),
2336            None => return,
2337        };
2338
2339        'outer: loop {
2340            let mut entry = match self.try_entry2(key).expect("size overflows MAX_SIZE") {
2341                Entry::Occupied(mut e) => {
2342                    // Replace all previous values while maintaining a handle to
2343                    // the entry.
2344                    e.insert(val);
2345                    e
2346                }
2347                Entry::Vacant(e) => e.insert_entry(val),
2348            };
2349
2350            // As long as `HeaderName` is none, keep inserting the value into
2351            // the current entry
2352            loop {
2353                match iter.next() {
2354                    Some((Some(k), v)) => {
2355                        key = k;
2356                        val = v;
2357                        continue 'outer;
2358                    }
2359                    Some((None, v)) => {
2360                        entry.append(v);
2361                    }
2362                    None => {
2363                        return;
2364                    }
2365                }
2366            }
2367        }
2368    }
2369}
2370
2371impl<T> Extend<(HeaderName, T)> for HeaderMap<T> {
2372    fn extend<I: IntoIterator<Item = (HeaderName, T)>>(&mut self, iter: I) {
2373        // Keys may be already present or show multiple times in the iterator.
2374        // Reserve the entire hint lower bound if the map is empty.
2375        // Otherwise reserve half the hint (rounded up), so the map
2376        // will only resize twice in the worst case.
2377        let iter = iter.into_iter();
2378
2379        let hint = if self.is_empty() {
2380            iter.size_hint().0
2381        } else {
2382            (iter.size_hint().0 + 1) / 2
2383        };
2384
2385        // Clamp the hint so an over-estimate cannot overflow `reserve`.
2386        let max_reserve = usable_capacity(MAX_SIZE).saturating_sub(self.entries.len());
2387        let reserve = hint.min(max_reserve);
2388
2389        self.reserve(reserve);
2390
2391        for (k, v) in iter {
2392            self.append(k, v);
2393        }
2394    }
2395}
2396
2397impl<T: PartialEq> PartialEq for HeaderMap<T> {
2398    fn eq(&self, other: &HeaderMap<T>) -> bool {
2399        if self.len() != other.len() {
2400            return false;
2401        }
2402
2403        self.keys()
2404            .all(|key| self.get_all(key) == other.get_all(key))
2405    }
2406}
2407
2408impl<T: Eq> Eq for HeaderMap<T> {}
2409
2410impl<T: fmt::Debug> fmt::Debug for HeaderMap<T> {
2411    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2412        f.debug_map().entries(self.iter()).finish()
2413    }
2414}
2415
2416impl serde::Serialize for HeaderMap<HeaderValue> {
2417    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2418    where
2419        S: serde::Serializer,
2420    {
2421        let headers: Result<Vec<_>, _> = self
2422            .ordered_iter()
2423            .map(|(name, value)| {
2424                let value = value.to_str().map_err(serde::ser::Error::custom)?;
2425                Ok::<_, S::Error>((name, value.to_owned()))
2426            })
2427            .collect();
2428        headers?.serialize(serializer)
2429    }
2430}
2431
2432impl<'de> serde::Deserialize<'de> for HeaderMap<HeaderValue> {
2433    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2434    where
2435        D: serde::Deserializer<'de>,
2436    {
2437        let headers = <Vec<(HeaderName, std::borrow::Cow<'de, str>)>>::deserialize(deserializer)?;
2438        headers
2439            .into_iter()
2440            .map(|(name, value)| {
2441                Ok::<_, D::Error>((
2442                    name,
2443                    HeaderValue::from_str(&value).map_err(serde::de::Error::custom)?,
2444                ))
2445            })
2446            .collect()
2447    }
2448}
2449
2450impl<K, T> ops::Index<K> for HeaderMap<T>
2451where
2452    K: AsHeaderName,
2453{
2454    type Output = T;
2455
2456    /// # Panics
2457    /// Using the index operator will cause a panic if the header you're querying isn't set.
2458    #[inline]
2459    fn index(&self, index: K) -> &T {
2460        match self.get2(&index) {
2461            Some(val) => val,
2462            None => panic!("no entry found for key {:?}", index.as_str()),
2463        }
2464    }
2465}
2466
2467/// phase 2 is post-insert where we forward-shift `Pos` in the indices.
2468///
2469/// returns the number of displaced elements
2470#[inline]
2471fn do_insert_phase_two(indices: &mut [Pos], mut probe: usize, mut old_pos: Pos) -> usize {
2472    let mut num_displaced = 0;
2473
2474    probe_loop!(probe < indices.len(), {
2475        let pos = &mut indices[probe];
2476
2477        if pos.is_none() {
2478            *pos = old_pos;
2479            break;
2480        } else {
2481            num_displaced += 1;
2482            old_pos = mem::replace(pos, old_pos);
2483        }
2484    });
2485
2486    num_displaced
2487}
2488
2489// ===== impl Iter =====
2490
2491impl<'a, T> Iterator for Iter<'a, T> {
2492    type Item = (&'a HeaderName, &'a T);
2493
2494    fn next(&mut self) -> Option<Self::Item> {
2495        use self::Cursor::*;
2496
2497        if self.cursor.is_none() {
2498            if (self.entry + 1) >= self.map.entries.len() {
2499                return None;
2500            }
2501
2502            self.entry += 1;
2503            self.cursor = Some(Cursor::Head);
2504        }
2505
2506        let entry = &self.map.entries[self.entry];
2507
2508        match self.cursor.unwrap() {
2509            Head => {
2510                self.cursor = entry.links.map(|l| Values(l.next));
2511                Some((&entry.key, &entry.value))
2512            }
2513            Values(idx) => {
2514                let extra = &self.map.extra_values[idx];
2515
2516                match extra.next {
2517                    Link::Entry(_) => self.cursor = None,
2518                    Link::Extra(i) => self.cursor = Some(Values(i)),
2519                }
2520
2521                Some((&entry.key, &extra.value))
2522            }
2523        }
2524    }
2525
2526    fn size_hint(&self) -> (usize, Option<usize>) {
2527        let map = self.map;
2528        debug_assert!(map.entries.len() >= self.entry);
2529
2530        let lower = map.entries.len() - self.entry;
2531        // We could pessimistically guess at the upper bound, saying
2532        // that its lower + map.extra_values.len(). That could be
2533        // way over though, such as if we're near the end, and have
2534        // already gone through several extra values...
2535        (lower, None)
2536    }
2537}
2538
2539impl<'a, T> FusedIterator for Iter<'a, T> {}
2540
2541unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {}
2542unsafe impl<'a, T: Sync> Send for Iter<'a, T> {}
2543
2544// ===== impl OrderedIter =====
2545
2546impl<'a, T> Iterator for OrderedIter<'a, T> {
2547    type Item = (&'a HeaderName, &'a T);
2548
2549    fn next(&mut self) -> Option<Self::Item> {
2550        while self.index < self.map.order.len() {
2551            let link = self.map.order[self.index];
2552            self.index += 1;
2553
2554            if let Some(link) = link {
2555                return match link {
2556                    Link::Entry(idx) => {
2557                        let entry = &self.map.entries[idx];
2558                        Some((&entry.key, &entry.value))
2559                    }
2560                    Link::Extra(idx) => {
2561                        let extra = &self.map.extra_values[idx];
2562                        Some((&extra.key, &extra.value))
2563                    }
2564                };
2565            }
2566        }
2567
2568        None
2569    }
2570
2571    fn size_hint(&self) -> (usize, Option<usize>) {
2572        let remaining = self.map.order[self.index..]
2573            .iter()
2574            .filter(|link| link.is_some())
2575            .count();
2576        (remaining, Some(remaining))
2577    }
2578}
2579
2580impl<'a, T> FusedIterator for OrderedIter<'a, T> {}
2581
2582unsafe impl<'a, T: Sync> Sync for OrderedIter<'a, T> {}
2583unsafe impl<'a, T: Sync> Send for OrderedIter<'a, T> {}
2584
2585// ===== impl IterMut =====
2586
2587impl<'a, T> IterMut<'a, T> {
2588    fn next_unsafe(&mut self) -> Option<(*const HeaderName, *mut T)> {
2589        use self::Cursor::*;
2590
2591        if self.cursor.is_none() {
2592            if (self.entry + 1) >= self.entries_len {
2593                return None;
2594            }
2595
2596            self.entry += 1;
2597            self.cursor = Some(Cursor::Head);
2598        }
2599
2600        // SAFETY: `self.entry < self.entries_len`, and the iterator has
2601        // exclusive access to the underlying map for `'a`, so the `entries`
2602        // allocation remains valid for the lifetime of the iterator.
2603        let entry = unsafe { self.entries.add(self.entry) };
2604
2605        match self.cursor.unwrap() {
2606            Head => {
2607                // SAFETY: `entry` points at a live bucket in `entries`.
2608                self.cursor = unsafe { (*entry).links }.map(|l| Values(l.next));
2609                // SAFETY: `entry` points at a live bucket, and the iterator only
2610                // yields each slot at most once, so materializing these field
2611                // pointers does not alias another yielded `&mut T`.
2612                Some(unsafe {
2613                    (
2614                        ptr::addr_of!((*entry).key),
2615                        ptr::addr_of_mut!((*entry).value),
2616                    )
2617                })
2618            }
2619            Values(idx) => {
2620                // SAFETY: `idx` comes from the `links` chain stored in a live
2621                // bucket / extra value, so it points at a live `extra_values`
2622                // slot for the duration of iteration.
2623                let extra = unsafe { self.extra_values.add(idx) };
2624
2625                // SAFETY: `extra` points at a live extra value.
2626                match unsafe { (*extra).next } {
2627                    Link::Entry(_) => self.cursor = None,
2628                    Link::Extra(i) => self.cursor = Some(Values(i)),
2629                }
2630
2631                // SAFETY: `entry` and `extra` both point at live elements in the
2632                // map backing storage, and the iterator only yields each value
2633                // slot at most once.
2634                Some(unsafe {
2635                    (
2636                        ptr::addr_of!((*entry).key),
2637                        ptr::addr_of_mut!((*extra).value),
2638                    )
2639                })
2640            }
2641        }
2642    }
2643}
2644
2645impl<'a, T> Iterator for IterMut<'a, T> {
2646    type Item = (&'a HeaderName, &'a mut T);
2647
2648    fn next(&mut self) -> Option<Self::Item> {
2649        self.next_unsafe()
2650            .map(|(key, ptr)| (unsafe { &*key }, unsafe { &mut *ptr }))
2651    }
2652
2653    fn size_hint(&self) -> (usize, Option<usize>) {
2654        debug_assert!(self.entries_len >= self.entry);
2655
2656        let lower = self.entries_len - self.entry;
2657        // We could pessimistically guess at the upper bound, saying
2658        // that its lower + map.extra_values.len(). That could be
2659        // way over though, such as if we're near the end, and have
2660        // already gone through several extra values...
2661        (lower, None)
2662    }
2663}
2664
2665impl<'a, T> FusedIterator for IterMut<'a, T> {}
2666
2667unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {}
2668unsafe impl<'a, T: Send> Send for IterMut<'a, T> {}
2669
2670// ===== impl Keys =====
2671
2672impl<'a, T> Iterator for Keys<'a, T> {
2673    type Item = &'a HeaderName;
2674
2675    fn next(&mut self) -> Option<Self::Item> {
2676        self.inner.next().map(|b| &b.key)
2677    }
2678
2679    fn size_hint(&self) -> (usize, Option<usize>) {
2680        self.inner.size_hint()
2681    }
2682
2683    fn nth(&mut self, n: usize) -> Option<Self::Item> {
2684        self.inner.nth(n).map(|b| &b.key)
2685    }
2686
2687    fn count(self) -> usize {
2688        self.inner.count()
2689    }
2690
2691    fn last(self) -> Option<Self::Item> {
2692        self.inner.last().map(|b| &b.key)
2693    }
2694}
2695
2696impl<'a, T> ExactSizeIterator for Keys<'a, T> {}
2697impl<'a, T> FusedIterator for Keys<'a, T> {}
2698
2699// ===== impl Values ====
2700
2701impl<'a, T> Iterator for Values<'a, T> {
2702    type Item = &'a T;
2703
2704    fn next(&mut self) -> Option<Self::Item> {
2705        self.inner.next().map(|(_, v)| v)
2706    }
2707
2708    fn size_hint(&self) -> (usize, Option<usize>) {
2709        self.inner.size_hint()
2710    }
2711}
2712
2713impl<'a, T> FusedIterator for Values<'a, T> {}
2714
2715// ===== impl ValuesMut ====
2716
2717impl<'a, T> Iterator for ValuesMut<'a, T> {
2718    type Item = &'a mut T;
2719
2720    fn next(&mut self) -> Option<Self::Item> {
2721        self.inner.next().map(|(_, v)| v)
2722    }
2723
2724    fn size_hint(&self) -> (usize, Option<usize>) {
2725        self.inner.size_hint()
2726    }
2727}
2728
2729impl<'a, T> FusedIterator for ValuesMut<'a, T> {}
2730
2731// ===== impl Drain =====
2732
2733impl<'a, T> Iterator for Drain<'a, T> {
2734    type Item = (Option<HeaderName>, T);
2735
2736    fn next(&mut self) -> Option<Self::Item> {
2737        if let Some(next) = self.next {
2738            // Remove the extra value
2739
2740            let raw_links = RawLinks(self.entries);
2741            let extra = unsafe { remove_extra_value(raw_links, &mut *self.extra_values, next) };
2742
2743            match extra.next {
2744                Link::Extra(idx) => self.next = Some(idx),
2745                Link::Entry(_) => self.next = None,
2746            }
2747
2748            return Some((None, extra.value));
2749        }
2750
2751        let idx = self.idx;
2752
2753        if idx == self.len {
2754            return None;
2755        }
2756
2757        self.idx += 1;
2758
2759        unsafe {
2760            let entry = &(*self.entries)[idx];
2761
2762            // Read the header name
2763            let key = ptr::read(&entry.key as *const _);
2764            let value = ptr::read(&entry.value as *const _);
2765            self.next = entry.links.map(|l| l.next);
2766
2767            Some((Some(key), value))
2768        }
2769    }
2770
2771    fn size_hint(&self) -> (usize, Option<usize>) {
2772        // At least this many names... It's unknown if the user wants
2773        // to count the extra_values on top.
2774        //
2775        // For instance, extending a new `HeaderMap` wouldn't need to
2776        // reserve the upper-bound in `entries`, only the lower-bound.
2777        let lower = self.len - self.idx;
2778        let upper = unsafe { (*self.extra_values).len() } + lower;
2779        (lower, Some(upper))
2780    }
2781}
2782
2783impl<'a, T> FusedIterator for Drain<'a, T> {}
2784
2785impl<'a, T> Drop for Drain<'a, T> {
2786    fn drop(&mut self) {
2787        struct Guard<'a, 'b, T>(&'a mut Drain<'b, T>);
2788        struct ExtraValuesGuard<T>(*mut Vec<ExtraValue<T>>);
2789
2790        impl<T> Drop for ExtraValuesGuard<T> {
2791            fn drop(&mut self) {
2792                unsafe {
2793                    (*self.0).set_len(0);
2794                }
2795            }
2796        }
2797
2798        impl<'a, 'b, T> Drop for Guard<'a, 'b, T> {
2799            fn drop(&mut self) {
2800                let _extra_values_guard = ExtraValuesGuard(self.0.extra_values);
2801
2802                for _ in self.0.by_ref() {}
2803            }
2804        }
2805
2806        let guard = Guard(self);
2807
2808        for _ in guard.0.by_ref() {}
2809    }
2810}
2811
2812unsafe impl<'a, T: Sync> Sync for Drain<'a, T> {}
2813unsafe impl<'a, T: Send> Send for Drain<'a, T> {}
2814
2815// ===== impl Entry =====
2816
2817impl<'a, T> Entry<'a, T> {
2818    /// Ensures a value is in the entry by inserting the default if empty.
2819    ///
2820    /// Returns a mutable reference to the **first** value in the entry.
2821    ///
2822    /// # Panics
2823    ///
2824    /// This method panics if capacity exceeds max `HeaderMap` capacity
2825    ///
2826    /// # Examples
2827    ///
2828    /// ```
2829    /// # use rama_http_types::HeaderMap;
2830    /// let mut map: HeaderMap<u32> = HeaderMap::default();
2831    ///
2832    /// let headers = &[
2833    ///     "content-length",
2834    ///     "x-hello",
2835    ///     "Content-Length",
2836    ///     "x-world",
2837    /// ];
2838    ///
2839    /// for &header in headers {
2840    ///     let counter = map.entry(header)
2841    ///         .or_insert(0);
2842    ///     *counter += 1;
2843    /// }
2844    ///
2845    /// assert_eq!(map["content-length"], 2);
2846    /// assert_eq!(map["x-hello"], 1);
2847    /// ```
2848    pub fn or_insert(self, default: T) -> &'a mut T {
2849        self.or_try_insert(default)
2850            .expect("size overflows MAX_SIZE")
2851    }
2852
2853    /// Ensures a value is in the entry by inserting the default if empty.
2854    ///
2855    /// Returns a mutable reference to the **first** value in the entry.
2856    ///
2857    /// # Errors
2858    ///
2859    /// This function may return an error if `HeaderMap` exceeds max capacity
2860    ///
2861    /// # Examples
2862    ///
2863    /// ```
2864    /// # use rama_http_types::HeaderMap;
2865    /// let mut map: HeaderMap<u32> = HeaderMap::default();
2866    ///
2867    /// let headers = &[
2868    ///     "content-length",
2869    ///     "x-hello",
2870    ///     "Content-Length",
2871    ///     "x-world",
2872    /// ];
2873    ///
2874    /// for &header in headers {
2875    ///     let counter = map.entry(header)
2876    ///         .or_try_insert(0)
2877    ///         .unwrap();
2878    ///     *counter += 1;
2879    /// }
2880    ///
2881    /// assert_eq!(map["content-length"], 2);
2882    /// assert_eq!(map["x-hello"], 1);
2883    /// ```
2884    pub fn or_try_insert(self, default: T) -> Result<&'a mut T, MaxSizeReached> {
2885        use self::Entry::*;
2886
2887        match self {
2888            Occupied(e) => Ok(e.into_mut()),
2889            Vacant(e) => e.try_insert(default),
2890        }
2891    }
2892
2893    /// Ensures a value is in the entry by inserting the result of the default
2894    /// function if empty.
2895    ///
2896    /// The default function is not called if the entry exists in the map.
2897    /// Returns a mutable reference to the **first** value in the entry.
2898    ///
2899    /// # Examples
2900    ///
2901    /// Basic usage.
2902    ///
2903    /// ```
2904    /// # use rama_http_types::HeaderMap;
2905    /// let mut map = HeaderMap::new();
2906    ///
2907    /// let res = map.entry("x-hello")
2908    ///     .or_insert_with(|| "world".parse().unwrap());
2909    ///
2910    /// assert_eq!(res, "world");
2911    /// ```
2912    ///
2913    /// The default function is not called if the entry exists in the map.
2914    ///
2915    /// ```
2916    /// # use rama_http_types::HeaderMap;
2917    /// # use rama_http_types::header::HOST;
2918    /// let mut map = HeaderMap::new();
2919    /// map.try_insert(HOST, "world".parse().unwrap()).unwrap();
2920    ///
2921    /// let res = map.try_entry("host")
2922    ///     .unwrap()
2923    ///     .or_try_insert_with(|| unreachable!())
2924    ///     .unwrap();
2925    ///
2926    ///
2927    /// assert_eq!(res, "world");
2928    /// ```
2929    pub fn or_insert_with<F: FnOnce() -> T>(self, default: F) -> &'a mut T {
2930        self.or_try_insert_with(default)
2931            .expect("size overflows MAX_SIZE")
2932    }
2933
2934    /// Ensures a value is in the entry by inserting the result of the default
2935    /// function if empty.
2936    ///
2937    /// The default function is not called if the entry exists in the map.
2938    /// Returns a mutable reference to the **first** value in the entry.
2939    ///
2940    /// # Examples
2941    ///
2942    /// Basic usage.
2943    ///
2944    /// ```
2945    /// # use rama_http_types::HeaderMap;
2946    /// let mut map = HeaderMap::new();
2947    ///
2948    /// let res = map.entry("x-hello")
2949    ///     .or_insert_with(|| "world".parse().unwrap());
2950    ///
2951    /// assert_eq!(res, "world");
2952    /// ```
2953    ///
2954    /// The default function is not called if the entry exists in the map.
2955    ///
2956    /// ```
2957    /// # use rama_http_types::HeaderMap;
2958    /// # use rama_http_types::header::HOST;
2959    /// let mut map = HeaderMap::new();
2960    /// map.try_insert(HOST, "world".parse().unwrap()).unwrap();
2961    ///
2962    /// let res = map.try_entry("host")
2963    ///     .unwrap()
2964    ///     .or_try_insert_with(|| unreachable!())
2965    ///     .unwrap();
2966    ///
2967    ///
2968    /// assert_eq!(res, "world");
2969    /// ```
2970    pub fn or_try_insert_with<F: FnOnce() -> T>(
2971        self,
2972        default: F,
2973    ) -> Result<&'a mut T, MaxSizeReached> {
2974        use self::Entry::*;
2975
2976        match self {
2977            Occupied(e) => Ok(e.into_mut()),
2978            Vacant(e) => e.try_insert(default()),
2979        }
2980    }
2981
2982    /// Returns a reference to the entry's key
2983    ///
2984    /// # Examples
2985    ///
2986    /// ```
2987    /// # use rama_http_types::HeaderMap;
2988    /// let mut map = HeaderMap::new();
2989    ///
2990    /// assert_eq!(map.entry("x-hello").key(), "x-hello");
2991    /// ```
2992    pub fn key(&self) -> &HeaderName {
2993        use self::Entry::*;
2994
2995        match *self {
2996            Vacant(ref e) => e.key(),
2997            Occupied(ref e) => e.key(),
2998        }
2999    }
3000}
3001
3002// ===== impl VacantEntry =====
3003
3004impl<'a, T> VacantEntry<'a, T> {
3005    /// Returns a reference to the entry's key
3006    ///
3007    /// # Examples
3008    ///
3009    /// ```
3010    /// # use rama_http_types::HeaderMap;
3011    /// let mut map = HeaderMap::new();
3012    ///
3013    /// assert_eq!(map.entry("x-hello").key().as_str(), "x-hello");
3014    /// ```
3015    pub fn key(&self) -> &HeaderName {
3016        &self.key
3017    }
3018
3019    /// Take ownership of the key
3020    ///
3021    /// # Examples
3022    ///
3023    /// ```
3024    /// # use rama_http_types::header::{HeaderMap, Entry};
3025    /// let mut map = HeaderMap::new();
3026    ///
3027    /// if let Entry::Vacant(v) = map.entry("x-hello") {
3028    ///     assert_eq!(v.into_key().as_str(), "x-hello");
3029    /// }
3030    /// ```
3031    pub fn into_key(self) -> HeaderName {
3032        self.key
3033    }
3034
3035    /// Insert the value into the entry.
3036    ///
3037    /// The value will be associated with this entry's key. A mutable reference
3038    /// to the inserted value will be returned.
3039    ///
3040    /// # Examples
3041    ///
3042    /// ```
3043    /// # use rama_http_types::header::{HeaderMap, Entry};
3044    /// let mut map = HeaderMap::new();
3045    ///
3046    /// if let Entry::Vacant(v) = map.entry("x-hello") {
3047    ///     v.insert("world".parse().unwrap());
3048    /// }
3049    ///
3050    /// assert_eq!(map["x-hello"], "world");
3051    /// ```
3052    pub fn insert(self, value: T) -> &'a mut T {
3053        self.try_insert(value).expect("size overflows MAX_SIZE")
3054    }
3055
3056    /// Insert the value into the entry.
3057    ///
3058    /// The value will be associated with this entry's key. A mutable reference
3059    /// to the inserted value will be returned.
3060    ///
3061    /// # Examples
3062    ///
3063    /// ```
3064    /// # use rama_http_types::header::{HeaderMap, Entry};
3065    /// let mut map = HeaderMap::new();
3066    ///
3067    /// if let Entry::Vacant(v) = map.entry("x-hello") {
3068    ///     v.insert("world".parse().unwrap());
3069    /// }
3070    ///
3071    /// assert_eq!(map["x-hello"], "world");
3072    /// ```
3073    pub fn try_insert(self, value: T) -> Result<&'a mut T, MaxSizeReached> {
3074        // Ensure that there is space in the map
3075        let index =
3076            self.map
3077                .try_insert_phase_two(self.key, value, self.hash, self.probe, self.danger)?;
3078
3079        Ok(&mut self.map.entries[index].value)
3080    }
3081
3082    /// Insert the value into the entry.
3083    ///
3084    /// The value will be associated with this entry's key. The new
3085    /// `OccupiedEntry` is returned, allowing for further manipulation.
3086    ///
3087    /// # Examples
3088    ///
3089    /// ```
3090    /// # use rama_http_types::header::*;
3091    /// let mut map = HeaderMap::new();
3092    ///
3093    /// if let Entry::Vacant(v) = map.try_entry("x-hello").unwrap() {
3094    ///     let mut e = v.try_insert_entry("world".parse().unwrap()).unwrap();
3095    ///     e.insert("world2".parse().unwrap());
3096    /// }
3097    ///
3098    /// assert_eq!(map["x-hello"], "world2");
3099    /// ```
3100    pub fn insert_entry(self, value: T) -> OccupiedEntry<'a, T> {
3101        self.try_insert_entry(value)
3102            .expect("size overflows MAX_SIZE")
3103    }
3104
3105    /// Insert the value into the entry.
3106    ///
3107    /// The value will be associated with this entry's key. The new
3108    /// `OccupiedEntry` is returned, allowing for further manipulation.
3109    ///
3110    /// # Examples
3111    ///
3112    /// ```
3113    /// # use rama_http_types::header::*;
3114    /// let mut map = HeaderMap::new();
3115    ///
3116    /// if let Entry::Vacant(v) = map.try_entry("x-hello").unwrap() {
3117    ///     let mut e = v.try_insert_entry("world".parse().unwrap()).unwrap();
3118    ///     e.insert("world2".parse().unwrap());
3119    /// }
3120    ///
3121    /// assert_eq!(map["x-hello"], "world2");
3122    /// ```
3123    pub fn try_insert_entry(self, value: T) -> Result<OccupiedEntry<'a, T>, MaxSizeReached> {
3124        // Ensure that there is space in the map
3125        let index =
3126            self.map
3127                .try_insert_phase_two(self.key, value, self.hash, self.probe, self.danger)?;
3128
3129        Ok(OccupiedEntry {
3130            map: self.map,
3131            index,
3132            probe: self.probe,
3133        })
3134    }
3135}
3136
3137// ===== impl GetAll =====
3138
3139impl<'a, T: 'a> GetAll<'a, T> {
3140    /// Returns an iterator visiting all values associated with the entry.
3141    ///
3142    /// Values are iterated in insertion order.
3143    ///
3144    /// # Examples
3145    ///
3146    /// ```
3147    /// # use rama_http_types::HeaderMap;
3148    /// # use rama_http_types::header::HOST;
3149    /// let mut map = HeaderMap::new();
3150    /// map.insert(HOST, "hello.world".parse().unwrap());
3151    /// map.append(HOST, "hello.earth".parse().unwrap());
3152    ///
3153    /// let values = map.get_all("host");
3154    /// let mut iter = values.iter();
3155    /// assert_eq!(&"hello.world", iter.next().unwrap());
3156    /// assert_eq!(&"hello.earth", iter.next().unwrap());
3157    /// assert!(iter.next().is_none());
3158    /// ```
3159    pub fn iter(&self) -> ValueIter<'a, T> {
3160        // This creates a new GetAll struct so that the lifetime
3161        // isn't bound to &self.
3162        GetAll {
3163            map: self.map,
3164            index: self.index,
3165        }
3166        .into_iter()
3167    }
3168}
3169
3170impl<'a, T: PartialEq> PartialEq for GetAll<'a, T> {
3171    fn eq(&self, other: &Self) -> bool {
3172        self.iter().eq(other.iter())
3173    }
3174}
3175
3176impl<'a, T> IntoIterator for GetAll<'a, T> {
3177    type Item = &'a T;
3178    type IntoIter = ValueIter<'a, T>;
3179
3180    fn into_iter(self) -> ValueIter<'a, T> {
3181        self.map.value_iter(self.index)
3182    }
3183}
3184
3185impl<'a, 'b: 'a, T> IntoIterator for &'b GetAll<'a, T> {
3186    type Item = &'a T;
3187    type IntoIter = ValueIter<'a, T>;
3188
3189    fn into_iter(self) -> ValueIter<'a, T> {
3190        self.map.value_iter(self.index)
3191    }
3192}
3193
3194// ===== impl ValueIter =====
3195
3196impl<'a, T: 'a> Iterator for ValueIter<'a, T> {
3197    type Item = &'a T;
3198
3199    fn next(&mut self) -> Option<Self::Item> {
3200        use self::Cursor::*;
3201
3202        match self.front {
3203            Some(Head) => {
3204                let entry = &self.map.entries[self.index];
3205
3206                if self.back == Some(Head) {
3207                    self.front = None;
3208                    self.back = None;
3209                } else {
3210                    // Update the iterator state
3211                    match entry.links {
3212                        Some(links) => {
3213                            self.front = Some(Values(links.next));
3214                        }
3215                        None => unreachable!(),
3216                    }
3217                }
3218
3219                Some(&entry.value)
3220            }
3221            Some(Values(idx)) => {
3222                let extra = &self.map.extra_values[idx];
3223
3224                if self.front == self.back {
3225                    self.front = None;
3226                    self.back = None;
3227                } else {
3228                    match extra.next {
3229                        Link::Entry(_) => self.front = None,
3230                        Link::Extra(i) => self.front = Some(Values(i)),
3231                    }
3232                }
3233
3234                Some(&extra.value)
3235            }
3236            None => None,
3237        }
3238    }
3239
3240    fn size_hint(&self) -> (usize, Option<usize>) {
3241        match (self.front, self.back) {
3242            // Exactly 1 value...
3243            (Some(Cursor::Head), Some(Cursor::Head)) => (1, Some(1)),
3244            // At least 1...
3245            (Some(_), _) => (1, None),
3246            // No more values...
3247            (None, _) => (0, Some(0)),
3248        }
3249    }
3250}
3251
3252impl<'a, T: 'a> DoubleEndedIterator for ValueIter<'a, T> {
3253    fn next_back(&mut self) -> Option<Self::Item> {
3254        use self::Cursor::*;
3255
3256        match self.back {
3257            Some(Head) => {
3258                self.front = None;
3259                self.back = None;
3260                Some(&self.map.entries[self.index].value)
3261            }
3262            Some(Values(idx)) => {
3263                let extra = &self.map.extra_values[idx];
3264
3265                if self.front == self.back {
3266                    self.front = None;
3267                    self.back = None;
3268                } else {
3269                    match extra.prev {
3270                        Link::Entry(_) => self.back = Some(Head),
3271                        Link::Extra(idx) => self.back = Some(Values(idx)),
3272                    }
3273                }
3274
3275                Some(&extra.value)
3276            }
3277            None => None,
3278        }
3279    }
3280}
3281
3282impl<'a, T> FusedIterator for ValueIter<'a, T> {}
3283
3284// ===== impl ValueIterMut =====
3285
3286impl<'a, T: 'a> Iterator for ValueIterMut<'a, T> {
3287    type Item = &'a mut T;
3288
3289    fn next(&mut self) -> Option<Self::Item> {
3290        use self::Cursor::*;
3291
3292        // SAFETY: `self.index` was created from a live occupied entry and stays
3293        // fixed for the lifetime of this iterator.
3294        let entry = unsafe { self.entries.add(self.index) };
3295
3296        match self.front {
3297            Some(Head) => {
3298                if self.back == Some(Head) {
3299                    self.front = None;
3300                    self.back = None;
3301                } else {
3302                    // Update the iterator state
3303                    // SAFETY: `entry` points at a live bucket in `entries`.
3304                    match unsafe { (*entry).links } {
3305                        Some(links) => {
3306                            self.front = Some(Values(links.next));
3307                        }
3308                        None => unreachable!(),
3309                    }
3310                }
3311
3312                // SAFETY: `entry` points at a live bucket, and `front`/`back`
3313                // ensure this value slot is yielded at most once.
3314                Some(unsafe { &mut *ptr::addr_of_mut!((*entry).value) })
3315            }
3316            Some(Values(idx)) => {
3317                // SAFETY: `idx` comes from the live linked list rooted at
3318                // `self.index`, so it refers to a live extra value slot.
3319                let extra = unsafe { self.extra_values.add(idx) };
3320
3321                if self.front == self.back {
3322                    self.front = None;
3323                    self.back = None;
3324                } else {
3325                    // SAFETY: `extra` points at a live extra value.
3326                    match unsafe { (*extra).next } {
3327                        Link::Entry(_) => self.front = None,
3328                        Link::Extra(i) => self.front = Some(Values(i)),
3329                    }
3330                }
3331
3332                // SAFETY: `extra` points at a live extra value, and
3333                // `front`/`back` ensure this value slot is yielded at most once.
3334                Some(unsafe { &mut *ptr::addr_of_mut!((*extra).value) })
3335            }
3336            None => None,
3337        }
3338    }
3339}
3340
3341impl<'a, T: 'a> DoubleEndedIterator for ValueIterMut<'a, T> {
3342    fn next_back(&mut self) -> Option<Self::Item> {
3343        use self::Cursor::*;
3344
3345        // SAFETY: `self.index` was created from a live occupied entry and stays
3346        // fixed for the lifetime of this iterator.
3347        let entry = unsafe { self.entries.add(self.index) };
3348
3349        match self.back {
3350            Some(Head) => {
3351                self.front = None;
3352                self.back = None;
3353                // SAFETY: `entry` points at a live bucket, and `front`/`back`
3354                // ensure this value slot is yielded at most once.
3355                Some(unsafe { &mut *ptr::addr_of_mut!((*entry).value) })
3356            }
3357            Some(Values(idx)) => {
3358                // SAFETY: `idx` comes from the live linked list rooted at
3359                // `self.index`, so it refers to a live extra value slot.
3360                let extra = unsafe { self.extra_values.add(idx) };
3361
3362                if self.front == self.back {
3363                    self.front = None;
3364                    self.back = None;
3365                } else {
3366                    // SAFETY: `extra` points at a live extra value.
3367                    match unsafe { (*extra).prev } {
3368                        Link::Entry(_) => self.back = Some(Head),
3369                        Link::Extra(idx) => self.back = Some(Values(idx)),
3370                    }
3371                }
3372
3373                // SAFETY: `extra` points at a live extra value, and
3374                // `front`/`back` ensure this value slot is yielded at most once.
3375                Some(unsafe { &mut *ptr::addr_of_mut!((*extra).value) })
3376            }
3377            None => None,
3378        }
3379    }
3380}
3381
3382impl<'a, T> FusedIterator for ValueIterMut<'a, T> {}
3383
3384unsafe impl<'a, T: Sync> Sync for ValueIterMut<'a, T> {}
3385unsafe impl<'a, T: Send> Send for ValueIterMut<'a, T> {}
3386
3387// ===== impl IntoIter =====
3388
3389impl<T> Iterator for IntoIter<T> {
3390    type Item = (Option<HeaderName>, T);
3391
3392    fn next(&mut self) -> Option<Self::Item> {
3393        if let Some(next) = self.next {
3394            self.next = match self.extra_values[next].next {
3395                Link::Entry(_) => None,
3396                Link::Extra(v) => Some(v),
3397            };
3398
3399            let key = ptr::addr_of_mut!(self.extra_values[next].key);
3400            let value = unsafe {
3401                ptr::drop_in_place(key);
3402                ptr::read(&self.extra_values[next].value)
3403            };
3404
3405            return Some((None, value));
3406        }
3407
3408        if let Some(bucket) = self.entries.next() {
3409            self.next = bucket.links.map(|l| l.next);
3410            let name = Some(bucket.key);
3411            let value = bucket.value;
3412
3413            return Some((name, value));
3414        }
3415
3416        None
3417    }
3418
3419    fn size_hint(&self) -> (usize, Option<usize>) {
3420        let (lower, _) = self.entries.size_hint();
3421        // There could be more than just the entries upper, as there
3422        // could be items in the `extra_values`. We could guess, saying
3423        // `upper + extra_values.len()`, but that could overestimate by a lot.
3424        (lower, None)
3425    }
3426}
3427
3428impl<T> FusedIterator for IntoIter<T> {}
3429
3430impl<T> Drop for IntoIter<T> {
3431    fn drop(&mut self) {
3432        struct Guard<'a, T>(&'a mut IntoIter<T>);
3433
3434        impl<'a, T> Drop for Guard<'a, T> {
3435            fn drop(&mut self) {
3436                unsafe {
3437                    self.0.extra_values.set_len(0);
3438                }
3439            }
3440        }
3441
3442        let guard = Guard(self);
3443
3444        // Ensure the iterator is consumed
3445        for _ in guard.0.by_ref() {}
3446    }
3447}
3448
3449// ===== impl IntoOrderedIter =====
3450
3451impl<T> Iterator for IntoOrderedIter<T> {
3452    type Item = (HeaderName, T);
3453
3454    fn next(&mut self) -> Option<Self::Item> {
3455        while self.index < self.order.len() {
3456            let link = self.order[self.index].take();
3457            self.index += 1;
3458
3459            if let Some(link) = link {
3460                return Some(match link {
3461                    Link::Entry(idx) => {
3462                        let entry = unsafe { self.entries.get_unchecked(idx) };
3463                        let name = unsafe { ptr::read(&entry.key) };
3464                        let value = unsafe { ptr::read(&entry.value) };
3465                        (name, value)
3466                    }
3467                    Link::Extra(idx) => {
3468                        let extra = unsafe { self.extra_values.get_unchecked(idx) };
3469                        let name = unsafe { ptr::read(&extra.key) };
3470                        let value = unsafe { ptr::read(&extra.value) };
3471                        (name, value)
3472                    }
3473                });
3474            }
3475        }
3476
3477        None
3478    }
3479
3480    fn size_hint(&self) -> (usize, Option<usize>) {
3481        (0, Some(self.entries.len() + self.extra_values.len()))
3482    }
3483}
3484
3485impl<T> FusedIterator for IntoOrderedIter<T> {}
3486
3487impl<T> Drop for IntoOrderedIter<T> {
3488    fn drop(&mut self) {
3489        struct Guard<'a, T>(&'a mut IntoOrderedIter<T>);
3490
3491        impl<'a, T> Drop for Guard<'a, T> {
3492            fn drop(&mut self) {
3493                unsafe {
3494                    self.0.entries.set_len(0);
3495                    self.0.extra_values.set_len(0);
3496                }
3497            }
3498        }
3499
3500        let guard = Guard(self);
3501
3502        for _ in guard.0.by_ref() {}
3503    }
3504}
3505
3506// ===== impl OccupiedEntry =====
3507
3508impl<'a, T> OccupiedEntry<'a, T> {
3509    /// Returns a reference to the entry's key.
3510    ///
3511    /// # Examples
3512    ///
3513    /// ```
3514    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3515    /// let mut map = HeaderMap::new();
3516    /// map.insert(HOST, "world".parse().unwrap());
3517    ///
3518    /// if let Entry::Occupied(e) = map.entry("host") {
3519    ///     assert_eq!("host", e.key());
3520    /// }
3521    /// ```
3522    pub fn key(&self) -> &HeaderName {
3523        &self.map.entries[self.index].key
3524    }
3525
3526    /// Get a reference to the first value in the entry.
3527    ///
3528    /// Values are stored in insertion order.
3529    ///
3530    /// # Panics
3531    ///
3532    /// `get` panics if there are no values associated with the entry.
3533    ///
3534    /// # Examples
3535    ///
3536    /// ```
3537    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3538    /// let mut map = HeaderMap::new();
3539    /// map.insert(HOST, "hello.world".parse().unwrap());
3540    ///
3541    /// if let Entry::Occupied(mut e) = map.entry("host") {
3542    ///     assert_eq!(e.get(), &"hello.world");
3543    ///
3544    ///     e.append("hello.earth".parse().unwrap());
3545    ///
3546    ///     assert_eq!(e.get(), &"hello.world");
3547    /// }
3548    /// ```
3549    pub fn get(&self) -> &T {
3550        &self.map.entries[self.index].value
3551    }
3552
3553    /// Get a mutable reference to the first value in the entry.
3554    ///
3555    /// Values are stored in insertion order.
3556    ///
3557    /// # Panics
3558    ///
3559    /// `get_mut` panics if there are no values associated with the entry.
3560    ///
3561    /// # Examples
3562    ///
3563    /// ```
3564    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3565    /// let mut map = HeaderMap::default();
3566    /// map.insert(HOST, "hello.world".to_string());
3567    ///
3568    /// if let Entry::Occupied(mut e) = map.entry("host") {
3569    ///     e.get_mut().push_str("-2");
3570    ///     assert_eq!(e.get(), &"hello.world-2");
3571    /// }
3572    /// ```
3573    pub fn get_mut(&mut self) -> &mut T {
3574        &mut self.map.entries[self.index].value
3575    }
3576
3577    /// Converts the `OccupiedEntry` into a mutable reference to the **first**
3578    /// value.
3579    ///
3580    /// The lifetime of the returned reference is bound to the original map.
3581    ///
3582    /// # Panics
3583    ///
3584    /// `into_mut` panics if there are no values associated with the entry.
3585    ///
3586    /// # Examples
3587    ///
3588    /// ```
3589    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3590    /// let mut map = HeaderMap::default();
3591    /// map.insert(HOST, "hello.world".to_string());
3592    /// map.append(HOST, "hello.earth".to_string());
3593    ///
3594    /// if let Entry::Occupied(e) = map.entry("host") {
3595    ///     e.into_mut().push_str("-2");
3596    /// }
3597    ///
3598    /// assert_eq!("hello.world-2", map["host"]);
3599    /// ```
3600    pub fn into_mut(self) -> &'a mut T {
3601        &mut self.map.entries[self.index].value
3602    }
3603
3604    /// Sets the value of the entry.
3605    ///
3606    /// All previous values associated with the entry are removed and the first
3607    /// one is returned. See `insert_mult` for an API that returns all values.
3608    ///
3609    /// # Examples
3610    ///
3611    /// ```
3612    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3613    /// let mut map = HeaderMap::new();
3614    /// map.insert(HOST, "hello.world".parse().unwrap());
3615    ///
3616    /// if let Entry::Occupied(mut e) = map.entry("host") {
3617    ///     let mut prev = e.insert("earth".parse().unwrap());
3618    ///     assert_eq!("hello.world", prev);
3619    /// }
3620    ///
3621    /// assert_eq!("earth", map["host"]);
3622    /// ```
3623    pub fn insert(&mut self, value: T) -> T {
3624        self.map.insert_occupied_same_key(self.index, value)
3625    }
3626
3627    /// Sets the value of the entry.
3628    ///
3629    /// This function does the same as `insert` except it returns an iterator
3630    /// that yields all values previously associated with the key.
3631    ///
3632    /// # Examples
3633    ///
3634    /// ```
3635    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3636    /// let mut map = HeaderMap::new();
3637    /// map.insert(HOST, "world".parse().unwrap());
3638    /// map.append(HOST, "world2".parse().unwrap());
3639    ///
3640    /// if let Entry::Occupied(mut e) = map.entry("host") {
3641    ///     let mut prev = e.insert_mult("earth".parse().unwrap());
3642    ///     assert_eq!("world", prev.next().unwrap());
3643    ///     assert_eq!("world2", prev.next().unwrap());
3644    ///     assert!(prev.next().is_none());
3645    /// }
3646    ///
3647    /// assert_eq!("earth", map["host"]);
3648    /// ```
3649    pub fn insert_mult(&mut self, value: T) -> ValueDrain<'_, T> {
3650        self.map.insert_occupied_mult_same_key(self.index, value)
3651    }
3652
3653    /// Insert the value into the entry.
3654    ///
3655    /// The new value is appended to the end of the entry's value list. All
3656    /// previous values associated with the entry are retained.
3657    ///
3658    /// # Examples
3659    ///
3660    /// ```
3661    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3662    /// let mut map = HeaderMap::new();
3663    /// map.insert(HOST, "world".parse().unwrap());
3664    ///
3665    /// if let Entry::Occupied(mut e) = map.entry("host") {
3666    ///     e.append("earth".parse().unwrap());
3667    /// }
3668    ///
3669    /// let values = map.get_all("host");
3670    /// let mut i = values.iter();
3671    /// assert_eq!("world", *i.next().unwrap());
3672    /// assert_eq!("earth", *i.next().unwrap());
3673    /// ```
3674    pub fn append(&mut self, value: T) {
3675        let idx = self.index;
3676        let key = self.map.entries[idx].key.clone();
3677        self.map.append_value(idx, key, value);
3678    }
3679
3680    /// Remove the entry from the map.
3681    ///
3682    /// All values associated with the entry are removed and the first one is
3683    /// returned. See `remove_entry_mult` for an API that returns all values.
3684    ///
3685    /// # Examples
3686    ///
3687    /// ```
3688    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3689    /// let mut map = HeaderMap::new();
3690    /// map.insert(HOST, "world".parse().unwrap());
3691    ///
3692    /// if let Entry::Occupied(e) = map.entry("host") {
3693    ///     let mut prev = e.remove();
3694    ///     assert_eq!("world", prev);
3695    /// }
3696    ///
3697    /// assert!(!map.contains_key("host"));
3698    /// ```
3699    pub fn remove(self) -> T {
3700        self.remove_entry().1
3701    }
3702
3703    /// Remove the entry from the map.
3704    ///
3705    /// The key and all values associated with the entry are removed and the
3706    /// first one is returned. See `remove_entry_mult` for an API that returns
3707    /// all values.
3708    ///
3709    /// # Examples
3710    ///
3711    /// ```
3712    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3713    /// let mut map = HeaderMap::new();
3714    /// map.insert(HOST, "world".parse().unwrap());
3715    ///
3716    /// if let Entry::Occupied(e) = map.entry("host") {
3717    ///     let (key, mut prev) = e.remove_entry();
3718    ///     assert_eq!("host", key.as_str());
3719    ///     assert_eq!("world", prev);
3720    /// }
3721    ///
3722    /// assert!(!map.contains_key("host"));
3723    /// ```
3724    pub fn remove_entry(self) -> (HeaderName, T) {
3725        if let Some(links) = self.map.entries[self.index].links {
3726            self.map.remove_all_extra_values(links.next);
3727        }
3728
3729        let entry = self.map.remove_found(self.probe, self.index);
3730
3731        (entry.key, entry.value)
3732    }
3733
3734    /// Remove the entry from the map.
3735    ///
3736    /// The key and all values associated with the entry are removed and
3737    /// returned.
3738    pub fn remove_entry_mult(self) -> (HeaderName, ValueDrain<'a, T>) {
3739        let next = self.map.entries[self.index]
3740            .links
3741            .map(|l| self.map.drain_all_extra_values(l.next).into_iter());
3742
3743        let entry = self.map.remove_found(self.probe, self.index);
3744
3745        let drain = ValueDrain {
3746            first: Some(entry.value),
3747            next,
3748            lt: PhantomData,
3749        };
3750        (entry.key, drain)
3751    }
3752
3753    /// Returns an iterator visiting all values associated with the entry.
3754    ///
3755    /// Values are iterated in insertion order.
3756    ///
3757    /// # Examples
3758    ///
3759    /// ```
3760    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3761    /// let mut map = HeaderMap::new();
3762    /// map.insert(HOST, "world".parse().unwrap());
3763    /// map.append(HOST, "earth".parse().unwrap());
3764    ///
3765    /// if let Entry::Occupied(e) = map.entry("host") {
3766    ///     let mut iter = e.iter();
3767    ///     assert_eq!(&"world", iter.next().unwrap());
3768    ///     assert_eq!(&"earth", iter.next().unwrap());
3769    ///     assert!(iter.next().is_none());
3770    /// }
3771    /// ```
3772    pub fn iter(&self) -> ValueIter<'_, T> {
3773        self.map.value_iter(Some(self.index))
3774    }
3775
3776    /// Returns an iterator mutably visiting all values associated with the
3777    /// entry.
3778    ///
3779    /// Values are iterated in insertion order.
3780    ///
3781    /// # Examples
3782    ///
3783    /// ```
3784    /// # use rama_http_types::header::{HeaderMap, Entry, HOST};
3785    /// let mut map = HeaderMap::default();
3786    /// map.insert(HOST, "world".to_string());
3787    /// map.append(HOST, "earth".to_string());
3788    ///
3789    /// if let Entry::Occupied(mut e) = map.entry("host") {
3790    ///     for e in e.iter_mut() {
3791    ///         e.push_str("-boop");
3792    ///     }
3793    /// }
3794    ///
3795    /// let mut values = map.get_all("host");
3796    /// let mut i = values.iter();
3797    /// assert_eq!(&"world-boop", i.next().unwrap());
3798    /// assert_eq!(&"earth-boop", i.next().unwrap());
3799    /// ```
3800    pub fn iter_mut(&mut self) -> ValueIterMut<'_, T> {
3801        self.map.value_iter_mut(self.index)
3802    }
3803}
3804
3805impl<'a, T> IntoIterator for OccupiedEntry<'a, T> {
3806    type Item = &'a mut T;
3807    type IntoIter = ValueIterMut<'a, T>;
3808
3809    fn into_iter(self) -> ValueIterMut<'a, T> {
3810        self.map.value_iter_mut(self.index)
3811    }
3812}
3813
3814impl<'a, 'b: 'a, T> IntoIterator for &'b OccupiedEntry<'a, T> {
3815    type Item = &'a T;
3816    type IntoIter = ValueIter<'a, T>;
3817
3818    fn into_iter(self) -> ValueIter<'a, T> {
3819        self.iter()
3820    }
3821}
3822
3823impl<'a, 'b: 'a, T> IntoIterator for &'b mut OccupiedEntry<'a, T> {
3824    type Item = &'a mut T;
3825    type IntoIter = ValueIterMut<'a, T>;
3826
3827    fn into_iter(self) -> ValueIterMut<'a, T> {
3828        self.iter_mut()
3829    }
3830}
3831
3832// ===== impl ValueDrain =====
3833
3834impl<'a, T> Iterator for ValueDrain<'a, T> {
3835    type Item = T;
3836
3837    fn next(&mut self) -> Option<T> {
3838        if self.first.is_some() {
3839            self.first.take()
3840        } else if let Some(ref mut extras) = self.next {
3841            extras.next()
3842        } else {
3843            None
3844        }
3845    }
3846
3847    fn size_hint(&self) -> (usize, Option<usize>) {
3848        match (&self.first, &self.next) {
3849            // Exactly 1
3850            (&Some(_), &None) => (1, Some(1)),
3851            // 1 + extras
3852            (&Some(_), Some(extras)) => {
3853                let (l, u) = extras.size_hint();
3854                (l + 1, u.map(|u| u + 1))
3855            }
3856            // Extras only
3857            (&None, Some(extras)) => extras.size_hint(),
3858            // No more
3859            (&None, &None) => (0, Some(0)),
3860        }
3861    }
3862}
3863
3864impl<'a, T> FusedIterator for ValueDrain<'a, T> {}
3865
3866impl<'a, T> Drop for ValueDrain<'a, T> {
3867    fn drop(&mut self) {
3868        for _ in self.by_ref() {}
3869    }
3870}
3871
3872unsafe impl<'a, T: Sync> Sync for ValueDrain<'a, T> {}
3873unsafe impl<'a, T: Send> Send for ValueDrain<'a, T> {}
3874
3875// ===== impl RawLinks =====
3876
3877impl<T> Clone for RawLinks<T> {
3878    fn clone(&self) -> RawLinks<T> {
3879        *self
3880    }
3881}
3882
3883impl<T> Copy for RawLinks<T> {}
3884
3885impl<T> ops::Index<usize> for RawLinks<T> {
3886    type Output = Option<Links>;
3887
3888    fn index(&self, idx: usize) -> &Self::Output {
3889        unsafe { &(*self.0)[idx].links }
3890    }
3891}
3892
3893impl<T> ops::IndexMut<usize> for RawLinks<T> {
3894    fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
3895        unsafe { &mut (*self.0)[idx].links }
3896    }
3897}
3898
3899// ===== impl Pos =====
3900
3901impl Pos {
3902    #[inline]
3903    fn new(index: usize, hash: HashValue) -> Self {
3904        debug_assert!(index < MAX_SIZE);
3905        Pos {
3906            index: index as Size,
3907            hash,
3908        }
3909    }
3910
3911    #[inline]
3912    fn none() -> Self {
3913        Pos {
3914            index: !0,
3915            hash: HashValue(0),
3916        }
3917    }
3918
3919    #[inline]
3920    fn is_some(&self) -> bool {
3921        !self.is_none()
3922    }
3923
3924    #[inline]
3925    fn is_none(&self) -> bool {
3926        self.index == !0
3927    }
3928
3929    #[inline]
3930    fn resolve(&self) -> Option<(usize, HashValue)> {
3931        if self.is_some() {
3932            Some((self.index as usize, self.hash))
3933        } else {
3934            None
3935        }
3936    }
3937}
3938
3939impl Danger {
3940    fn is_red(&self) -> bool {
3941        matches!(*self, Danger::Red(_))
3942    }
3943
3944    fn set_red(&mut self) {
3945        debug_assert!(self.is_yellow());
3946        *self = Danger::Red(RandomState::new());
3947    }
3948
3949    fn is_yellow(&self) -> bool {
3950        matches!(*self, Danger::Yellow)
3951    }
3952
3953    fn set_yellow(&mut self) {
3954        if let Danger::Green = *self {
3955            *self = Danger::Yellow;
3956        }
3957    }
3958
3959    fn set_green(&mut self) {
3960        debug_assert!(self.is_yellow());
3961        *self = Danger::Green;
3962    }
3963}
3964
3965// ===== impl MaxSizeReached =====
3966
3967impl MaxSizeReached {
3968    fn new() -> Self {
3969        MaxSizeReached { _priv: () }
3970    }
3971}
3972
3973impl fmt::Debug for MaxSizeReached {
3974    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3975        f.debug_struct("MaxSizeReached")
3976            // skip _priv noise
3977            .finish()
3978    }
3979}
3980
3981impl fmt::Display for MaxSizeReached {
3982    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3983        f.write_str("max size reached")
3984    }
3985}
3986
3987impl std::error::Error for MaxSizeReached {}
3988
3989// ===== impl Utils =====
3990
3991#[inline]
3992fn usable_capacity(cap: usize) -> usize {
3993    cap - cap / 4
3994}
3995
3996#[inline]
3997fn to_raw_capacity(n: usize) -> Result<usize, MaxSizeReached> {
3998    n.checked_add(n / 3).ok_or_else(MaxSizeReached::new)
3999}
4000
4001#[inline]
4002fn desired_pos(mask: Size, hash: HashValue) -> usize {
4003    (hash.0 & mask) as usize
4004}
4005
4006/// The number of steps that `current` is forward of the desired position for hash
4007#[inline]
4008fn probe_distance(mask: Size, hash: HashValue, current: usize) -> usize {
4009    current.wrapping_sub(desired_pos(mask, hash)) & mask as usize
4010}
4011
4012fn hash_elem_using<K>(danger: &Danger, k: &K) -> HashValue
4013where
4014    K: Hash + ?Sized,
4015{
4016    const MASK: u64 = (MAX_SIZE as u64) - 1;
4017
4018    let hash = match *danger {
4019        // Safe hash
4020        Danger::Red(ref hasher) => {
4021            let mut h = hasher.build_hasher();
4022            k.hash(&mut h);
4023            h.finish()
4024        }
4025        // Fast hash
4026        _ => {
4027            let mut h = FnvHasher::new();
4028            k.hash(&mut h);
4029            h.finish()
4030        }
4031    };
4032
4033    HashValue((hash & MASK) as u16)
4034}
4035
4036struct FnvHasher(u64);
4037
4038impl FnvHasher {
4039    #[inline]
4040    fn new() -> Self {
4041        FnvHasher(0xcbf29ce484222325)
4042    }
4043}
4044
4045impl std::hash::Hasher for FnvHasher {
4046    #[inline]
4047    fn finish(&self) -> u64 {
4048        self.0
4049    }
4050
4051    #[inline]
4052    fn write(&mut self, bytes: &[u8]) {
4053        let mut hash = self.0;
4054        for &b in bytes {
4055            hash ^= b as u64;
4056            hash = hash.wrapping_mul(0x100000001b3);
4057        }
4058        self.0 = hash;
4059    }
4060}
4061
4062/*
4063 *
4064 * ===== impl IntoHeaderName / AsHeaderName =====
4065 *
4066 */
4067
4068mod into_header_name {
4069    use super::{Entry, HdrName, HeaderMap, HeaderName, MaxSizeReached};
4070
4071    /// A marker trait used to identify values that can be used as insert keys
4072    /// to a `HeaderMap`.
4073    pub trait IntoHeaderName: Sealed {}
4074
4075    // All methods are on this pub(super) trait, instead of `IntoHeaderName`,
4076    // so that they aren't publicly exposed to the world.
4077    //
4078    // Being on the `IntoHeaderName` trait would mean users could call
4079    // `"host".insert(&mut map, "localhost")`.
4080    //
4081    // Ultimately, this allows us to adjust the signatures of these methods
4082    // without breaking any external crate.
4083    pub trait Sealed {
4084        #[doc(hidden)]
4085        fn try_insert<T>(self, map: &mut HeaderMap<T>, val: T)
4086        -> Result<Option<T>, MaxSizeReached>;
4087
4088        #[doc(hidden)]
4089        fn try_append<T>(self, map: &mut HeaderMap<T>, val: T) -> Result<bool, MaxSizeReached>;
4090
4091        #[doc(hidden)]
4092        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, MaxSizeReached>;
4093    }
4094
4095    // ==== impls ====
4096
4097    impl Sealed for HeaderName {
4098        #[inline]
4099        fn try_insert<T>(
4100            self,
4101            map: &mut HeaderMap<T>,
4102            val: T,
4103        ) -> Result<Option<T>, MaxSizeReached> {
4104            map.try_insert2(self, val)
4105        }
4106
4107        #[inline]
4108        fn try_append<T>(self, map: &mut HeaderMap<T>, val: T) -> Result<bool, MaxSizeReached> {
4109            map.try_append2(self, val)
4110        }
4111
4112        #[inline]
4113        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, MaxSizeReached> {
4114            map.try_entry2(self)
4115        }
4116    }
4117
4118    impl IntoHeaderName for HeaderName {}
4119
4120    impl Sealed for &HeaderName {
4121        #[inline]
4122        fn try_insert<T>(
4123            self,
4124            map: &mut HeaderMap<T>,
4125            val: T,
4126        ) -> Result<Option<T>, MaxSizeReached> {
4127            map.try_insert2(self, val)
4128        }
4129        #[inline]
4130        fn try_append<T>(self, map: &mut HeaderMap<T>, val: T) -> Result<bool, MaxSizeReached> {
4131            map.try_append2(self, val)
4132        }
4133
4134        #[inline]
4135        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, MaxSizeReached> {
4136            map.try_entry2(self)
4137        }
4138    }
4139
4140    impl IntoHeaderName for &HeaderName {}
4141
4142    impl Sealed for &'static str {
4143        #[inline]
4144        fn try_insert<T>(
4145            self,
4146            map: &mut HeaderMap<T>,
4147            val: T,
4148        ) -> Result<Option<T>, MaxSizeReached> {
4149            HdrName::from_static(self, move |hdr| map.try_insert2(hdr, val))
4150        }
4151        #[inline]
4152        fn try_append<T>(self, map: &mut HeaderMap<T>, val: T) -> Result<bool, MaxSizeReached> {
4153            HdrName::from_static(self, move |hdr| map.try_append2(hdr, val))
4154        }
4155
4156        #[inline]
4157        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, MaxSizeReached> {
4158            HdrName::from_static(self, move |hdr| map.try_entry2(hdr))
4159        }
4160    }
4161
4162    impl IntoHeaderName for &'static str {}
4163}
4164
4165mod as_header_name {
4166    use super::{Entry, HdrName, HeaderMap, HeaderName, InvalidHeaderName, MaxSizeReached};
4167
4168    /// A marker trait used to identify values that can be used as search keys
4169    /// to a `HeaderMap`.
4170    pub trait AsHeaderName: Sealed {}
4171
4172    // Debug not currently needed, save on compiling it
4173    #[allow(missing_debug_implementations)]
4174    pub enum TryEntryError {
4175        InvalidHeaderName(InvalidHeaderName),
4176        MaxSizeReached(MaxSizeReached),
4177    }
4178
4179    impl From<InvalidHeaderName> for TryEntryError {
4180        fn from(e: InvalidHeaderName) -> TryEntryError {
4181            TryEntryError::InvalidHeaderName(e)
4182        }
4183    }
4184
4185    impl From<MaxSizeReached> for TryEntryError {
4186        fn from(e: MaxSizeReached) -> TryEntryError {
4187            TryEntryError::MaxSizeReached(e)
4188        }
4189    }
4190
4191    // All methods are on this pub(super) trait, instead of `AsHeaderName`,
4192    // so that they aren't publicly exposed to the world.
4193    //
4194    // Being on the `AsHeaderName` trait would mean users could call
4195    // `"host".find(&map)`.
4196    //
4197    // Ultimately, this allows us to adjust the signatures of these methods
4198    // without breaking any external crate.
4199    pub trait Sealed {
4200        #[doc(hidden)]
4201        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError>;
4202
4203        #[doc(hidden)]
4204        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)>;
4205
4206        #[doc(hidden)]
4207        fn as_str(&self) -> &str;
4208    }
4209
4210    // ==== impls ====
4211
4212    impl Sealed for HeaderName {
4213        #[inline]
4214        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
4215            Ok(map.try_entry2(self)?)
4216        }
4217
4218        #[inline]
4219        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
4220            map.find(self)
4221        }
4222
4223        fn as_str(&self) -> &str {
4224            <HeaderName>::as_str(self)
4225        }
4226    }
4227
4228    impl AsHeaderName for HeaderName {}
4229
4230    impl Sealed for &HeaderName {
4231        #[inline]
4232        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
4233            Ok(map.try_entry2(self)?)
4234        }
4235
4236        #[inline]
4237        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
4238            map.find(*self)
4239        }
4240
4241        fn as_str(&self) -> &str {
4242            <HeaderName>::as_str(self)
4243        }
4244    }
4245
4246    impl AsHeaderName for &HeaderName {}
4247
4248    impl Sealed for &str {
4249        #[inline]
4250        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
4251            Ok(HdrName::from_bytes(self.as_bytes(), move |hdr| {
4252                map.try_entry2(hdr)
4253            })??)
4254        }
4255
4256        #[inline]
4257        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
4258            HdrName::from_bytes(self.as_bytes(), move |hdr| map.find(&hdr)).unwrap_or(None)
4259        }
4260
4261        fn as_str(&self) -> &str {
4262            self
4263        }
4264    }
4265
4266    impl AsHeaderName for &str {}
4267
4268    impl Sealed for String {
4269        #[inline]
4270        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
4271            self.as_str().try_entry(map)
4272        }
4273
4274        #[inline]
4275        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
4276            Sealed::find(&self.as_str(), map)
4277        }
4278
4279        fn as_str(&self) -> &str {
4280            self
4281        }
4282    }
4283
4284    impl AsHeaderName for String {}
4285
4286    impl Sealed for &String {
4287        #[inline]
4288        fn try_entry<T>(self, map: &mut HeaderMap<T>) -> Result<Entry<'_, T>, TryEntryError> {
4289            self.as_str().try_entry(map)
4290        }
4291
4292        #[inline]
4293        fn find<T>(&self, map: &HeaderMap<T>) -> Option<(usize, usize)> {
4294            Sealed::find(*self, map)
4295        }
4296
4297        fn as_str(&self) -> &str {
4298            self
4299        }
4300    }
4301
4302    impl AsHeaderName for &String {}
4303}
4304
4305#[test]
4306fn test_bounds() {
4307    fn check_bounds<T: Send + Send>() {}
4308
4309    check_bounds::<HeaderMap<()>>();
4310    check_bounds::<Iter<'static, ()>>();
4311    check_bounds::<IterMut<'static, ()>>();
4312    check_bounds::<Keys<'static, ()>>();
4313    check_bounds::<Values<'static, ()>>();
4314    check_bounds::<ValuesMut<'static, ()>>();
4315    check_bounds::<Drain<'static, ()>>();
4316    check_bounds::<GetAll<'static, ()>>();
4317    check_bounds::<Entry<'static, ()>>();
4318    check_bounds::<VacantEntry<'static, ()>>();
4319    check_bounds::<OccupiedEntry<'static, ()>>();
4320    check_bounds::<ValueIter<'static, ()>>();
4321    check_bounds::<ValueIterMut<'static, ()>>();
4322    check_bounds::<ValueDrain<'static, ()>>();
4323}
4324
4325#[test]
4326fn extend_size_hint_above_capacity() {
4327    // A `HeaderMap` may hold more values than the table can index when many
4328    // values are appended under one name, so an exact size hint can exceed the
4329    // largest `reserve` request. Extending must not panic in that case.
4330    let name = HeaderName::from_static("h");
4331    let value = HeaderValue::from_static("0");
4332    let pairs: Vec<(HeaderName, HeaderValue)> =
4333        std::iter::repeat_with(|| (name.clone(), value.clone()))
4334            .take(24_577)
4335            .collect();
4336
4337    let map = HeaderMap::from_iter(pairs);
4338    assert_eq!(map.len(), 24_577);
4339    assert_eq!(map.keys_len(), 1);
4340}
4341
4342#[cfg(test)]
4343fn assert_header_map_invariants<T>(map: &HeaderMap<T>) {
4344    assert_eq!(map.len(), map.entries.len() + map.extra_values.len());
4345
4346    let live_order_slots = map.order.iter().filter(|link| link.is_some()).count();
4347    let order_holes = map.order.iter().filter(|link| link.is_none()).count();
4348    assert_eq!(live_order_slots, map.len());
4349    assert_eq!(order_holes, map.order_holes);
4350
4351    let mut indexed_entries = vec![false; map.entries.len()];
4352    for pos in &*map.indices {
4353        if let Some((idx, hash)) = pos.resolve() {
4354            assert!(idx < map.entries.len(), "index points past entries: {idx}");
4355            assert_eq!(map.entries[idx].hash, hash);
4356            assert!(!indexed_entries[idx], "entry indexed more than once: {idx}");
4357            indexed_entries[idx] = true;
4358        }
4359    }
4360    assert!(
4361        indexed_entries.iter().all(|seen| *seen),
4362        "not every entry is reachable from indices"
4363    );
4364
4365    let mut linked_extras = vec![false; map.extra_values.len()];
4366    for (entry_idx, entry) in map.entries.iter().enumerate() {
4367        assert_eq!(map.order[entry.order], Some(Link::Entry(entry_idx)));
4368
4369        let Some(links) = entry.links else {
4370            continue;
4371        };
4372
4373        assert!(links.next < map.extra_values.len());
4374        assert!(links.tail < map.extra_values.len());
4375
4376        let mut prev = Link::Entry(entry_idx);
4377        let mut current = links.next;
4378        loop {
4379            assert!(current < map.extra_values.len());
4380            assert!(
4381                !linked_extras[current],
4382                "extra value linked more than once: {current}"
4383            );
4384            linked_extras[current] = true;
4385
4386            let extra = &map.extra_values[current];
4387            assert_eq!(extra.prev, prev);
4388            assert_eq!(extra.key, entry.key);
4389            assert_eq!(map.order[extra.order], Some(Link::Extra(current)));
4390
4391            match extra.next {
4392                Link::Entry(idx) => {
4393                    assert_eq!(idx, entry_idx);
4394                    assert_eq!(current, links.tail);
4395                    break;
4396                }
4397                Link::Extra(next) => {
4398                    prev = Link::Extra(current);
4399                    current = next;
4400                }
4401            }
4402        }
4403    }
4404    assert!(
4405        linked_extras.iter().all(|seen| *seen),
4406        "not every extra value is linked from an entry"
4407    );
4408
4409    for (order_idx, link) in map.order.iter().enumerate() {
4410        match link {
4411            Some(Link::Entry(idx)) => {
4412                assert!(*idx < map.entries.len());
4413                assert_eq!(map.entries[*idx].order, order_idx);
4414            }
4415            Some(Link::Extra(idx)) => {
4416                assert!(*idx < map.extra_values.len());
4417                assert_eq!(map.extra_values[*idx].order, order_idx);
4418            }
4419            None => {}
4420        }
4421    }
4422}
4423
4424#[cfg(test)]
4425#[derive(Clone, Debug)]
4426struct HeaderMapOps(Vec<HeaderMapOp>);
4427
4428#[cfg(test)]
4429#[derive(Clone, Debug)]
4430enum HeaderMapOp {
4431    Append { name: u8, value: u8 },
4432    Insert { name: u8, value: u8 },
4433    Remove { name: u8 },
4434    RemoveEntryMult { name: u8 },
4435    EntryAppend { name: u8, value: u8 },
4436    EntryInsertMult { name: u8, value: u8 },
4437}
4438
4439#[cfg(test)]
4440#[derive(Clone, Debug, Eq, PartialEq)]
4441struct ModelHeader {
4442    semantic_id: usize,
4443    name: String,
4444    value: String,
4445    head: bool,
4446}
4447
4448#[cfg(test)]
4449#[derive(Default)]
4450struct HeaderMapModel {
4451    fields: Vec<Option<ModelHeader>>,
4452}
4453
4454#[cfg(test)]
4455impl HeaderMapModel {
4456    fn len(&self) -> usize {
4457        self.fields.iter().filter(|field| field.is_some()).count()
4458    }
4459
4460    fn ordered(&self) -> Vec<(String, String)> {
4461        self.fields
4462            .iter()
4463            .filter_map(|field| {
4464                field
4465                    .as_ref()
4466                    .map(|field| (field.name.clone(), field.value.clone()))
4467            })
4468            .collect()
4469    }
4470
4471    fn values_for(&self, semantic_id: usize) -> Vec<String> {
4472        self.fields
4473            .iter()
4474            .filter_map(|field| {
4475                let field = field.as_ref()?;
4476                (field.semantic_id == semantic_id).then(|| field.value.clone())
4477            })
4478            .collect()
4479    }
4480
4481    fn head_index(&self, semantic_id: usize) -> Option<usize> {
4482        self.fields.iter().position(|field| {
4483            field
4484                .as_ref()
4485                .is_some_and(|field| field.semantic_id == semantic_id && field.head)
4486        })
4487    }
4488
4489    fn remove(&mut self, semantic_id: usize) {
4490        for field in &mut self.fields {
4491            if field
4492                .as_ref()
4493                .is_some_and(|field| field.semantic_id == semantic_id)
4494            {
4495                *field = None;
4496            }
4497        }
4498    }
4499
4500    fn append(&mut self, semantic_id: usize, name: String, value: String) {
4501        let head = self.head_index(semantic_id).is_none();
4502        self.fields.push(Some(ModelHeader {
4503            semantic_id,
4504            name,
4505            value,
4506            head,
4507        }));
4508    }
4509
4510    fn insert(&mut self, semantic_id: usize, name: String, value: String) {
4511        if let Some(head_idx) = self.head_index(semantic_id) {
4512            for (idx, field) in self.fields.iter_mut().enumerate() {
4513                if idx != head_idx
4514                    && field
4515                        .as_ref()
4516                        .is_some_and(|field| field.semantic_id == semantic_id)
4517                {
4518                    *field = None;
4519                }
4520            }
4521            self.fields[head_idx] = Some(ModelHeader {
4522                semantic_id,
4523                name,
4524                value,
4525                head: true,
4526            });
4527        } else {
4528            self.append(semantic_id, name, value);
4529        }
4530    }
4531
4532    fn entry_append(&mut self, semantic_id: usize, name: String, value: String) {
4533        if let Some(head_idx) = self.head_index(semantic_id) {
4534            let entry_name = self.fields[head_idx].as_ref().unwrap().name.clone();
4535            self.append(semantic_id, entry_name, value);
4536        } else {
4537            self.append(semantic_id, name, value);
4538        }
4539    }
4540
4541    fn entry_insert_mult(&mut self, semantic_id: usize, name: String, value: String) {
4542        if let Some(head_idx) = self.head_index(semantic_id) {
4543            let entry_name = self.fields[head_idx].as_ref().unwrap().name.clone();
4544            self.insert(semantic_id, entry_name, value);
4545        } else {
4546            self.append(semantic_id, name, value);
4547        }
4548    }
4549}
4550
4551#[cfg(test)]
4552impl quickcheck::Arbitrary for HeaderMapOps {
4553    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
4554        let len = usize::from(u8::arbitrary(g) % 80);
4555        let ops = (0..len).map(|_| HeaderMapOp::arbitrary(g)).collect();
4556        Self(ops)
4557    }
4558
4559    fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
4560        Box::new(self.0.shrink().map(Self))
4561    }
4562}
4563
4564#[cfg(test)]
4565impl quickcheck::Arbitrary for HeaderMapOp {
4566    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
4567        let name = u8::arbitrary(g);
4568        let value = u8::arbitrary(g);
4569        match u8::arbitrary(g) % 6 {
4570            0 => Self::Append { name, value },
4571            1 => Self::Insert { name, value },
4572            2 => Self::Remove { name },
4573            3 => Self::RemoveEntryMult { name },
4574            4 => Self::EntryAppend { name, value },
4575            _ => Self::EntryInsertMult { name, value },
4576        }
4577    }
4578}
4579
4580#[cfg(test)]
4581fn model_header_name(selector: u8) -> (&'static str, usize) {
4582    match selector % 8 {
4583        0 => ("x-a", 0),
4584        1 => ("X-A", 0),
4585        2 => ("x-b", 1),
4586        3 => ("X-B", 1),
4587        4 => ("cookie", 2),
4588        5 => ("Cookie", 2),
4589        6 => ("x-c", 3),
4590        _ => ("X-C", 3),
4591    }
4592}
4593
4594#[cfg(test)]
4595fn model_header_value(value: u8) -> String {
4596    format!("v{value}")
4597}
4598
4599#[cfg(test)]
4600fn make_header_name(name: &str) -> HeaderName {
4601    HeaderName::from_bytes(name.as_bytes()).unwrap()
4602}
4603
4604#[cfg(test)]
4605fn model_original_name(name: &str) -> String {
4606    make_header_name(name).to_string()
4607}
4608
4609#[cfg(test)]
4610fn make_header_value(value: &str) -> HeaderValue {
4611    HeaderValue::from_bytes(value.as_bytes()).unwrap()
4612}
4613
4614#[cfg(test)]
4615fn assert_header_map_matches_model(map: &HeaderMap, model: &HeaderMapModel) {
4616    assert_header_map_invariants(map);
4617    assert_eq!(map.len(), model.len());
4618
4619    let ordered = map
4620        .ordered_iter()
4621        .map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_owned()))
4622        .collect::<Vec<_>>();
4623    assert_eq!(ordered, model.ordered());
4624
4625    let consumed = map
4626        .clone()
4627        .into_ordered_iter()
4628        .map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_owned()))
4629        .collect::<Vec<_>>();
4630    assert_eq!(consumed, ordered);
4631
4632    for (selector, semantic_id) in [
4633        ("x-a", 0),
4634        ("X-A", 0),
4635        ("x-b", 1),
4636        ("X-B", 1),
4637        ("cookie", 2),
4638        ("Cookie", 2),
4639        ("x-c", 3),
4640        ("X-C", 3),
4641    ] {
4642        let actual = map
4643            .get_all(make_header_name(selector))
4644            .iter()
4645            .map(|value| value.to_str().unwrap().to_owned())
4646            .collect::<Vec<_>>();
4647        assert_eq!(actual, model.values_for(semantic_id));
4648    }
4649}
4650
4651#[cfg(test)]
4652impl HeaderMapOps {
4653    fn run(self) {
4654        let mut map = HeaderMap::new();
4655        let mut model = HeaderMapModel::default();
4656
4657        assert_header_map_matches_model(&map, &model);
4658
4659        for op in self.0 {
4660            match op {
4661                HeaderMapOp::Append { name, value } => {
4662                    let (name, semantic_id) = model_header_name(name);
4663                    let value = model_header_value(value);
4664                    map.append(make_header_name(name), make_header_value(&value));
4665                    model.append(semantic_id, model_original_name(name), value);
4666                }
4667                HeaderMapOp::Insert { name, value } => {
4668                    let (name, semantic_id) = model_header_name(name);
4669                    let value = model_header_value(value);
4670                    map.insert(make_header_name(name), make_header_value(&value));
4671                    model.insert(semantic_id, model_original_name(name), value);
4672                }
4673                HeaderMapOp::Remove { name } => {
4674                    let (name, semantic_id) = model_header_name(name);
4675                    map.remove(make_header_name(name));
4676                    model.remove(semantic_id);
4677                }
4678                HeaderMapOp::RemoveEntryMult { name } => {
4679                    let (name, semantic_id) = model_header_name(name);
4680                    if let Entry::Occupied(entry) = map.entry(make_header_name(name)) {
4681                        let _ = entry.remove_entry_mult();
4682                    }
4683                    model.remove(semantic_id);
4684                }
4685                HeaderMapOp::EntryAppend { name, value } => {
4686                    let (name, semantic_id) = model_header_name(name);
4687                    let value = model_header_value(value);
4688                    match map.entry(make_header_name(name)) {
4689                        Entry::Occupied(mut entry) => {
4690                            entry.append(make_header_value(&value));
4691                        }
4692                        Entry::Vacant(entry) => {
4693                            entry.insert_entry(make_header_value(&value));
4694                        }
4695                    }
4696                    model.entry_append(semantic_id, model_original_name(name), value);
4697                }
4698                HeaderMapOp::EntryInsertMult { name, value } => {
4699                    let (name, semantic_id) = model_header_name(name);
4700                    let value = model_header_value(value);
4701                    match map.entry(make_header_name(name)) {
4702                        Entry::Occupied(mut entry) => {
4703                            let _ = entry.insert_mult(make_header_value(&value));
4704                        }
4705                        Entry::Vacant(entry) => {
4706                            entry.insert_entry(make_header_value(&value));
4707                        }
4708                    }
4709                    model.entry_insert_mult(semantic_id, model_original_name(name), value);
4710                }
4711            }
4712
4713            assert_header_map_matches_model(&map, &model);
4714        }
4715    }
4716}
4717
4718#[test]
4719fn quickcheck_header_map_order_and_multivalue_model() {
4720    fn prop(ops: HeaderMapOps) -> bool {
4721        ops.run();
4722        true
4723    }
4724
4725    quickcheck::QuickCheck::new()
4726        .tests(500)
4727        .quickcheck(prop as fn(HeaderMapOps) -> bool);
4728}
4729
4730#[test]
4731fn ensure_miri_itermut_not_violated() {
4732    let mut headers = HeaderMap::<u32>::default();
4733    headers.insert(HeaderName::from_static("hello"), 1u32);
4734    headers.insert(HeaderName::from_static("zomg"), 2u32);
4735
4736    let mut iter = headers.iter_mut();
4737    let (_, first) = iter.next().unwrap();
4738    let (_, second) = iter.next().unwrap();
4739
4740    *first += 10;
4741    *second += 20;
4742}
4743
4744#[test]
4745fn ensure_miri_valueitermut_not_violated() {
4746    let mut headers = HeaderMap::<u32>::default();
4747    headers.insert(HeaderName::from_static("hello"), 1u32);
4748    headers.append(HeaderName::from_static("hello"), 2u32);
4749    headers.append(HeaderName::from_static("hello"), 3u32);
4750
4751    let mut entry = match headers.entry(HeaderName::from_static("hello")) {
4752        Entry::Occupied(entry) => entry,
4753        Entry::Vacant(_) => panic!(),
4754    };
4755
4756    let mut iter = entry.iter_mut();
4757    let first = iter.next().unwrap();
4758    let second = iter.next().unwrap();
4759
4760    *first += 10;
4761    *second += 20;
4762}
4763
4764struct ManuallyAllocated {
4765    ptr: *mut u8,
4766    panic_on_drop: bool,
4767}
4768
4769impl ManuallyAllocated {
4770    fn new(byte: u8, panic_on_drop: bool) -> Self {
4771        Self {
4772            ptr: Box::into_raw(Box::new(byte)),
4773            panic_on_drop,
4774        }
4775    }
4776}
4777
4778impl Drop for ManuallyAllocated {
4779    fn drop(&mut self) {
4780        unsafe {
4781            drop(Box::from_raw(self.ptr));
4782        }
4783
4784        if self.panic_on_drop {
4785            panic!("intentional drop panic");
4786        }
4787    }
4788}
4789
4790#[test]
4791fn into_iter_drop_panic_after_yielding_extra_value_double_drops() {
4792    use std::panic::{AssertUnwindSafe, catch_unwind};
4793
4794    let mut map: HeaderMap<ManuallyAllocated> = HeaderMap::default();
4795    map.append("x-first", ManuallyAllocated::new(1, false));
4796    map.append("x-first", ManuallyAllocated::new(2, false));
4797    map.insert("x-second", ManuallyAllocated::new(3, true));
4798
4799    let mut iter = map.into_iter();
4800
4801    // HeaderMap::IntoIter yields extra values with ptr::read from
4802    // self.extra_values and relies on Drop setting self.extra_values.len() to
4803    // zero after the iterator has been fully consumed. If a later value's Drop
4804    // panics while IntoIter::drop is draining the iterator, that set_len(0) is
4805    // skipped. The Vec then drops already-yielded extra value slots again.
4806    drop(iter.next().unwrap());
4807    drop(iter.next().unwrap());
4808
4809    let _ = catch_unwind(AssertUnwindSafe(|| drop(iter)));
4810}
4811
4812#[test]
4813fn into_ordered_iter_drop_panic_after_yielding_values_does_not_double_drop() {
4814    use std::panic::{AssertUnwindSafe, catch_unwind};
4815
4816    let mut map: HeaderMap<ManuallyAllocated> = HeaderMap::default();
4817    map.append("x-first", ManuallyAllocated::new(1, false));
4818    map.append("x-first", ManuallyAllocated::new(2, false));
4819    map.insert("x-second", ManuallyAllocated::new(3, true));
4820
4821    let mut iter = map.into_ordered_iter();
4822
4823    drop(iter.next().unwrap());
4824    drop(iter.next().unwrap());
4825
4826    let _ = catch_unwind(AssertUnwindSafe(|| drop(iter)));
4827}
4828
4829#[test]
4830fn drain_drop_panic_leaves_map_empty_and_valid() {
4831    use std::panic::{AssertUnwindSafe, catch_unwind};
4832
4833    let mut map: HeaderMap<ManuallyAllocated> = HeaderMap::default();
4834    map.insert("x-panic", ManuallyAllocated::new(1, true));
4835    map.append("x-rest", ManuallyAllocated::new(2, false));
4836    map.append("x-rest", ManuallyAllocated::new(3, false));
4837
4838    let drain = map.drain();
4839    let _ = catch_unwind(AssertUnwindSafe(|| drop(drain)));
4840
4841    assert_header_map_invariants(&map);
4842}
4843
4844#[test]
4845fn skip_duplicates_during_key_iteration() {
4846    let mut map = HeaderMap::new();
4847    map.try_append("a", HeaderValue::from_static("a")).unwrap();
4848    map.try_append("a", HeaderValue::from_static("b")).unwrap();
4849    assert_eq!(map.keys().count(), map.keys_len());
4850}
4851
4852#[test]
4853fn ordered_iter_size_hint_counts_remaining_live_order_slots() {
4854    let mut map = HeaderMap::new();
4855    map.try_insert("a", HeaderValue::from_static("a")).unwrap();
4856    map.try_insert("b", HeaderValue::from_static("b")).unwrap();
4857    map.try_insert("c", HeaderValue::from_static("c")).unwrap();
4858    map.remove("b");
4859
4860    let mut iter = map.ordered_iter();
4861    assert_eq!((2, Some(2)), iter.size_hint());
4862    assert_eq!("a", iter.next().unwrap().0);
4863    assert_eq!((1, Some(1)), iter.size_hint());
4864    assert_eq!("c", iter.next().unwrap().0);
4865    assert_eq!((0, Some(0)), iter.size_hint());
4866    assert!(iter.next().is_none());
4867}
4868
4869#[test]
4870fn drain_size_hint_counts_remaining_entries_and_extras() {
4871    let mut map = HeaderMap::new();
4872    map.append("x-first", HeaderValue::from_static("one"));
4873    map.append("x-first", HeaderValue::from_static("two"));
4874    map.insert("x-second", HeaderValue::from_static("three"));
4875
4876    let mut drain = map.drain();
4877    assert_eq!(drain.size_hint(), (2, Some(3)));
4878
4879    assert_eq!(
4880        drain.next().map(|(name, value)| (
4881            name.map(|name| name.to_string()),
4882            value.to_str().unwrap().to_owned()
4883        )),
4884        Some((Some("x-first".to_owned()), "one".to_owned()))
4885    );
4886    assert_eq!(drain.size_hint(), (1, Some(2)));
4887
4888    assert_eq!(
4889        drain.next().map(|(name, value)| (
4890            name.map(|name| name.to_string()),
4891            value.to_str().unwrap().to_owned()
4892        )),
4893        Some((None, "two".to_owned()))
4894    );
4895    assert_eq!(drain.size_hint(), (1, Some(1)));
4896
4897    assert_eq!(
4898        drain.next().map(|(name, value)| (
4899            name.map(|name| name.to_string()),
4900            value.to_str().unwrap().to_owned()
4901        )),
4902        Some((Some("x-second".to_owned()), "three".to_owned()))
4903    );
4904    assert_eq!(drain.size_hint(), (0, Some(0)));
4905    assert!(drain.next().is_none());
4906}
4907
4908#[test]
4909fn into_iter_size_hint_counts_remaining_entries_only() {
4910    let mut map = HeaderMap::new();
4911    map.append("x-first", HeaderValue::from_static("one"));
4912    map.append("x-first", HeaderValue::from_static("two"));
4913    map.insert("x-second", HeaderValue::from_static("three"));
4914
4915    let mut iter = map.into_iter();
4916    assert_eq!(iter.size_hint(), (2, None));
4917
4918    assert_eq!(
4919        iter.next().map(|(name, value)| (
4920            name.map(|name| name.to_string()),
4921            value.to_str().unwrap().to_owned()
4922        )),
4923        Some((Some("x-first".to_owned()), "one".to_owned()))
4924    );
4925    assert_eq!(iter.size_hint(), (1, None));
4926
4927    assert_eq!(
4928        iter.next().map(|(name, value)| (
4929            name.map(|name| name.to_string()),
4930            value.to_str().unwrap().to_owned()
4931        )),
4932        Some((None, "two".to_owned()))
4933    );
4934    assert_eq!(iter.size_hint(), (1, None));
4935
4936    assert_eq!(
4937        iter.next().map(|(name, value)| (
4938            name.map(|name| name.to_string()),
4939            value.to_str().unwrap().to_owned()
4940        )),
4941        Some((Some("x-second".to_owned()), "three".to_owned()))
4942    );
4943    assert_eq!(iter.size_hint(), (0, None));
4944    assert!(iter.next().is_none());
4945}
4946
4947#[test]
4948fn into_ordered_iter_size_hint_upper_bound_counts_backing_values() {
4949    let mut map = HeaderMap::new();
4950    map.append("x-first", HeaderValue::from_static("one"));
4951    map.append("x-first", HeaderValue::from_static("two"));
4952    map.insert("x-second", HeaderValue::from_static("three"));
4953
4954    let mut iter = map.into_ordered_iter();
4955    assert_eq!(iter.size_hint(), (0, Some(3)));
4956
4957    assert_eq!(
4958        iter.next()
4959            .map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_owned())),
4960        Some(("x-first".to_owned(), "one".to_owned()))
4961    );
4962    assert_eq!(iter.size_hint(), (0, Some(3)));
4963
4964    assert_eq!(
4965        iter.next()
4966            .map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_owned())),
4967        Some(("x-first".to_owned(), "two".to_owned()))
4968    );
4969    assert_eq!(iter.size_hint(), (0, Some(3)));
4970
4971    assert_eq!(
4972        iter.next()
4973            .map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_owned())),
4974        Some(("x-second".to_owned(), "three".to_owned()))
4975    );
4976    assert_eq!(iter.size_hint(), (0, Some(3)));
4977    assert!(iter.next().is_none());
4978}
4979
4980#[test]
4981fn value_drain_size_hint_counts_remaining_values() {
4982    let mut map = HeaderMap::new();
4983    map.append("x-first", HeaderValue::from_static("one"));
4984    map.append("x-first", HeaderValue::from_static("two"));
4985    map.append("x-first", HeaderValue::from_static("three"));
4986
4987    let Entry::Occupied(mut entry) = map.entry("x-first") else {
4988        panic!("expected x-first header");
4989    };
4990    let mut drain = entry.insert_mult(HeaderValue::from_static("replacement"));
4991
4992    assert_eq!(drain.size_hint(), (3, Some(3)));
4993    assert_eq!(
4994        drain.next().map(|value| value.to_str().unwrap().to_owned()),
4995        Some("one".to_owned())
4996    );
4997    assert_eq!(drain.size_hint(), (2, Some(2)));
4998    assert_eq!(
4999        drain.next().map(|value| value.to_str().unwrap().to_owned()),
5000        Some("two".to_owned())
5001    );
5002    assert_eq!(drain.size_hint(), (1, Some(1)));
5003    assert_eq!(
5004        drain.next().map(|value| value.to_str().unwrap().to_owned()),
5005        Some("three".to_owned())
5006    );
5007    assert_eq!(drain.size_hint(), (0, Some(0)));
5008    assert!(drain.next().is_none());
5009}
5010
5011#[test]
5012fn dropping_value_drain_drops_remaining_values() {
5013    use std::cell::Cell;
5014    use std::rc::Rc;
5015
5016    struct DropCounter(Rc<Cell<usize>>);
5017
5018    impl Drop for DropCounter {
5019        fn drop(&mut self) {
5020            self.0.set(self.0.get() + 1);
5021        }
5022    }
5023
5024    let drops = Rc::new(Cell::new(0));
5025    let mut map: HeaderMap<DropCounter> = HeaderMap::default();
5026    map.append("x-first", DropCounter(drops.clone()));
5027    map.append("x-first", DropCounter(drops.clone()));
5028    map.append("x-first", DropCounter(drops.clone()));
5029
5030    let Entry::Occupied(mut entry) = map.entry("x-first") else {
5031        panic!("expected x-first header");
5032    };
5033    let mut drain = entry.insert_mult(DropCounter(drops.clone()));
5034    drop(drain.next());
5035    assert_eq!(drops.get(), 1);
5036
5037    drop(drain);
5038    assert_eq!(drops.get(), 3);
5039}
5040
5041#[test]
5042fn basic_map_api_contracts_cover_capacity_lookup_and_clear() {
5043    let empty = HeaderMap::<HeaderValue>::try_with_capacity(0).unwrap();
5044    assert!(empty.is_empty());
5045    assert_eq!(empty.capacity(), 0);
5046
5047    let mut map = HeaderMap::<HeaderValue>::with_capacity(10);
5048    assert!(map.is_empty());
5049    assert_eq!(map.len(), 0);
5050    assert_eq!(map.keys_len(), 0);
5051    assert_eq!(map.capacity(), 12);
5052    assert_eq!(map.mask as usize, 15);
5053
5054    let mut reserved = HeaderMap::<HeaderValue>::new();
5055    reserved.reserve(10);
5056    assert_eq!(reserved.capacity(), 12);
5057    assert_eq!(reserved.mask as usize, 15);
5058
5059    let mut try_reserved = HeaderMap::<HeaderValue>::new();
5060    try_reserved.try_reserve(10).unwrap();
5061    assert_eq!(try_reserved.capacity(), 12);
5062    assert_eq!(try_reserved.mask as usize, 15);
5063
5064    let max_capacity =
5065        HeaderMap::<HeaderValue>::try_with_capacity(usable_capacity(MAX_SIZE)).unwrap();
5066    assert_eq!(max_capacity.capacity(), usable_capacity(MAX_SIZE));
5067    assert_eq!(max_capacity.mask as usize, MAX_SIZE - 1);
5068    let max_size_err =
5069        HeaderMap::<HeaderValue>::try_with_capacity(usable_capacity(MAX_SIZE) + 1).unwrap_err();
5070    assert_eq!(max_size_err.to_string(), "max size reached");
5071    assert_eq!(format!("{max_size_err:?}"), "MaxSizeReached");
5072    let mut too_large_reserve = HeaderMap::<HeaderValue>::new();
5073    assert!(
5074        too_large_reserve
5075            .try_reserve(usable_capacity(MAX_SIZE) + 1)
5076            .is_err()
5077    );
5078
5079    let key = HeaderName::from_static("x-basic");
5080    let key_string = "x-basic".to_owned();
5081    assert!(!map.contains_key(&key));
5082    assert!(!map.contains_key(&key_string));
5083    assert!(map.get(&key).is_none());
5084    assert!(map.get("x-basic").is_none());
5085    assert!(map.get(key_string.clone()).is_none());
5086    assert!(map.get(&key_string).is_none());
5087
5088    assert!(
5089        map.insert(key.clone(), HeaderValue::from_static("one"))
5090            .is_none()
5091    );
5092    assert!(!map.is_empty());
5093    assert_eq!(map.len(), 1);
5094    assert_eq!(map.keys_len(), 1);
5095    assert!(map.contains_key(&key));
5096    assert!(map.contains_key("x-basic"));
5097    assert!(map.contains_key(key_string.clone()));
5098    assert!(map.contains_key(&key_string));
5099    assert_eq!(map.get(&key).unwrap(), "one");
5100    assert_eq!(map.get(key.clone()).unwrap(), "one");
5101    assert_eq!(map.get("x-basic").unwrap(), "one");
5102    assert_eq!(map.get(key_string.clone()).unwrap(), "one");
5103    assert_eq!(map.get(&key_string).unwrap(), "one");
5104    assert_eq!(map.get("missing"), None);
5105
5106    *map.get_mut("x-basic").unwrap() = HeaderValue::from_static("mutated");
5107    assert_eq!(map.get("x-basic").unwrap(), "mutated");
5108
5109    assert!(map.append("x-basic", HeaderValue::from_static("two")));
5110    assert_eq!(map.len(), 2);
5111    assert_eq!(map.keys_len(), 1);
5112
5113    let borrowed = HeaderName::from_static("x-borrowed");
5114    assert!(
5115        map.insert(&borrowed, HeaderValue::from_static("borrowed"))
5116            .is_none()
5117    );
5118    assert!(map.append(&borrowed, HeaderValue::from_static("borrowed-again")));
5119    assert_eq!(
5120        map.get_all(&borrowed)
5121            .iter()
5122            .map(|value| value.to_str().unwrap().to_owned())
5123            .collect::<Vec<_>>(),
5124        ["borrowed".to_owned(), "borrowed-again".to_owned()]
5125    );
5126
5127    map.clear();
5128    assert!(map.is_empty());
5129    assert_eq!(map.len(), 0);
5130    assert_eq!(map.keys_len(), 0);
5131    assert!(map.capacity() >= 12);
5132    assert!(!map.contains_key("x-basic"));
5133}
5134
5135#[test]
5136fn extend_try_from_equality_debug_and_serde_contracts() {
5137    use std::collections::HashMap;
5138    use std::convert::TryFrom;
5139
5140    let mut raw = HashMap::new();
5141    raw.insert("x-one".to_owned(), "one".to_owned());
5142    raw.insert("x-two".to_owned(), "two".to_owned());
5143
5144    let from_hash = HeaderMap::<HeaderValue>::try_from(&raw).unwrap();
5145    assert_eq!(from_hash.len(), 2);
5146    assert_eq!(from_hash.get("x-one").unwrap(), "one");
5147    assert_eq!(from_hash.get("x-two").unwrap(), "two");
5148
5149    let mut by_name = HeaderMap::new();
5150    by_name.extend([
5151        (
5152            HeaderName::from_static("x-a"),
5153            HeaderValue::from_static("a1"),
5154        ),
5155        (
5156            HeaderName::from_static("x-a"),
5157            HeaderValue::from_static("a2"),
5158        ),
5159        (
5160            HeaderName::from_static("x-b"),
5161            HeaderValue::from_static("b1"),
5162        ),
5163    ]);
5164    assert_eq!(
5165        by_name
5166            .get_all("x-a")
5167            .iter()
5168            .map(|value| value.to_str().unwrap().to_owned())
5169            .collect::<Vec<_>>(),
5170        ["a1".to_owned(), "a2".to_owned()]
5171    );
5172
5173    let mut by_optional_name = HeaderMap::new();
5174    by_optional_name.extend([
5175        (
5176            Some(HeaderName::from_static("x-c")),
5177            HeaderValue::from_static("c1"),
5178        ),
5179        (None, HeaderValue::from_static("c2")),
5180        (
5181            Some(HeaderName::from_static("x-d")),
5182            HeaderValue::from_static("d1"),
5183        ),
5184    ]);
5185    assert_eq!(
5186        by_optional_name
5187            .get_all("x-c")
5188            .iter()
5189            .map(|value| value.to_str().unwrap().to_owned())
5190            .collect::<Vec<_>>(),
5191        ["c1".to_owned(), "c2".to_owned()]
5192    );
5193
5194    let same = by_optional_name.clone();
5195    assert_eq!(by_optional_name, same);
5196    let mut different = same.clone();
5197    different.append("x-c", HeaderValue::from_static("c3"));
5198    assert_ne!(by_optional_name, different);
5199
5200    let debug = format!("{by_optional_name:?}");
5201    assert!(debug.contains("x-c"));
5202    assert!(debug.contains("c1"));
5203
5204    let json = serde_json::to_string(&by_optional_name).unwrap();
5205    let roundtrip: HeaderMap = serde_json::from_str(&json).unwrap();
5206    assert_eq!(
5207        roundtrip
5208            .ordered_iter()
5209            .map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_owned()))
5210            .collect::<Vec<_>>(),
5211        by_optional_name
5212            .ordered_iter()
5213            .map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_owned()))
5214            .collect::<Vec<_>>()
5215    );
5216}
5217
5218#[test]
5219fn unordered_iterators_and_key_value_views_have_stable_contracts() {
5220    let mut map = HeaderMap::new();
5221    map.append("x-first", HeaderValue::from_static("one"));
5222    map.append("x-first", HeaderValue::from_static("two"));
5223    map.insert("x-second", HeaderValue::from_static("three"));
5224
5225    let iter = map.iter();
5226    assert_eq!(iter.size_hint(), (2, None));
5227    let mut iter = iter;
5228    let first = iter
5229        .next()
5230        .map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_owned()))
5231        .unwrap();
5232    assert_eq!(iter.size_hint(), (2, None));
5233    let mut pairs = iter
5234        .map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_owned()))
5235        .collect::<Vec<_>>();
5236    pairs.push(first);
5237    pairs.sort();
5238    assert_eq!(
5239        pairs,
5240        [
5241            ("x-first".to_owned(), "one".to_owned()),
5242            ("x-first".to_owned(), "two".to_owned()),
5243            ("x-second".to_owned(), "three".to_owned()),
5244        ]
5245    );
5246
5247    assert_eq!(map.keys().size_hint(), (2, Some(2)));
5248    let mut keys = map.keys().map(|name| name.to_string()).collect::<Vec<_>>();
5249    keys.sort();
5250    assert_eq!(keys, ["x-first".to_owned(), "x-second".to_owned()]);
5251    assert_eq!(map.keys().count(), 2);
5252    assert!(map.keys().nth(1).is_some());
5253    assert!(map.keys().last().is_some());
5254
5255    assert_eq!(map.values().size_hint(), (2, None));
5256    let mut values = map
5257        .values()
5258        .map(|value| value.to_str().unwrap().to_owned())
5259        .collect::<Vec<_>>();
5260    values.sort();
5261    assert_eq!(
5262        values,
5263        ["one".to_owned(), "three".to_owned(), "two".to_owned()]
5264    );
5265
5266    let mut mutable: HeaderMap<String> = HeaderMap::default();
5267    mutable.append("x-first", "one".to_owned());
5268    mutable.append("x-first", "two".to_owned());
5269    mutable.insert("x-second", "three".to_owned());
5270
5271    let mut iter_mut = mutable.iter_mut();
5272    assert_eq!(iter_mut.size_hint(), (2, None));
5273    iter_mut.next().unwrap().1.push('!');
5274    assert_eq!(iter_mut.size_hint(), (2, None));
5275    for (_, value) in iter_mut {
5276        value.push('!');
5277    }
5278    assert_eq!(mutable.values_mut().size_hint(), (2, None));
5279    for value in mutable.values_mut() {
5280        value.push('?');
5281    }
5282
5283    let mut values = mutable.values().cloned().collect::<Vec<_>>();
5284    values.sort();
5285    assert_eq!(
5286        values,
5287        ["one!?".to_owned(), "three!?".to_owned(), "two!?".to_owned()]
5288    );
5289}
5290
5291#[test]
5292fn per_key_value_iterators_support_forward_and_reverse_traversal() {
5293    let mut map = HeaderMap::new();
5294    map.append("x-multi", HeaderValue::from_static("one"));
5295    map.append("x-multi", HeaderValue::from_static("two"));
5296    map.append("x-multi", HeaderValue::from_static("three"));
5297    map.insert("x-other", HeaderValue::from_static("solo"));
5298
5299    assert_eq!(
5300        map.get_all("x-multi"),
5301        map.get_all(&HeaderName::from_static("x-multi"))
5302    );
5303    assert_ne!(map.get_all("x-multi"), map.get_all("x-other"));
5304
5305    let all = map.get_all("x-multi");
5306    let mut iter = all.iter();
5307    assert_eq!(iter.size_hint(), (1, None));
5308    assert_eq!(iter.next().unwrap(), "one");
5309    assert_eq!(iter.size_hint(), (1, None));
5310    assert_eq!(iter.next_back().unwrap(), "three");
5311    assert_eq!(iter.size_hint(), (1, None));
5312    assert_eq!(iter.next().unwrap(), "two");
5313    assert_eq!(iter.size_hint(), (0, Some(0)));
5314    assert!(iter.next().is_none());
5315    assert!(iter.next_back().is_none());
5316
5317    let mut mutable: HeaderMap<String> = HeaderMap::default();
5318    mutable.append("x-multi", "one".to_owned());
5319    mutable.append("x-multi", "two".to_owned());
5320    mutable.append("x-multi", "three".to_owned());
5321
5322    let Entry::Occupied(mut entry) = mutable.entry("x-multi") else {
5323        panic!("expected x-multi header");
5324    };
5325    let mut iter = entry.iter_mut();
5326    assert_eq!(iter.size_hint(), (0, None));
5327    iter.next().unwrap().push('1');
5328    assert_eq!(iter.size_hint(), (0, None));
5329    iter.next_back().unwrap().push('3');
5330    assert_eq!(iter.size_hint(), (0, None));
5331    iter.next().unwrap().push('2');
5332    assert_eq!(iter.size_hint(), (0, None));
5333    assert!(iter.next().is_none());
5334    assert!(iter.next_back().is_none());
5335
5336    assert_eq!(
5337        mutable
5338            .get_all("x-multi")
5339            .iter()
5340            .cloned()
5341            .collect::<Vec<_>>(),
5342        ["one1".to_owned(), "two2".to_owned(), "three3".to_owned()]
5343    );
5344}
5345
5346#[test]
5347fn growth_remove_and_lookup_stress_preserves_entries_and_order() {
5348    let mut map: HeaderMap<String> = HeaderMap::with_capacity(1);
5349
5350    for i in 0..96 {
5351        let name = HeaderName::from_bytes(format!("x-grow-{i}").as_bytes()).unwrap();
5352        map.insert(name, format!("v{i}"));
5353    }
5354
5355    assert_eq!(map.len(), 96);
5356    assert_eq!(map.keys_len(), 96);
5357    assert!(map.capacity() >= 96);
5358    assert_header_map_invariants(&map);
5359
5360    for i in (0..96).step_by(3) {
5361        let name = HeaderName::from_bytes(format!("x-grow-{i}").as_bytes()).unwrap();
5362        assert_eq!(map.remove(name), Some(format!("v{i}")));
5363    }
5364
5365    assert_eq!(map.len(), 64);
5366    assert_eq!(map.keys_len(), 64);
5367    assert_header_map_invariants(&map);
5368
5369    for i in 0..96 {
5370        let name = format!("x-grow-{i}");
5371        if i % 3 == 0 {
5372            assert!(!map.contains_key(name.as_str()));
5373            assert!(map.get(name.as_str()).is_none());
5374        } else {
5375            assert_eq!(map.get(name.as_str()).unwrap(), &format!("v{i}"));
5376        }
5377    }
5378
5379    for i in (0..96).step_by(3) {
5380        let name = HeaderName::from_bytes(format!("x-grow-{i}").as_bytes()).unwrap();
5381        map.insert(name, format!("reinserted-{i}"));
5382    }
5383
5384    assert_eq!(map.len(), 96);
5385    assert_header_map_invariants(&map);
5386    assert_eq!(map.get("x-grow-0").unwrap(), "reinserted-0");
5387    assert_eq!(map.get("x-grow-95").unwrap(), "v95");
5388}
5389
5390#[test]
5391fn header_map_invariants_survive_ordered_multi_value_mutations() {
5392    let mut map = HeaderMap::new();
5393    assert_header_map_invariants(&map);
5394
5395    map.append("x-first", HeaderValue::from_static("one"));
5396    map.append("x-first", HeaderValue::from_static("two"));
5397    map.append("x-second", HeaderValue::from_static("alpha"));
5398    map.append("x-second", HeaderValue::from_static("beta"));
5399    map.insert("x-third", HeaderValue::from_static("single"));
5400    assert_header_map_invariants(&map);
5401
5402    assert_eq!(map.remove("x-first").unwrap(), "one");
5403    assert_header_map_invariants(&map);
5404
5405    map.append("x-second", HeaderValue::from_static("gamma"));
5406    assert_header_map_invariants(&map);
5407
5408    assert_eq!(
5409        map.insert("x-second", HeaderValue::from_static("replaced"))
5410            .unwrap(),
5411        "alpha"
5412    );
5413    assert_header_map_invariants(&map);
5414
5415    map.append("x-second", HeaderValue::from_static("delta"));
5416    map.append("x-fourth", HeaderValue::from_static("uno"));
5417    map.append("x-fourth", HeaderValue::from_static("dos"));
5418    assert_header_map_invariants(&map);
5419
5420    let Entry::Occupied(fourth) = map.entry("x-fourth") else {
5421        panic!("expected x-fourth header");
5422    };
5423    let (name, values) = fourth.remove_entry_mult();
5424    assert_eq!(name, "x-fourth");
5425    assert_eq!(
5426        values
5427            .map(|value| value.to_str().unwrap().to_owned())
5428            .collect::<Vec<_>>(),
5429        ["uno".to_owned(), "dos".to_owned()]
5430    );
5431    assert_header_map_invariants(&map);
5432
5433    map.append("x-fifth", HeaderValue::from_static("eins"));
5434    map.append("x-fifth", HeaderValue::from_static("zwei"));
5435    let Entry::Occupied(mut fifth) = map.entry("x-fifth") else {
5436        panic!("expected x-fifth header");
5437    };
5438    assert_eq!(
5439        fifth
5440            .insert_mult(HeaderValue::from_static("new"))
5441            .map(|value| value.to_str().unwrap().to_owned())
5442            .collect::<Vec<_>>(),
5443        ["eins".to_owned(), "zwei".to_owned()]
5444    );
5445    assert_header_map_invariants(&map);
5446
5447    let drained = map.drain().collect::<Vec<_>>();
5448    assert!(!drained.is_empty());
5449    assert_header_map_invariants(&map);
5450}
5451
5452#[test]
5453fn remove_entry_mult_then_into_ordered_iter_skips_removed_extras() {
5454    let mut map = HeaderMap::new();
5455    map.append("cookie", HeaderValue::from_static("a=1"));
5456    map.append("cookie", HeaderValue::from_static("b=2"));
5457    map.append("cookie", HeaderValue::from_static("c=3"));
5458
5459    let Entry::Occupied(cookie_headers) = map.entry("cookie") else {
5460        panic!("expected cookie header");
5461    };
5462    let (name, values) = cookie_headers.remove_entry_mult();
5463    let merged = values
5464        .map(|value| value.to_str().unwrap().to_owned())
5465        .collect::<Vec<_>>()
5466        .join("; ");
5467
5468    map.insert(name, HeaderValue::from_str(&merged).unwrap());
5469
5470    let ordered = map
5471        .into_ordered_iter()
5472        .map(|(name, value)| (name.as_str().to_owned(), value.to_str().unwrap().to_owned()))
5473        .collect::<Vec<_>>();
5474
5475    assert_eq!(ordered, [("cookie".to_owned(), "a=1; b=2; c=3".to_owned())]);
5476}
5477
5478#[test]
5479fn insert_mult_then_ordered_iter_updates_moved_extra_order_links() {
5480    let mut map = HeaderMap::new();
5481    map.append("x-first", HeaderValue::from_static("one"));
5482    map.append("x-first", HeaderValue::from_static("two"));
5483    map.append("x-second", HeaderValue::from_static("alpha"));
5484    map.append("x-second", HeaderValue::from_static("beta"));
5485
5486    let Entry::Occupied(mut entry) = map.entry("x-first") else {
5487        panic!("expected x-first header");
5488    };
5489    let old = entry
5490        .insert_mult(HeaderValue::from_static("replaced"))
5491        .map(|value| value.to_str().unwrap().to_owned())
5492        .collect::<Vec<_>>();
5493
5494    assert_eq!(old, ["one".to_owned(), "two".to_owned()]);
5495
5496    let ordered = map
5497        .ordered_iter()
5498        .map(|(name, value)| (name.as_str().to_owned(), value.to_str().unwrap().to_owned()))
5499        .collect::<Vec<_>>();
5500
5501    assert_eq!(
5502        ordered,
5503        [
5504            ("x-first".to_owned(), "replaced".to_owned()),
5505            ("x-second".to_owned(), "alpha".to_owned()),
5506            ("x-second".to_owned(), "beta".to_owned()),
5507        ]
5508    );
5509}
5510
5511#[test]
5512fn order_capacity_grows_with_entry_capacity() {
5513    let mut map = HeaderMap::<HeaderValue>::new();
5514    map.try_insert("a", HeaderValue::from_static("a")).unwrap();
5515
5516    assert!(
5517        map.order.capacity() >= map.entries.capacity(),
5518        "order capacity {} should track entries capacity {}",
5519        map.order.capacity(),
5520        map.entries.capacity()
5521    );
5522
5523    let initial_order_cap = map.order.capacity();
5524    let initial_entry_cap = map.entries.capacity();
5525
5526    for i in 0..initial_entry_cap {
5527        let name = format!("x-rama-{i}");
5528        let name = HeaderName::from_bytes(name.as_bytes()).unwrap();
5529        map.try_insert(name, HeaderValue::from_static("x")).unwrap();
5530    }
5531
5532    assert!(
5533        map.order.capacity() >= map.entries.capacity(),
5534        "order capacity {} should track grown entries capacity {}",
5535        map.order.capacity(),
5536        map.entries.capacity()
5537    );
5538    assert!(map.order.capacity() >= initial_order_cap);
5539}