From 8071cfa8bff10092e54a1d5e9495cd9e93465673 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Sat, 29 Nov 2014 13:55:50 -0800 Subject: [PATCH] feat(headers): header ergonomics - Reexports all common::* as header::* - Most headers implement Deref where it makes sense. Closes #156 --- src/client/request.rs | 2 +- src/header/common/accept.rs | 2 ++ src/header/common/authorization.rs | 12 ++++++++++++ src/header/common/connection.rs | 2 ++ src/header/common/content_length.rs | 6 ++++-- src/header/common/content_type.rs | 2 ++ src/header/common/cookie.rs | 2 ++ src/header/common/date.rs | 2 ++ src/header/common/location.rs | 2 ++ src/header/common/mod.rs | 16 ++++++++++++++++ src/header/common/server.rs | 2 ++ src/header/common/set_cookie.rs | 2 ++ src/header/common/transfer_encoding.rs | 2 ++ src/header/common/upgrade.rs | 4 +++- src/header/common/user_agent.rs | 2 ++ src/header/mod.rs | 2 ++ src/lib.rs | 2 +- src/server/response.rs | 2 +- 18 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/client/request.rs b/src/client/request.rs index 6f9c8579..634dcceb 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -134,7 +134,7 @@ impl Request { match self.headers.get::() { Some(cl) => { chunked = false; - len = cl.len(); + len = **cl; }, None => () }; diff --git a/src/header/common/accept.rs b/src/header/common/accept.rs index 0afa8f70..372a1c62 100644 --- a/src/header/common/accept.rs +++ b/src/header/common/accept.rs @@ -21,6 +21,8 @@ use mime::Mime; #[deriving(Clone, PartialEq, Show)] pub struct Accept(pub Vec); +deref!(Accept -> Vec) + impl Header for Accept { fn header_name(_: Option) -> &'static str { "Accept" diff --git a/src/header/common/authorization.rs b/src/header/common/authorization.rs index 99225f92..27f97ec4 100644 --- a/src/header/common/authorization.rs +++ b/src/header/common/authorization.rs @@ -7,6 +7,18 @@ use header::{Header, HeaderFormat}; #[deriving(Clone, PartialEq, Show)] pub struct Authorization(pub S); +impl Deref for Authorization { + fn deref<'a>(&'a self) -> &'a S { + &self.0 + } +} + +impl DerefMut for Authorization { + fn deref_mut<'a>(&'a mut self) -> &'a mut S { + &mut self.0 + } +} + impl Header for Authorization { fn header_name(_: Option>) -> &'static str { "Authorization" diff --git a/src/header/common/connection.rs b/src/header/common/connection.rs index a868c785..2357aa12 100644 --- a/src/header/common/connection.rs +++ b/src/header/common/connection.rs @@ -9,6 +9,8 @@ pub use self::ConnectionOption::{KeepAlive, Close, ConnectionHeader}; #[deriving(Clone, PartialEq, Show)] pub struct Connection(pub Vec); +deref!(Connection -> Vec) + /// Values that can be in the `Connection` header. #[deriving(Clone, PartialEq)] pub enum ConnectionOption { diff --git a/src/header/common/content_length.rs b/src/header/common/content_length.rs index b1895c60..044b7b71 100644 --- a/src/header/common/content_length.rs +++ b/src/header/common/content_length.rs @@ -9,6 +9,8 @@ use super::util::from_one_raw_str; #[deriving(Clone, PartialEq, Show)] pub struct ContentLength(pub uint); +deref!(ContentLength -> uint) + impl Header for ContentLength { fn header_name(_: Option) -> &'static str { "Content-Length" @@ -28,10 +30,10 @@ impl HeaderFormat for ContentLength { impl ContentLength { /// Returns the wrapped length. + #[deprecated = "use Deref instead"] #[inline] pub fn len(&self) -> uint { - let ContentLength(len) = *self; - len + **self } } diff --git a/src/header/common/content_type.rs b/src/header/common/content_type.rs index 2a664d97..5b786516 100644 --- a/src/header/common/content_type.rs +++ b/src/header/common/content_type.rs @@ -10,6 +10,8 @@ use mime::Mime; #[deriving(Clone, PartialEq, Show)] pub struct ContentType(pub Mime); +deref!(ContentType -> Mime) + impl Header for ContentType { fn header_name(_: Option) -> &'static str { "Content-Type" diff --git a/src/header/common/cookie.rs b/src/header/common/cookie.rs index efd8db46..7d41f4a2 100644 --- a/src/header/common/cookie.rs +++ b/src/header/common/cookie.rs @@ -16,6 +16,8 @@ use cookie::CookieJar; #[deriving(Clone, PartialEq, Show)] pub struct Cookies(pub Vec); +deref!(Cookies -> Vec) + impl Header for Cookies { fn header_name(_: Option) -> &'static str { "Cookie" diff --git a/src/header/common/date.rs b/src/header/common/date.rs index ab540456..ffa0a548 100644 --- a/src/header/common/date.rs +++ b/src/header/common/date.rs @@ -9,6 +9,8 @@ use time::{Tm, strptime}; #[deriving(PartialEq, Clone)] pub struct Date(pub Tm); +deref!(Date -> Tm) + impl Header for Date { fn header_name(_: Option) -> &'static str { "Date" diff --git a/src/header/common/location.rs b/src/header/common/location.rs index e266e607..0aa3f8a4 100644 --- a/src/header/common/location.rs +++ b/src/header/common/location.rs @@ -16,6 +16,8 @@ use super::util::from_one_raw_str; #[deriving(Clone, PartialEq, Show)] pub struct Location(pub String); +deref!(Location -> String) + impl Header for Location { fn header_name(_: Option) -> &'static str { "Location" diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index c910a170..664966d1 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -59,6 +59,22 @@ macro_rules! bench_header( } ) +macro_rules! deref( + ($from:ty -> $to:ty) => { + impl Deref<$to> for $from { + fn deref<'a>(&'a self) -> &'a $to { + &self.0 + } + } + + impl DerefMut<$to> for $from { + fn deref_mut<'a>(&'a mut self) -> &'a mut $to { + &mut self.0 + } + } + } +) + /// Exposes the Accept header. pub mod accept; diff --git a/src/header/common/server.rs b/src/header/common/server.rs index 7e6420c9..0b4085af 100644 --- a/src/header/common/server.rs +++ b/src/header/common/server.rs @@ -8,6 +8,8 @@ use super::util::from_one_raw_str; #[deriving(Clone, PartialEq, Show)] pub struct Server(pub String); +deref!(Server -> String) + impl Header for Server { fn header_name(_: Option) -> &'static str { "Server" diff --git a/src/header/common/set_cookie.rs b/src/header/common/set_cookie.rs index 77f18a3e..4d845c20 100644 --- a/src/header/common/set_cookie.rs +++ b/src/header/common/set_cookie.rs @@ -13,6 +13,8 @@ use cookie::CookieJar; #[deriving(Clone, PartialEq, Show)] pub struct SetCookie(pub Vec); +deref!(SetCookie -> Vec) + impl Header for SetCookie { fn header_name(_: Option) -> &'static str { "Set-Cookie" diff --git a/src/header/common/transfer_encoding.rs b/src/header/common/transfer_encoding.rs index 38a64457..ac628e0b 100644 --- a/src/header/common/transfer_encoding.rs +++ b/src/header/common/transfer_encoding.rs @@ -21,6 +21,8 @@ use self::Encoding::{Chunked, Gzip, Deflate, Compress, EncodingExt}; #[deriving(Clone, PartialEq, Show)] pub struct TransferEncoding(pub Vec); +deref!(TransferEncoding -> Vec) + /// A value to be used with the `Transfer-Encoding` header. /// /// Example: diff --git a/src/header/common/upgrade.rs b/src/header/common/upgrade.rs index 1f2a9d86..4083c84d 100644 --- a/src/header/common/upgrade.rs +++ b/src/header/common/upgrade.rs @@ -7,7 +7,9 @@ use self::Protocol::{WebSocket, ProtocolExt}; /// The `Upgrade` header. #[deriving(Clone, PartialEq, Show)] -pub struct Upgrade(Vec); +pub struct Upgrade(pub Vec); + +deref!(Upgrade -> Vec) /// Protocol values that can appear in the Upgrade header. #[deriving(Clone, PartialEq)] diff --git a/src/header/common/user_agent.rs b/src/header/common/user_agent.rs index 225de119..d201e4b1 100644 --- a/src/header/common/user_agent.rs +++ b/src/header/common/user_agent.rs @@ -8,6 +8,8 @@ use super::util::from_one_raw_str; #[deriving(Clone, PartialEq, Show)] pub struct UserAgent(pub String); +deref!(UserAgent -> String) + impl Header for UserAgent { fn header_name(_: Option) -> &'static str { "User-Agent" diff --git a/src/header/mod.rs b/src/header/mod.rs index 5ef772a6..f9d256eb 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -21,6 +21,8 @@ use uany::{UncheckedAnyDowncast, UncheckedAnyMutDowncast}; use http::{mod, LineEnding}; use {HttpResult}; +pub use self::common::*; + /// Common Headers pub mod common; diff --git a/src/lib.rs b/src/lib.rs index 01880b83..9e86f2a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![feature(macro_rules, phase, default_type_params, if_let, slicing_syntax, - tuple_indexing)] + tuple_indexing, globs)] #![deny(missing_docs)] #![deny(warnings)] #![experimental] diff --git a/src/server/response.rs b/src/server/response.rs index fe80be2f..538be0c1 100644 --- a/src/server/response.rs +++ b/src/server/response.rs @@ -83,7 +83,7 @@ impl<'a> Response<'a, Fresh> { match self.headers.get::() { Some(cl) => { chunked = false; - len = cl.len(); + len = **cl; }, None => () };