diff --git a/src/header/internals/vec_map.rs b/src/header/internals/vec_map.rs index ecdaa17b..977ff9d8 100644 --- a/src/header/internals/vec_map.rs +++ b/src/header/internals/vec_map.rs @@ -4,12 +4,14 @@ pub struct VecMap { } impl VecMap { + #[inline] pub fn with_capacity(cap: usize) -> VecMap { VecMap { vec: Vec::with_capacity(cap) } } + #[inline] pub fn insert(&mut self, key: K, value: V) { match self.find(&key) { Some(pos) => self.vec[pos] = (key, value), @@ -17,6 +19,7 @@ impl VecMap { } } + #[inline] pub fn entry(&mut self, key: K) -> Entry { match self.find(&key) { Some(pos) => Entry::Occupied(OccupiedEntry { @@ -30,31 +33,47 @@ impl VecMap { } } - pub fn get(&self, key: &K) -> Option<&V> { + #[inline] + pub fn get(&self, key: &K2) -> Option<&V> + where K2: PartialEq + ?Sized { self.find(key).map(move |pos| &self.vec[pos].1) } - pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { + #[inline] + pub fn get_mut(&mut self, key: &K2) -> Option<&mut V> + where K2: PartialEq { self.find(key).map(move |pos| &mut self.vec[pos].1) } - pub fn contains_key(&self, key: &K) -> bool { + #[inline] + pub fn contains_key(&self, key: &K2) -> bool + where K2: PartialEq { self.find(key).is_some() } + #[inline] pub fn len(&self) -> usize { self.vec.len() } + #[inline] + pub fn is_empty(&self) -> bool { self.vec.is_empty() } + + #[inline] pub fn iter(&self) -> ::std::slice::Iter<(K, V)> { self.vec.iter() } - pub fn remove(&mut self, key: &K) -> Option { + #[inline] + pub fn remove(&mut self, key: &K2) -> Option + where K2: PartialEq + ?Sized { self.find(key).map(|pos| self.vec.remove(pos)).map(|(_, v)| v) } + #[inline] pub fn clear(&mut self) { self.vec.clear(); } - fn find(&self, key: &K) -> Option { + #[inline] + fn find(&self, key: &K2) -> Option + where K2: PartialEq + ?Sized { self.vec.iter().position(|entry| key == &entry.0) } } diff --git a/src/header/mod.rs b/src/header/mod.rs index cfbf390a..88a9c9c5 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -464,7 +464,7 @@ impl Headers { /// ``` pub fn get_raw(&self, name: &str) -> Option<&Raw> { self.data - .get(&HeaderName(UniCase(Cow::Borrowed(unsafe { mem::transmute::<&str, &str>(name) })))) + .get(name) .map(Item::raw) } @@ -517,9 +517,7 @@ impl Headers { /// Remove a header by name. pub fn remove_raw(&mut self, name: &str) { trace!("Headers.remove_raw( {:?} )", name); - self.data.remove( - &HeaderName(UniCase(Cow::Borrowed(unsafe { mem::transmute::<&str, &str>(name) }))) - ); + self.data.remove(name); } @@ -685,6 +683,16 @@ impl PartialEq for HeaderName { } } +impl PartialEq for str { + fn eq(&self, other: &HeaderName) -> bool { + let k = other.as_ref(); + if self.len() == k.len() && self.as_ptr() == k.as_ptr() { + true + } else { + other.0 == self + } + } +} #[cfg(test)] mod tests {