1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/src/server/shared.rs

139 lines
3.4 KiB
Rust
Raw Normal View History

2018-04-14 01:02:01 +02:00
use bytes::{BufMut, BytesMut};
2018-01-15 02:00:28 +01:00
use std::cell::RefCell;
use std::collections::VecDeque;
2018-04-29 07:55:47 +02:00
use std::io;
2018-04-14 01:02:01 +02:00
use std::rc::Rc;
2018-01-15 02:00:28 +01:00
use body::Binary;
/// Internal use only! unsafe
#[derive(Debug)]
pub(crate) struct SharedBytesPool(RefCell<VecDeque<Rc<BytesMut>>>);
impl SharedBytesPool {
pub fn new() -> SharedBytesPool {
SharedBytesPool(RefCell::new(VecDeque::with_capacity(128)))
}
pub fn get_bytes(&self) -> Rc<BytesMut> {
if let Some(bytes) = self.0.borrow_mut().pop_front() {
bytes
} else {
Rc::new(BytesMut::new())
}
}
pub fn release_bytes(&self, mut bytes: Rc<BytesMut>) {
let v = &mut self.0.borrow_mut();
if v.len() < 128 {
2018-03-09 02:19:50 +01:00
Rc::get_mut(&mut bytes).unwrap().clear();
2018-01-15 02:00:28 +01:00
v.push_front(bytes);
}
}
}
#[derive(Debug)]
2018-04-14 01:02:01 +02:00
pub(crate) struct SharedBytes(Option<Rc<BytesMut>>, Option<Rc<SharedBytesPool>>);
2018-01-15 02:00:28 +01:00
impl Drop for SharedBytes {
fn drop(&mut self) {
2018-04-10 19:29:10 +02:00
if let Some(pool) = self.1.take() {
2018-01-15 02:00:28 +01:00
if let Some(bytes) = self.0.take() {
if Rc::strong_count(&bytes) == 1 {
pool.release_bytes(bytes);
}
}
}
}
}
impl SharedBytes {
pub fn new(bytes: Rc<BytesMut>, pool: Rc<SharedBytesPool>) -> SharedBytes {
SharedBytes(Some(bytes), Some(pool))
}
#[inline(always)]
#[allow(mutable_transmutes)]
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))]
2018-03-09 02:19:50 +01:00
pub(crate) fn get_mut(&self) -> &mut BytesMut {
2018-01-15 02:00:28 +01:00
let r: &BytesMut = self.0.as_ref().unwrap().as_ref();
2018-04-29 07:55:47 +02:00
unsafe { &mut *(r as *const _ as *mut _) }
2018-01-15 02:00:28 +01:00
}
#[inline]
pub fn len(&self) -> usize {
self.0.as_ref().unwrap().len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.as_ref().unwrap().is_empty()
}
#[inline]
pub fn as_ref(&self) -> &[u8] {
self.0.as_ref().unwrap().as_ref()
}
pub fn split_to(&self, n: usize) -> BytesMut {
self.get_mut().split_to(n)
}
pub fn take(&self) -> BytesMut {
self.get_mut().take()
}
#[inline]
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
pub fn extend(&self, data: Binary) {
2018-02-20 02:21:04 +01:00
let buf = self.get_mut();
let data = data.as_ref();
buf.reserve(data.len());
SharedBytes::put_slice(buf, data);
2018-01-15 02:00:28 +01:00
}
#[inline]
pub fn extend_from_slice(&self, data: &[u8]) {
2018-02-20 02:21:04 +01:00
let buf = self.get_mut();
buf.reserve(data.len());
SharedBytes::put_slice(buf, data);
}
#[inline]
pub(crate) fn put_slice(buf: &mut BytesMut, src: &[u8]) {
let len = src.len();
unsafe {
buf.bytes_mut()[..len].copy_from_slice(src);
buf.advance_mut(len);
}
}
#[inline]
pub(crate) fn extend_from_slice_(buf: &mut BytesMut, data: &[u8]) {
buf.reserve(data.len());
SharedBytes::put_slice(buf, data);
2018-01-15 02:00:28 +01:00
}
}
impl Default for SharedBytes {
fn default() -> Self {
SharedBytes(Some(Rc::new(BytesMut::new())), None)
}
}
impl Clone for SharedBytes {
fn clone(&self) -> SharedBytes {
SharedBytes(self.0.clone(), self.1.clone())
}
}
2018-03-22 15:44:16 +01:00
impl io::Write for SharedBytes {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}