diff --git a/actix-http/src/message.rs b/actix-http/src/message.rs index 1a5500c31..bccb4d53e 100644 --- a/actix-http/src/message.rs +++ b/actix-http/src/message.rs @@ -34,7 +34,9 @@ bitflags! { pub trait Head: Default + 'static { fn clear(&mut self); - fn pool() -> &'static MessagePool; + fn with_pool(f: F) -> R + where + F: FnOnce(&MessagePool) -> R; } #[derive(Debug)] @@ -69,8 +71,11 @@ impl Head for RequestHead { self.extensions.get_mut().clear(); } - fn pool() -> &'static MessagePool { - REQUEST_POOL.with(|p| *p) + fn with_pool(f: F) -> R + where + F: FnOnce(&MessagePool) -> R, + { + REQUEST_POOL.with(|p| f(p)) } } @@ -344,7 +349,7 @@ pub struct Message { impl Message { /// Get new message from the pool of objects pub fn new() -> Self { - T::pool().get_message() + T::with_pool(|p| p.get_message()) } } @@ -373,7 +378,7 @@ impl std::ops::DerefMut for Message { impl Drop for Message { fn drop(&mut self) { if Rc::strong_count(&self.head) == 1 { - T::pool().release(self.head.clone()); + T::with_pool(|p| p.release(self.head.clone())) } } } @@ -426,18 +431,17 @@ pub struct MessagePool(RefCell>>); /// Request's objects pool pub struct BoxedResponsePool(RefCell>>); -thread_local!(static REQUEST_POOL: &'static MessagePool = MessagePool::::create()); -thread_local!(static RESPONSE_POOL: &'static BoxedResponsePool = BoxedResponsePool::create()); +thread_local!(static REQUEST_POOL: MessagePool = MessagePool::::create()); +thread_local!(static RESPONSE_POOL: BoxedResponsePool = BoxedResponsePool::create()); impl MessagePool { - fn create() -> &'static MessagePool { - let pool = MessagePool(RefCell::new(Vec::with_capacity(128))); - Box::leak(Box::new(pool)) + fn create() -> MessagePool { + MessagePool(RefCell::new(Vec::with_capacity(128))) } /// Get message from the pool #[inline] - fn get_message(&'static self) -> Message { + fn get_message(&self) -> Message { if let Some(mut msg) = self.0.borrow_mut().pop() { // Message is put in pool only when it's the last copy. // which means it's guaranteed to be unique when popped out. @@ -463,14 +467,13 @@ impl MessagePool { } impl BoxedResponsePool { - fn create() -> &'static BoxedResponsePool { - let pool = BoxedResponsePool(RefCell::new(Vec::with_capacity(128))); - Box::leak(Box::new(pool)) + fn create() -> BoxedResponsePool { + BoxedResponsePool(RefCell::new(Vec::with_capacity(128))) } /// Get message from the pool #[inline] - fn get_message(&'static self, status: StatusCode) -> BoxedResponseHead { + fn get_message(&self, status: StatusCode) -> BoxedResponseHead { if let Some(mut head) = self.0.borrow_mut().pop() { head.reason = None; head.status = status;