aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_proc_macro/src/process.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_proc_macro/src/process.rs')
-rw-r--r--crates/ra_proc_macro/src/process.rs38
1 files changed, 18 insertions, 20 deletions
diff --git a/crates/ra_proc_macro/src/process.rs b/crates/ra_proc_macro/src/process.rs
index 37dd3f496..51ffcaa78 100644
--- a/crates/ra_proc_macro/src/process.rs
+++ b/crates/ra_proc_macro/src/process.rs
@@ -1,21 +1,22 @@
1//! Handle process life-time and message passing for proc-macro client 1//! Handle process life-time and message passing for proc-macro client
2 2
3use crossbeam_channel::{bounded, Receiver, Sender};
4use ra_tt::Subtree;
5
6use crate::msg::{ErrorCode, Message, Request, Response, ResponseError};
7use crate::rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
8
9use io::{BufRead, BufReader};
10use std::{ 3use std::{
11 convert::{TryFrom, TryInto}, 4 convert::{TryFrom, TryInto},
12 ffi::{OsStr, OsString}, 5 ffi::{OsStr, OsString},
13 io::{self, Write}, 6 io::{self, BufRead, BufReader, Write},
14 path::{Path, PathBuf}, 7 path::{Path, PathBuf},
15 process::{Child, Command, Stdio}, 8 process::{Child, Command, Stdio},
16 sync::{Arc, Weak}, 9 sync::{Arc, Weak},
17}; 10};
18 11
12use crossbeam_channel::{bounded, Receiver, Sender};
13use tt::Subtree;
14
15use crate::{
16 msg::{ErrorCode, Message, Request, Response, ResponseError},
17 rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind},
18};
19
19#[derive(Debug, Default)] 20#[derive(Debug, Default)]
20pub(crate) struct ProcMacroProcessSrv { 21pub(crate) struct ProcMacroProcessSrv {
21 inner: Option<Weak<Sender<Task>>>, 22 inner: Option<Weak<Sender<Task>>>,
@@ -50,7 +51,7 @@ impl ProcMacroProcessSrv {
50 pub fn find_proc_macros( 51 pub fn find_proc_macros(
51 &self, 52 &self,
52 dylib_path: &Path, 53 dylib_path: &Path,
53 ) -> Result<Vec<(String, ProcMacroKind)>, ra_tt::ExpansionError> { 54 ) -> Result<Vec<(String, ProcMacroKind)>, tt::ExpansionError> {
54 let task = ListMacrosTask { lib: dylib_path.to_path_buf() }; 55 let task = ListMacrosTask { lib: dylib_path.to_path_buf() };
55 56
56 let result: ListMacrosResult = self.send_task(Request::ListMacro(task))?; 57 let result: ListMacrosResult = self.send_task(Request::ListMacro(task))?;
@@ -62,7 +63,7 @@ impl ProcMacroProcessSrv {
62 dylib_path: &Path, 63 dylib_path: &Path,
63 subtree: &Subtree, 64 subtree: &Subtree,
64 derive_name: &str, 65 derive_name: &str,
65 ) -> Result<Subtree, ra_tt::ExpansionError> { 66 ) -> Result<Subtree, tt::ExpansionError> {
66 let task = ExpansionTask { 67 let task = ExpansionTask {
67 macro_body: subtree.clone(), 68 macro_body: subtree.clone(),
68 macro_name: derive_name.to_string(), 69 macro_name: derive_name.to_string(),
@@ -74,38 +75,35 @@ impl ProcMacroProcessSrv {
74 Ok(result.expansion) 75 Ok(result.expansion)
75 } 76 }
76 77
77 pub fn send_task<R>(&self, req: Request) -> Result<R, ra_tt::ExpansionError> 78 pub fn send_task<R>(&self, req: Request) -> Result<R, tt::ExpansionError>
78 where 79 where
79 R: TryFrom<Response, Error = &'static str>, 80 R: TryFrom<Response, Error = &'static str>,
80 { 81 {
81 let sender = match &self.inner { 82 let sender = match &self.inner {
82 None => return Err(ra_tt::ExpansionError::Unknown("No sender is found.".to_string())), 83 None => return Err(tt::ExpansionError::Unknown("No sender is found.".to_string())),
83 Some(it) => it, 84 Some(it) => it,
84 }; 85 };
85 86
86 let (result_tx, result_rx) = bounded(0); 87 let (result_tx, result_rx) = bounded(0);
87 let sender = match sender.upgrade() { 88 let sender = match sender.upgrade() {
88 None => { 89 None => {
89 return Err(ra_tt::ExpansionError::Unknown("Proc macro process is closed.".into())) 90 return Err(tt::ExpansionError::Unknown("Proc macro process is closed.".into()))
90 } 91 }
91 Some(it) => it, 92 Some(it) => it,
92 }; 93 };
93 sender.send(Task { req, result_tx }).unwrap(); 94 sender.send(Task { req, result_tx }).unwrap();
94 let res = result_rx 95 let res = result_rx
95 .recv() 96 .recv()
96 .map_err(|_| ra_tt::ExpansionError::Unknown("Proc macro thread is closed.".into()))?; 97 .map_err(|_| tt::ExpansionError::Unknown("Proc macro thread is closed.".into()))?;
97 98
98 match res { 99 match res {
99 Some(Response::Error(err)) => { 100 Some(Response::Error(err)) => {
100 return Err(ra_tt::ExpansionError::ExpansionError(err.message)); 101 return Err(tt::ExpansionError::ExpansionError(err.message));
101 } 102 }
102 Some(res) => Ok(res.try_into().map_err(|err| { 103 Some(res) => Ok(res.try_into().map_err(|err| {
103 ra_tt::ExpansionError::Unknown(format!( 104 tt::ExpansionError::Unknown(format!("Fail to get response, reason : {:#?} ", err))
104 "Fail to get response, reason : {:#?} ",
105 err
106 ))
107 })?), 105 })?),
108 None => Err(ra_tt::ExpansionError::Unknown("Empty result".into())), 106 None => Err(tt::ExpansionError::Unknown("Empty result".into())),
109 } 107 }
110 } 108 }
111} 109}