pub struct HeaderMap { /* private fields */ }
Expand description
This is a multimap representing HTTP headers. Not that this is not a true hashmap, as the count of headers is generally too small to be worth representing as a map.
Implementations§
Source§impl HeaderMap
impl HeaderMap
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty HeaderMap
.
The map will be created without any capacity. This function will not allocate.
§Examples
let map = HeaderMap::new();
assert!(map.is_empty());
assert_eq!(0, map.capacity());
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create an empty HeaderMap
with the specified capacity.
The returned map will allocate internal storage in order to hold about
capacity
elements without reallocating. However, this is a “best
effort” as there are usage patterns that could cause additional
allocations before capacity
headers are stored in the map.
More capacity than requested may be allocated.
§Examples
let map: HeaderMap<u32> = HeaderMap::with_capacity(10);
assert!(map.is_empty());
assert_eq!(12, map.capacity());
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of headers the map can hold without reallocating.
This number is an approximation as certain usage patterns could cause additional allocations before the returned capacity is filled.
§Examples
let mut map = HeaderMap::new();
assert_eq!(0, map.capacity());
map.insert(HOST, "hello.world".parse().unwrap());
assert_eq!(6, map.capacity());
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more headers to be inserted
into the HeaderMap
.
The header map may reserve more space to avoid frequent reallocations.
Like with with_capacity
, this will be a “best effort” to avoid
allocations until additional
more headers are inserted. Certain usage
patterns could cause additional allocations before the number is
reached.
§Panics
Panics if the new allocation size overflows usize
.
§Examples
let mut map = HeaderMap::new();
map.reserve(10);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
§Examples
let mut map = HeaderMap::new();
map.insert(HOST, "hello.world".parse().unwrap());
map.clear();
assert!(map.is_empty());
assert!(map.capacity() > 0);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
§Examples
let mut map = HeaderMap::new();
assert!(map.is_empty());
map.insert(HOST, "hello.world".parse().unwrap());
assert!(!map.is_empty());
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of headers stored in the map.
This number represents the total number of values stored in the map. This number can be greater than or equal to the number of keys stored given that a single key may have more than one associated value.
§Examples
let mut map = HeaderMap::new();
assert_eq!(0, map.len());
map.insert(ACCEPT, "text/plain".parse().unwrap());
map.insert(HOST, "localhost".parse().unwrap());
assert_eq!(2, map.len());
map.append(ACCEPT, "text/html".parse().unwrap());
assert_eq!(3, map.len());
Sourcepub fn append(&mut self, name: impl AsRef<str>, value: impl Into<String>)
pub fn append(&mut self, name: impl AsRef<str>, value: impl Into<String>)
Appends a key-value pair into the map.
If the map did have this key present, the new value is pushed to the end
of the list of values currently associated with the key. The key is not
updated, though; this matters for types that can be ==
without being
identical.
§Examples
let mut map = HeaderMap::new();
assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
assert!(!map.is_empty());
map.append(HOST, "earth".parse().unwrap());
let values = map.get_all("host");
let mut i = values.iter();
assert_eq!("world", *i.next().unwrap());
assert_eq!("earth", *i.next().unwrap());
Sourcepub fn append_static(
&mut self,
name: impl AsRef<str>,
value: impl Into<&'static str>,
)
pub fn append_static( &mut self, name: impl AsRef<str>, value: impl Into<&'static str>, )
Appends a key-value pair into the map with a static value.
If the map did have this key present, the new value is pushed to the end
of the list of values currently associated with the key. The key is not
updated, though; this matters for types that can be ==
without being
identical.
§Examples
let mut map = HeaderMap::new();
assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
assert!(!map.is_empty());
map.append(HOST, "earth".parse().unwrap());
let values = map.get_all("host");
let mut i = values.iter();
assert_eq!("world", *i.next().unwrap());
assert_eq!("earth", *i.next().unwrap());
Sourcepub fn append_typed<H: TypedHeader>(&mut self, header: &H)
pub fn append_typed<H: TypedHeader>(&mut self, header: &H)
Appends a typed key-value pair into the map.
If the map did have this key present, the new value is pushed to the end
of the list of values currently associated with the key. The key is not
updated, though; this matters for types that can be ==
without being
identical.
§Examples
let mut map = HeaderMap::new();
assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
assert!(!map.is_empty());
map.append(HOST, "earth".parse().unwrap());
let values = map.get_all("host");
let mut i = values.iter();
assert_eq!("world", *i.next().unwrap());
assert_eq!("earth", *i.next().unwrap());
Sourcepub fn insert(
&mut self,
name: impl AsRef<str>,
value: impl Into<String>,
) -> Option<Cow<'static, str>>
pub fn insert( &mut self, name: impl AsRef<str>, value: impl Into<String>, ) -> Option<Cow<'static, str>>
Inserts a key-value pair into the map.
If the map did have this key present, the new value is associated with
the key and all previous values are removed. Note that only a single
one of the previous values is returned. If there are multiple values
that have been previously associated with the key, then the first one is
returned. See insert_mult
on OccupiedEntry
for an API that returns
all values.
The key is not updated, though; this matters for types that can be ==
without being identical.
§Examples
let mut map = HeaderMap::new();
assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
assert!(!map.is_empty());
let mut prev = map.insert(HOST, "earth".parse().unwrap()).unwrap();
assert_eq!("world", prev);
Sourcepub fn insert_static(
&mut self,
name: impl AsRef<str>,
value: impl Into<&'static str>,
) -> Option<Cow<'static, str>>
pub fn insert_static( &mut self, name: impl AsRef<str>, value: impl Into<&'static str>, ) -> Option<Cow<'static, str>>
Inserts a key-value pair into the map with a static value.
If the map did have this key present, the new value is associated with
the key and all previous values are removed. Note that only a single
one of the previous values is returned. If there are multiple values
that have been previously associated with the key, then the first one is
returned. See insert_mult
on OccupiedEntry
for an API that returns
all values.
The key is not updated, though; this matters for types that can be ==
without being identical.
§Examples
let mut map = HeaderMap::new();
assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
assert!(!map.is_empty());
let mut prev = map.insert(HOST, "earth".parse().unwrap()).unwrap();
assert_eq!("world", prev);
Sourcepub fn insert_typed<H: TypedHeader>(
&mut self,
header: &H,
) -> Option<Cow<'static, str>>
pub fn insert_typed<H: TypedHeader>( &mut self, header: &H, ) -> Option<Cow<'static, str>>
Inserts a typed key-value pair into the map.
Note that if the header is a TypedHeader with multiple values returned, only the last value is used.
If the map did have this key present, the new value is associated with
the key and all previous values are removed. Note that only a single
one of the previous values is returned. If there are multiple values
that have been previously associated with the key, then the first one is
returned. See insert_mult
on OccupiedEntry
for an API that returns
all values.
The key is not updated, though; this matters for types that can be ==
without being identical.
§Examples
let mut map = HeaderMap::new();
assert!(map.insert(HOST, "world".parse().unwrap()).is_none());
assert!(!map.is_empty());
let mut prev = map.insert(HOST, "earth".parse().unwrap()).unwrap();
assert_eq!("world", prev);
Sourcepub fn contains_key(&self, name: &str) -> bool
pub fn contains_key(&self, name: &str) -> bool
Returns true if the map contains a value for the specified key.
§Examples
let mut map = HeaderMap::new();
assert!(!map.contains_key(HOST));
map.insert(HOST, "world".parse().unwrap());
assert!(map.contains_key("host"));
Sourcepub fn get(&self, name: &str) -> Option<&str>
pub fn get(&self, name: &str) -> Option<&str>
Returns a reference to the value associated with the key.
If there are multiple values associated with the key, then the first one
is returned. Use get_all
to get all values associated with a given
key. Returns None
if there are no values associated with the key.
§Examples
let mut map = HeaderMap::new();
assert!(map.get("host").is_none());
map.insert(HOST, "hello".parse().unwrap());
assert_eq!(map.get(HOST).unwrap(), &"hello");
assert_eq!(map.get("host").unwrap(), &"hello");
map.append(HOST, "world".parse().unwrap());
assert_eq!(map.get("host").unwrap(), &"hello");
Sourcepub fn get_typed<H: TypedHeader>(&self) -> Option<H>
pub fn get_typed<H: TypedHeader>(&self) -> Option<H>
Returns a reference to the value associated with the key.
The key is defined with an associated type referenced a TypedHeader. If the header is malformed, None is transparently returned
Note that this causes an allocation depending on the implementation of the TypedHeader.
If there are multiple values associated with the key, then the first one
is returned. Use get_all
to get all values associated with a given
key. Returns None
if there are no values associated with the key.
§Examples
let mut map = HeaderMap::new();
assert!(map.get::<Host>().is_none());
map.insert(HOST, "hello".parse().unwrap());
assert_eq!(map.get::<Host>().unwrap(), &"hello");
map.append(HOST, "world".parse().unwrap());
assert_eq!(map.get::<Host>().unwrap(), &"hello");
Sourcepub fn get_all<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a str>
pub fn get_all<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a str>
Returns a view of all values associated with a key.
The returned view does not incur any allocations and allows iterating
the values associated with the key. See [GetAll
] for more details.
Returns None
if there are no values associated with the key.
§Examples
let mut map = HeaderMap::new();
map.insert(HOST, "hello".parse().unwrap());
map.append(HOST, "goodbye".parse().unwrap());
let view = map.get_all("host");
let mut iter = view.iter();
assert_eq!(&"hello", iter.next().unwrap());
assert_eq!(&"goodbye", iter.next().unwrap());
assert!(iter.next().is_none());
Sourcepub fn get_all_typed<'a, H: TypedHeader>(
&'a self,
) -> impl Iterator<Item = H> + 'a
pub fn get_all_typed<'a, H: TypedHeader>( &'a self, ) -> impl Iterator<Item = H> + 'a
Returns a view of all values associated with a key.
The key is defined with an associated type referenced a TypedHeader. If the header is malformed, it is skipped.
Note that this causes an allocation for each header value returned depending on the implementation of the TypedHeader.
The returned view does not incur any allocations and allows iterating
the values associated with the key. See [GetAll
] for more details.
Returns None
if there are no values associated with the key.
§Examples
let mut map = HeaderMap::new();
map.insert(HOST, "hello".parse().unwrap());
map.append(HOST, "goodbye".parse().unwrap());
let view = map.get_all("host");
let mut iter = view.iter();
assert_eq!(&"hello", iter.next().unwrap());
assert_eq!(&"goodbye", iter.next().unwrap());
assert!(iter.next().is_none());
Sourcepub fn get_mut(&mut self, name: &str) -> Option<&mut Cow<'static, str>>
pub fn get_mut(&mut self, name: &str) -> Option<&mut Cow<'static, str>>
Returns a mutable reference to the value associated with the key.
If there are multiple values associated with the key, then the first one
is returned. Use entry
to get all values associated with a given
key. Returns None
if there are no values associated with the key.
§Examples
let mut map = HeaderMap::default();
map.insert(HOST, "hello".to_string());
map.get_mut("host").unwrap().push_str("-world");
assert_eq!(map.get(HOST).unwrap(), &"hello-world");
Sourcepub fn remove(&mut self, name: impl AsRef<str>) -> Vec<Cow<'static, str>>
pub fn remove(&mut self, name: impl AsRef<str>) -> Vec<Cow<'static, str>>
Removes a key from the map, returning the value associated with the key.
Returns an empty vec if the map does not contain the key. If there are multiple values associated with the key, then all are returned.
§Examples
let mut map = HeaderMap::new();
map.insert(HOST, "hello.world".parse().unwrap());
let prev = map.remove(HOST);
assert_eq!("hello.world", &prev[0]);
assert!(map.remove(HOST).is_empty());
Sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &str)>
pub fn iter(&self) -> impl Iterator<Item = (&str, &str)>
An iterator visiting all key-value pairs.
The iteration order is in insertion order.
§Examples
let mut map = HeaderMap::new();
map.insert(HOST, "hello".parse().unwrap());
map.append(HOST, "goodbye".parse().unwrap());
map.insert(CONTENT_LENGTH, "123".parse().unwrap());
for (key, value) in map.iter() {
println!("{:?}: {:?}", key, value);
}
pub fn grouped(&self) -> Vec<(&str, SmallVec<[&str; 2]>)>
Trait Implementations§
Source§impl<K: Into<Cow<'static, str>>, V: Into<String>> Extend<(K, V)> for HeaderMap
impl<K: Into<Cow<'static, str>>, V: Into<String>> Extend<(K, V)> for HeaderMap
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'a> IntoIterator for &'a HeaderMap
impl<'a> IntoIterator for &'a HeaderMap
Source§impl<'a> IntoIterator for &'a mut HeaderMap
impl<'a> IntoIterator for &'a mut HeaderMap
Source§impl IntoIterator for HeaderMap
impl IntoIterator for HeaderMap
impl Eq for HeaderMap
impl StructuralPartialEq for HeaderMap
Auto Trait Implementations§
impl Freeze for HeaderMap
impl RefUnwindSafe for HeaderMap
impl Send for HeaderMap
impl Sync for HeaderMap
impl Unpin for HeaderMap
impl UnwindSafe for HeaderMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.