pub struct Headers { /* private fields */ }
Expand description
A list of headers.
Implementations§
source§impl Headers
impl Headers
sourcepub fn with_capacity(c: usize) -> Self
pub fn with_capacity(c: usize) -> Self
Initialize an empty list of headers backed by a vector with the provided capacity.
sourcepub fn insert<N, V>(&mut self, name: N, value: V) -> Result<&mut Self, Error>
pub fn insert<N, V>(&mut self, name: N, value: V) -> Result<&mut Self, Error>
Insert or update a new header.
This returns an error if you attempt to specify a header with an invalid value (values must be printable ASCII and may not contain \r or \n)
§Example
let mut h = Headers::new();
h.insert("My-Cool-Header", "example")?;
assert_eq!(h.get("My-Cool-Header"), Some("example"));
// Note: names are sensitive
assert_eq!(h.get("my-cool-header"), Some("example"));
// Also note, constants for headers are in `viaduct::header_names`, and
// you can chain the result of this function.
h.insert(viaduct::header_names::CONTENT_TYPE, "something...")?
.insert("Something-Else", "etc")?;
sourcepub fn insert_if_missing<N, V>(
&mut self,
name: N,
value: V,
) -> Result<&mut Self, Error>
pub fn insert_if_missing<N, V>( &mut self, name: N, value: V, ) -> Result<&mut Self, Error>
Insert the provided header unless a header is already specified.
Mostly used internally, e.g. to set “Content-Type: application/json”
in Request::json()
unless it has been set specifically.
sourcepub fn insert_header(&mut self, new: Header) -> &mut Self
pub fn insert_header(&mut self, new: Header) -> &mut Self
Insert or update a header directly. Typically you will want to use
insert
over this, as it performs less work if the header needs
updating instead of insertion.
sourcepub fn extend<I>(&mut self, iter: I) -> &mut Selfwhere
I: IntoIterator<Item = Header>,
pub fn extend<I>(&mut self, iter: I) -> &mut Selfwhere
I: IntoIterator<Item = Header>,
Add all the headers in the provided iterator to this list of headers.
sourcepub fn try_extend<I, E>(&mut self, iter: I) -> Result<&mut Self, E>
pub fn try_extend<I, E>(&mut self, iter: I) -> Result<&mut Self, E>
Add all the headers in the provided iterator, unless any of them are Err.
sourcepub fn get_header<S>(&self, name: S) -> Option<&Header>where
S: PartialEq<HeaderName>,
pub fn get_header<S>(&self, name: S) -> Option<&Header>where
S: PartialEq<HeaderName>,
Get the header object with the requested name. Usually, you will
want to use get()
or get_as::<T>()
instead.
sourcepub fn get<S>(&self, name: S) -> Option<&str>where
S: PartialEq<HeaderName>,
pub fn get<S>(&self, name: S) -> Option<&str>where
S: PartialEq<HeaderName>,
Get the value of the header with the provided name.
See also get_as
.
§Example
let mut h = Headers::new();
h.insert(CONTENT_TYPE, "application/json")?;
assert_eq!(h.get(CONTENT_TYPE), Some("application/json"));
assert_eq!(h.get("Something-Else"), None);
sourcepub fn get_as<T, S>(&self, name: S) -> Option<Result<T, <T as FromStr>::Err>>
pub fn get_as<T, S>(&self, name: S) -> Option<Result<T, <T as FromStr>::Err>>
Get the value of the header with the provided name, and
attempt to parse it using std::str::FromStr
.
- If the header is missing, it returns None.
- If the header is present but parsing failed, returns
Some(Err(<error returned by parsing>))
. - Otherwise, returns
Some(Ok(result))
.
Note that if Option<Result<T, E>>
is inconvenient for you,
and you wish this returned Result<Option<T>, E>
, you may use
the built-in transpose()
method to convert between them.
let mut h = Headers::new();
h.insert("Example", "1234")?.insert("Illegal", "abcd")?;
let v: Option<Result<i64, _>> = h.get_as("Example");
assert_eq!(v, Some(Ok(1234)));
assert_eq!(h.get_as::<i64, _>("Example"), Some(Ok(1234)));
assert_eq!(h.get_as::<i64, _>("Illegal"), Some("abcd".parse::<i64>()));
assert_eq!(h.get_as::<i64, _>("Something-Else"), None);
sourcepub fn try_get<T, S>(&self, name: S) -> Option<T>
pub fn try_get<T, S>(&self, name: S) -> Option<T>
Get the value of the header with the provided name, and
attempt to parse it using std::str::FromStr
.
This is a variant of get_as
that returns None on error,
intended to be used for cases where missing and invalid
headers should be treated the same. (With get_as
this
requires h.get_as(...).and_then(|r| r.ok())
, which is
somewhat opaque.
sourcepub fn iter(&self) -> <&Headers as IntoIterator>::IntoIter
pub fn iter(&self) -> <&Headers as IntoIterator>::IntoIter
Get an iterator over the headers in no particular order.
Note that we also implement IntoIterator.
Trait Implementations§
source§impl FromIterator<Header> for Headers
impl FromIterator<Header> for Headers
source§impl<'a> IntoIterator for &'a Headers
impl<'a> IntoIterator for &'a Headers
source§impl IntoIterator for Headers
impl IntoIterator for Headers
impl Eq for Headers
impl StructuralPartialEq for Headers
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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.