From 94e7e0de69c7a46fd084a72078cf07167e7a189c Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Sat, 23 May 2020 01:45:32 +0200 Subject: [PATCH] Impl Index for Request,Response --- src/request.rs | 29 +++++++++++++++++++++++++++++ src/response.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) 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] + } +}