mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Move interior mutability into ProcMacroSrvProcess
This commit is contained in:
parent
db15273d4d
commit
e92646962a
2 changed files with 31 additions and 43 deletions
|
@ -3,7 +3,7 @@
|
|||
use std::{
|
||||
io::{self, BufRead, BufReader, Read, Write},
|
||||
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
|
||||
sync::Arc,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use paths::AbsPath;
|
||||
|
@ -17,13 +17,20 @@ use crate::{
|
|||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ProcMacroProcessSrv {
|
||||
/// The state of the proc-macro server process, the protocol is currently strictly sequential
|
||||
/// hence the lock on the state.
|
||||
state: Mutex<ProcessSrvState>,
|
||||
version: u32,
|
||||
mode: SpanMode,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ProcessSrvState {
|
||||
process: Process,
|
||||
stdin: ChildStdin,
|
||||
stdout: BufReader<ChildStdout>,
|
||||
/// Populated when the server exits.
|
||||
server_exited: Option<ServerError>,
|
||||
version: u32,
|
||||
mode: SpanMode,
|
||||
}
|
||||
|
||||
impl ProcMacroProcessSrv {
|
||||
|
@ -36,10 +43,7 @@ impl ProcMacroProcessSrv {
|
|||
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");
|
||||
|
||||
io::Result::Ok(ProcMacroProcessSrv {
|
||||
process,
|
||||
stdin,
|
||||
stdout,
|
||||
server_exited: None,
|
||||
state: Mutex::new(ProcessSrvState { process, stdin, stdout, server_exited: None }),
|
||||
version: 0,
|
||||
mode: SpanMode::Id,
|
||||
})
|
||||
|
@ -76,7 +80,7 @@ impl ProcMacroProcessSrv {
|
|||
self.version
|
||||
}
|
||||
|
||||
pub(crate) fn version_check(&mut self) -> Result<u32, ServerError> {
|
||||
fn version_check(&self) -> Result<u32, ServerError> {
|
||||
let request = Request::ApiVersionCheck {};
|
||||
let response = self.send_task(request)?;
|
||||
|
||||
|
@ -86,7 +90,7 @@ impl ProcMacroProcessSrv {
|
|||
}
|
||||
}
|
||||
|
||||
fn enable_rust_analyzer_spans(&mut self) -> Result<SpanMode, ServerError> {
|
||||
fn enable_rust_analyzer_spans(&self) -> Result<SpanMode, ServerError> {
|
||||
let request = Request::SetConfig(crate::msg::ServerConfig {
|
||||
span_mode: crate::msg::SpanMode::RustAnalyzer,
|
||||
});
|
||||
|
@ -99,7 +103,7 @@ impl ProcMacroProcessSrv {
|
|||
}
|
||||
|
||||
pub(crate) fn find_proc_macros(
|
||||
&mut self,
|
||||
&self,
|
||||
dylib_path: &AbsPath,
|
||||
) -> Result<Result<Vec<(String, ProcMacroKind)>, String>, ServerError> {
|
||||
let request = Request::ListMacros { dylib_path: dylib_path.to_path_buf().into() };
|
||||
|
@ -112,20 +116,21 @@ impl ProcMacroProcessSrv {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn send_task(&mut self, req: Request) -> Result<Response, ServerError> {
|
||||
if let Some(server_error) = &self.server_exited {
|
||||
pub(crate) fn send_task(&self, req: Request) -> Result<Response, ServerError> {
|
||||
let state = &mut *self.state.lock().unwrap();
|
||||
if let Some(server_error) = &state.server_exited {
|
||||
return Err(server_error.clone());
|
||||
}
|
||||
|
||||
let mut buf = String::new();
|
||||
send_request(&mut self.stdin, &mut self.stdout, req, &mut buf).map_err(|e| {
|
||||
send_request(&mut state.stdin, &mut state.stdout, req, &mut buf).map_err(|e| {
|
||||
if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) {
|
||||
match self.process.child.try_wait() {
|
||||
match state.process.child.try_wait() {
|
||||
Ok(None) => e,
|
||||
Ok(Some(status)) => {
|
||||
let mut msg = String::new();
|
||||
if !status.success() {
|
||||
if let Some(stderr) = self.process.child.stderr.as_mut() {
|
||||
if let Some(stderr) = state.process.child.stderr.as_mut() {
|
||||
_ = stderr.read_to_string(&mut msg);
|
||||
}
|
||||
}
|
||||
|
@ -133,7 +138,7 @@ impl ProcMacroProcessSrv {
|
|||
message: format!("server exited with {status}: {msg}"),
|
||||
io: None,
|
||||
};
|
||||
self.server_exited = Some(server_error.clone());
|
||||
state.server_exited = Some(server_error.clone());
|
||||
server_error
|
||||
}
|
||||
Err(_) => e,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue