pub struct Headers(pub HeaderMap);Expand description
Headers extractor
Provides access to all request headers as a typed map.
§Example
use rustapi_core::extract::Headers;
async fn handler(headers: Headers) -> impl IntoResponse {
if let Some(content_type) = headers.get("content-type") {
format!("Content-Type: {:?}", content_type)
} else {
"No Content-Type header".to_string()
}
}Tuple Fields§
§0: HeaderMapImplementations§
Methods from Deref<Target = HeaderMap>§
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 keys_len(&self) -> usize
pub fn keys_len(&self) -> usize
Returns 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
let mut map = HeaderMap::new();
assert_eq!(0, map.keys_len());
map.insert(ACCEPT, "text/plain".parse().unwrap());
map.insert(HOST, "localhost".parse().unwrap());
assert_eq!(2, map.keys_len());
map.insert(ACCEPT, "text/html".parse().unwrap());
assert_eq!(2, map.keys_len());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 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 get<K>(&self, key: K) -> Option<&T>where
K: AsHeaderName,
pub fn get<K>(&self, key: K) -> Option<&T>where
K: AsHeaderName,
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_all<K>(&self, key: K) -> GetAll<'_, T>where
K: AsHeaderName,
pub fn get_all<K>(&self, key: K) -> GetAll<'_, T>where
K: AsHeaderName,
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 contains_key<K>(&self, key: K) -> boolwhere
K: AsHeaderName,
pub fn contains_key<K>(&self, key: K) -> boolwhere
K: AsHeaderName,
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 iter(&self) -> Iter<'_, T>
pub fn iter(&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
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);
}Sourcepub fn keys(&self) -> Keys<'_, T>
pub fn keys(&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
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 in map.keys() {
println!("{:?}", key);
}Sourcepub fn values(&self) -> Values<'_, T>
pub fn values(&self) -> Values<'_, T>
An iterator visiting all values.
The iteration order is arbitrary, but consistent across platforms for the same crate version.
§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 value in map.values() {
println!("{:?}", value);
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Headers
impl RefUnwindSafe for Headers
impl Send for Headers
impl Sync for Headers
impl Unpin for Headers
impl UnwindSafe for Headers
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<T> FromRequest for Twhere
T: FromRequestParts,
impl<T> FromRequest for Twhere
T: FromRequestParts,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more