http_types/headers/
values.rs

1use std::collections::hash_map;
2use std::iter::Iterator;
3
4use crate::headers::{HeaderName, HeaderValue, HeaderValues};
5
6/// Iterator over the header values.
7#[derive(Debug)]
8pub struct Values<'a> {
9    pub(super) inner: Option<hash_map::Values<'a, HeaderName, HeaderValues>>,
10    slot: Option<&'a HeaderValues>,
11    cursor: usize,
12}
13
14impl<'a> Values<'a> {
15    /// Constructor for `Headers`.
16    pub(crate) fn new(inner: hash_map::Values<'a, HeaderName, HeaderValues>) -> Self {
17        Self {
18            inner: Some(inner),
19            slot: None,
20            cursor: 0,
21        }
22    }
23
24    /// Constructor for `HeaderValues`.
25    pub(crate) fn new_values(values: &'a HeaderValues) -> Self {
26        Self {
27            inner: None,
28            slot: Some(values),
29            cursor: 0,
30        }
31    }
32}
33
34impl<'a> Iterator for Values<'a> {
35    type Item = &'a HeaderValue;
36
37    fn next(&mut self) -> Option<Self::Item> {
38        loop {
39            // Check if we have a vec in the current slot, and if not set one.
40            if self.slot.is_none() {
41                let next = match self.inner.as_mut() {
42                    Some(inner) => inner.next()?,
43                    None => return None,
44                };
45                self.cursor = 0;
46                self.slot = Some(next);
47            }
48
49            // Get the next item
50            match self.slot.unwrap().get(self.cursor) {
51                // If an item is found, increment the cursor and return the item.
52                Some(item) => {
53                    self.cursor += 1;
54                    return Some(item);
55                }
56                // If no item is found, unset the slot and loop again.
57                None => {
58                    self.slot = None;
59                    continue;
60                }
61            }
62        }
63    }
64
65    #[inline]
66    fn size_hint(&self) -> (usize, Option<usize>) {
67        match self.inner.as_ref() {
68            Some(inner) => inner.size_hint(),
69            None => (0, None),
70        }
71    }
72}