viaduct

Struct Headers

source
pub struct Headers { /* private fields */ }
Expand description

A list of headers.

Implementations§

source§

impl Headers

source

pub fn new() -> Self

Initialize an empty list of headers.

source

pub fn with_capacity(c: usize) -> Self

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

source

pub fn into_vec(self) -> Vec<Header>

Convert this list of headers to a Vec

source

pub fn len(&self) -> usize

Returns the number of headers.

source

pub fn is_empty(&self) -> bool

Returns true if len() is zero.

source

pub fn clear(&mut self)

Clear this set of headers.

source

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

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.

source

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.

source

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

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

source

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

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

source

pub fn get_header<S>(&self, name: S) -> Option<&Header>

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

source

pub fn get<S>(&self, name: S) -> Option<&str>

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

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

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.

source

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 Clone for Headers

source§

fn clone(&self) -> Headers

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Headers

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Headers

source§

fn default() -> Headers

Returns the “default value” for a type. Read more
source§

impl From<Headers> for HashMap<String, String>

source§

fn from(headers: Headers) -> HashMap<String, String>

Converts to this type from the input type.
source§

impl FromIterator<Header> for Headers

source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = Header>,

Creates a value from an iterator. Read more
source§

impl<'a> IntoIterator for &'a Headers

source§

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

Which kind of iterator are we turning this into?
source§

type Item = &'a Header

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for Headers

source§

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

Which kind of iterator are we turning this into?
source§

type Item = Header

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq for Headers

source§

fn eq(&self, other: &Headers) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Headers

source§

impl StructuralPartialEq for Headers

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T