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