|
| 1 | +//! lib-proc-macro Buffer management for same-process client<->server communication. |
| 2 | +//! |
| 3 | +//! Copy from <https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/buffer.rs> |
| 4 | +//! augmented with removing unstable features |
| 5 | +
|
| 6 | +use std::io::{self, Write}; |
| 7 | +use std::mem; |
| 8 | +use std::ops::{Deref, DerefMut}; |
| 9 | +use std::slice; |
| 10 | + |
| 11 | +#[repr(C)] |
| 12 | +struct Slice<'a, T> { |
| 13 | + data: &'a [T; 0], |
| 14 | + len: usize, |
| 15 | +} |
| 16 | + |
| 17 | +unsafe impl<'a, T: Sync> Sync for Slice<'a, T> {} |
| 18 | +unsafe impl<'a, T: Sync> Send for Slice<'a, T> {} |
| 19 | + |
| 20 | +impl<'a, T> Copy for Slice<'a, T> {} |
| 21 | +impl<'a, T> Clone for Slice<'a, T> { |
| 22 | + fn clone(&self) -> Self { |
| 23 | + *self |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl<'a, T> From<&'a [T]> for Slice<'a, T> { |
| 28 | + fn from(xs: &'a [T]) -> Self { |
| 29 | + Slice { data: unsafe { &*(xs.as_ptr() as *const [T; 0]) }, len: xs.len() } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl<'a, T> Deref for Slice<'a, T> { |
| 34 | + type Target = [T]; |
| 35 | + fn deref(&self) -> &[T] { |
| 36 | + unsafe { slice::from_raw_parts(self.data.as_ptr(), self.len) } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +#[repr(C)] |
| 41 | +pub struct Buffer<T: Copy> { |
| 42 | + data: *mut T, |
| 43 | + len: usize, |
| 44 | + capacity: usize, |
| 45 | + reserve: extern "C" fn(Buffer<T>, usize) -> Buffer<T>, |
| 46 | + drop: extern "C" fn(Buffer<T>), |
| 47 | +} |
| 48 | + |
| 49 | +unsafe impl<T: Copy + Sync> Sync for Buffer<T> {} |
| 50 | +unsafe impl<T: Copy + Send> Send for Buffer<T> {} |
| 51 | + |
| 52 | +impl<T: Copy> Default for Buffer<T> { |
| 53 | + fn default() -> Self { |
| 54 | + Self::from(vec![]) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<T: Copy> Deref for Buffer<T> { |
| 59 | + type Target = [T]; |
| 60 | + fn deref(&self) -> &[T] { |
| 61 | + unsafe { slice::from_raw_parts(self.data as *const T, self.len) } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<T: Copy> DerefMut for Buffer<T> { |
| 66 | + fn deref_mut(&mut self) -> &mut [T] { |
| 67 | + unsafe { slice::from_raw_parts_mut(self.data, self.len) } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<T: Copy> Buffer<T> { |
| 72 | + pub(super) fn new() -> Self { |
| 73 | + Self::default() |
| 74 | + } |
| 75 | + |
| 76 | + pub(super) fn clear(&mut self) { |
| 77 | + self.len = 0; |
| 78 | + } |
| 79 | + |
| 80 | + pub(super) fn take(&mut self) -> Self { |
| 81 | + mem::take(self) |
| 82 | + } |
| 83 | + |
| 84 | + pub(super) fn extend_from_slice(&mut self, xs: &[T]) { |
| 85 | + if xs.len() > self.capacity.wrapping_sub(self.len) { |
| 86 | + let b = self.take(); |
| 87 | + *self = (b.reserve)(b, xs.len()); |
| 88 | + } |
| 89 | + unsafe { |
| 90 | + xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len()); |
| 91 | + self.len += xs.len(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + pub(super) fn push(&mut self, v: T) { |
| 96 | + // The code here is taken from Vec::push, and we know that reserve() |
| 97 | + // will panic if we're exceeding isize::MAX bytes and so there's no need |
| 98 | + // to check for overflow. |
| 99 | + if self.len == self.capacity { |
| 100 | + let b = self.take(); |
| 101 | + *self = (b.reserve)(b, 1); |
| 102 | + } |
| 103 | + unsafe { |
| 104 | + *self.data.add(self.len) = v; |
| 105 | + self.len += 1; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl Write for Buffer<u8> { |
| 111 | + fn write(&mut self, xs: &[u8]) -> io::Result<usize> { |
| 112 | + self.extend_from_slice(xs); |
| 113 | + Ok(xs.len()) |
| 114 | + } |
| 115 | + |
| 116 | + fn write_all(&mut self, xs: &[u8]) -> io::Result<()> { |
| 117 | + self.extend_from_slice(xs); |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | + |
| 121 | + fn flush(&mut self) -> io::Result<()> { |
| 122 | + Ok(()) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl<T: Copy> Drop for Buffer<T> { |
| 127 | + fn drop(&mut self) { |
| 128 | + let b = self.take(); |
| 129 | + (b.drop)(b); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl<T: Copy> From<Vec<T>> for Buffer<T> { |
| 134 | + fn from(mut v: Vec<T>) -> Self { |
| 135 | + let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity()); |
| 136 | + mem::forget(v); |
| 137 | + |
| 138 | + // This utility function is nested in here because it can *only* |
| 139 | + // be safely called on `Buffer`s created by *this* `proc_macro`. |
| 140 | + fn to_vec<T: Copy>(b: Buffer<T>) -> Vec<T> { |
| 141 | + unsafe { |
| 142 | + let Buffer { data, len, capacity, .. } = b; |
| 143 | + mem::forget(b); |
| 144 | + Vec::from_raw_parts(data, len, capacity) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + extern "C" fn reserve<T: Copy>(b: Buffer<T>, additional: usize) -> Buffer<T> { |
| 149 | + let mut v = to_vec(b); |
| 150 | + v.reserve(additional); |
| 151 | + Buffer::from(v) |
| 152 | + } |
| 153 | + |
| 154 | + extern "C" fn drop<T: Copy>(b: Buffer<T>) { |
| 155 | + mem::drop(to_vec(b)); |
| 156 | + } |
| 157 | + |
| 158 | + Buffer { data, len, capacity, reserve, drop } |
| 159 | + } |
| 160 | +} |
0 commit comments