1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 16:32:59 +01:00
actix-web/actix-http/src/message.rs

107 lines
2.6 KiB
Rust
Raw Normal View History

2021-12-28 03:37:13 +01:00
use std::{cell::RefCell, ops, rc::Rc};
2018-11-17 04:28:07 +01:00
2019-03-27 18:38:01 +01:00
use bitflags::bitflags;
/// Represents various types of connection
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ConnectionType {
/// Close connection after response
Close,
2021-02-12 01:15:25 +01:00
/// Keep connection alive after response
KeepAlive,
2021-02-12 01:15:25 +01:00
/// Connection is upgraded to different type
Upgrade,
}
2019-03-27 18:38:01 +01:00
bitflags! {
pub(crate) struct Flags: u8 {
const CLOSE = 0b0000_0001;
const KEEP_ALIVE = 0b0000_0010;
const UPGRADE = 0b0000_0100;
2019-04-06 01:46:44 +02:00
const EXPECT = 0b0000_1000;
const NO_CHUNKING = 0b0001_0000;
const CAMEL_CASE = 0b0010_0000;
2019-03-27 18:38:01 +01:00
}
}
2018-11-17 04:28:07 +01:00
#[doc(hidden)]
pub trait Head: Default + 'static {
fn clear(&mut self);
fn with_pool<F, R>(f: F) -> R
where
F: FnOnce(&MessagePool<Self>) -> R;
2018-11-17 04:28:07 +01:00
}
pub struct Message<T: Head> {
/// Rc here should not be cloned by anyone.
/// It's used to reuse allocation of T and no shared ownership is allowed.
2019-02-09 19:33:49 +01:00
head: Rc<T>,
2018-11-17 04:28:07 +01:00
}
impl<T: Head> Message<T> {
2019-02-08 06:16:46 +01:00
/// Get new message from the pool of objects
2021-12-22 08:16:07 +01:00
#[allow(clippy::new_without_default)]
2019-02-08 06:16:46 +01:00
pub fn new() -> Self {
T::with_pool(MessagePool::get_message)
2019-02-08 06:16:46 +01:00
}
}
2021-12-28 03:37:13 +01:00
impl<T: Head> ops::Deref for Message<T> {
2019-02-08 06:16:46 +01:00
type Target = T;
fn deref(&self) -> &Self::Target {
2021-08-13 19:49:58 +02:00
self.head.as_ref()
2019-02-08 06:16:46 +01:00
}
}
2021-12-28 03:37:13 +01:00
impl<T: Head> ops::DerefMut for Message<T> {
2019-02-08 06:16:46 +01:00
fn deref_mut(&mut self) -> &mut Self::Target {
2019-02-09 19:33:49 +01:00
Rc::get_mut(&mut self.head).expect("Multiple copies exist")
2019-02-08 06:16:46 +01:00
}
}
impl<T: Head> Drop for Message<T> {
fn drop(&mut self) {
2021-02-07 21:19:10 +01:00
T::with_pool(|p| p.release(self.head.clone()))
2018-11-17 04:28:07 +01:00
}
}
#[doc(hidden)]
/// Request's objects pool
pub struct MessagePool<T: Head>(RefCell<Vec<Rc<T>>>);
2018-11-17 04:28:07 +01:00
impl<T: Head> MessagePool<T> {
pub(crate) fn create() -> MessagePool<T> {
MessagePool(RefCell::new(Vec::with_capacity(128)))
2018-11-17 04:28:07 +01:00
}
2019-02-08 06:16:46 +01:00
/// Get message from the pool
#[inline]
fn get_message(&self) -> Message<T> {
if let Some(mut msg) = self.0.borrow_mut().pop() {
2021-01-04 14:03:46 +01:00
// Message is put in pool only when it's the last copy.
// which means it's guaranteed to be unique when popped out.
Rc::get_mut(&mut msg)
.expect("Multiple copies exist")
.clear();
Message { head: msg }
2019-02-08 06:16:46 +01:00
} else {
Message {
2019-02-09 19:33:49 +01:00
head: Rc::new(T::default()),
2019-02-08 06:16:46 +01:00
}
}
}
2018-11-17 04:28:07 +01:00
#[inline]
/// Release message instance
2019-02-09 19:33:49 +01:00
fn release(&self, msg: Rc<T>) {
let pool = &mut self.0.borrow_mut();
if pool.len() < 128 {
pool.push(msg);
}
}
}