Struct HeaderMap
struct HeaderMap<T = super::HeaderValue> { ... }
A set of HTTP headers
HeaderMap is a multimap of HeaderName to values.
Examples
Basic usage
# use HeaderMap;
# use ;
let mut headers = new;
headers.insert;
headers.insert;
assert!;
assert!;
assert_eq!;
headers.remove;
assert!;
Implementations
impl HeaderMap
fn new() -> SelfCreate an empty
HeaderMap.The map will be created without any capacity. This function will not allocate.
Examples
# use HeaderMap; let map = new; assert!; assert_eq!;
impl<T> HeaderMap<T>
fn with_capacity(capacity: usize) -> HeaderMap<T>Create an empty
HeaderMapwith the specified capacity.The returned map will allocate internal storage in order to hold about
capacityelements without reallocating. However, this is a "best effort" as there are usage patterns that could cause additional allocations beforecapacityheaders are stored in the map.More capacity than requested may be allocated.
Panics
This method panics if capacity exceeds max
HeaderMapcapacity.Examples
# use HeaderMap; let map: = with_capacity; assert!; assert_eq!;fn try_with_capacity(capacity: usize) -> Result<HeaderMap<T>, MaxSizeReached>Create an empty
HeaderMapwith the specified capacity.The returned map will allocate internal storage in order to hold about
capacityelements without reallocating. However, this is a "best effort" as there are usage patterns that could cause additional allocations beforecapacityheaders are stored in the map.More capacity than requested may be allocated.
Errors
This function may return an error if
HeaderMapexceeds max capacityExamples
# use HeaderMap; let map: = try_with_capacity.unwrap; assert!; assert_eq!;fn len(self: &Self) -> usizeReturns 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
# use HeaderMap; # use ; let mut map = new; assert_eq!; map.insert; map.insert; assert_eq!; map.append; assert_eq!;fn keys_len(self: &Self) -> usizeReturns the number of keys stored in the map.
This number will be less than or equal to
len()as each key may have more than one associated value.Examples
# use HeaderMap; # use ; let mut map = new; assert_eq!; map.insert; map.insert; assert_eq!; map.insert; assert_eq!;fn is_empty(self: &Self) -> boolReturns true if the map contains no elements.
Examples
# use HeaderMap; # use HOST; let mut map = new; assert!; map.insert; assert!;fn clear(self: &mut Self)Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
Examples
# use HeaderMap; # use HOST; let mut map = new; map.insert; map.clear; assert!; assert!;fn capacity(self: &Self) -> usizeReturns 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
# use HeaderMap; # use HOST; let mut map = new; assert_eq!; map.insert; assert_eq!;fn reserve(self: &mut Self, additional: usize)Reserves capacity for at least
additionalmore headers to be inserted into theHeaderMap.The header map may reserve more space to avoid frequent reallocations. Like with
with_capacity, this will be a "best effort" to avoid allocations untiladditionalmore headers are inserted. Certain usage patterns could cause additional allocations before the number is reached.Panics
Panics if the new allocation size overflows
HeaderMapMAX_SIZE.Examples
# use HeaderMap; # use HOST; let mut map = new; map.reserve; # map.insert;fn try_reserve(self: &mut Self, additional: usize) -> Result<(), MaxSizeReached>Reserves capacity for at least
additionalmore headers to be inserted into theHeaderMap.The header map may reserve more space to avoid frequent reallocations. Like with
with_capacity, this will be a "best effort" to avoid allocations untiladditionalmore headers are inserted. Certain usage patterns could cause additional allocations before the number is reached.Errors
This method differs from
reserveby returning an error instead of panicking if the value is too large.Examples
# use HeaderMap; # use HOST; let mut map = new; map.try_reserve.unwrap; # map.try_insert.unwrap;fn get<K>(self: &Self, key: K) -> Option<&T> where K: AsHeaderNameReturns 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_allto get all values associated with a given key. ReturnsNoneif there are no values associated with the key.Examples
# use HeaderMap; # use HOST; let mut map = new; assert!; map.insert; assert_eq!; assert_eq!; map.append; assert_eq!;fn get_mut<K>(self: &mut Self, key: K) -> Option<&mut T> where K: AsHeaderNameReturns 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
entryto get all values associated with a given key. ReturnsNoneif there are no values associated with the key.Examples
# use HeaderMap; # use HOST; let mut map = default; map.insert; map.get_mut.unwrap.push_str; assert_eq!;fn get_all<K>(self: &Self, key: K) -> GetAll<'_, T> where K: AsHeaderNameReturns 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
GetAllfor more details. ReturnsNoneif there are no values associated with the key.Examples
# use HeaderMap; # use HOST; let mut map = new; map.insert; map.append; let view = map.get_all; let mut iter = view.iter; assert_eq!; assert_eq!; assert!;fn contains_key<K>(self: &Self, key: K) -> bool where K: AsHeaderNameReturns true if the map contains a value for the specified key.
Examples
# use HeaderMap; # use HOST; let mut map = new; assert!; map.insert; assert!;fn iter(self: &Self) -> Iter<'_, T>An iterator visiting all key-value pairs.
The iteration order is arbitrary, but consistent across platforms for the same crate version. Each key will be yielded once per associated value. So, if a key has 3 associated values, it will be yielded 3 times.
Examples
# use HeaderMap; # use ; let mut map = new; map.insert; map.append; map.insert; for in map.iterfn iter_mut(self: &mut Self) -> IterMut<'_, T>An iterator visiting all key-value pairs, with mutable value references.
The iterator order is arbitrary, but consistent across platforms for the same crate version. Each key will be yielded once per associated value, so if a key has 3 associated values, it will be yielded 3 times.
Examples
# use HeaderMap; # use ; let mut map = default; map.insert; map.append; map.insert; for in map.iter_mutfn keys(self: &Self) -> Keys<'_, T>An iterator visiting all keys.
The iteration order is arbitrary, but consistent across platforms for the same crate version. Each key will be yielded only once even if it has multiple associated values.
Examples
# use HeaderMap; # use ; let mut map = new; map.insert; map.append; map.insert; for key in map.keysfn values(self: &Self) -> Values<'_, T>An iterator visiting all values.
The iteration order is arbitrary, but consistent across platforms for the same crate version.
Examples
# use HeaderMap; # use ; let mut map = new; map.insert; map.append; map.insert; for value in map.valuesfn values_mut(self: &mut Self) -> ValuesMut<'_, T>An iterator visiting all values mutably.
The iteration order is arbitrary, but consistent across platforms for the same crate version.
Examples
# use HeaderMap; # use ; let mut map = default; map.insert; map.append; map.insert; for value in map.values_mutfn drain(self: &mut Self) -> Drain<'_, T>Clears the map, returning all entries as an iterator.
The internal memory is kept for reuse.
For each yielded item that has
Noneprovided for theHeaderName, then the associated header name is the same as that of the previously yielded item. The first yielded item will haveHeaderNameset.Examples
# use HeaderMap; # use ; let mut map = new; map.insert; map.append; map.insert; let mut drain = map.drain; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn entry<K>(self: &mut Self, key: K) -> Entry<'_, T> where K: IntoHeaderNameGets the given key's corresponding entry in the map for in-place manipulation.
Panics
This method panics if capacity exceeds max
HeaderMapcapacityExamples
# use HeaderMap; let mut map: = default; let headers = &; for &header in headers assert_eq!; assert_eq!;fn try_entry<K>(self: &mut Self, key: K) -> Result<Entry<'_, T>, InvalidHeaderName> where K: AsHeaderNameGets the given key's corresponding entry in the map for in-place manipulation.
Errors
This method differs from
entryby allowing types that may not be validHeaderNames to passed as the key (such asString). If they do not parse as a validHeaderName, this returns anInvalidHeaderNameerror.If reserving space goes over the maximum, this will also return an error. However, to prevent breaking changes to the return type, the error will still say
InvalidHeaderName, unlike othertry_*methods which return aMaxSizeReachederror.fn insert<K>(self: &mut Self, key: K, val: T) -> Option<T> where K: IntoHeaderNameInserts a key-value pair into the map.
If the map did not previously have this key present, then
Noneis returned.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_multonOccupiedEntryfor an API that returns all values.The key is not updated, though; this matters for types that can be
==without being identical.Panics
This method panics if capacity exceeds max
HeaderMapcapacityExamples
# use HeaderMap; # use HOST; let mut map = new; assert!; assert!; let mut prev = map.insert.unwrap; assert_eq!;fn try_insert<K>(self: &mut Self, key: K, val: T) -> Result<Option<T>, MaxSizeReached> where K: IntoHeaderNameInserts a key-value pair into the map.
If the map did not previously have this key present, then
Noneis returned.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_multonOccupiedEntryfor an API that returns all values.The key is not updated, though; this matters for types that can be
==without being identical.Errors
This function may return an error if
HeaderMapexceeds max capacityExamples
# use HeaderMap; # use HOST; let mut map = new; assert!; assert!; let mut prev = map.try_insert.unwrap.unwrap; assert_eq!;fn append<K>(self: &mut Self, key: K, value: T) -> bool where K: IntoHeaderNameInserts a key-value pair into the map.
If the map did not previously have this key present, then
falseis returned.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.Panics
This method panics if capacity exceeds max
HeaderMapcapacityExamples
# use HeaderMap; # use HOST; let mut map = new; assert!; assert!; map.append; let values = map.get_all; let mut i = values.iter; assert_eq!; assert_eq!;fn try_append<K>(self: &mut Self, key: K, value: T) -> Result<bool, MaxSizeReached> where K: IntoHeaderNameInserts a key-value pair into the map.
If the map did not previously have this key present, then
falseis returned.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.Errors
This function may return an error if
HeaderMapexceeds max capacityExamples
# use HeaderMap; # use HOST; let mut map = new; assert!; assert!; map.try_append.unwrap; let values = map.get_all; let mut i = values.iter; assert_eq!; assert_eq!;fn remove<K>(self: &mut Self, key: K) -> Option<T> where K: AsHeaderNameRemoves a key from the map, returning the value associated with the key.
Returns
Noneif the map does not contain the key. If there are multiple values associated with the key, then the first one is returned. Seeremove_entry_multonOccupiedEntryfor an API that yields all values.Examples
# use HeaderMap; # use HOST; let mut map = new; map.insert; let prev = map.remove.unwrap; assert_eq!; assert!;
impl<'a, K, V, S, T> TryFrom for HeaderMap<T>
fn try_from(c: &'a HashMap<K, V, S>) -> Result<Self, <Self as >::Error>
impl<K, T> Index for HeaderMap<T>
fn index(self: &Self, index: K) -> &TPanics
Using the index operator will cause a panic if the header you're querying isn't set.
impl<T> Any for HeaderMap<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for HeaderMap<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for HeaderMap<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for HeaderMap<T>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> Default for HeaderMap<T>
fn default() -> Self
impl<T> Extend for HeaderMap<T>
fn extend<I: IntoIterator<Item = (HeaderName, T)>>(self: &mut Self, iter: I)
impl<T> Extend for HeaderMap<T>
fn extend<I: IntoIterator<Item = (Option<HeaderName>, T)>>(self: &mut Self, iter: I)Extend a
HeaderMapwith the contents of anotherHeaderMap.This function expects the yielded items to follow the same structure as
IntoIter.Panics
This panics if the first yielded item does not have a
HeaderName.Examples
# use *; let mut map = new; map.insert; map.insert; let mut extra = new; extra.insert; extra.insert; extra.append; map.extend; assert_eq!; assert_eq!; assert_eq!; let v = map.get_all; assert_eq!; let v = map.get_all; assert_eq!;
impl<T> Freeze for HeaderMap<T>
impl<T> From for HeaderMap<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> FromIterator for HeaderMap<T>
fn from_iter<I>(iter: I) -> Self where I: IntoIterator<Item = (HeaderName, T)>
impl<T> IntoIterator for HeaderMap<T>
fn into_iter(self: Self) -> IntoIter<T>Creates a consuming iterator, that is, one that moves keys and values out of the map in arbitrary order. The map cannot be used after calling this.
For each yielded item that has
Noneprovided for theHeaderName, then the associated header name is the same as that of the previously yielded item. The first yielded item will haveHeaderNameset.Examples
Basic usage.
# use header; # use *; let mut map = new; map.insert; map.insert; let mut iter = map.into_iter; assert_eq!; assert_eq!; assert!;Multiple values per key.
# use header; # use *; let mut map = new; map.append; map.append; map.append; map.append; map.append; let mut iter = map.into_iter; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert!;
impl<T> RefUnwindSafe for HeaderMap<T>
impl<T> Send for HeaderMap<T>
impl<T> Sync for HeaderMap<T>
impl<T> ToOwned for HeaderMap<T>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> Unpin for HeaderMap<T>
impl<T> UnwindSafe for HeaderMap<T>
impl<T, U> Into for HeaderMap<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for HeaderMap<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for HeaderMap<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: $crate::clone::Clone> Clone for HeaderMap<T>
fn clone(self: &Self) -> HeaderMap<T>
impl<T: Eq> Eq for HeaderMap<T>
impl<T: PartialEq> PartialEq for HeaderMap<T>
fn eq(self: &Self, other: &HeaderMap<T>) -> bool
impl<T: fmt::Debug> Debug for HeaderMap<T>
fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::Result