[][src]Struct viaduct::Headers

pub struct Headers {
    headers: Vec<Header>,
}

A list of headers.

Fields

headers: Vec<Header>

Implementations

impl Headers[src]

pub fn new() -> Self[src]

Initialize an empty list of headers.

pub fn with_capacity(c: usize) -> Self[src]

Initialize an empty list of headers backed by a vector with the provided capacity.

pub fn into_vec(self) -> Vec<Header>[src]

Convert this list of headers to a Vec

pub fn len(&self) -> usize[src]

Returns the number of headers.

pub fn is_empty(&self) -> bool[src]

Returns true if len() is zero.

pub fn clear(&mut self)[src]

Clear this set of headers.

pub fn insert<N, V>(&mut self, name: N, value: V) -> Result<&mut Self, Error> where
    N: Into<HeaderName> + PartialEq<HeaderName>,
    V: Into<String> + AsRef<str>, 
[src]

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")?;

pub fn insert_if_missing<N, V>(
    &mut self,
    name: N,
    value: V
) -> Result<&mut Self, Error> where
    N: Into<HeaderName> + PartialEq<HeaderName>,
    V: Into<String> + AsRef<str>, 
[src]

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.

pub fn insert_header(&mut self, new: Header) -> &mut Self[src]

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.

pub fn extend<I>(&mut self, iter: I) -> &mut Self where
    I: IntoIterator<Item = Header>, 
[src]

Add all the headers in the provided iterator to this list of headers.

pub fn try_extend<I, E>(&mut self, iter: I) -> Result<&mut Self, E> where
    I: IntoIterator<Item = Result<Header, E>>, 
[src]

Add all the headers in the provided iterator, unless any of them are Err.

pub fn get_header<S>(&self, name: S) -> Option<&Header> where
    S: PartialEq<HeaderName>, 
[src]

Get the header object with the requested name. Usually, you will want to use get() or get_as::<T>() instead.

pub fn get<S>(&self, name: S) -> Option<&str> where
    S: PartialEq<HeaderName>, 
[src]

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);

pub fn get_as<T, S>(&self, name: S) -> Option<Result<T, <T as FromStr>::Err>> where
    T: FromStr,
    S: PartialEq<HeaderName>, 
[src]

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);

pub fn try_get<T, S>(&self, name: S) -> Option<T> where
    T: FromStr,
    S: PartialEq<HeaderName>, 
[src]

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.

pub fn iter(&self) -> <&Headers as IntoIterator>::IntoIter[src]

Get an iterator over the headers in no particular order.

Note that we also implement IntoIterator.

Trait Implementations

impl Clone for Headers[src]

impl Debug for Headers[src]

impl Default for Headers[src]

impl From<Headers> for HashMap<String, String>[src]

impl FromIterator<Header> for Headers[src]

impl IntoIterator for Headers[src]

type IntoIter = <Vec<Header> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?

type Item = Header

The type of the elements being iterated over.

impl<'a> IntoIterator for &'a Headers[src]

type IntoIter = <&'a [Header] as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?

type Item = &'a Header

The type of the elements being iterated over.

impl PartialEq<Headers> for Headers[src]

impl StructuralPartialEq for Headers[src]

Auto Trait Implementations

impl RefUnwindSafe for Headers

impl Send for Headers

impl Sync for Headers

impl Unpin for Headers

impl UnwindSafe for Headers

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.