Merge pull request #1121 from hannesg/allocator_friendly_header2

refactor(header): make Origin header more allocator friendly
This commit is contained in:
Sean McArthur
2017-04-10 09:57:15 -07:00
committed by GitHub

View File

@@ -1,4 +1,5 @@
use header::{Header, Raw, Host}; use header::{Header, Raw, Host};
use std::borrow::Cow;
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use header::parsing::from_one_raw_str; use header::parsing::from_one_raw_str;
@@ -33,19 +34,39 @@ use header::parsing::from_one_raw_str;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Origin { pub struct Origin {
/// The scheme, such as http or https /// The scheme, such as http or https
pub scheme: String, scheme: Cow<'static,str>,
/// The host, such as Host{hostname: "hyper.rs".to_owned(), port: None} /// The host, such as Host{hostname: "hyper.rs".to_owned(), port: None}
pub host: Host, host: Host,
} }
impl Origin { impl Origin {
/// Creates a new `Origin` header. /// Creates a new `Origin` header.
pub fn new<S: Into<String>, H: Into<String>>(scheme: S, hostname: H, port: Option<u16>) -> Origin{ pub fn new<S: Into<Cow<'static,str>>, H: Into<Cow<'static,str>>>(scheme: S, hostname: H, port: Option<u16>) -> Origin{
Origin { Origin {
scheme: scheme.into(), scheme: scheme.into(),
host: Host::new(hostname.into(), port), host: Host::new(hostname, port),
} }
} }
/// The scheme, such as http or https
/// ```
/// use hyper::header::Origin;
/// let origin = Origin::new("https", "foo.com", Some(443));
/// assert_eq!(origin.scheme(), "https");
/// ```
pub fn scheme(&self) -> &str {
&(self.scheme)
}
/// The host, such as Host{hostname: "hyper.rs".to_owned(), port: None}
/// ```
/// use hyper::header::{Origin,Host};
/// let origin = Origin::new("https", "foo.com", Some(443));
/// assert_eq!(origin.host(), &Host::new("foo.com", Some(443)));
/// ```
pub fn host(&self) -> &Host {
&(self.host)
}
} }
impl Header for Origin { impl Header for Origin {
@@ -77,7 +98,7 @@ impl FromStr for Origin {
Ok(Origin{ Ok(Origin{
scheme: scheme.to_owned(), scheme: scheme.to_owned().into(),
host: host host: host
}) })
} }