@@ -121,10 +121,13 @@ macro_rules! define_dispatcher {
121121}
122122with_api ! ( define_dispatcher, MarkedTokenStream <S >, MarkedSpan <S >, MarkedSymbol <S >) ;
123123
124+ // This trait is currently only implemented and used once, inside of this crate.
125+ // We keep it public to allow implementing more complex execution strategies in
126+ // the future, such as wasm proc-macros.
124127pub trait ExecutionStrategy {
125- fn run_bridge_and_client < S : Server > (
128+ fn run_bridge_and_client (
126129 & self ,
127- dispatcher : & mut Dispatcher < S > ,
130+ dispatcher : & mut Dispatcher < impl Server > ,
128131 input : Buffer ,
129132 run_client : extern "C" fn ( BridgeConfig < ' _ > ) -> Buffer ,
130133 force_show_panics : bool ,
@@ -164,82 +167,55 @@ impl Drop for RunningSameThreadGuard {
164167}
165168
166169pub struct MaybeCrossThread {
167- cross_thread : bool ,
170+ pub cross_thread : bool ,
168171}
169172
170- impl MaybeCrossThread {
171- pub const fn new ( cross_thread : bool ) -> Self {
172- MaybeCrossThread { cross_thread }
173- }
174- }
173+ pub const SAME_THREAD : MaybeCrossThread = MaybeCrossThread { cross_thread : false } ;
174+ pub const CROSS_THREAD : MaybeCrossThread = MaybeCrossThread { cross_thread : true } ;
175175
176176impl ExecutionStrategy for MaybeCrossThread {
177- fn run_bridge_and_client < S : Server > (
177+ fn run_bridge_and_client (
178178 & self ,
179- dispatcher : & mut Dispatcher < S > ,
179+ dispatcher : & mut Dispatcher < impl Server > ,
180180 input : Buffer ,
181181 run_client : extern "C" fn ( BridgeConfig < ' _ > ) -> Buffer ,
182182 force_show_panics : bool ,
183183 ) -> Buffer {
184184 if self . cross_thread || ALREADY_RUNNING_SAME_THREAD . get ( ) {
185- CrossThread . run_bridge_and_client ( dispatcher, input, run_client, force_show_panics)
186- } else {
187- SameThread . run_bridge_and_client ( dispatcher, input, run_client, force_show_panics)
188- }
189- }
190- }
191-
192- pub struct SameThread ;
193-
194- impl ExecutionStrategy for SameThread {
195- fn run_bridge_and_client < S : Server > (
196- & self ,
197- dispatcher : & mut Dispatcher < S > ,
198- input : Buffer ,
199- run_client : extern "C" fn ( BridgeConfig < ' _ > ) -> Buffer ,
200- force_show_panics : bool ,
201- ) -> Buffer {
202- let _guard = RunningSameThreadGuard :: new ( ) ;
203-
204- let mut dispatch = |buf| dispatcher. dispatch ( buf) ;
205-
206- run_client ( BridgeConfig { input, dispatch : ( & mut dispatch) . into ( ) , force_show_panics } )
207- }
208- }
209-
210- pub struct CrossThread ;
185+ let ( mut server, mut client) = MessagePipe :: new ( ) ;
186+
187+ let join_handle = thread:: spawn ( move || {
188+ let mut dispatch = |b : Buffer | -> Buffer {
189+ client. send ( b) ;
190+ client. recv ( ) . expect ( "server died while client waiting for reply" )
191+ } ;
192+
193+ run_client ( BridgeConfig {
194+ input,
195+ dispatch : ( & mut dispatch) . into ( ) ,
196+ force_show_panics,
197+ } )
198+ } ) ;
199+
200+ while let Some ( b) = server. recv ( ) {
201+ server. send ( dispatcher. dispatch ( b) ) ;
202+ }
211203
212- impl ExecutionStrategy for CrossThread {
213- fn run_bridge_and_client < S : Server > (
214- & self ,
215- dispatcher : & mut Dispatcher < S > ,
216- input : Buffer ,
217- run_client : extern "C" fn ( BridgeConfig < ' _ > ) -> Buffer ,
218- force_show_panics : bool ,
219- ) -> Buffer {
220- let ( mut server, mut client) = MessagePipe :: new ( ) ;
204+ join_handle. join ( ) . unwrap ( )
205+ } else {
206+ let _guard = RunningSameThreadGuard :: new ( ) ;
221207
222- let join_handle = thread:: spawn ( move || {
223- let mut dispatch = |b : Buffer | -> Buffer {
224- client. send ( b) ;
225- client. recv ( ) . expect ( "server died while client waiting for reply" )
226- } ;
208+ let mut dispatch = |buf| dispatcher. dispatch ( buf) ;
227209
228210 run_client ( BridgeConfig { input, dispatch : ( & mut dispatch) . into ( ) , force_show_panics } )
229- } ) ;
230-
231- while let Some ( b) = server. recv ( ) {
232- server. send ( dispatcher. dispatch ( b) ) ;
233211 }
234-
235- join_handle. join ( ) . unwrap ( )
236212 }
237213}
238214
239215/// A message pipe used for communicating between server and client threads.
240216struct MessagePipe < T > {
241- tx : std :: sync :: mpsc:: SyncSender < T > ,
242- rx : std :: sync :: mpsc:: Receiver < T > ,
217+ tx : mpsc:: SyncSender < T > ,
218+ rx : mpsc:: Receiver < T > ,
243219}
244220
245221impl < T > MessagePipe < T > {
0 commit comments