Skip to content

Commit 02b849a

Browse files
committed
Refactor a bit
1 parent f461dc4 commit 02b849a

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

crates/ra_proc_macro/src/process.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) struct ProcMacroProcessThread {
2929

3030
struct Task {
3131
req: Request,
32-
result_tx: Sender<Response>,
32+
result_tx: Sender<Option<Response>>,
3333
}
3434

3535
struct Process {
@@ -131,18 +131,21 @@ impl ProcMacroProcessSrv {
131131
Some(it) => it,
132132
};
133133
sender.send(Task { req: req.into(), result_tx }).unwrap();
134+
let res = result_rx
135+
.recv()
136+
.map_err(|_| ra_tt::ExpansionError::Unknown("Proc macro thread is closed.".into()))?;
134137

135-
let res = result_rx.recv().unwrap();
136138
match res {
137-
Response::Error(err) => {
139+
Some(Response::Error(err)) => {
138140
return Err(ra_tt::ExpansionError::ExpansionError(err.message));
139141
}
140-
_ => Ok(res.try_into().map_err(|err| {
142+
Some(res) => Ok(res.try_into().map_err(|err| {
141143
ra_tt::ExpansionError::Unknown(format!(
142144
"Fail to get response, reason : {:#?} ",
143145
err
144146
))
145147
})?),
148+
None => Err(ra_tt::ExpansionError::Unknown("Empty result".into())),
146149
}
147150
}
148151
}
@@ -156,8 +159,8 @@ fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
156159
for task in task_rx {
157160
let Task { req, result_tx } = task;
158161

159-
let res = match send_request(&mut stdin, &mut stdout, req) {
160-
Ok(res) => res,
162+
match send_request(&mut stdin, &mut stdout, req) {
163+
Ok(res) => result_tx.send(res).unwrap(),
161164
Err(_err) => {
162165
let res = Response::Error(ResponseError {
163166
code: ErrorCode::ServerErrorEnd,
@@ -174,12 +177,7 @@ fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
174177
};
175178
stdin = stdio.0;
176179
stdout = stdio.1;
177-
continue;
178180
}
179-
};
180-
181-
if let Some(res) = res {
182-
result_tx.send(res).unwrap();
183181
}
184182
}
185183
}

0 commit comments

Comments
 (0)