Skip to content

Commit b1761ad

Browse files
committed
Merge pull request #162 from hyperium/headers-ergo
feat(headers): header ergonomics
2 parents 1014f63 + 8071cfa commit b1761ad

18 files changed

+60
-6
lines changed

src/client/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl Request<Fresh> {
134134
match self.headers.get::<common::ContentLength>() {
135135
Some(cl) => {
136136
chunked = false;
137-
len = cl.len();
137+
len = **cl;
138138
},
139139
None => ()
140140
};

src/header/common/accept.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use mime::Mime;
2121
#[deriving(Clone, PartialEq, Show)]
2222
pub struct Accept(pub Vec<Mime>);
2323

24+
deref!(Accept -> Vec<Mime>)
25+
2426
impl Header for Accept {
2527
fn header_name(_: Option<Accept>) -> &'static str {
2628
"Accept"

src/header/common/authorization.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ use header::{Header, HeaderFormat};
77
#[deriving(Clone, PartialEq, Show)]
88
pub struct Authorization<S: Scheme>(pub S);
99

10+
impl<S: Scheme> Deref<S> for Authorization<S> {
11+
fn deref<'a>(&'a self) -> &'a S {
12+
&self.0
13+
}
14+
}
15+
16+
impl<S: Scheme> DerefMut<S> for Authorization<S> {
17+
fn deref_mut<'a>(&'a mut self) -> &'a mut S {
18+
&mut self.0
19+
}
20+
}
21+
1022
impl<S: Scheme> Header for Authorization<S> {
1123
fn header_name(_: Option<Authorization<S>>) -> &'static str {
1224
"Authorization"

src/header/common/connection.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub use self::ConnectionOption::{KeepAlive, Close, ConnectionHeader};
99
#[deriving(Clone, PartialEq, Show)]
1010
pub struct Connection(pub Vec<ConnectionOption>);
1111

12+
deref!(Connection -> Vec<ConnectionOption>)
13+
1214
/// Values that can be in the `Connection` header.
1315
#[deriving(Clone, PartialEq)]
1416
pub enum ConnectionOption {

src/header/common/content_length.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use super::util::from_one_raw_str;
99
#[deriving(Clone, PartialEq, Show)]
1010
pub struct ContentLength(pub uint);
1111

12+
deref!(ContentLength -> uint)
13+
1214
impl Header for ContentLength {
1315
fn header_name(_: Option<ContentLength>) -> &'static str {
1416
"Content-Length"
@@ -28,10 +30,10 @@ impl HeaderFormat for ContentLength {
2830

2931
impl ContentLength {
3032
/// Returns the wrapped length.
33+
#[deprecated = "use Deref instead"]
3134
#[inline]
3235
pub fn len(&self) -> uint {
33-
let ContentLength(len) = *self;
34-
len
36+
**self
3537
}
3638
}
3739

src/header/common/content_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use mime::Mime;
1010
#[deriving(Clone, PartialEq, Show)]
1111
pub struct ContentType(pub Mime);
1212

13+
deref!(ContentType -> Mime)
14+
1315
impl Header for ContentType {
1416
fn header_name(_: Option<ContentType>) -> &'static str {
1517
"Content-Type"

src/header/common/cookie.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use cookie::CookieJar;
1616
#[deriving(Clone, PartialEq, Show)]
1717
pub struct Cookies(pub Vec<Cookie>);
1818

19+
deref!(Cookies -> Vec<Cookie>)
20+
1921
impl Header for Cookies {
2022
fn header_name(_: Option<Cookies>) -> &'static str {
2123
"Cookie"

src/header/common/date.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use time::{Tm, strptime};
99
#[deriving(PartialEq, Clone)]
1010
pub struct Date(pub Tm);
1111

12+
deref!(Date -> Tm)
13+
1214
impl Header for Date {
1315
fn header_name(_: Option<Date>) -> &'static str {
1416
"Date"

src/header/common/location.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use super::util::from_one_raw_str;
1616
#[deriving(Clone, PartialEq, Show)]
1717
pub struct Location(pub String);
1818

19+
deref!(Location -> String)
20+
1921
impl Header for Location {
2022
fn header_name(_: Option<Location>) -> &'static str {
2123
"Location"

src/header/common/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ macro_rules! bench_header(
5959
}
6060
)
6161

62+
macro_rules! deref(
63+
($from:ty -> $to:ty) => {
64+
impl Deref<$to> for $from {
65+
fn deref<'a>(&'a self) -> &'a $to {
66+
&self.0
67+
}
68+
}
69+
70+
impl DerefMut<$to> for $from {
71+
fn deref_mut<'a>(&'a mut self) -> &'a mut $to {
72+
&mut self.0
73+
}
74+
}
75+
}
76+
)
77+
6278
/// Exposes the Accept header.
6379
pub mod accept;
6480

src/header/common/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use super::util::from_one_raw_str;
88
#[deriving(Clone, PartialEq, Show)]
99
pub struct Server(pub String);
1010

11+
deref!(Server -> String)
12+
1113
impl Header for Server {
1214
fn header_name(_: Option<Server>) -> &'static str {
1315
"Server"

src/header/common/set_cookie.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use cookie::CookieJar;
1313
#[deriving(Clone, PartialEq, Show)]
1414
pub struct SetCookie(pub Vec<Cookie>);
1515

16+
deref!(SetCookie -> Vec<Cookie>)
17+
1618
impl Header for SetCookie {
1719
fn header_name(_: Option<SetCookie>) -> &'static str {
1820
"Set-Cookie"

src/header/common/transfer_encoding.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use self::Encoding::{Chunked, Gzip, Deflate, Compress, EncodingExt};
2121
#[deriving(Clone, PartialEq, Show)]
2222
pub struct TransferEncoding(pub Vec<Encoding>);
2323

24+
deref!(TransferEncoding -> Vec<Encoding>)
25+
2426
/// A value to be used with the `Transfer-Encoding` header.
2527
///
2628
/// Example:

src/header/common/upgrade.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use self::Protocol::{WebSocket, ProtocolExt};
77

88
/// The `Upgrade` header.
99
#[deriving(Clone, PartialEq, Show)]
10-
pub struct Upgrade(Vec<Protocol>);
10+
pub struct Upgrade(pub Vec<Protocol>);
11+
12+
deref!(Upgrade -> Vec<Protocol>)
1113

1214
/// Protocol values that can appear in the Upgrade header.
1315
#[deriving(Clone, PartialEq)]

src/header/common/user_agent.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use super::util::from_one_raw_str;
88
#[deriving(Clone, PartialEq, Show)]
99
pub struct UserAgent(pub String);
1010

11+
deref!(UserAgent -> String)
12+
1113
impl Header for UserAgent {
1214
fn header_name(_: Option<UserAgent>) -> &'static str {
1315
"User-Agent"

src/header/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use uany::{UncheckedAnyDowncast, UncheckedAnyMutDowncast};
2121
use http::{mod, LineEnding};
2222
use {HttpResult};
2323

24+
pub use self::common::*;
25+
2426
/// Common Headers
2527
pub mod common;
2628

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(macro_rules, phase, default_type_params, if_let, slicing_syntax,
2-
tuple_indexing)]
2+
tuple_indexing, globs)]
33
#![deny(missing_docs)]
44
#![deny(warnings)]
55
#![experimental]

src/server/response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'a> Response<'a, Fresh> {
8383
match self.headers.get::<common::ContentLength>() {
8484
Some(cl) => {
8585
chunked = false;
86-
len = cl.len();
86+
len = **cl;
8787
},
8888
None => ()
8989
};

0 commit comments

Comments
 (0)