Skip to content

Commit 86e1529

Browse files
authored
Merge pull request #21160 from Wilfred/clarify_process_naming
minor: Clarify generic (not just cargo) process handling
2 parents 97a427c + 90d7580 commit 86e1529

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

src/tools/rust-analyzer/crates/rust-analyzer/src/command.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,36 @@ use paths::Utf8PathBuf;
1515
use process_wrap::std::{StdChildWrapper, StdCommandWrap};
1616
use stdx::process::streaming_output;
1717

18-
/// Cargo output is structured as one JSON per line. This trait abstracts parsing one line of
19-
/// cargo output into a Rust data type
20-
pub(crate) trait CargoParser<T>: Send + 'static {
18+
/// This trait abstracts parsing one line of JSON output into a Rust
19+
/// data type.
20+
///
21+
/// This is useful for `cargo check` output, `cargo test` output, as
22+
/// well as custom discover commands.
23+
pub(crate) trait JsonLinesParser<T>: Send + 'static {
2124
fn from_line(&self, line: &str, error: &mut String) -> Option<T>;
2225
fn from_eof(&self) -> Option<T>;
2326
}
2427

25-
struct CargoActor<T> {
26-
parser: Box<dyn CargoParser<T>>,
28+
struct CommandActor<T> {
29+
parser: Box<dyn JsonLinesParser<T>>,
2730
sender: Sender<T>,
2831
stdout: ChildStdout,
2932
stderr: ChildStderr,
3033
}
3134

32-
impl<T: Sized + Send + 'static> CargoActor<T> {
35+
impl<T: Sized + Send + 'static> CommandActor<T> {
3336
fn new(
34-
parser: impl CargoParser<T>,
37+
parser: impl JsonLinesParser<T>,
3538
sender: Sender<T>,
3639
stdout: ChildStdout,
3740
stderr: ChildStderr,
3841
) -> Self {
3942
let parser = Box::new(parser);
40-
CargoActor { parser, sender, stdout, stderr }
43+
CommandActor { parser, sender, stdout, stderr }
4144
}
4245
}
4346

44-
impl<T: Sized + Send + 'static> CargoActor<T> {
47+
impl<T: Sized + Send + 'static> CommandActor<T> {
4548
fn run(self, outfile: Option<Utf8PathBuf>) -> io::Result<(bool, String)> {
4649
// We manually read a line at a time, instead of using serde's
4750
// stream deserializers, because the deserializer cannot recover
@@ -113,6 +116,9 @@ impl<T: Sized + Send + 'static> CargoActor<T> {
113116
}
114117
}
115118

119+
/// 'Join On Drop' wrapper for a child process.
120+
///
121+
/// This wrapper kills the process when the wrapper is dropped.
116122
struct JodGroupChild(Box<dyn StdChildWrapper>);
117123

118124
impl Drop for JodGroupChild {
@@ -122,9 +128,9 @@ impl Drop for JodGroupChild {
122128
}
123129
}
124130

125-
/// A handle to a cargo process used for fly-checking.
131+
/// A handle to a shell command, such as cargo for diagnostics (flycheck).
126132
pub(crate) struct CommandHandle<T> {
127-
/// The handle to the actual cargo process. As we cannot cancel directly from with
133+
/// The handle to the actual child process. As we cannot cancel directly from with
128134
/// a read syscall dropping and therefore terminating the process is our best option.
129135
child: JodGroupChild,
130136
thread: stdx::thread::JoinHandle<io::Result<(bool, String)>>,
@@ -147,7 +153,7 @@ impl<T> fmt::Debug for CommandHandle<T> {
147153
impl<T: Sized + Send + 'static> CommandHandle<T> {
148154
pub(crate) fn spawn(
149155
mut command: Command,
150-
parser: impl CargoParser<T>,
156+
parser: impl JsonLinesParser<T>,
151157
sender: Sender<T>,
152158
out_file: Option<Utf8PathBuf>,
153159
) -> std::io::Result<Self> {
@@ -167,7 +173,7 @@ impl<T: Sized + Send + 'static> CommandHandle<T> {
167173
let stdout = child.0.stdout().take().unwrap();
168174
let stderr = child.0.stderr().take().unwrap();
169175

170-
let actor = CargoActor::<T>::new(parser, sender, stdout, stderr);
176+
let actor = CommandActor::<T>::new(parser, sender, stdout, stderr);
171177
let thread =
172178
stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker, "CommandHandle")
173179
.spawn(move || actor.run(out_file))

src/tools/rust-analyzer/crates/rust-analyzer/src/discover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use project_model::ProjectJsonData;
99
use serde::{Deserialize, Serialize};
1010
use tracing::{info_span, span::EnteredSpan};
1111

12-
use crate::command::{CargoParser, CommandHandle};
12+
use crate::command::{CommandHandle, JsonLinesParser};
1313

1414
pub(crate) const ARG_PLACEHOLDER: &str = "{arg}";
1515

@@ -118,7 +118,7 @@ impl DiscoverProjectMessage {
118118

119119
struct DiscoverProjectParser;
120120

121-
impl CargoParser<DiscoverProjectMessage> for DiscoverProjectParser {
121+
impl JsonLinesParser<DiscoverProjectMessage> for DiscoverProjectParser {
122122
fn from_line(&self, line: &str, _error: &mut String) -> Option<DiscoverProjectMessage> {
123123
match serde_json::from_str::<DiscoverProjectData>(line) {
124124
Ok(data) => {

src/tools/rust-analyzer/crates/rust-analyzer/src/flycheck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use toolchain::Tool;
2525
use triomphe::Arc;
2626

2727
use crate::{
28-
command::{CargoParser, CommandHandle},
28+
command::{CommandHandle, JsonLinesParser},
2929
diagnostics::DiagnosticsGeneration,
3030
};
3131

@@ -753,7 +753,7 @@ enum CargoCheckMessage {
753753

754754
struct CargoCheckParser;
755755

756-
impl CargoParser<CargoCheckMessage> for CargoCheckParser {
756+
impl JsonLinesParser<CargoCheckMessage> for CargoCheckParser {
757757
fn from_line(&self, line: &str, error: &mut String) -> Option<CargoCheckMessage> {
758758
let mut deserializer = serde_json::Deserializer::from_str(line);
759759
deserializer.disable_recursion_limit();

src/tools/rust-analyzer/crates/rust-analyzer/src/test_runner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serde_derive::Deserialize;
99
use toolchain::Tool;
1010

1111
use crate::{
12-
command::{CargoParser, CommandHandle},
12+
command::{CommandHandle, JsonLinesParser},
1313
flycheck::CargoOptions,
1414
};
1515

@@ -57,7 +57,7 @@ impl CargoTestOutputParser {
5757
}
5858
}
5959

60-
impl CargoParser<CargoTestMessage> for CargoTestOutputParser {
60+
impl JsonLinesParser<CargoTestMessage> for CargoTestOutputParser {
6161
fn from_line(&self, line: &str, _error: &mut String) -> Option<CargoTestMessage> {
6262
let mut deserializer = serde_json::Deserializer::from_str(line);
6363
deserializer.disable_recursion_limit();

0 commit comments

Comments
 (0)