From f8d7162070b3539bd83f42a8792807d0343824f6 Mon Sep 17 00:00:00 2001 From: hag Date: Sat, 8 Apr 2017 02:41:04 +0200 Subject: [PATCH] header: turned several String headers into Cow<'static,str> headers This change was suggested in #1104 . As suggested I've also made the fields private and added a constructor. --- src/header/common/location.rs | 6 ++--- src/header/common/mod.rs | 43 +++++++++++++++++++++++++++++++++ src/header/common/referer.rs | 6 ++--- src/header/common/server.rs | 4 +-- src/header/common/user_agent.rs | 6 ++--- src/header/mod.rs | 2 +- 6 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/header/common/location.rs b/src/header/common/location.rs index 5369e87e7a..857d114e0d 100644 --- a/src/header/common/location.rs +++ b/src/header/common/location.rs @@ -21,16 +21,16 @@ header! { /// use hyper::header::{Headers, Location}; /// /// let mut headers = Headers::new(); - /// headers.set(Location("/People.html#tim".to_owned())); + /// headers.set(Location::new("/People.html#tim")); /// ``` /// ``` /// use hyper::header::{Headers, Location}; /// /// let mut headers = Headers::new(); - /// headers.set(Location("http://www.example.com/index.html".to_owned())); + /// headers.set(Location::new("http://www.example.com/index.html")); /// ``` // TODO: Use URL - (Location, "Location") => [String] + (Location, "Location") => Cow[str] test_location { // Testcase from RFC diff --git a/src/header/common/mod.rs b/src/header/common/mod.rs index 18f4349353..c2a977255d 100644 --- a/src/header/common/mod.rs +++ b/src/header/common/mod.rs @@ -264,6 +264,41 @@ macro_rules! header { } } }; + // Single value cow header + ($(#[$a:meta])*($id:ident, $n:expr) => Cow[$value:ty]) => { + $(#[$a])* + #[derive(Clone, Debug, PartialEq)] + pub struct $id(::std::borrow::Cow<'static,$value>); + impl $id { + /// Creates a new $id + pub fn new>>(value: I) -> Self { + $id(value.into()) + } + } + impl ::std::ops::Deref for $id { + type Target = ::std::borrow::Cow<'static,$value>; + fn deref(&self) -> &Self::Target { + &(self.0) + } + } + impl $crate::header::Header for $id { + fn header_name() -> &'static str { + static NAME: &'static str = $n; + NAME + } + fn parse_header(raw: &$crate::header::Raw) -> $crate::Result { + $crate::header::parsing::from_one_raw_str::<<$value as ::std::borrow::ToOwned>::Owned>(raw).map($id::new) + } + fn fmt_header(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + ::std::fmt::Display::fmt(&**self, f) + } + } + impl ::std::fmt::Display for $id { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + ::std::fmt::Display::fmt(&**self, f) + } + } + }; // List header, one or more items with "*" option ($(#[$a:meta])*($id:ident, $n:expr) => {Any / ($item:ty)+}) => { $(#[$a])* @@ -329,6 +364,14 @@ macro_rules! header { __hyper__tm! { $id, $tm { $($tf)* }} }; + ($(#[$a:meta])*($id:ident, $n:expr) => Cow[$item:ty] $tm:ident{$($tf:item)*}) => { + header! { + $(#[$a])* + ($id, $n) => Cow[$item] + } + + __hyper__tm! { $id, $tm { $($tf)* }} + }; ($(#[$a:meta])*($id:ident, $n:expr) => {Any / ($item:ty)+} $tm:ident{$($tf:item)*}) => { header! { $(#[$a])* diff --git a/src/header/common/referer.rs b/src/header/common/referer.rs index 2c7bf14bf1..15bd3bf775 100644 --- a/src/header/common/referer.rs +++ b/src/header/common/referer.rs @@ -21,16 +21,16 @@ header! { /// use hyper::header::{Headers, Referer}; /// /// let mut headers = Headers::new(); - /// headers.set(Referer("/People.html#tim".to_owned())); + /// headers.set(Referer::new("/People.html#tim")); /// ``` /// ``` /// use hyper::header::{Headers, Referer}; /// /// let mut headers = Headers::new(); - /// headers.set(Referer("http://www.example.com/index.html".to_owned())); + /// headers.set(Referer::new("http://www.example.com/index.html")); /// ``` // TODO Use URL - (Referer, "Referer") => [String] + (Referer, "Referer") => Cow[str] test_referer { // Testcase from the RFC diff --git a/src/header/common/server.rs b/src/header/common/server.rs index 8b6c90837f..965054e223 100644 --- a/src/header/common/server.rs +++ b/src/header/common/server.rs @@ -22,10 +22,10 @@ header! { /// use hyper::header::{Headers, Server}; /// /// let mut headers = Headers::new(); - /// headers.set(Server("hyper/0.5.2".to_owned())); + /// headers.set(Server::new("hyper/0.5.2")); /// ``` // TODO: Maybe parse as defined in the spec? - (Server, "Server") => [String] + (Server, "Server") => Cow[str] test_server { // Testcase from RFC diff --git a/src/header/common/user_agent.rs b/src/header/common/user_agent.rs index 90e5bd00fe..abe0906d14 100644 --- a/src/header/common/user_agent.rs +++ b/src/header/common/user_agent.rs @@ -29,14 +29,14 @@ header! { /// use hyper::header::{Headers, UserAgent}; /// /// let mut headers = Headers::new(); - /// headers.set(UserAgent("hyper/0.5.2".to_owned())); + /// headers.set(UserAgent::new("hyper/0.5.2")); /// ``` - (UserAgent, "User-Agent") => [String] + (UserAgent, "User-Agent") => Cow[str] test_user_agent { // Testcase from RFC test_header!(test1, vec![b"CERN-LineMode/2.15 libwww/2.17b3"]); // Own testcase - test_header!(test2, vec![b"Bunnies"], Some(UserAgent("Bunnies".to_owned()))); + test_header!(test2, vec![b"Bunnies"], Some(UserAgent::new("Bunnies"))); } } diff --git a/src/header/mod.rs b/src/header/mod.rs index 891e27527f..74fcc47936 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -148,7 +148,7 @@ fn test_get_type() { use ::header::{ContentLength, UserAgent}; let len = ContentLength(5); - let agent = UserAgent("hyper".to_owned()); + let agent = UserAgent::new("hyper"); assert_eq!(TypeId::of::(), len.get_type()); assert_eq!(TypeId::of::(), agent.get_type());