From 6a343fae06c90aacd7c843d28c742a52cd7bc9f6 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 9 Feb 2019 10:33:49 -0800 Subject: [PATCH] simplify Message type --- src/message.rs | 76 +++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 48 deletions(-) diff --git a/src/message.rs b/src/message.rs index 12da9eaf1..7fb45bcc5 100644 --- a/src/message.rs +++ b/src/message.rs @@ -45,6 +45,7 @@ pub struct RequestHead { pub version: Version, pub headers: HeaderMap, pub ctype: Option, + pub extensions: RefCell, } impl Default for RequestHead { @@ -55,6 +56,7 @@ impl Default for RequestHead { version: Version::HTTP_11, headers: HeaderMap::with_capacity(16), ctype: None, + extensions: RefCell::new(Extensions::new()), } } } @@ -63,6 +65,7 @@ impl Head for RequestHead { fn clear(&mut self) { self.ctype = None; self.headers.clear(); + self.extensions.borrow_mut().clear(); } fn set_connection_type(&mut self, ctype: ConnectionType) { @@ -84,6 +87,20 @@ impl Head for RequestHead { } } +impl RequestHead { + /// Message extensions + #[inline] + pub fn extensions(&self) -> Ref { + self.extensions.borrow() + } + + /// Mutable reference to a the message's extensions + #[inline] + pub fn extensions_mut(&self) -> RefMut { + self.extensions.borrow_mut() + } +} + #[derive(Debug)] pub struct ResponseHead { pub version: Version, @@ -146,7 +163,7 @@ impl ResponseHead { } pub struct Message { - inner: Rc>, + head: Rc, pool: &'static MessagePool, } @@ -155,24 +172,12 @@ impl Message { pub fn new() -> Self { T::pool().get_message() } - - /// Message extensions - #[inline] - pub fn extensions(&self) -> Ref { - self.inner.as_ref().extensions.borrow() - } - - /// Mutable reference to a the message's extensions - #[inline] - pub fn extensions_mut(&self) -> RefMut { - self.inner.as_ref().extensions.borrow_mut() - } } impl Clone for Message { fn clone(&self) -> Self { Message { - inner: self.inner.clone(), + head: self.head.clone(), pool: self.pool, } } @@ -182,52 +187,27 @@ impl std::ops::Deref for Message { type Target = T; fn deref(&self) -> &Self::Target { - &self.inner.as_ref().head + &self.head.as_ref() } } impl std::ops::DerefMut for Message { fn deref_mut(&mut self) -> &mut Self::Target { - &mut Rc::get_mut(&mut self.inner) - .expect("Multiple copies exist") - .head + Rc::get_mut(&mut self.head).expect("Multiple copies exist") } } impl Drop for Message { fn drop(&mut self) { - if Rc::strong_count(&self.inner) == 1 { - self.pool.release(self.inner.clone()); - } - } -} - -struct MessageInner { - head: T, - extensions: RefCell, -} - -impl MessageInner { - #[inline] - /// Reset request instance - pub fn reset(&mut self) { - self.head.clear(); - self.extensions.borrow_mut().clear(); - } -} - -impl Default for MessageInner { - fn default() -> Self { - MessageInner { - head: T::default(), - extensions: RefCell::new(Extensions::new()), + if Rc::strong_count(&self.head) == 1 { + self.pool.release(self.head.clone()); } } } #[doc(hidden)] /// Request's objects pool -pub struct MessagePool(RefCell>>>); +pub struct MessagePool(RefCell>>); thread_local!(static REQUEST_POOL: &'static MessagePool = MessagePool::::create()); thread_local!(static RESPONSE_POOL: &'static MessagePool = MessagePool::::create()); @@ -243,15 +223,15 @@ impl MessagePool { fn get_message(&'static self) -> Message { if let Some(mut msg) = self.0.borrow_mut().pop_front() { if let Some(r) = Rc::get_mut(&mut msg) { - r.reset(); + r.clear(); } Message { - inner: msg, + head: msg, pool: self, } } else { Message { - inner: Rc::new(MessageInner::default()), + head: Rc::new(T::default()), pool: self, } } @@ -259,7 +239,7 @@ impl MessagePool { #[inline] /// Release request instance - fn release(&self, msg: Rc>) { + fn release(&self, msg: Rc) { let v = &mut self.0.borrow_mut(); if v.len() < 128 { v.push_front(msg);