Coverage Report

Created: 2026-06-15 06:09

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