mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
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:
parent
e02789c9f8
commit
2ece157e17
@ -6,37 +6,37 @@ use std::ops::{Deref, DerefMut};
|
||||
use std::slice;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Buffer<T: Copy> {
|
||||
data: *mut T,
|
||||
pub struct Buffer {
|
||||
data: *mut u8,
|
||||
len: usize,
|
||||
capacity: usize,
|
||||
reserve: extern "C" fn(Buffer<T>, usize) -> Buffer<T>,
|
||||
drop: extern "C" fn(Buffer<T>),
|
||||
reserve: extern "C" fn(Buffer, usize) -> Buffer,
|
||||
drop: extern "C" fn(Buffer),
|
||||
}
|
||||
|
||||
unsafe impl<T: Copy + Sync> Sync for Buffer<T> {}
|
||||
unsafe impl<T: Copy + Send> Send for Buffer<T> {}
|
||||
unsafe impl Sync for Buffer {}
|
||||
unsafe impl Send for Buffer {}
|
||||
|
||||
impl<T: Copy> Default for Buffer<T> {
|
||||
impl Default for Buffer {
|
||||
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 Deref for Buffer {
|
||||
type Target = [u8];
|
||||
fn deref(&self) -> &[u8] {
|
||||
unsafe { slice::from_raw_parts(self.data as *const u8, self.len) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> DerefMut for Buffer<T> {
|
||||
fn deref_mut(&mut self) -> &mut [T] {
|
||||
impl DerefMut for Buffer {
|
||||
fn deref_mut(&mut self) -> &mut [u8] {
|
||||
unsafe { slice::from_raw_parts_mut(self.data, self.len) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> Buffer<T> {
|
||||
impl Buffer {
|
||||
pub(super) fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
@ -53,7 +53,7 @@ impl<T: Copy> Buffer<T> {
|
||||
// because in the case of small arrays, codegen can be more efficient
|
||||
// (avoiding a memmove call). With extend_from_slice, LLVM at least
|
||||
// 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) {
|
||||
let b = self.take();
|
||||
*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) {
|
||||
let b = self.take();
|
||||
*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()
|
||||
// will panic if we're exceeding isize::MAX bytes and so there's no need
|
||||
// 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> {
|
||||
self.extend_from_slice(xs);
|
||||
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) {
|
||||
let b = self.take();
|
||||
(b.drop)(b);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> From<Vec<T>> for Buffer<T> {
|
||||
fn from(mut v: Vec<T>) -> Self {
|
||||
impl From<Vec<u8>> for Buffer {
|
||||
fn from(mut v: Vec<u8>) -> 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> {
|
||||
fn to_vec(b: Buffer) -> Vec<u8> {
|
||||
unsafe {
|
||||
let Buffer { data, len, capacity, .. } = 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);
|
||||
v.reserve(additional);
|
||||
Buffer::from(v)
|
||||
}
|
||||
|
||||
extern "C" fn drop<T: Copy>(b: Buffer<T>) {
|
||||
extern "C" fn drop(b: Buffer) {
|
||||
mem::drop(to_vec(b));
|
||||
}
|
||||
|
||||
|
@ -367,7 +367,7 @@ pub struct Client<F> {
|
||||
// FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
|
||||
// a wrapper `fn` pointer, once `const fn` can reference `static`s.
|
||||
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,
|
||||
}
|
||||
|
||||
@ -377,7 +377,7 @@ pub struct Client<F> {
|
||||
fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
|
||||
mut bridge: Bridge<'_>,
|
||||
f: impl FnOnce(A) -> R,
|
||||
) -> Buffer<u8> {
|
||||
) -> Buffer {
|
||||
// The initial `cached_buffer` contains the input.
|
||||
let mut b = bridge.cached_buffer.take();
|
||||
|
||||
@ -420,7 +420,7 @@ impl Client<fn(crate::TokenStream) -> crate::TokenStream> {
|
||||
extern "C" fn run(
|
||||
bridge: Bridge<'_>,
|
||||
f: impl FnOnce(crate::TokenStream) -> crate::TokenStream,
|
||||
) -> Buffer<u8> {
|
||||
) -> Buffer {
|
||||
run_client(bridge, |input| f(crate::TokenStream(input)).0)
|
||||
}
|
||||
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(
|
||||
bridge: Bridge<'_>,
|
||||
f: impl FnOnce(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
|
||||
) -> Buffer<u8> {
|
||||
) -> Buffer {
|
||||
run_client(bridge, |(input, input2)| {
|
||||
f(crate::TokenStream(input), crate::TokenStream(input2)).0
|
||||
})
|
||||
|
@ -224,10 +224,10 @@ use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
|
||||
pub struct Bridge<'a> {
|
||||
/// Reusable buffer (only `clear`-ed, never shrunk), primarily
|
||||
/// 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.
|
||||
dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>,
|
||||
dispatch: closure::Closure<'a, Buffer, Buffer>,
|
||||
|
||||
/// If 'true', always invoke the default panic hook
|
||||
force_show_panics: bool,
|
||||
|
@ -7,7 +7,7 @@ use std::num::NonZeroU32;
|
||||
use std::ops::Bound;
|
||||
use std::str;
|
||||
|
||||
pub(super) type Writer = super::buffer::Buffer<u8>;
|
||||
pub(super) type Writer = super::buffer::Buffer;
|
||||
|
||||
pub(super) trait Encode<S>: Sized {
|
||||
fn encode(self, w: &mut Writer, s: &mut S);
|
||||
|
@ -80,12 +80,12 @@ macro_rules! define_dispatcher_impl {
|
||||
pub trait DispatcherTrait {
|
||||
// HACK(eddyb) these are here to allow `Self::$name` to work below.
|
||||
$(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>> {
|
||||
$(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 mut reader = &b[..];
|
||||
@ -123,11 +123,11 @@ pub trait ExecutionStrategy {
|
||||
fn run_bridge_and_client<D: Copy + Send + 'static>(
|
||||
&self,
|
||||
dispatcher: &mut impl DispatcherTrait,
|
||||
input: Buffer<u8>,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
|
||||
input: Buffer,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
|
||||
client_data: D,
|
||||
force_show_panics: bool,
|
||||
) -> Buffer<u8>;
|
||||
) -> Buffer;
|
||||
}
|
||||
|
||||
pub struct SameThread;
|
||||
@ -136,11 +136,11 @@ impl ExecutionStrategy for SameThread {
|
||||
fn run_bridge_and_client<D: Copy + Send + 'static>(
|
||||
&self,
|
||||
dispatcher: &mut impl DispatcherTrait,
|
||||
input: Buffer<u8>,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
|
||||
input: Buffer,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
|
||||
client_data: D,
|
||||
force_show_panics: bool,
|
||||
) -> Buffer<u8> {
|
||||
) -> Buffer {
|
||||
let mut dispatch = |b| dispatcher.dispatch(b);
|
||||
|
||||
run_client(
|
||||
@ -164,11 +164,11 @@ impl ExecutionStrategy for CrossThread1 {
|
||||
fn run_bridge_and_client<D: Copy + Send + 'static>(
|
||||
&self,
|
||||
dispatcher: &mut impl DispatcherTrait,
|
||||
input: Buffer<u8>,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
|
||||
input: Buffer,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
|
||||
client_data: D,
|
||||
force_show_panics: bool,
|
||||
) -> Buffer<u8> {
|
||||
) -> Buffer {
|
||||
use std::sync::mpsc::channel;
|
||||
|
||||
let (req_tx, req_rx) = channel();
|
||||
@ -205,11 +205,11 @@ impl ExecutionStrategy for CrossThread2 {
|
||||
fn run_bridge_and_client<D: Copy + Send + 'static>(
|
||||
&self,
|
||||
dispatcher: &mut impl DispatcherTrait,
|
||||
input: Buffer<u8>,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
|
||||
input: Buffer,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
|
||||
client_data: D,
|
||||
force_show_panics: bool,
|
||||
) -> Buffer<u8> {
|
||||
) -> Buffer {
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
enum State<T> {
|
||||
@ -276,7 +276,7 @@ fn run_server<
|
||||
handle_counters: &'static client::HandleCounters,
|
||||
server: S,
|
||||
input: I,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>,
|
||||
run_client: extern "C" fn(Bridge<'_>, D) -> Buffer,
|
||||
client_data: D,
|
||||
force_show_panics: bool,
|
||||
) -> Result<O, PanicMessage> {
|
||||
|
Loading…
Reference in New Issue
Block a user