33use std:: {
44 io:: { self , BufRead , BufReader , Read , Write } ,
55 process:: { Child , ChildStdin , ChildStdout , Command , Stdio } ,
6- sync:: Arc ,
6+ sync:: { Arc , Mutex } ,
77} ;
88
99use paths:: AbsPath ;
@@ -17,13 +17,20 @@ use crate::{
1717
1818#[ derive( Debug ) ]
1919pub ( crate ) struct ProcMacroProcessSrv {
20+ /// The state of the proc-macro server process, the protocol is currently strictly sequential
21+ /// hence the lock on the state.
22+ state : Mutex < ProcessSrvState > ,
23+ version : u32 ,
24+ mode : SpanMode ,
25+ }
26+
27+ #[ derive( Debug ) ]
28+ struct ProcessSrvState {
2029 process : Process ,
2130 stdin : ChildStdin ,
2231 stdout : BufReader < ChildStdout > ,
2332 /// Populated when the server exits.
2433 server_exited : Option < ServerError > ,
25- version : u32 ,
26- mode : SpanMode ,
2734}
2835
2936impl ProcMacroProcessSrv {
@@ -36,10 +43,7 @@ impl ProcMacroProcessSrv {
3643 let ( stdin, stdout) = process. stdio ( ) . expect ( "couldn't access child stdio" ) ;
3744
3845 io:: Result :: Ok ( ProcMacroProcessSrv {
39- process,
40- stdin,
41- stdout,
42- server_exited : None ,
46+ state : Mutex :: new ( ProcessSrvState { process, stdin, stdout, server_exited : None } ) ,
4347 version : 0 ,
4448 mode : SpanMode :: Id ,
4549 } )
@@ -76,7 +80,7 @@ impl ProcMacroProcessSrv {
7680 self . version
7781 }
7882
79- pub ( crate ) fn version_check ( & mut self ) -> Result < u32 , ServerError > {
83+ fn version_check ( & self ) -> Result < u32 , ServerError > {
8084 let request = Request :: ApiVersionCheck { } ;
8185 let response = self . send_task ( request) ?;
8286
@@ -86,7 +90,7 @@ impl ProcMacroProcessSrv {
8690 }
8791 }
8892
89- fn enable_rust_analyzer_spans ( & mut self ) -> Result < SpanMode , ServerError > {
93+ fn enable_rust_analyzer_spans ( & self ) -> Result < SpanMode , ServerError > {
9094 let request = Request :: SetConfig ( crate :: msg:: ServerConfig {
9195 span_mode : crate :: msg:: SpanMode :: RustAnalyzer ,
9296 } ) ;
@@ -99,7 +103,7 @@ impl ProcMacroProcessSrv {
99103 }
100104
101105 pub ( crate ) fn find_proc_macros (
102- & mut self ,
106+ & self ,
103107 dylib_path : & AbsPath ,
104108 ) -> Result < Result < Vec < ( String , ProcMacroKind ) > , String > , ServerError > {
105109 let request = Request :: ListMacros { dylib_path : dylib_path. to_path_buf ( ) . into ( ) } ;
@@ -112,28 +116,29 @@ impl ProcMacroProcessSrv {
112116 }
113117 }
114118
115- pub ( crate ) fn send_task ( & mut self , req : Request ) -> Result < Response , ServerError > {
116- if let Some ( server_error) = & self . server_exited {
119+ pub ( crate ) fn send_task ( & self , req : Request ) -> Result < Response , ServerError > {
120+ let state = & mut * self . state . lock ( ) . unwrap ( ) ;
121+ if let Some ( server_error) = & state. server_exited {
117122 return Err ( server_error. clone ( ) ) ;
118123 }
119124
120125 let mut buf = String :: new ( ) ;
121- send_request ( & mut self . stdin , & mut self . stdout , req, & mut buf) . map_err ( |e| {
126+ send_request ( & mut state . stdin , & mut state . stdout , req, & mut buf) . map_err ( |e| {
122127 if e. io . as_ref ( ) . map ( |it| it. kind ( ) ) == Some ( io:: ErrorKind :: BrokenPipe ) {
123- match self . process . child . try_wait ( ) {
128+ match state . process . child . try_wait ( ) {
124129 Ok ( None ) => e,
125130 Ok ( Some ( status) ) => {
126131 let mut msg = String :: new ( ) ;
127132 if !status. success ( ) {
128- if let Some ( stderr) = self . process . child . stderr . as_mut ( ) {
133+ if let Some ( stderr) = state . process . child . stderr . as_mut ( ) {
129134 _ = stderr. read_to_string ( & mut msg) ;
130135 }
131136 }
132137 let server_error = ServerError {
133138 message : format ! ( "server exited with {status}: {msg}" ) ,
134139 io : None ,
135140 } ;
136- self . server_exited = Some ( server_error. clone ( ) ) ;
141+ state . server_exited = Some ( server_error. clone ( ) ) ;
137142 server_error
138143 }
139144 Err ( _) => e,
0 commit comments