From b49c89af7a0dbc9346b5bbe4acef11507610c36b Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 30 Mar 2017 15:38:51 -0700 Subject: [PATCH] Rename UncasedAscii and UncasedAsciiRef to Uncased and UncasedStr. --- lib/src/config/mod.rs | 2 +- lib/src/http/header.rs | 22 ++-- lib/src/http/media_type.rs | 10 +- lib/src/http/method.rs | 3 +- lib/src/http/mod.rs | 2 +- lib/src/http/{ascii.rs => uncased.rs} | 144 +++++++++++++------------- 6 files changed, 91 insertions(+), 92 deletions(-) rename lib/src/http/{ascii.rs => uncased.rs} (59%) diff --git a/lib/src/config/mod.rs b/lib/src/config/mod.rs index d7caf835..401eeec7 100644 --- a/lib/src/config/mod.rs +++ b/lib/src/config/mod.rs @@ -172,7 +172,7 @@ use self::Environment::*; use self::environment::CONFIG_ENV; use self::toml_ext::parse_simple_toml_value; use logger::{self, LoggingLevel}; -use http::ascii::uncased_eq; +use http::uncased::uncased_eq; static INIT: Once = ONCE_INIT; static mut CONFIG: Option = None; diff --git a/lib/src/http/header.rs b/lib/src/http/header.rs index 2d203e13..24685b7d 100644 --- a/lib/src/http/header.rs +++ b/lib/src/http/header.rs @@ -2,13 +2,13 @@ use std::collections::HashMap; use std::borrow::{Borrow, Cow}; use std::fmt; -use http::ascii::{UncasedAscii, UncasedAsciiRef}; +use http::uncased::{Uncased, UncasedStr}; /// Simple representation of an HTTP header. #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct Header<'h> { /// The name of the header. - pub name: UncasedAscii<'h>, + pub name: Uncased<'h>, /// The value of the header. pub value: Cow<'h, str>, } @@ -46,7 +46,7 @@ impl<'h> Header<'h> { where N: Into>, V: Into> { Header { - name: UncasedAscii::new(name), + name: Uncased::new(name), value: value.into() } } @@ -109,7 +109,7 @@ impl<'h> fmt::Display for Header<'h> { /// A collection of headers, mapping a header name to its many ordered values. #[derive(Clone, Debug, PartialEq, Default)] pub struct HeaderMap<'h> { - headers: HashMap, Vec>> + headers: HashMap, Vec>> } impl<'h> HeaderMap<'h> { @@ -134,7 +134,7 @@ impl<'h> HeaderMap<'h> { /// ``` #[inline] pub fn contains(&self, name: &str) -> bool { - self.headers.get(name.into() : &UncasedAsciiRef).is_some() + self.headers.get(name.into() : &UncasedStr).is_some() } /// Returns the number of _values_ stored in the map. @@ -199,7 +199,7 @@ impl<'h> HeaderMap<'h> { #[inline] pub fn get<'a>(&'a self, name: &str) -> impl Iterator { self.headers - .get(name.into() : &UncasedAsciiRef) + .get(name.into() : &UncasedStr) .into_iter() .flat_map(|values| values.iter().map(|val| val.borrow())) } @@ -237,7 +237,7 @@ impl<'h> HeaderMap<'h> { /// ``` #[inline] pub fn get_one<'a>(&'a self, name: &str) -> Option<&'a str> { - self.headers.get(name.into() : &UncasedAsciiRef) + self.headers.get(name.into() : &UncasedStr) .and_then(|values| { if values.len() >= 1 { Some(values[0].borrow()) } else { None } @@ -329,7 +329,7 @@ impl<'h> HeaderMap<'h> { pub fn replace_all<'n, 'v: 'h, H>(&mut self, name: H, values: Vec>) where 'n: 'h, H: Into> { - self.headers.insert(UncasedAscii::new(name), values); + self.headers.insert(Uncased::new(name), values); } /// Adds `header` into the map. If a header with `header.name` was @@ -404,7 +404,7 @@ impl<'h> HeaderMap<'h> { pub fn add_all<'n, H>(&mut self, name: H, values: &mut Vec>) where 'n:'h, H: Into> { - self.headers.entry(UncasedAscii::new(name)) + self.headers.entry(Uncased::new(name)) .or_insert(vec![]) .append(values) } @@ -427,7 +427,7 @@ impl<'h> HeaderMap<'h> { /// assert_eq!(map.len(), 1); #[inline(always)] pub fn remove(&mut self, name: &str) { - self.headers.remove(name.into() : &UncasedAsciiRef); + self.headers.remove(name.into() : &UncasedStr); } /// Removes all of the headers stored in this map and returns a vector @@ -504,7 +504,7 @@ impl<'h> HeaderMap<'h> { /// should likely not be used. #[inline] pub(crate) fn into_iter_raw(self) - -> impl Iterator, Vec>)> { + -> impl Iterator, Vec>)> { self.headers.into_iter() } } diff --git a/lib/src/http/media_type.rs b/lib/src/http/media_type.rs index a354046a..081b330d 100644 --- a/lib/src/http/media_type.rs +++ b/lib/src/http/media_type.rs @@ -4,7 +4,7 @@ use std::fmt; use std::hash::{Hash, Hasher}; use http::IntoCollection; -use http::ascii::{uncased_eq, UncasedAsciiRef}; +use http::uncased::{uncased_eq, UncasedStr}; use http::parse::{IndexedStr, parse_media_type}; use smallvec::SmallVec; @@ -193,7 +193,7 @@ impl MediaType { known_extensions!(from_extension); /// Returns the top-level type for this media type. The return type, - /// `UncasedAsciiRef`, has caseless equality comparison and hashing. + /// `UncasedStr`, has caseless equality comparison and hashing. /// /// # Example /// @@ -206,12 +206,12 @@ impl MediaType { /// assert_eq!(plain.top(), "Text"); /// ``` #[inline] - pub fn top(&self) -> &UncasedAsciiRef { + pub fn top(&self) -> &UncasedStr { self.top.to_str(self.source.as_ref()).into() } /// Returns the subtype for this media type. The return type, - /// `UncasedAsciiRef`, has caseless equality comparison and hashing. + /// `UncasedStr`, has caseless equality comparison and hashing. /// /// # Example /// @@ -224,7 +224,7 @@ impl MediaType { /// assert_eq!(plain.sub(), "pLaIn"); /// ``` #[inline] - pub fn sub(&self) -> &UncasedAsciiRef { + pub fn sub(&self) -> &UncasedStr { self.sub.to_str(self.source.as_ref()).into() } diff --git a/lib/src/http/method.rs b/lib/src/http/method.rs index 529e261d..a61c9128 100644 --- a/lib/src/http/method.rs +++ b/lib/src/http/method.rs @@ -1,10 +1,9 @@ use std::fmt; use std::str::FromStr; - use error::Error; use http::hyper; -use http::ascii::uncased_eq; +use http::uncased::uncased_eq; use self::Method::*; diff --git a/lib/src/http/mod.rs b/lib/src/http/mod.rs index 94999693..62e0780b 100644 --- a/lib/src/http/mod.rs +++ b/lib/src/http/mod.rs @@ -23,7 +23,7 @@ pub(crate) mod parse; // We need to export these for codegen, but otherwise it's unnecessary. // TODO: Expose a `const fn` from ContentType when possible. (see RFC#1817) -#[doc(hidden)] pub mod ascii; +pub mod uncased; #[doc(hidden)] pub use self::parse::IndexedStr; #[doc(hidden)] pub use self::media_type::MediaParams; diff --git a/lib/src/http/ascii.rs b/lib/src/http/uncased.rs similarity index 59% rename from lib/src/http/ascii.rs rename to lib/src/http/uncased.rs index 4f25318f..1a7dc2f1 100644 --- a/lib/src/http/ascii.rs +++ b/lib/src/http/uncased.rs @@ -15,64 +15,64 @@ use std::fmt; /// created from an `&str` as follows: /// /// ```rust,ignore -/// use rocket::http::ascii::UncasedAsciiRef; +/// use rocket::http::ascii::UncasedStr; /// -/// let ascii_ref: &UncasedAsciiRef = "Hello, world!".into(); +/// let ascii_ref: &UncasedStr = "Hello, world!".into(); /// ``` #[derive(Debug)] -pub struct UncasedAsciiRef(str); +pub struct UncasedStr(str); -impl UncasedAsciiRef { +impl UncasedStr { pub fn as_str(&self) -> &str { &self.0 } } -impl PartialEq for UncasedAsciiRef { +impl PartialEq for UncasedStr { #[inline(always)] - fn eq(&self, other: &UncasedAsciiRef) -> bool { + fn eq(&self, other: &UncasedStr) -> bool { self.0.eq_ignore_ascii_case(&other.0) } } -impl PartialEq for UncasedAsciiRef { +impl PartialEq for UncasedStr { #[inline(always)] fn eq(&self, other: &str) -> bool { self.0.eq_ignore_ascii_case(other) } } -impl PartialEq for str { +impl PartialEq for str { #[inline(always)] - fn eq(&self, other: &UncasedAsciiRef) -> bool { + fn eq(&self, other: &UncasedStr) -> bool { other.0.eq_ignore_ascii_case(self) } } -impl<'a> PartialEq<&'a str> for UncasedAsciiRef { +impl<'a> PartialEq<&'a str> for UncasedStr { #[inline(always)] fn eq(&self, other: & &'a str) -> bool { self.0.eq_ignore_ascii_case(other) } } -impl<'a> PartialEq for &'a str { +impl<'a> PartialEq for &'a str { #[inline(always)] - fn eq(&self, other: &UncasedAsciiRef) -> bool { + fn eq(&self, other: &UncasedStr) -> bool { other.0.eq_ignore_ascii_case(self) } } -impl<'a> From<&'a str> for &'a UncasedAsciiRef { +impl<'a> From<&'a str> for &'a UncasedStr { #[inline(always)] - fn from(string: &'a str) -> &'a UncasedAsciiRef { + fn from(string: &'a str) -> &'a UncasedStr { unsafe { ::std::mem::transmute(string) } } } -impl Eq for UncasedAsciiRef { } +impl Eq for UncasedStr { } -impl Hash for UncasedAsciiRef { +impl Hash for UncasedStr { #[inline(always)] fn hash(&self, hasher: &mut H) { for byte in self.0.bytes() { @@ -81,14 +81,14 @@ impl Hash for UncasedAsciiRef { } } -impl PartialOrd for UncasedAsciiRef { +impl PartialOrd for UncasedStr { #[inline(always)] - fn partial_cmp(&self, other: &UncasedAsciiRef) -> Option { + fn partial_cmp(&self, other: &UncasedStr) -> Option { Some(self.cmp(other)) } } -impl Ord for UncasedAsciiRef { +impl Ord for UncasedStr { fn cmp(&self, other: &Self) -> Ordering { let self_chars = self.0.chars().map(|c| c.to_ascii_lowercase()); let other_chars = other.0.chars().map(|c| c.to_ascii_lowercase()); @@ -96,7 +96,7 @@ impl Ord for UncasedAsciiRef { } } -impl fmt::Display for UncasedAsciiRef { +impl fmt::Display for UncasedStr { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(f) @@ -105,23 +105,23 @@ impl fmt::Display for UncasedAsciiRef { /// An uncased (case-preserving) ASCII string. #[derive(Clone, Debug)] -pub struct UncasedAscii<'s> { +pub struct Uncased<'s> { pub string: Cow<'s, str> } -impl<'s> UncasedAscii<'s> { +impl<'s> Uncased<'s> { /// Creates a new UncaseAscii string. /// /// # Example /// /// ```rust,ignore - /// use rocket::http::ascii::UncasedAscii; + /// use rocket::http::ascii::Uncased; /// /// let uncased_ascii = UncasedAScii::new("Content-Type"); /// ``` #[inline(always)] - pub fn new>>(string: S) -> UncasedAscii<'s> { - UncasedAscii { string: string.into() } + pub fn new>>(string: S) -> Uncased<'s> { + Uncased { string: string.into() } } /// Converts `self` into an owned `String`, allocating if necessary, @@ -144,115 +144,115 @@ impl<'s> UncasedAscii<'s> { } } -impl<'a> Deref for UncasedAscii<'a> { - type Target = UncasedAsciiRef; +impl<'a> Deref for Uncased<'a> { + type Target = UncasedStr; #[inline(always)] - fn deref(&self) -> &UncasedAsciiRef { + fn deref(&self) -> &UncasedStr { self.as_str().into() } } -impl<'a> AsRef for UncasedAscii<'a>{ +impl<'a> AsRef for Uncased<'a>{ #[inline(always)] - fn as_ref(&self) -> &UncasedAsciiRef { + fn as_ref(&self) -> &UncasedStr { self.as_str().into() } } -impl<'a> Borrow for UncasedAscii<'a> { +impl<'a> Borrow for Uncased<'a> { #[inline(always)] - fn borrow(&self) -> &UncasedAsciiRef { + fn borrow(&self) -> &UncasedStr { self.as_str().into() } } -impl<'s, 'c: 's> From<&'c str> for UncasedAscii<'s> { +impl<'s, 'c: 's> From<&'c str> for Uncased<'s> { #[inline(always)] fn from(string: &'c str) -> Self { - UncasedAscii::new(string) + Uncased::new(string) } } -impl From for UncasedAscii<'static> { +impl From for Uncased<'static> { #[inline(always)] fn from(string: String) -> Self { - UncasedAscii::new(string) + Uncased::new(string) } } -impl<'s, 'c: 's> From> for UncasedAscii<'s> { +impl<'s, 'c: 's> From> for Uncased<'s> { #[inline(always)] fn from(string: Cow<'c, str>) -> Self { - UncasedAscii::new(string) + Uncased::new(string) } } -impl<'s, 'c: 's, T: Into>> From for UncasedAscii<'s> { +impl<'s, 'c: 's, T: Into>> From for Uncased<'s> { #[inline(always)] default fn from(string: T) -> Self { - UncasedAscii::new(string) + Uncased::new(string) } } -impl<'a, 'b> PartialOrd> for UncasedAscii<'a> { +impl<'a, 'b> PartialOrd> for Uncased<'a> { #[inline(always)] - fn partial_cmp(&self, other: &UncasedAscii<'b>) -> Option { + fn partial_cmp(&self, other: &Uncased<'b>) -> Option { self.as_ref().partial_cmp(other.as_ref()) } } -impl<'a> Ord for UncasedAscii<'a> { +impl<'a> Ord for Uncased<'a> { fn cmp(&self, other: &Self) -> Ordering { self.as_ref().cmp(other.as_ref()) } } -impl<'s> fmt::Display for UncasedAscii<'s> { +impl<'s> fmt::Display for Uncased<'s> { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.string.fmt(f) } } -impl<'a, 'b> PartialEq> for UncasedAscii<'a> { +impl<'a, 'b> PartialEq> for Uncased<'a> { #[inline(always)] - fn eq(&self, other: &UncasedAscii<'b>) -> bool { + fn eq(&self, other: &Uncased<'b>) -> bool { self.as_ref().eq(other.as_ref()) } } -impl<'a> PartialEq for UncasedAscii<'a> { +impl<'a> PartialEq for Uncased<'a> { #[inline(always)] fn eq(&self, other: &str) -> bool { self.as_ref().eq(other) } } -impl<'b> PartialEq> for str { +impl<'b> PartialEq> for str { #[inline(always)] - fn eq(&self, other: &UncasedAscii<'b>) -> bool { + fn eq(&self, other: &Uncased<'b>) -> bool { other.as_ref().eq(self) } } -impl<'a, 'b> PartialEq<&'b str> for UncasedAscii<'a> { +impl<'a, 'b> PartialEq<&'b str> for Uncased<'a> { #[inline(always)] fn eq(&self, other: & &'b str) -> bool { self.as_ref().eq(other) } } -impl<'a, 'b> PartialEq> for &'a str { +impl<'a, 'b> PartialEq> for &'a str { #[inline(always)] - fn eq(&self, other: &UncasedAscii<'b>) -> bool { + fn eq(&self, other: &Uncased<'b>) -> bool { other.as_ref().eq(self) } } -impl<'s> Eq for UncasedAscii<'s> { } +impl<'s> Eq for Uncased<'s> { } -impl<'s> Hash for UncasedAscii<'s> { +impl<'s> Hash for Uncased<'s> { #[inline(always)] fn hash(&self, hasher: &mut H) { self.as_ref().hash(hasher) @@ -264,14 +264,14 @@ impl<'s> Hash for UncasedAscii<'s> { /// does it in a much faster way. #[inline(always)] pub fn uncased_eq, S2: AsRef>(s1: S1, s2: S2) -> bool { - let ascii_ref_1: &UncasedAsciiRef = s1.as_ref().into(); - let ascii_ref_2: &UncasedAsciiRef = s2.as_ref().into(); + let ascii_ref_1: &UncasedStr = s1.as_ref().into(); + let ascii_ref_2: &UncasedStr = s2.as_ref().into(); ascii_ref_1 == ascii_ref_2 } #[cfg(test)] mod tests { - use super::UncasedAscii; + use super::Uncased; use std::hash::{Hash, Hasher}; use std::collections::hash_map::DefaultHasher; @@ -289,8 +289,8 @@ mod tests { for i in 0..strings.len() { for j in i..strings.len() { let (str_a, str_b) = (strings[i], strings[j]); - let ascii_a = UncasedAscii::from(str_a); - let ascii_b = UncasedAscii::from(str_b); + let ascii_a = Uncased::from(str_a); + let ascii_b = Uncased::from(str_b); assert_eq!(ascii_a, ascii_b); assert_eq!(hash(&ascii_a), hash(&ascii_b)); assert_eq!(ascii_a, str_a); @@ -312,20 +312,20 @@ mod tests { #[test] fn test_case_cmp() { - assert!(UncasedAscii::from("foobar") == UncasedAscii::from("FOOBAR")); - assert!(UncasedAscii::from("a") == UncasedAscii::from("A")); + assert!(Uncased::from("foobar") == Uncased::from("FOOBAR")); + assert!(Uncased::from("a") == Uncased::from("A")); - assert!(UncasedAscii::from("a") < UncasedAscii::from("B")); - assert!(UncasedAscii::from("A") < UncasedAscii::from("B")); - assert!(UncasedAscii::from("A") < UncasedAscii::from("b")); + assert!(Uncased::from("a") < Uncased::from("B")); + assert!(Uncased::from("A") < Uncased::from("B")); + assert!(Uncased::from("A") < Uncased::from("b")); - assert!(UncasedAscii::from("aa") > UncasedAscii::from("a")); - assert!(UncasedAscii::from("aa") > UncasedAscii::from("A")); - assert!(UncasedAscii::from("AA") > UncasedAscii::from("a")); - assert!(UncasedAscii::from("AA") > UncasedAscii::from("a")); - assert!(UncasedAscii::from("Aa") > UncasedAscii::from("a")); - assert!(UncasedAscii::from("Aa") > UncasedAscii::from("A")); - assert!(UncasedAscii::from("aA") > UncasedAscii::from("a")); - assert!(UncasedAscii::from("aA") > UncasedAscii::from("A")); + assert!(Uncased::from("aa") > Uncased::from("a")); + assert!(Uncased::from("aa") > Uncased::from("A")); + assert!(Uncased::from("AA") > Uncased::from("a")); + assert!(Uncased::from("AA") > Uncased::from("a")); + assert!(Uncased::from("Aa") > Uncased::from("a")); + assert!(Uncased::from("Aa") > Uncased::from("A")); + assert!(Uncased::from("aA") > Uncased::from("a")); + assert!(Uncased::from("aA") > Uncased::from("A")); } }