@@ -35,6 +35,7 @@ use std::{
3535 ffi:: OsString ,
3636 fs,
3737 path:: { Path , PathBuf } ,
38+ sync:: { Arc , Mutex , PoisonError } ,
3839 thread,
3940} ;
4041
@@ -53,7 +54,7 @@ pub enum ProcMacroKind {
5354pub const RUSTC_VERSION_STRING : & str = env ! ( "RUSTC_VERSION" ) ;
5455
5556pub struct ProcMacroSrv < ' env > {
56- expanders : HashMap < Utf8PathBuf , dylib:: Expander > ,
57+ expanders : Mutex < HashMap < Utf8PathBuf , Arc < dylib:: Expander > > > ,
5758 env : & ' env EnvSnapshot ,
5859}
5960
@@ -67,7 +68,7 @@ const EXPANDER_STACK_SIZE: usize = 8 * 1024 * 1024;
6768
6869impl ProcMacroSrv < ' _ > {
6970 pub fn expand < S : ProcMacroSrvSpan > (
70- & mut self ,
71+ & self ,
7172 lib : impl AsRef < Utf8Path > ,
7273 env : Vec < ( String , String ) > ,
7374 current_dir : Option < impl AsRef < Path > > ,
@@ -118,29 +119,37 @@ impl ProcMacroSrv<'_> {
118119 }
119120
120121 pub fn list_macros (
121- & mut self ,
122+ & self ,
122123 dylib_path : & Utf8Path ,
123124 ) -> Result < Vec < ( String , ProcMacroKind ) > , String > {
124125 let expander = self . expander ( dylib_path) ?;
125126 Ok ( expander. list_macros ( ) )
126127 }
127128
128- fn expander ( & mut self , path : & Utf8Path ) -> Result < & dylib:: Expander , String > {
129+ fn expander ( & self , path : & Utf8Path ) -> Result < Arc < dylib:: Expander > , String > {
129130 let expander = || {
130- dylib:: Expander :: new ( path)
131- . map_err ( |err| format ! ( "Cannot create expander for {path}: {err}" , ) )
131+ let expander = dylib:: Expander :: new ( path)
132+ . map_err ( |err| format ! ( "Cannot create expander for {path}: {err}" , ) ) ;
133+ expander. map ( Arc :: new)
132134 } ;
133135
134- Ok ( match self . expanders . entry ( path. to_path_buf ( ) ) {
135- Entry :: Vacant ( v) => v. insert ( expander ( ) ?) ,
136- Entry :: Occupied ( mut e) => {
137- let time = fs:: metadata ( path) . and_then ( |it| it. modified ( ) ) . ok ( ) ;
138- if Some ( e. get ( ) . modified_time ( ) ) != time {
139- e. insert ( expander ( ) ?) ;
136+ Ok (
137+ match self
138+ . expanders
139+ . lock ( )
140+ . unwrap_or_else ( PoisonError :: into_inner)
141+ . entry ( path. to_path_buf ( ) )
142+ {
143+ Entry :: Vacant ( v) => v. insert ( expander ( ) ?) . clone ( ) ,
144+ Entry :: Occupied ( mut e) => {
145+ let time = fs:: metadata ( path) . and_then ( |it| it. modified ( ) ) . ok ( ) ;
146+ if Some ( e. get ( ) . modified_time ( ) ) != time {
147+ e. insert ( expander ( ) ?) ;
148+ }
149+ e. get ( ) . clone ( )
140150 }
141- e. into_mut ( )
142- }
143- } )
151+ } ,
152+ )
144153 }
145154}
146155
0 commit comments