From 68691cedbfc82f7c2a57f6aaa0f2552f8ad1f8ba Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 27 Feb 2022 16:07:56 -0500 Subject: simplify --- src/main.rs | 1 + src/shell/event.rs | 76 +++++++++++++++++++++--------------------------------- 2 files changed, 31 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 65f1e52..f8db695 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ #![warn(clippy::get_unwrap)] #![allow(clippy::cognitive_complexity)] #![allow(clippy::missing_const_for_fn)] +#![allow(clippy::option_option)] #![allow(clippy::similar_names)] #![allow(clippy::struct_excessive_bools)] #![allow(clippy::too_many_arguments)] diff --git a/src/shell/event.rs b/src/shell/event.rs index 8f21081..b159a95 100644 --- a/src/shell/event.rs +++ b/src/shell/event.rs @@ -35,8 +35,20 @@ impl Writer { pub struct Reader(std::sync::Arc); impl Reader { - pub fn new(input: tokio::sync::mpsc::UnboundedReceiver) -> Self { - Self(InnerReader::new(input)) + pub fn new( + mut input: tokio::sync::mpsc::UnboundedReceiver, + ) -> Self { + let inner = std::sync::Arc::new(InnerReader::new()); + { + let inner = inner.clone(); + tokio::task::spawn(async move { + while let Some(event) = input.recv().await { + inner.new_event(Some(event)).await; + } + inner.new_event(None).await; + }); + } + Self(inner) } pub async fn recv(&self) -> Option { @@ -50,40 +62,24 @@ struct InnerReader { } impl InnerReader { - fn new( - mut input: tokio::sync::mpsc::UnboundedReceiver, - ) -> std::sync::Arc { - let this = Self { + fn new() -> Self { + Self { pending: tokio::sync::Mutex::new(Pending::new()), cvar: tokio::sync::Notify::new(), - }; - let this = std::sync::Arc::new(this); - { - let this = this.clone(); - tokio::task::spawn(async move { - while let Some(event) = input.recv().await { - this.new_event(Some(event)).await; - } - this.new_event(None).await; - }); } - this } async fn recv(&self) -> Option { loop { - let mut pending = self.pending.lock().await; - if pending.has_event() { - return pending.get_event(); + if let Some(event) = self.pending.lock().await.get_event() { + return event; } - drop(pending); self.cvar.notified().await; } } async fn new_event(&self, event: Option) { - let mut pending = self.pending.lock().await; - pending.new_event(event); + self.pending.lock().await.new_event(event); self.cvar.notify_one(); } } @@ -107,53 +103,41 @@ impl Pending { Self::default() } - fn has_event(&self) -> bool { - self.done - || !self.key.is_empty() - || self.size.is_some() - || self.pty_output - || self.pty_close - || !self.child_run_pipeline.is_empty() - || !self.child_suspend.is_empty() - || self.git_info.is_some() - || self.clock_timer - } - - fn get_event(&mut self) -> Option { + fn get_event(&mut self) -> Option> { if self.done { - return None; + return Some(None); } if let Some(key) = self.key.pop_front() { - return Some(Event::Key(key)); + return Some(Some(Event::Key(key))); } if let Some(size) = self.size.take() { - return Some(Event::Resize(size)); + return Some(Some(Event::Resize(size))); } if self.pty_close { self.pty_close = false; - return Some(Event::PtyClose); + return Some(Some(Event::PtyClose)); } if let Some((idx, span)) = self.child_run_pipeline.pop_front() { - return Some(Event::ChildRunPipeline(idx, span)); + return Some(Some(Event::ChildRunPipeline(idx, span))); } if let Some(idx) = self.child_suspend.pop_front() { - return Some(Event::ChildSuspend(idx)); + return Some(Some(Event::ChildSuspend(idx))); } if let Some(info) = self.git_info.take() { - return Some(Event::GitInfo(info)); + return Some(Some(Event::GitInfo(info))); } if self.clock_timer { self.clock_timer = false; - return Some(Event::ClockTimer); + return Some(Some(Event::ClockTimer)); } // process_output should be last because it will often be the case // that there is ~always new process output (cat on large files, yes, // etc) and that shouldn't prevent other events from happening if self.pty_output { self.pty_output = false; - return Some(Event::PtyOutput); + return Some(Some(Event::PtyOutput)); } - unreachable!() + None } fn new_event(&mut self, event: Option) { -- cgit v1.2.3-54-g00ecf