Make Buffer<T> non-generic.

`u8` is the only type that makes sense for `T`, as demonstrated by the
fact that several impls and functions are hardwired to `Buffer<u8>`.
This commit is contained in:
Nicholas Nethercote 2022-05-13 09:38:02 +10:00
parent e02789c9f8
commit 2ece157e17
5 changed files with 46 additions and 46 deletions

View file

@ -6,37 +6,37 @@ use std::ops::{Deref, DerefMut};
use std::slice; use std::slice;
#[repr(C)] #[repr(C)]
pub struct Buffer<T: Copy> { pub struct Buffer {
data: *mut T, data: *mut u8,
len: usize, len: usize,
capacity: usize, capacity: usize,
reserve: extern "C" fn(Buffer<T>, usize) -> Buffer<T>, reserve: extern "C" fn(Buffer, usize) -> Buffer,
drop: extern "C" fn(Buffer<T>), drop: extern "C" fn(Buffer),
} }
unsafe impl<T: Copy + Sync> Sync for Buffer<T> {} unsafe impl Sync for Buffer {}
unsafe impl<T: Copy + Send> Send for Buffer<T> {} unsafe impl Send for Buffer {}
impl<T: Copy> Default for Buffer<T> { impl Default for Buffer {
fn default() -> Self { fn default() -> Self {
Self::from(vec![]) Self::from(vec![])
} }
} }
impl<T: Copy> Deref for Buffer<T> { impl Deref for Buffer {
type Target = [T]; type Target = [u8];
fn deref(&self) -> &[T] { fn deref(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.data as *const T, self.len) } unsafe { slice::from_raw_parts(self.data as *const u8, self.len) }
} }
} }
impl<T: Copy> DerefMut for Buffer<T> { impl DerefMut for Buffer {
fn deref_mut(&mut self) -> &mut [T] { fn deref_mut(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.data, self.len) } unsafe { slice::from_raw_parts_mut(self.data, self.len) }
} }
} }
impl<T: Copy> Buffer<T> { impl Buffer {
pub(super) fn new() -> Self { pub(super) fn new() -> Self {
Self::default() Self::default()
} }
@ -53,7 +53,7 @@ impl<T: Copy> Buffer<T> {
// because in the case of small arrays, codegen can be more efficient // because in the case of small arrays, codegen can be more efficient
// (avoiding a memmove call). With extend_from_slice, LLVM at least // (avoiding a memmove call). With extend_from_slice, LLVM at least
// currently is not able to make that optimization. // currently is not able to make that optimization.
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[T; N]) { pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[u8; N]) {
if xs.len() > (self.capacity - self.len) { if xs.len() > (self.capacity - self.len) {
let b = self.take(); let b = self.take();
*self = (b.reserve)(b, xs.len()); *self = (b.reserve)(b, xs.len());
@ -64,7 +64,7 @@ impl<T: Copy> Buffer<T> {
} }
} }
pub(super) fn extend_from_slice(&mut self, xs: &[T]) { pub(super) fn extend_from_slice(&mut self, xs: &[u8]) {
if xs.len() > (self.capacity - self.len) { if xs.len() > (self.capacity - self.len) {
let b = self.take(); let b = self.take();
*self = (b.reserve)(b, xs.len()); *self = (b.reserve)(b, xs.len());
@ -75,7 +75,7 @@ impl<T: Copy> Buffer<T> {
} }
} }
pub(super) fn push(&mut self, v: T) { pub(super) fn push(&mut self, v: u8) {
// The code here is taken from Vec::push, and we know that reserve() // 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 // will panic if we're exceeding isize::MAX bytes and so there's no need
// to check for overflow. // to check for overflow.
@ -90,7 +90,7 @@ impl<T: Copy> Buffer<T> {
} }
} }
impl Write for Buffer<u8> { impl Write for Buffer {
fn write(&mut self, xs: &[u8]) -> io::Result<usize> { fn write(&mut self, xs: &[u8]) -> io::Result<usize> {
self.extend_from_slice(xs); self.extend_from_slice(xs);
Ok(xs.len()) Ok(xs.len())
@ -106,21 +106,21 @@ impl Write for Buffer<u8> {
} }
} }
impl<T: Copy> Drop for Buffer<T> { impl Drop for Buffer {
fn drop(&mut self) { fn drop(&mut self) {
let b = self.take(); let b = self.take();
(b.drop)(b); (b.drop)(b);
} }
} }
impl<T: Copy> From<Vec<T>> for Buffer<T> { impl From<Vec<u8>> for Buffer {
fn from(mut v: Vec<T>) -> Self { fn from(mut v: Vec<u8>) -> Self {
let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity()); let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity());
mem::forget(v); mem::forget(v);
// This utility function is nested in here because it can *only* // This utility function is nested in here because it can *only*
// be safely called on `Buffer`s created by *this* `proc_macro`. // be safely called on `Buffer`s created by *this* `proc_macro`.
fn to_vec<T: Copy>(b: Buffer<T>) -> Vec<T> { fn to_vec(b: Buffer) -> Vec<u8> {
unsafe { unsafe {
let Buffer { data, len, capacity, .. } = b; let Buffer { data, len, capacity, .. } = b;
mem::forget(b); mem::forget(b);
@ -128,13 +128,13 @@ impl<T: Copy> From<Vec<T>> for Buffer<T> {
} }
} }
extern "C" fn reserve<T: Copy>(b: Buffer<T>, additional: usize) -> Buffer<T> { extern "C" fn reserve(b: Buffer, additional: usize) -> Buffer {
let mut v = to_vec(b); let mut v = to_vec(b);
v.reserve(additional); v.reserve(additional);
Buffer::from(v) Buffer::from(v)
} }
extern "C" fn drop<T: Copy>(b: Buffer<T>) { extern "C" fn drop(b: Buffer) {
mem::drop(to_vec(b)); mem::drop(to_vec(b));
} }

View file

@ -367,7 +367,7 @@ pub struct Client<F> {
// FIXME(eddyb) use a reference to the `static COUNTERS`, instead of // FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
// a wrapper `fn` pointer, once `const fn` can reference `static`s. // a wrapper `fn` pointer, once `const fn` can reference `static`s.
pub(super) get_handle_counters: extern "C" fn() -> &'static HandleCounters, pub(super) get_handle_counters: extern "C" fn() -> &'static HandleCounters,
pub(super) run: extern "C" fn(Bridge<'_>, F) -> Buffer<u8>, pub(super) run: extern "C" fn(Bridge<'_>, F) -> Buffer,
pub(super) f: F, pub(super) f: F,
} }
@ -377,7 +377,7 @@ pub struct Client<F> {
fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>( fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
mut bridge: Bridge<'_>, mut bridge: Bridge<'_>,
f: impl FnOnce(A) -> R, f: impl FnOnce(A) -> R,
) -> Buffer<u8> { ) -> Buffer {
// The initial `cached_buffer` contains the input. // The initial `cached_buffer` contains the input.
let mut b = bridge.cached_buffer.take(); let mut b = bridge.cached_buffer.take();
@ -420,7 +420,7 @@ impl Client<fn(crate::TokenStream) -> crate::TokenStream> {
extern "C" fn run( extern "C" fn run(
bridge: Bridge<'_>, bridge: Bridge<'_>,
f: impl FnOnce(crate::TokenStream) -> crate::TokenStream, f: impl FnOnce(crate::TokenStream) -> crate::TokenStream,
) -> Buffer<u8> { ) -> Buffer {
run_client(bridge, |input| f(crate::TokenStream(input)).0) run_client(bridge, |input| f(crate::TokenStream(input)).0)
} }
Client { get_handle_counters: HandleCounters::get, run, f } Client { get_handle_counters: HandleCounters::get, run, f }
@ -434,7 +434,7 @@ impl Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> {
extern "C" fn run( extern "C" fn run(
bridge: Bridge<'_>, bridge: Bridge<'_>,
f: impl FnOnce(crate::TokenStream, crate::TokenStream) -> crate::TokenStream, f: impl FnOnce(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
) -> Buffer<u8> { ) -> Buffer {
run_client(bridge, |(input, input2)| { run_client(bridge, |(input, input2)| {
f(crate::TokenStream(input), crate::TokenStream(input2)).0 f(crate::TokenStream(input), crate::TokenStream(input2)).0
}) })

View file

@ -224,10 +224,10 @@ use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
pub struct Bridge<'a> { pub struct Bridge<'a> {
/// Reusable buffer (only `clear`-ed, never shrunk), primarily /// Reusable buffer (only `clear`-ed, never shrunk), primarily
/// used for making requests, but also for passing input to client. /// used for making requests, but also for passing input to client.
cached_buffer: Buffer<u8>, cached_buffer: Buffer,
/// Server-side function that the client uses to make requests. /// Server-side function that the client uses to make requests.
dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>, dispatch: closure::Closure<'a, Buffer, Buffer>,
/// If 'true', always invoke the default panic hook /// If 'true', always invoke the default panic hook
force_show_panics: bool, force_show_panics: bool,

View file

@ -7,7 +7,7 @@ use std::num::NonZeroU32;
use std::ops::Bound; use std::ops::Bound;
use std::str; use std::str;
pub(super) type Writer = super::buffer::Buffer<u8>; pub(super) type Writer = super::buffer::Buffer;
pub(super) trait Encode<S>: Sized { pub(super) trait Encode<S>: Sized {
fn encode(self, w: &mut Writer, s: &mut S); fn encode(self, w: &mut Writer, s: &mut S);

View file

@ -80,12 +80,12 @@ macro_rules! define_dispatcher_impl {
pub trait DispatcherTrait { pub trait DispatcherTrait {
// HACK(eddyb) these are here to allow `Self::$name` to work below. // HACK(eddyb) these are here to allow `Self::$name` to work below.
$(type $name;)* $(type $name;)*
fn dispatch(&mut self, b: Buffer<u8>) -> Buffer<u8>; fn dispatch(&mut self, b: Buffer) -> Buffer;
} }
impl<S: Server> DispatcherTrait for Dispatcher<MarkedTypes<S>> { impl<S: Server> DispatcherTrait for Dispatcher<MarkedTypes<S>> {
$(type $name = <MarkedTypes<S> as Types>::$name;)* $(type $name = <MarkedTypes<S> as Types>::$name;)*
fn dispatch(&mut self, mut b: Buffer<u8>) -> Buffer<u8> { fn dispatch(&mut self, mut b: Buffer) -> Buffer {
let Dispatcher { handle_store, server } = self; let Dispatcher { handle_store, server } = self;
let mut reader = &b[..]; let mut reader = &b[..];
@ -123,11 +123,11 @@ pub trait ExecutionStrategy {
fn run_bridge_and_client<D: Copy + Send + 'static>( fn run_bridge_and_client<D: Copy + Send + 'static>(
&self, &self,
dispatcher: &mut impl DispatcherTrait, dispatcher: &mut impl DispatcherTrait,
input: Buffer<u8>, input: Buffer,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
client_data: D, client_data: D,
force_show_panics: bool, force_show_panics: bool,
) -> Buffer<u8>; ) -> Buffer;
} }
pub struct SameThread; pub struct SameThread;
@ -136,11 +136,11 @@ impl ExecutionStrategy for SameThread {
fn run_bridge_and_client<D: Copy + Send + 'static>( fn run_bridge_and_client<D: Copy + Send + 'static>(
&self, &self,
dispatcher: &mut impl DispatcherTrait, dispatcher: &mut impl DispatcherTrait,
input: Buffer<u8>, input: Buffer,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
client_data: D, client_data: D,
force_show_panics: bool, force_show_panics: bool,
) -> Buffer<u8> { ) -> Buffer {
let mut dispatch = |b| dispatcher.dispatch(b); let mut dispatch = |b| dispatcher.dispatch(b);
run_client( run_client(
@ -164,11 +164,11 @@ impl ExecutionStrategy for CrossThread1 {
fn run_bridge_and_client<D: Copy + Send + 'static>( fn run_bridge_and_client<D: Copy + Send + 'static>(
&self, &self,
dispatcher: &mut impl DispatcherTrait, dispatcher: &mut impl DispatcherTrait,
input: Buffer<u8>, input: Buffer,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
client_data: D, client_data: D,
force_show_panics: bool, force_show_panics: bool,
) -> Buffer<u8> { ) -> Buffer {
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
let (req_tx, req_rx) = channel(); let (req_tx, req_rx) = channel();
@ -205,11 +205,11 @@ impl ExecutionStrategy for CrossThread2 {
fn run_bridge_and_client<D: Copy + Send + 'static>( fn run_bridge_and_client<D: Copy + Send + 'static>(
&self, &self,
dispatcher: &mut impl DispatcherTrait, dispatcher: &mut impl DispatcherTrait,
input: Buffer<u8>, input: Buffer,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
client_data: D, client_data: D,
force_show_panics: bool, force_show_panics: bool,
) -> Buffer<u8> { ) -> Buffer {
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
enum State<T> { enum State<T> {
@ -276,7 +276,7 @@ fn run_server<
handle_counters: &'static client::HandleCounters, handle_counters: &'static client::HandleCounters,
server: S, server: S,
input: I, input: I,
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
client_data: D, client_data: D,
force_show_panics: bool, force_show_panics: bool,
) -> Result<O, PanicMessage> { ) -> Result<O, PanicMessage> {