2018-03-15 23:09:22 +00:00
|
|
|
//! Buffer management for same-process client<->server communication.
|
|
|
|
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::mem;
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::slice;
|
|
|
|
|
|
|
|
#[repr(C)]
|
2019-02-03 18:55:40 +00:00
|
|
|
struct Slice<'a, T> {
|
2018-03-15 23:09:22 +00:00
|
|
|
data: &'a [T; 0],
|
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<'a, T: Sync> Sync for Slice<'a, T> {}
|
|
|
|
unsafe impl<'a, T: Sync> Send for Slice<'a, T> {}
|
|
|
|
|
|
|
|
impl<T> Copy for Slice<'a, T> {}
|
|
|
|
impl<T> Clone for Slice<'a, T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<&'a [T]> for Slice<'a, T> {
|
|
|
|
fn from(xs: &'a [T]) -> Self {
|
2019-12-22 22:42:04 +00:00
|
|
|
Slice { data: unsafe { &*(xs.as_ptr() as *const [T; 0]) }, len: xs.len() }
|
2018-03-15 23:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for Slice<'a, T> {
|
|
|
|
type Target = [T];
|
|
|
|
fn deref(&self) -> &[T] {
|
|
|
|
unsafe { slice::from_raw_parts(self.data.as_ptr(), self.len) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct Buffer<T: Copy> {
|
|
|
|
data: *mut T,
|
|
|
|
len: usize,
|
|
|
|
capacity: usize,
|
2021-05-15 23:49:54 +00:00
|
|
|
reserve: extern "C" fn(Buffer<T>, usize) -> Buffer<T>,
|
2018-03-15 23:09:22 +00:00
|
|
|
drop: extern "C" fn(Buffer<T>),
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: Copy + Sync> Sync for Buffer<T> {}
|
|
|
|
unsafe impl<T: Copy + Send> Send for Buffer<T> {}
|
|
|
|
|
|
|
|
impl<T: Copy> Default for Buffer<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::from(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Copy> Deref for Buffer<T> {
|
|
|
|
type Target = [T];
|
|
|
|
fn deref(&self) -> &[T] {
|
|
|
|
unsafe { slice::from_raw_parts(self.data as *const T, self.len) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Copy> DerefMut for Buffer<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut [T] {
|
|
|
|
unsafe { slice::from_raw_parts_mut(self.data, self.len) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Copy> Buffer<T> {
|
|
|
|
pub(super) fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn clear(&mut self) {
|
|
|
|
self.len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn take(&mut self) -> Self {
|
2019-06-30 18:13:41 +00:00
|
|
|
mem::take(self)
|
2018-03-15 23:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn extend_from_slice(&mut self, xs: &[T]) {
|
2021-05-15 23:49:54 +00:00
|
|
|
if xs.len() > self.capacity.wrapping_sub(self.len) {
|
|
|
|
let b = self.take();
|
|
|
|
*self = (b.reserve)(b, xs.len());
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len());
|
|
|
|
self.len += xs.len();
|
2018-03-15 23:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-15 22:54:57 +00:00
|
|
|
|
|
|
|
pub(super) fn push(&mut self, v: T) {
|
2021-05-15 23:49:54 +00:00
|
|
|
// The code here is taken from Vec::push, and we know that reserve()
|
|
|
|
// will panic if we're exceeding isize::MAX bytes and so there's no need
|
|
|
|
// to check for overflow.
|
|
|
|
if self.len == self.capacity {
|
|
|
|
let b = self.take();
|
|
|
|
*self = (b.reserve)(b, 1);
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
*self.data.add(self.len) = v;
|
|
|
|
self.len += 1;
|
2021-05-15 22:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-15 23:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Write for Buffer<u8> {
|
|
|
|
fn write(&mut self, xs: &[u8]) -> io::Result<usize> {
|
|
|
|
self.extend_from_slice(xs);
|
|
|
|
Ok(xs.len())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_all(&mut self, xs: &[u8]) -> io::Result<()> {
|
|
|
|
self.extend_from_slice(xs);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Copy> Drop for Buffer<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let b = self.take();
|
|
|
|
(b.drop)(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Copy> From<Vec<T>> for Buffer<T> {
|
|
|
|
fn from(mut v: Vec<T>) -> Self {
|
|
|
|
let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity());
|
|
|
|
mem::forget(v);
|
|
|
|
|
|
|
|
// This utility function is nested in here because it can *only*
|
|
|
|
// be safely called on `Buffer`s created by *this* `proc_macro`.
|
|
|
|
fn to_vec<T: Copy>(b: Buffer<T>) -> Vec<T> {
|
|
|
|
unsafe {
|
2019-12-22 22:42:04 +00:00
|
|
|
let Buffer { data, len, capacity, .. } = b;
|
2018-03-15 23:09:22 +00:00
|
|
|
mem::forget(b);
|
|
|
|
Vec::from_raw_parts(data, len, capacity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 23:49:54 +00:00
|
|
|
extern "C" fn reserve<T: Copy>(b: Buffer<T>, additional: usize) -> Buffer<T> {
|
2018-03-15 23:09:22 +00:00
|
|
|
let mut v = to_vec(b);
|
2021-05-15 23:49:54 +00:00
|
|
|
v.reserve(additional);
|
2018-03-15 23:09:22 +00:00
|
|
|
Buffer::from(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn drop<T: Copy>(b: Buffer<T>) {
|
|
|
|
mem::drop(to_vec(b));
|
|
|
|
}
|
|
|
|
|
2021-05-15 23:49:54 +00:00
|
|
|
Buffer { data, len, capacity, reserve, drop }
|
2018-03-15 23:09:22 +00:00
|
|
|
}
|
|
|
|
}
|