Skip to content

Commit

Permalink
Merge pull request #529 from http-rs/index
Browse files Browse the repository at this point in the history
Impl Index for Request,Response
  • Loading branch information
yoshuawuyts authored May 22, 2020
2 parents 204268a + 94e7e0d commit 11c1272
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -377,6 +378,34 @@ impl<'a, State> IntoIterator for &'a mut Request<State> {
}
}

impl<State> Index<HeaderName> for Request<State> {
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<State> Index<&str> for Request<State> {
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()
Expand Down
29 changes: 29 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use async_std::io::prelude::*;
use std::convert::TryFrom;
use std::ops::Index;

use mime::Mime;
use serde::Serialize;
Expand Down Expand Up @@ -324,3 +325,31 @@ impl<'a> IntoIterator for &'a mut Response {
self.res.iter_mut()
}
}

impl Index<HeaderName> 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]
}
}

0 comments on commit 11c1272

Please sign in to comment.