From b4851087651b24a925cb4245fc8564cac9b255d4 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 21 Mar 2024 10:56:23 -0700 Subject: remove abort --- src/main.rs | 25 ++++++------------------- src/subprocess.rs | 22 +++++++++++++++++----- src/types.rs | 34 +--------------------------------- 3 files changed, 24 insertions(+), 57 deletions(-) diff --git a/src/main.rs b/src/main.rs index f44dba4..a2e9b51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ use { thread::available_parallelism, }, tokio::runtime::Builder, - types::{Abort, Fail}, + types::Fail, }; async fn run(threads: usize) -> Result<(), Fail> { @@ -62,24 +62,11 @@ fn main() -> impl Termination { .build() .expect("runtime failure"); - let errors = rt.block_on(async { - let abort = Abort::new(); - if let Err(err) = run(threads).await { - let mut errs = abort.fin().await; - errs.push(err); - errs - } else { - abort.fin().await - } - }); - - match errors[..] { - [] => ExitCode::SUCCESS, - [Fail::Interrupt] => ExitCode::from(130), - _ => { - for err in errors { - eprintln!("{}", Colour::Red.paint(format!("{err}"))); - } + match rt.block_on(run(threads)).err() { + None => ExitCode::SUCCESS, + Some(Fail::Interrupt) => ExitCode::from(130), + Some(e) => { + eprintln!("{}", Colour::Red.paint(format!("{e}"))); ExitCode::FAILURE } } diff --git a/src/subprocess.rs b/src/subprocess.rs index 427613c..99f492f 100644 --- a/src/subprocess.rs +++ b/src/subprocess.rs @@ -30,10 +30,22 @@ pub fn stream_into( where { let buf = BufWriter::new(writer); - try_unfold((buf, stream, path), |mut s| async { - match s.1.next().await { - None => Ok(None), - Some(Err(e)) => Err(e), + try_unfold((stream, buf, path), |mut s| async { + match s.0.next().await { + None => { + s.1 + .shutdown() + .await + .map_err(|e| Fail::IO(s.2.clone(), e.kind()))?; + Ok(None) + } + Some(Err(e)) => { + s.1 + .shutdown() + .await + .map_err(|e| Fail::IO(s.2.clone(), e.kind()))?; + Err(e) + } Some(Ok(print)) => { #[cfg(target_family = "unix")] let bytes = { @@ -45,7 +57,7 @@ where let tmp = print.to_string_lossy(); tmp.as_bytes() }; - s.0 + s.1 .write_all(bytes) .await .map_err(|e| Fail::IO(s.2.clone(), e.kind()))?; diff --git a/src/types.rs b/src/types.rs index a375467..0da5600 100644 --- a/src/types.rs +++ b/src/types.rs @@ -10,7 +10,7 @@ use { path::PathBuf, sync::Arc, }, - tokio::{sync::Notify, task::JoinError}, + tokio::task::JoinError, }; #[derive(Clone, Debug)] @@ -53,35 +53,3 @@ impl From for Fail { Self::BuildError(e) } } - -pub struct Abort { - errors: Mutex>, - rx: Notify, -} - -impl Abort { - pub fn new() -> Arc { - Arc::new(Self { - errors: Mutex::new(Vec::default()), - rx: Notify::new(), - }) - } - - pub async fn fin(&self) -> Vec { - self.errors.lock().await.to_vec() - } - - pub async fn send(&self, fail: Fail) { - let mut errors = self.errors.lock().await; - errors.push(fail); - self.rx.notify_waiters(); - } - - pub async fn notified(&self) { - let errors = self.errors.lock().await; - if errors.len() > 0 { - } else { - self.rx.notified().await; - } - } -} -- cgit v1.2.3