Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions library/proc_macro/src/bridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro_rules! define_client_handles {
$(pub(super) $ity: AtomicU32,)*
}

static COUNTERS: HandleCounters = HandleCounters {
pub(super) static COUNTERS: HandleCounters = HandleCounters {
$($oty: AtomicU32::new(1),)*
$($ity: AtomicU32::new(1),)*
};
Expand Down Expand Up @@ -58,7 +58,7 @@ macro_rules! define_client_handles {
}
}

impl<S> DecodeMut<'_, '_, S> for $oty {
impl<S> Decode<'_, '_, S> for $oty {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$oty {
handle: handle::Handle::decode(r, s),
Expand All @@ -82,7 +82,7 @@ macro_rules! define_client_handles {
}
}

impl<S> DecodeMut<'_, '_, S> for $ity {
impl<S> Decode<'_, '_, S> for $ity {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$ity {
handle: handle::Handle::decode(r, s),
Expand Down Expand Up @@ -241,8 +241,6 @@ pub(crate) fn is_available() -> bool {
/// and forcing the use of APIs that take/return `S::TokenStream`, server-side.
#[repr(C)]
pub struct Client<I, O> {
pub(super) handle_counters: &'static HandleCounters,

pub(super) run: extern "C" fn(BridgeConfig<'_>) -> Buffer,

pub(super) _marker: PhantomData<fn(I) -> O>,
Expand Down Expand Up @@ -276,7 +274,7 @@ fn maybe_install_panic_hook(force_show_panics: bool) {
/// Client-side helper for handling client panics, entering the bridge,
/// deserializing input and serializing output.
// FIXME(eddyb) maybe replace `Bridge::enter` with this?
fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
fn run_client<A: for<'a, 's> Decode<'a, 's, ()>, R: Encode<()>>(
config: BridgeConfig<'_>,
f: impl FnOnce(A) -> R,
) -> Buffer {
Expand All @@ -296,9 +294,6 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(

let output = state::set(&state, || f(input));

// Take the `cached_buffer` back out, for the output value.
buf = RefCell::into_inner(state).cached_buffer;

// HACK(eddyb) Separate encoding a success value (`Ok(output)`)
// from encoding a panic (`Err(e: PanicMessage)`) to avoid
// having handles outside the `bridge.enter(|| ...)` scope, and
Expand All @@ -308,7 +303,6 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
// this is defensively trying to avoid any accidental panicking
// reaching the `extern "C"` (which should `abort` but might not
// at the moment, so this is also potentially preventing UB).
buf.clear();
Ok::<_, ()>(output).encode(&mut buf, &mut ());
}))
.map_err(PanicMessage::from)
Expand All @@ -326,7 +320,6 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
impl Client<crate::TokenStream, crate::TokenStream> {
pub const fn expand1(f: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy) -> Self {
Client {
handle_counters: &COUNTERS,
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
run_client(bridge, |input| f(crate::TokenStream(Some(input))).0)
}),
Expand All @@ -340,7 +333,6 @@ impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
f: impl Fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream + Copy,
) -> Self {
Client {
handle_counters: &COUNTERS,
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
run_client(bridge, |(input, input2)| {
f(crate::TokenStream(Some(input)), crate::TokenStream(Some(input2))).0
Expand Down
4 changes: 2 additions & 2 deletions library/proc_macro/src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ mod symbol;

use buffer::Buffer;
pub use rpc::PanicMessage;
use rpc::{DecodeMut, Encode, Reader, Writer};
use rpc::{Decode, Encode, Reader, Writer};

/// Configuration for establishing an active connection between a server and a
/// client. The server creates the bridge config (`run_server` in `server.rs`),
Expand All @@ -168,7 +168,7 @@ impl !Sync for BridgeConfig<'_> {}
#[forbid(unsafe_code)]
#[allow(non_camel_case_types)]
mod api_tags {
use super::rpc::{DecodeMut, Encode, Reader, Writer};
use super::rpc::{Decode, Encode, Reader, Writer};

macro_rules! declare_tags {
($($name:ident {
Expand Down
45 changes: 20 additions & 25 deletions library/proc_macro/src/bridge/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(super) trait Encode<S>: Sized {

pub(super) type Reader<'a> = &'a [u8];

pub(super) trait DecodeMut<'a, 's, S>: Sized {
pub(super) trait Decode<'a, 's, S>: Sized {
fn decode(r: &mut Reader<'a>, s: &'s mut S) -> Self;
}

Expand All @@ -24,7 +24,7 @@ macro_rules! rpc_encode_decode {
}
}

impl<S> DecodeMut<'_, '_, S> for $ty {
impl<S> Decode<'_, '_, S> for $ty {
fn decode(r: &mut Reader<'_>, _: &mut S) -> Self {
const N: usize = size_of::<$ty>();

Expand All @@ -43,12 +43,12 @@ macro_rules! rpc_encode_decode {
}
}

impl<'a, S, $($($T: for<'s> DecodeMut<'a, 's, S>),+)?> DecodeMut<'a, '_, S>
impl<'a, S, $($($T: for<'s> Decode<'a, 's, S>),+)?> Decode<'a, '_, S>
for $name $(<$($T),+>)?
{
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
$name {
$($field: DecodeMut::decode(r, s)),*
$($field: Decode::decode(r, s)),*
}
}
}
Expand All @@ -58,23 +58,18 @@ macro_rules! rpc_encode_decode {
fn encode(self, w: &mut Writer, s: &mut S) {
// HACK(eddyb): `Tag` enum duplicated between the
// two impls as there's no other place to stash it.
#[allow(non_upper_case_globals)]
mod tag {
#[repr(u8)] enum Tag { $($variant),* }

$(pub(crate) const $variant: u8 = Tag::$variant as u8;)*
}
#[repr(u8)] enum Tag { $($variant),* }

match self {
$($name::$variant $(($field))* => {
tag::$variant.encode(w, s);
(Tag::$variant as u8).encode(w, s);
$($field.encode(w, s);)*
})*
}
}
}

impl<'a, S, $($($T: for<'s> DecodeMut<'a, 's, S>),+)?> DecodeMut<'a, '_, S>
impl<'a, S, $($($T: for<'s> Decode<'a, 's, S>),+)?> Decode<'a, '_, S>
for $name $(<$($T),+>)?
{
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
Expand All @@ -89,7 +84,7 @@ macro_rules! rpc_encode_decode {

match u8::decode(r, s) {
$(tag::$variant => {
$(let $field = DecodeMut::decode(r, s);)*
$(let $field = Decode::decode(r, s);)*
$name::$variant $(($field))*
})*
_ => unreachable!(),
Expand All @@ -103,7 +98,7 @@ impl<S> Encode<S> for () {
fn encode(self, _: &mut Writer, _: &mut S) {}
}

impl<S> DecodeMut<'_, '_, S> for () {
impl<S> Decode<'_, '_, S> for () {
fn decode(_: &mut Reader<'_>, _: &mut S) -> Self {}
}

Expand All @@ -113,7 +108,7 @@ impl<S> Encode<S> for u8 {
}
}

impl<S> DecodeMut<'_, '_, S> for u8 {
impl<S> Decode<'_, '_, S> for u8 {
fn decode(r: &mut Reader<'_>, _: &mut S) -> Self {
let x = r[0];
*r = &r[1..];
Expand All @@ -130,7 +125,7 @@ impl<S> Encode<S> for bool {
}
}

impl<S> DecodeMut<'_, '_, S> for bool {
impl<S> Decode<'_, '_, S> for bool {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
match u8::decode(r, s) {
0 => false,
Expand All @@ -146,7 +141,7 @@ impl<S> Encode<S> for char {
}
}

impl<S> DecodeMut<'_, '_, S> for char {
impl<S> Decode<'_, '_, S> for char {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
char::from_u32(u32::decode(r, s)).unwrap()
}
Expand All @@ -158,7 +153,7 @@ impl<S> Encode<S> for NonZero<u32> {
}
}

impl<S> DecodeMut<'_, '_, S> for NonZero<u32> {
impl<S> Decode<'_, '_, S> for NonZero<u32> {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
Self::new(u32::decode(r, s)).unwrap()
}
Expand All @@ -171,11 +166,11 @@ impl<S, A: Encode<S>, B: Encode<S>> Encode<S> for (A, B) {
}
}

impl<'a, S, A: for<'s> DecodeMut<'a, 's, S>, B: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S>
impl<'a, S, A: for<'s> Decode<'a, 's, S>, B: for<'s> Decode<'a, 's, S>> Decode<'a, '_, S>
for (A, B)
{
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
(DecodeMut::decode(r, s), DecodeMut::decode(r, s))
(Decode::decode(r, s), Decode::decode(r, s))
}
}

Expand All @@ -186,7 +181,7 @@ impl<S> Encode<S> for &[u8] {
}
}

impl<'a, S> DecodeMut<'a, '_, S> for &'a [u8] {
impl<'a, S> Decode<'a, '_, S> for &'a [u8] {
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
let len = usize::decode(r, s);
let xs = &r[..len];
Expand All @@ -201,7 +196,7 @@ impl<S> Encode<S> for &str {
}
}

impl<'a, S> DecodeMut<'a, '_, S> for &'a str {
impl<'a, S> Decode<'a, '_, S> for &'a str {
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
str::from_utf8(<&[u8]>::decode(r, s)).unwrap()
}
Expand All @@ -213,7 +208,7 @@ impl<S> Encode<S> for String {
}
}

impl<S> DecodeMut<'_, '_, S> for String {
impl<S> Decode<'_, '_, S> for String {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
<&str>::decode(r, s).to_string()
}
Expand All @@ -228,7 +223,7 @@ impl<S, T: Encode<S>> Encode<S> for Vec<T> {
}
}

impl<'a, S, T: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> for Vec<T> {
impl<'a, S, T: for<'s> Decode<'a, 's, S>> Decode<'a, '_, S> for Vec<T> {
fn decode(r: &mut Reader<'a>, s: &mut S) -> Self {
let len = usize::decode(r, s);
let mut vec = Vec::with_capacity(len);
Expand Down Expand Up @@ -288,7 +283,7 @@ impl<S> Encode<S> for PanicMessage {
}
}

impl<S> DecodeMut<'_, '_, S> for PanicMessage {
impl<S> Decode<'_, '_, S> for PanicMessage {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
match Option::<String>::decode(r, s) {
Some(s) => PanicMessage::String(s),
Expand Down
2 changes: 1 addition & 1 deletion library/proc_macro/src/bridge/selfless_reify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ macro_rules! define_reify_functions {
>(f: F) -> $(extern $abi)? fn($($arg_ty),*) -> $ret_ty {
// FIXME(eddyb) describe the `F` type (e.g. via `type_name::<F>`) once panic
// formatting becomes possible in `const fn`.
assert!(size_of::<F>() == 0, "selfless_reify: closure must be zero-sized");
const { assert!(size_of::<F>() == 0, "selfless_reify: closure must be zero-sized"); }

$(extern $abi)? fn wrapper<
$($($param,)*)?
Expand Down
26 changes: 12 additions & 14 deletions library/proc_macro/src/bridge/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ macro_rules! define_server_handles {
}

impl<S: Types> HandleStore<S> {
fn new(handle_counters: &'static client::HandleCounters) -> Self {
fn new() -> Self {
use super::client::COUNTERS;
HandleStore {
$($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
$($ity: handle::InternedStore::new(&handle_counters.$ity),)*
$($oty: handle::OwnedStore::new(&COUNTERS.$oty),)*
$($ity: handle::InternedStore::new(&COUNTERS.$ity),)*
}
}
}
Expand All @@ -32,23 +33,23 @@ macro_rules! define_server_handles {
}
}

impl<S: Types> DecodeMut<'_, '_, HandleStore<MarkedTypes<S>>>
impl<S: Types> Decode<'_, '_, HandleStore<MarkedTypes<S>>>
for Marked<S::$oty, client::$oty>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
s.$oty.take(handle::Handle::decode(r, &mut ()))
}
}

impl<'s, S: Types> DecodeMut<'_, 's, HandleStore<MarkedTypes<S>>>
impl<'s, S: Types> Decode<'_, 's, HandleStore<MarkedTypes<S>>>
for &'s Marked<S::$oty, client::$oty>
{
fn decode(r: &mut Reader<'_>, s: &'s mut HandleStore<MarkedTypes<S>>) -> Self {
&s.$oty[handle::Handle::decode(r, &mut ())]
}
}

impl<'s, S: Types> DecodeMut<'_, 's, HandleStore<MarkedTypes<S>>>
impl<'s, S: Types> Decode<'_, 's, HandleStore<MarkedTypes<S>>>
for &'s mut Marked<S::$oty, client::$oty>
{
fn decode(
Expand All @@ -67,7 +68,7 @@ macro_rules! define_server_handles {
}
}

impl<S: Types> DecodeMut<'_, '_, HandleStore<MarkedTypes<S>>>
impl<S: Types> Decode<'_, '_, HandleStore<MarkedTypes<S>>>
for Marked<S::$ity, client::$ity>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
Expand Down Expand Up @@ -355,17 +356,16 @@ pub trait MessagePipe<T>: Sized {
fn run_server<
S: Server,
I: Encode<HandleStore<MarkedTypes<S>>>,
O: for<'a, 's> DecodeMut<'a, 's, HandleStore<MarkedTypes<S>>>,
O: for<'a, 's> Decode<'a, 's, HandleStore<MarkedTypes<S>>>,
>(
strategy: &impl ExecutionStrategy,
handle_counters: &'static client::HandleCounters,
server: S,
input: I,
run_client: extern "C" fn(BridgeConfig<'_>) -> Buffer,
force_show_panics: bool,
) -> Result<O, PanicMessage> {
let mut dispatcher =
Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) };
Dispatcher { handle_store: HandleStore::new(), server: MarkedTypes(server) };

let globals = dispatcher.server.globals();

Expand All @@ -389,10 +389,9 @@ impl client::Client<crate::TokenStream, crate::TokenStream> {
S: Server,
S::TokenStream: Default,
{
let client::Client { handle_counters, run, _marker } = *self;
let client::Client { run, _marker } = *self;
run_server(
strategy,
handle_counters,
server,
<MarkedTypes<S> as Types>::TokenStream::mark(input),
run,
Expand All @@ -415,10 +414,9 @@ impl client::Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream
S: Server,
S::TokenStream: Default,
{
let client::Client { handle_counters, run, _marker } = *self;
let client::Client { run, _marker } = *self;
run_server(
strategy,
handle_counters,
server,
(
<MarkedTypes<S> as Types>::TokenStream::mark(input),
Expand Down
4 changes: 2 additions & 2 deletions library/proc_macro/src/bridge/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<S> Encode<S> for Symbol {
}
}

impl<S: server::Server> DecodeMut<'_, '_, server::HandleStore<server::MarkedTypes<S>>>
impl<S: server::Server> Decode<'_, '_, server::HandleStore<server::MarkedTypes<S>>>
for Marked<S::Symbol, Symbol>
{
fn decode(r: &mut Reader<'_>, s: &mut server::HandleStore<server::MarkedTypes<S>>) -> Self {
Expand All @@ -118,7 +118,7 @@ impl<S: server::Server> Encode<server::HandleStore<server::MarkedTypes<S>>>
}
}

impl<S> DecodeMut<'_, '_, S> for Symbol {
impl<S> Decode<'_, '_, S> for Symbol {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
Symbol::new(<&str>::decode(r, s))
}
Expand Down
Loading