diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-06-25 08:01:14 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-06-25 08:01:14 +0100 |
commit | 44d525d4e02a8a4f2e21cde3377c4c0bd9269b41 (patch) | |
tree | f486439231cff1dcd0ef118bbbe72708d9113d75 /crates/ra_flycheck | |
parent | fb70eb6e21da82c585d06e57c3c78f4aed4878f3 (diff) | |
parent | 5a184fe85517507fd3b07c6fb36b017e558665f7 (diff) |
Merge #5048
5048: Unify code style for worker threads r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_flycheck')
-rw-r--r-- | crates/ra_flycheck/src/lib.rs | 66 |
1 files changed, 38 insertions, 28 deletions
diff --git a/crates/ra_flycheck/src/lib.rs b/crates/ra_flycheck/src/lib.rs index 0e2ee8698..af75adbe2 100644 --- a/crates/ra_flycheck/src/lib.rs +++ b/crates/ra_flycheck/src/lib.rs | |||
@@ -48,21 +48,23 @@ impl fmt::Display for FlycheckConfig { | |||
48 | /// diagnostics based on the output. | 48 | /// diagnostics based on the output. |
49 | /// The spawned thread is shut down when this struct is dropped. | 49 | /// The spawned thread is shut down when this struct is dropped. |
50 | #[derive(Debug)] | 50 | #[derive(Debug)] |
51 | pub struct Flycheck { | 51 | pub struct FlycheckHandle { |
52 | // XXX: drop order is significant | 52 | // XXX: drop order is significant |
53 | cmd_send: Sender<CheckCommand>, | 53 | cmd_send: Sender<CheckCommand>, |
54 | handle: jod_thread::JoinHandle<()>, | 54 | handle: jod_thread::JoinHandle<()>, |
55 | pub task_recv: Receiver<CheckTask>, | ||
56 | } | 55 | } |
57 | 56 | ||
58 | impl Flycheck { | 57 | impl FlycheckHandle { |
59 | pub fn new(config: FlycheckConfig, workspace_root: PathBuf) -> Flycheck { | 58 | pub fn spawn( |
60 | let (task_send, task_recv) = unbounded::<CheckTask>(); | 59 | sender: Box<dyn Fn(CheckTask) + Send>, |
60 | config: FlycheckConfig, | ||
61 | workspace_root: PathBuf, | ||
62 | ) -> FlycheckHandle { | ||
61 | let (cmd_send, cmd_recv) = unbounded::<CheckCommand>(); | 63 | let (cmd_send, cmd_recv) = unbounded::<CheckCommand>(); |
62 | let handle = jod_thread::spawn(move || { | 64 | let handle = jod_thread::spawn(move || { |
63 | FlycheckThread::new(config, workspace_root).run(&task_send, &cmd_recv); | 65 | FlycheckActor::new(sender, config, workspace_root).run(&cmd_recv); |
64 | }); | 66 | }); |
65 | Flycheck { task_recv, cmd_send, handle } | 67 | FlycheckHandle { cmd_send, handle } |
66 | } | 68 | } |
67 | 69 | ||
68 | /// Schedule a re-start of the cargo check worker. | 70 | /// Schedule a re-start of the cargo check worker. |
@@ -95,7 +97,8 @@ pub enum CheckCommand { | |||
95 | Update, | 97 | Update, |
96 | } | 98 | } |
97 | 99 | ||
98 | struct FlycheckThread { | 100 | struct FlycheckActor { |
101 | sender: Box<dyn Fn(CheckTask) + Send>, | ||
99 | config: FlycheckConfig, | 102 | config: FlycheckConfig, |
100 | workspace_root: PathBuf, | 103 | workspace_root: PathBuf, |
101 | last_update_req: Option<Instant>, | 104 | last_update_req: Option<Instant>, |
@@ -109,9 +112,14 @@ struct FlycheckThread { | |||
109 | check_process: Option<jod_thread::JoinHandle<()>>, | 112 | check_process: Option<jod_thread::JoinHandle<()>>, |
110 | } | 113 | } |
111 | 114 | ||
112 | impl FlycheckThread { | 115 | impl FlycheckActor { |
113 | fn new(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckThread { | 116 | fn new( |
114 | FlycheckThread { | 117 | sender: Box<dyn Fn(CheckTask) + Send>, |
118 | config: FlycheckConfig, | ||
119 | workspace_root: PathBuf, | ||
120 | ) -> FlycheckActor { | ||
121 | FlycheckActor { | ||
122 | sender, | ||
115 | config, | 123 | config, |
116 | workspace_root, | 124 | workspace_root, |
117 | last_update_req: None, | 125 | last_update_req: None, |
@@ -120,9 +128,9 @@ impl FlycheckThread { | |||
120 | } | 128 | } |
121 | } | 129 | } |
122 | 130 | ||
123 | fn run(&mut self, task_send: &Sender<CheckTask>, cmd_recv: &Receiver<CheckCommand>) { | 131 | fn run(&mut self, cmd_recv: &Receiver<CheckCommand>) { |
124 | // If we rerun the thread, we need to discard the previous check results first | 132 | // If we rerun the thread, we need to discard the previous check results first |
125 | self.clean_previous_results(task_send); | 133 | self.clean_previous_results(); |
126 | 134 | ||
127 | loop { | 135 | loop { |
128 | select! { | 136 | select! { |
@@ -134,7 +142,7 @@ impl FlycheckThread { | |||
134 | }, | 142 | }, |
135 | }, | 143 | }, |
136 | recv(self.message_recv) -> msg => match msg { | 144 | recv(self.message_recv) -> msg => match msg { |
137 | Ok(msg) => self.handle_message(msg, task_send), | 145 | Ok(msg) => self.handle_message(msg), |
138 | Err(RecvError) => { | 146 | Err(RecvError) => { |
139 | // Watcher finished, replace it with a never channel to | 147 | // Watcher finished, replace it with a never channel to |
140 | // avoid busy-waiting. | 148 | // avoid busy-waiting. |
@@ -146,15 +154,15 @@ impl FlycheckThread { | |||
146 | 154 | ||
147 | if self.should_recheck() { | 155 | if self.should_recheck() { |
148 | self.last_update_req = None; | 156 | self.last_update_req = None; |
149 | task_send.send(CheckTask::ClearDiagnostics).unwrap(); | 157 | self.send(CheckTask::ClearDiagnostics); |
150 | self.restart_check_process(); | 158 | self.restart_check_process(); |
151 | } | 159 | } |
152 | } | 160 | } |
153 | } | 161 | } |
154 | 162 | ||
155 | fn clean_previous_results(&self, task_send: &Sender<CheckTask>) { | 163 | fn clean_previous_results(&self) { |
156 | task_send.send(CheckTask::ClearDiagnostics).unwrap(); | 164 | self.send(CheckTask::ClearDiagnostics); |
157 | task_send.send(CheckTask::Status(Status::End)).unwrap(); | 165 | self.send(CheckTask::Status(Status::End)); |
158 | } | 166 | } |
159 | 167 | ||
160 | fn should_recheck(&mut self) -> bool { | 168 | fn should_recheck(&mut self) -> bool { |
@@ -173,27 +181,25 @@ impl FlycheckThread { | |||
173 | } | 181 | } |
174 | } | 182 | } |
175 | 183 | ||
176 | fn handle_message(&self, msg: CheckEvent, task_send: &Sender<CheckTask>) { | 184 | fn handle_message(&self, msg: CheckEvent) { |
177 | match msg { | 185 | match msg { |
178 | CheckEvent::Begin => { | 186 | CheckEvent::Begin => { |
179 | task_send.send(CheckTask::Status(Status::Being)).unwrap(); | 187 | self.send(CheckTask::Status(Status::Being)); |
180 | } | 188 | } |
181 | 189 | ||
182 | CheckEvent::End => { | 190 | CheckEvent::End => { |
183 | task_send.send(CheckTask::Status(Status::End)).unwrap(); | 191 | self.send(CheckTask::Status(Status::End)); |
184 | } | 192 | } |
185 | 193 | ||
186 | CheckEvent::Msg(Message::CompilerArtifact(msg)) => { | 194 | CheckEvent::Msg(Message::CompilerArtifact(msg)) => { |
187 | task_send.send(CheckTask::Status(Status::Progress(msg.target.name))).unwrap(); | 195 | self.send(CheckTask::Status(Status::Progress(msg.target.name))); |
188 | } | 196 | } |
189 | 197 | ||
190 | CheckEvent::Msg(Message::CompilerMessage(msg)) => { | 198 | CheckEvent::Msg(Message::CompilerMessage(msg)) => { |
191 | task_send | 199 | self.send(CheckTask::AddDiagnostic { |
192 | .send(CheckTask::AddDiagnostic { | 200 | workspace_root: self.workspace_root.clone(), |
193 | workspace_root: self.workspace_root.clone(), | 201 | diagnostic: msg.message, |
194 | diagnostic: msg.message, | 202 | }); |
195 | }) | ||
196 | .unwrap(); | ||
197 | } | 203 | } |
198 | 204 | ||
199 | CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {} | 205 | CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {} |
@@ -271,6 +277,10 @@ impl FlycheckThread { | |||
271 | let _ = message_send.send(CheckEvent::End); | 277 | let _ = message_send.send(CheckEvent::End); |
272 | })) | 278 | })) |
273 | } | 279 | } |
280 | |||
281 | fn send(&self, check_task: CheckTask) { | ||
282 | (self.sender)(check_task) | ||
283 | } | ||
274 | } | 284 | } |
275 | 285 | ||
276 | enum CheckEvent { | 286 | enum CheckEvent { |