diff --git a/src/request.rs b/src/request.rs index a262e3372..9994227a8 100644 --- a/src/request.rs +++ b/src/request.rs @@ -4,6 +4,7 @@ use serde::Deserialize; use async_std::io::{self, prelude::*, BufReader}; use async_std::task::{Context, Poll}; +use std::ops::Index; use std::pin::Pin; use std::{str::FromStr, sync::Arc}; @@ -377,6 +378,34 @@ impl<'a, State> IntoIterator for &'a mut Request { } } +impl Index for Request { + type Output = HeaderValues; + + /// Returns a reference to the value corresponding to the supplied name. + /// + /// # Panics + /// + /// Panics if the name is not present in `Request`. + #[inline] + fn index(&self, name: HeaderName) -> &HeaderValues { + &self.request[name] + } +} + +impl Index<&str> for Request { + type Output = HeaderValues; + + /// Returns a reference to the value corresponding to the supplied name. + /// + /// # Panics + /// + /// Panics if the name is not present in `Request`. + #[inline] + fn index(&self, name: &str) -> &HeaderValues { + &self.request[name] + } +} + pub(crate) fn rest(route_params: &[Params]) -> Option<&str> { route_params .last() diff --git a/src/response.rs b/src/response.rs index 620ac2fba..2d043e6ee 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,5 +1,6 @@ use async_std::io::prelude::*; use std::convert::TryFrom; +use std::ops::Index; use mime::Mime; use serde::Serialize; @@ -324,3 +325,31 @@ impl<'a> IntoIterator for &'a mut Response { self.res.iter_mut() } } + +impl Index for Response { + type Output = HeaderValues; + + /// Returns a reference to the value corresponding to the supplied name. + /// + /// # Panics + /// + /// Panics if the name is not present in `Response`. + #[inline] + fn index(&self, name: HeaderName) -> &HeaderValues { + &self.res[name] + } +} + +impl Index<&str> for Response { + type Output = HeaderValues; + + /// Returns a reference to the value corresponding to the supplied name. + /// + /// # Panics + /// + /// Panics if the name is not present in `Response`. + #[inline] + fn index(&self, name: &str) -> &HeaderValues { + &self.res[name] + } +}