11//! The main loop of the proc-macro server.
2- use std:: io;
3-
42use proc_macro_api:: {
53 Codec ,
64 bidirectional_protocol:: msg as bidirectional,
75 legacy_protocol:: msg as legacy,
86 transport:: codec:: { json:: JsonProtocol , postcard:: PostcardProtocol } ,
97 version:: CURRENT_API_VERSION ,
108} ;
9+ use std:: { io, sync:: mpsc} ;
1110
1211use legacy:: Message ;
1312
@@ -170,6 +169,21 @@ fn handle_expand_id<W: std::io::Write, C: Codec>(
170169 send_response :: < _ , C > ( stdout, bidirectional:: Response :: ExpandMacro ( res) )
171170}
172171
172+ struct ProcMacroClientHandle {
173+ subreq_tx : mpsc:: Sender < bidirectional:: SubRequest > ,
174+ subresp_rx : mpsc:: Receiver < bidirectional:: SubResponse > ,
175+ }
176+
177+ impl proc_macro_srv:: ProcMacroClientInterface for ProcMacroClientHandle {
178+ fn source_text ( & self , file_id : u32 , start : u32 , end : u32 ) -> Option < String > {
179+ self . subreq_tx . send ( bidirectional:: SubRequest :: SourceText { file_id, start, end } ) . ok ( ) ?;
180+
181+ match self . subresp_rx . recv ( ) . ok ( ) ? {
182+ bidirectional:: SubResponse :: SourceTextResult { text } => text,
183+ }
184+ }
185+ }
186+
173187fn handle_expand_ra < W : io:: Write , R : io:: BufRead , C : Codec > (
174188 srv : & proc_macro_srv:: ProcMacroSrv < ' _ > ,
175189 stdin : & mut R ,
@@ -201,22 +215,20 @@ fn handle_expand_ra<W: io::Write, R: io::BufRead, C: Codec>(
201215 macro_body. to_tokenstream_resolved ( CURRENT_API_VERSION , & span_data_table, |a, b| {
202216 srv. join_spans ( a, b) . unwrap_or ( b)
203217 } ) ;
218+
204219 let attributes = attributes. map ( |it| {
205220 it. to_tokenstream_resolved ( CURRENT_API_VERSION , & span_data_table, |a, b| {
206221 srv. join_spans ( a, b) . unwrap_or ( b)
207222 } )
208223 } ) ;
209224
210- let ( subreq_tx, subreq_rx) = crossbeam_channel :: unbounded ( ) ;
211- let ( subresp_tx, subresp_rx) = crossbeam_channel :: unbounded ( ) ;
212- let ( result_tx, result_rx) = crossbeam_channel :: bounded ( 1 ) ;
225+ let ( subreq_tx, subreq_rx) = mpsc :: channel ( ) ;
226+ let ( subresp_tx, subresp_rx) = mpsc :: channel ( ) ;
227+ let ( result_tx, result_rx) = mpsc :: channel ( ) ;
213228
214- std:: thread:: scope ( |scope| {
215- scope. spawn ( || {
216- let callback = Box :: new ( move |req : proc_macro_srv:: SubRequest | {
217- subreq_tx. send ( req) . unwrap ( ) ;
218- subresp_rx. recv ( ) . unwrap ( )
219- } ) ;
229+ std:: thread:: scope ( |s| {
230+ s. spawn ( || {
231+ let callback = ProcMacroClientHandle { subreq_tx, subresp_rx } ;
220232
221233 let res = srv
222234 . expand (
@@ -229,7 +241,7 @@ fn handle_expand_ra<W: io::Write, R: io::BufRead, C: Codec>(
229241 def_site,
230242 call_site,
231243 mixed_site,
232- Some ( callback) ,
244+ Some ( Box :: new ( callback) ) ,
233245 )
234246 . map ( |it| {
235247 (
@@ -253,27 +265,31 @@ fn handle_expand_ra<W: io::Write, R: io::BufRead, C: Codec>(
253265
254266 loop {
255267 if let Ok ( res) = result_rx. try_recv ( ) {
256- send_response :: < _ , C > ( stdout, bidirectional:: Response :: ExpandMacroExtended ( res) )
257- . unwrap ( ) ;
268+ let _ = send_response :: < _ , C > (
269+ stdout,
270+ bidirectional:: Response :: ExpandMacroExtended ( res) ,
271+ ) ;
258272 break ;
259273 }
260274
261- let subreq = match subreq_rx. recv ( ) {
275+ let sub_req = match subreq_rx. recv ( ) {
262276 Ok ( r) => r,
263277 Err ( _) => break ,
264278 } ;
265279
266- let api_req = from_srv_req ( subreq) ;
267- bidirectional:: BidirectionalMessage :: SubRequest ( api_req) . write :: < _ , C > ( stdout) . unwrap ( ) ;
268-
269- let resp = bidirectional:: BidirectionalMessage :: read :: < _ , C > ( stdin, buf)
270- . unwrap ( )
271- . expect ( "client closed connection" ) ;
272-
280+ if bidirectional:: BidirectionalMessage :: SubRequest ( sub_req)
281+ . write :: < _ , C > ( stdout)
282+ . is_err ( )
283+ {
284+ break ;
285+ }
286+ let resp = match bidirectional:: BidirectionalMessage :: read :: < _ , C > ( stdin, buf) {
287+ Ok ( Some ( r) ) => r,
288+ _ => break ,
289+ } ;
273290 match resp {
274- bidirectional:: BidirectionalMessage :: SubResponse ( api_resp) => {
275- let srv_resp = from_client_res ( api_resp) ;
276- subresp_tx. send ( srv_resp) . unwrap ( ) ;
291+ bidirectional:: BidirectionalMessage :: SubResponse ( resp) => {
292+ let _ = subresp_tx. send ( resp) ;
277293 }
278294 other => panic ! ( "expected SubResponse, got {other:?}" ) ,
279295 }
@@ -425,22 +441,6 @@ fn run_<C: Codec>() -> io::Result<()> {
425441 Ok ( ( ) )
426442}
427443
428- fn from_srv_req ( value : proc_macro_srv:: SubRequest ) -> bidirectional:: SubRequest {
429- match value {
430- proc_macro_srv:: SubRequest :: SourceText { file_id, start, end } => {
431- bidirectional:: SubRequest :: SourceText { file_id : file_id. file_id ( ) . index ( ) , start, end }
432- }
433- }
434- }
435-
436- fn from_client_res ( value : bidirectional:: SubResponse ) -> proc_macro_srv:: SubResponse {
437- match value {
438- bidirectional:: SubResponse :: SourceTextResult { text } => {
439- proc_macro_srv:: SubResponse :: SourceTextResult { text }
440- }
441- }
442- }
443-
444444fn send_response < W : std:: io:: Write , C : Codec > (
445445 stdout : & mut W ,
446446 resp : bidirectional:: Response ,
0 commit comments