From 31053484a791cef24e04e8b9bb9e28366dd63e91 Mon Sep 17 00:00:00 2001 From: cyrgani Date: Thu, 12 Feb 2026 12:14:14 +0000 Subject: [PATCH] replace `MessagePipe` trait with its impl --- compiler/rustc_expand/src/proc_macro.rs | 23 +--------- library/proc_macro/src/bridge/server.rs | 57 +++++++++++-------------- 2 files changed, 27 insertions(+), 53 deletions(-) diff --git a/compiler/rustc_expand/src/proc_macro.rs b/compiler/rustc_expand/src/proc_macro.rs index ea63ff7bfc46..530e7b998d41 100644 --- a/compiler/rustc_expand/src/proc_macro.rs +++ b/compiler/rustc_expand/src/proc_macro.rs @@ -11,29 +11,8 @@ use {rustc_ast as ast, rustc_proc_macro as pm}; use crate::base::{self, *}; use crate::{errors, proc_macro_server}; -struct MessagePipe { - tx: std::sync::mpsc::SyncSender, - rx: std::sync::mpsc::Receiver, -} - -impl pm::bridge::server::MessagePipe for MessagePipe { - fn new() -> (Self, Self) { - let (tx1, rx1) = std::sync::mpsc::sync_channel(1); - let (tx2, rx2) = std::sync::mpsc::sync_channel(1); - (MessagePipe { tx: tx1, rx: rx2 }, MessagePipe { tx: tx2, rx: rx1 }) - } - - fn send(&mut self, value: T) { - self.tx.send(value).unwrap(); - } - - fn recv(&mut self) -> Option { - self.rx.recv().ok() - } -} - fn exec_strategy(sess: &Session) -> impl pm::bridge::server::ExecutionStrategy + 'static { - pm::bridge::server::MaybeCrossThread::>::new( + pm::bridge::server::MaybeCrossThread::new( sess.opts.unstable_opts.proc_macro_execution_strategy == ProcMacroExecutionStrategy::CrossThread, ) diff --git a/library/proc_macro/src/bridge/server.rs b/library/proc_macro/src/bridge/server.rs index 3ab9f40de750..b5b63ead4464 100644 --- a/library/proc_macro/src/bridge/server.rs +++ b/library/proc_macro/src/bridge/server.rs @@ -1,7 +1,7 @@ //! Server-side traits. use std::cell::Cell; -use std::marker::PhantomData; +use std::sync::mpsc; use super::*; @@ -163,21 +163,17 @@ impl Drop for RunningSameThreadGuard { } } -pub struct MaybeCrossThread

{ +pub struct MaybeCrossThread { cross_thread: bool, - marker: PhantomData

, } -impl

MaybeCrossThread

{ +impl MaybeCrossThread { pub const fn new(cross_thread: bool) -> Self { - MaybeCrossThread { cross_thread, marker: PhantomData } + MaybeCrossThread { cross_thread } } } -impl

ExecutionStrategy for MaybeCrossThread

-where - P: MessagePipe + Send + 'static, -{ +impl ExecutionStrategy for MaybeCrossThread { fn run_bridge_and_client( &self, dispatcher: &mut Dispatcher, @@ -186,12 +182,7 @@ where force_show_panics: bool, ) -> Buffer { if self.cross_thread || ALREADY_RUNNING_SAME_THREAD.get() { - >::new().run_bridge_and_client( - dispatcher, - input, - run_client, - force_show_panics, - ) + CrossThread.run_bridge_and_client(dispatcher, input, run_client, force_show_panics) } else { SameThread.run_bridge_and_client(dispatcher, input, run_client, force_show_panics) } @@ -216,18 +207,9 @@ impl ExecutionStrategy for SameThread { } } -pub struct CrossThread

(PhantomData

); +pub struct CrossThread; -impl

CrossThread

{ - pub const fn new() -> Self { - CrossThread(PhantomData) - } -} - -impl

ExecutionStrategy for CrossThread

-where - P: MessagePipe + Send + 'static, -{ +impl ExecutionStrategy for CrossThread { fn run_bridge_and_client( &self, dispatcher: &mut Dispatcher, @@ -235,7 +217,7 @@ where run_client: extern "C" fn(BridgeConfig<'_>) -> Buffer, force_show_panics: bool, ) -> Buffer { - let (mut server, mut client) = P::new(); + let (mut server, mut client) = MessagePipe::new(); let join_handle = thread::spawn(move || { let mut dispatch = |b: Buffer| -> Buffer { @@ -255,18 +237,31 @@ where } /// A message pipe used for communicating between server and client threads. -pub trait MessagePipe: Sized { +struct MessagePipe { + tx: std::sync::mpsc::SyncSender, + rx: std::sync::mpsc::Receiver, +} + +impl MessagePipe { /// Creates a new pair of endpoints for the message pipe. - fn new() -> (Self, Self); + fn new() -> (Self, Self) { + let (tx1, rx1) = mpsc::sync_channel(1); + let (tx2, rx2) = mpsc::sync_channel(1); + (MessagePipe { tx: tx1, rx: rx2 }, MessagePipe { tx: tx2, rx: rx1 }) + } /// Send a message to the other endpoint of this pipe. - fn send(&mut self, value: T); + fn send(&mut self, value: T) { + self.tx.send(value).unwrap(); + } /// Receive a message from the other endpoint of this pipe. /// /// Returns `None` if the other end of the pipe has been destroyed, and no /// message was received. - fn recv(&mut self) -> Option; + fn recv(&mut self) -> Option { + self.rx.recv().ok() + } } fn run_server<