@@ -12,7 +12,6 @@ use std::{
1212 io:: { self , Write } ,
1313 path:: { Path , PathBuf } ,
1414 process:: { Child , Command , Stdio } ,
15- thread:: { spawn, JoinHandle } ,
1615} ;
1716
1817#[ derive( Debug , Default ) ]
@@ -22,8 +21,18 @@ pub(crate) struct ProcMacroProcessSrv {
2221
2322#[ derive( Debug ) ]
2423pub ( crate ) struct ProcMacroProcessThread {
25- handle : Option < JoinHandle < ( ) > > ,
26- sender : Sender < Task > ,
24+ // XXX: drop order is significant
25+ sender : SenderGuard ,
26+ handle : jod_thread:: JoinHandle < ( ) > ,
27+ }
28+
29+ #[ derive( Debug ) ]
30+ struct SenderGuard ( pub Sender < Task > ) ;
31+
32+ impl std:: ops:: Drop for SenderGuard {
33+ fn drop ( & mut self ) {
34+ let _ = self . 0 . send ( Task :: Close ) ;
35+ }
2736}
2837
2938enum Task {
@@ -62,31 +71,19 @@ impl Process {
6271 }
6372}
6473
65- impl std:: ops:: Drop for ProcMacroProcessThread {
66- fn drop ( & mut self ) {
67- if let Some ( handle) = self . handle . take ( ) {
68- let _ = self . sender . send ( Task :: Close ) ;
69-
70- // Join the thread, it should finish shortly. We don't really care
71- // whether it panicked, so it is safe to ignore the result
72- let _ = handle. join ( ) ;
73- }
74- }
75- }
76-
7774impl ProcMacroProcessSrv {
7875 pub fn run (
7976 process_path : & Path ,
8077 ) -> Result < ( ProcMacroProcessThread , ProcMacroProcessSrv ) , io:: Error > {
8178 let process = Process :: run ( process_path) ?;
8279
8380 let ( task_tx, task_rx) = bounded ( 0 ) ;
84- let handle = spawn ( move || {
81+ let handle = jod_thread :: spawn ( move || {
8582 client_loop ( task_rx, process) ;
8683 } ) ;
8784
8885 let srv = ProcMacroProcessSrv { inner : Some ( task_tx. clone ( ) ) } ;
89- let thread = ProcMacroProcessThread { handle : Some ( handle ) , sender : task_tx } ;
86+ let thread = ProcMacroProcessThread { handle, sender : SenderGuard ( task_tx) } ;
9087
9188 Ok ( ( thread, srv) )
9289 }
0 commit comments