From dab8808e82b26a45cff00d1f49863562cf4f5ce8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 25 Jun 2020 08:39:33 +0200 Subject: Abstract over channel --- crates/ra_flycheck/src/lib.rs | 56 +++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 23 deletions(-) (limited to 'crates/ra_flycheck/src') diff --git a/crates/ra_flycheck/src/lib.rs b/crates/ra_flycheck/src/lib.rs index 6751e5c38..063603b45 100644 --- a/crates/ra_flycheck/src/lib.rs +++ b/crates/ra_flycheck/src/lib.rs @@ -52,17 +52,19 @@ pub struct FlycheckHandle { // XXX: drop order is significant cmd_send: Sender, handle: jod_thread::JoinHandle<()>, - pub task_recv: Receiver, } impl FlycheckHandle { - pub fn spawn(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckHandle { - let (task_send, task_recv) = unbounded::(); + pub fn spawn( + config: FlycheckConfig, + workspace_root: PathBuf, + sender: Box, + ) -> FlycheckHandle { let (cmd_send, cmd_recv) = unbounded::(); let handle = jod_thread::spawn(move || { - FlycheckActor::new(config, workspace_root).run(&task_send, &cmd_recv); + FlycheckActor::new(config, workspace_root, sender).run(&cmd_recv); }); - FlycheckHandle { task_recv, cmd_send, handle } + FlycheckHandle { cmd_send, handle } } /// Schedule a re-start of the cargo check worker. @@ -96,6 +98,7 @@ pub enum CheckCommand { } struct FlycheckActor { + sender: Box, config: FlycheckConfig, workspace_root: PathBuf, last_update_req: Option, @@ -110,8 +113,13 @@ struct FlycheckActor { } impl FlycheckActor { - fn new(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckActor { + fn new( + config: FlycheckConfig, + workspace_root: PathBuf, + sender: Box, + ) -> FlycheckActor { FlycheckActor { + sender, config, workspace_root, last_update_req: None, @@ -120,9 +128,9 @@ impl FlycheckActor { } } - fn run(&mut self, task_send: &Sender, cmd_recv: &Receiver) { + fn run(&mut self, cmd_recv: &Receiver) { // If we rerun the thread, we need to discard the previous check results first - self.clean_previous_results(task_send); + self.clean_previous_results(); loop { select! { @@ -134,7 +142,7 @@ impl FlycheckActor { }, }, recv(self.message_recv) -> msg => match msg { - Ok(msg) => self.handle_message(msg, task_send), + Ok(msg) => self.handle_message(msg), Err(RecvError) => { // Watcher finished, replace it with a never channel to // avoid busy-waiting. @@ -146,15 +154,15 @@ impl FlycheckActor { if self.should_recheck() { self.last_update_req = None; - task_send.send(CheckTask::ClearDiagnostics).unwrap(); + self.send(CheckTask::ClearDiagnostics); self.restart_check_process(); } } } - fn clean_previous_results(&self, task_send: &Sender) { - task_send.send(CheckTask::ClearDiagnostics).unwrap(); - task_send.send(CheckTask::Status(Status::End)).unwrap(); + fn clean_previous_results(&self) { + self.send(CheckTask::ClearDiagnostics); + self.send(CheckTask::Status(Status::End)); } fn should_recheck(&mut self) -> bool { @@ -173,27 +181,25 @@ impl FlycheckActor { } } - fn handle_message(&self, msg: CheckEvent, task_send: &Sender) { + fn handle_message(&self, msg: CheckEvent) { match msg { CheckEvent::Begin => { - task_send.send(CheckTask::Status(Status::Being)).unwrap(); + self.send(CheckTask::Status(Status::Being)); } CheckEvent::End => { - task_send.send(CheckTask::Status(Status::End)).unwrap(); + self.send(CheckTask::Status(Status::End)); } CheckEvent::Msg(Message::CompilerArtifact(msg)) => { - task_send.send(CheckTask::Status(Status::Progress(msg.target.name))).unwrap(); + self.send(CheckTask::Status(Status::Progress(msg.target.name))); } CheckEvent::Msg(Message::CompilerMessage(msg)) => { - task_send - .send(CheckTask::AddDiagnostic { - workspace_root: self.workspace_root.clone(), - diagnostic: msg.message, - }) - .unwrap(); + self.send(CheckTask::AddDiagnostic { + workspace_root: self.workspace_root.clone(), + diagnostic: msg.message, + }); } CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {} @@ -271,6 +277,10 @@ impl FlycheckActor { let _ = message_send.send(CheckEvent::End); })) } + + fn send(&self, check_task: CheckTask) { + (self.sender)(check_task) + } } enum CheckEvent { -- cgit v1.2.3