From 592438b630fc9ebe91ee6463edc195fa8f171540 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 3 Sep 2014 11:05:48 -0700 Subject: [PATCH] change headers to use static strs --- src/header.rs | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/header.rs b/src/header.rs index ecad693d..9f229a85 100644 --- a/src/header.rs +++ b/src/header.rs @@ -17,7 +17,7 @@ use std::fmt::{mod, Show}; use std::from_str::{FromStr, from_str}; use std::mem::{transmute, transmute_copy}; use std::raw::TraitObject; -use std::str::{from_utf8, SendStr, Slice}; +use std::str::from_utf8; use std::string::raw; use std::collections::hashmap::{HashMap, Entries}; @@ -29,7 +29,7 @@ use {HttpResult}; /// A trait for any object that will represent a header field and value. pub trait Header: Any { /// Returns the name of the header field this belongs to. - fn header_name(marker: Option) -> SendStr; + fn header_name(marker: Option) -> &'static str; /// Parse a header from a raw stream of bytes. /// /// It's possible that a request can include a header field more than once, @@ -58,7 +58,7 @@ impl<'a> UncheckedAnyRefExt<'a> for &'a Header + 'a { } } -fn header_name() -> SendStr { +fn header_name() -> &'static str { let name = Header::header_name(None::); debug_assert!(name.as_slice().chars().all(|c| c == '-' || is_lowercase(c)), "Header names should be lowercase: {}", name); @@ -67,7 +67,7 @@ fn header_name() -> SendStr { /// A map of header fields on requests and responses. pub struct Headers { - data: HashMap + data: HashMap<&'static str, Item> } impl Headers { @@ -89,7 +89,8 @@ impl Headers { // means its safe utf8 let name = unsafe { raw::from_utf8(name) - }.into_ascii_lower().into_maybe_owned(); + }.into_ascii_lower(); + let name: &'static str = unsafe { transmute(name.as_slice()) }; match headers.data.find_or_insert(name, Raw(vec![])) { &Raw(ref mut pieces) => pieces.push(value), // at this point, Raw is the only thing that has been inserted @@ -190,11 +191,11 @@ impl fmt::Show for Headers { /// An `Iterator` over the fields in a `Headers` map. pub struct HeadersItems<'a> { - inner: Entries<'a, SendStr, Item> + inner: Entries<'a, &'static str, Item> } -impl<'a> Iterator<(&'a SendStr, HeaderView<'a>)> for HeadersItems<'a> { - fn next(&mut self) -> Option<(&'a SendStr, HeaderView<'a>)> { +impl<'a> Iterator<(&'a &'static str, HeaderView<'a>)> for HeadersItems<'a> { + fn next(&mut self) -> Option<(&'a &'static str, HeaderView<'a>)> { match self.inner.next() { Some((k, v)) => Some((k, HeaderView(v))), None => None @@ -257,8 +258,8 @@ impl fmt::Show for Item { pub struct Host(pub String); impl Header for Host { - fn header_name(_: Option) -> SendStr { - Slice("host") + fn header_name(_: Option) -> &'static str { + "host" } fn parse_header(raw: &[Vec]) -> Option { @@ -278,8 +279,8 @@ impl Header for Host { pub struct ContentLength(pub uint); impl Header for ContentLength { - fn header_name(_: Option) -> SendStr { - Slice("content-length") + fn header_name(_: Option) -> &'static str { + "content-length" } fn parse_header(raw: &[Vec]) -> Option { @@ -300,8 +301,8 @@ impl Header for ContentLength { pub struct ContentType(pub Mime); impl Header for ContentType { - fn header_name(_: Option) -> SendStr { - Slice("content-type") + fn header_name(_: Option) -> &'static str { + "content-type" } fn parse_header(raw: &[Vec]) -> Option { @@ -332,8 +333,8 @@ impl Header for ContentType { pub struct Accept(pub Vec); impl Header for Accept { - fn header_name(_: Option) -> SendStr { - Slice("accept") + fn header_name(_: Option) -> &'static str { + "accept" } fn parse_header(_raw: &[Vec]) -> Option { @@ -377,8 +378,8 @@ impl FromStr for Connection { } impl Header for Connection { - fn header_name(_: Option) -> SendStr { - Slice("connection") + fn header_name(_: Option) -> &'static str { + "connection" } fn parse_header(raw: &[Vec]) -> Option { @@ -443,8 +444,8 @@ impl FromStr for Encoding { } impl Header for TransferEncoding { - fn header_name(_: Option) -> SendStr { - Slice("transfer-encoding") + fn header_name(_: Option) -> &'static str { + "transfer-encoding" } fn parse_header(raw: &[Vec]) -> Option { @@ -483,8 +484,8 @@ impl Header for TransferEncoding { pub struct UserAgent(pub String); impl Header for UserAgent { - fn header_name(_: Option) -> SendStr { - Slice("user-agent") + fn header_name(_: Option) -> &'static str { + "user-agent" } fn parse_header(raw: &[Vec]) -> Option {