From 1af79ba82cbee24ccd900c986c99a94f731fbc6b Mon Sep 17 00:00:00 2001 From: matsu Date: Sun, 7 Jan 2024 08:37:10 +0000 Subject: [PATCH] refactor: remove futures --- Cargo.lock | 93 ----------------------------- Cargo.toml | 1 - src/app/components/cmd_executor.rs | 4 +- src/app/components/cmd_not_found.rs | 8 +-- src/app/retry.rs | 21 ++++--- src/app/supervise.rs | 20 +++---- src/main.rs | 39 +++++------- 7 files changed, 43 insertions(+), 143 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8f889c..b58e3f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -160,7 +160,6 @@ dependencies = [ "async-trait", "derive-getters", "derive-new", - "futures", "futures-lite", "lite-async-test", "predicates 1.0.8", @@ -229,48 +228,12 @@ dependencies = [ "num-traits", ] -[[package]] -name = "futures" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" -dependencies = [ - "futures-channel", - "futures-core", - "futures-executor", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-channel" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" -dependencies = [ - "futures-core", - "futures-sink", -] - [[package]] name = "futures-core" version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" -[[package]] -name = "futures-executor" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" -dependencies = [ - "futures-core", - "futures-task", - "futures-util", -] - [[package]] name = "futures-io" version = "0.3.30" @@ -290,47 +253,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "futures-macro" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.43", -] - -[[package]] -name = "futures-sink" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" - -[[package]] -name = "futures-task" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" - -[[package]] -name = "futures-util" -version = "0.3.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" -dependencies = [ - "futures-channel", - "futures-core", - "futures-io", - "futures-macro", - "futures-sink", - "futures-task", - "memchr", - "pin-project-lite", - "pin-utils", - "slab", -] - [[package]] name = "gimli" version = "0.28.1" @@ -464,12 +386,6 @@ version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - [[package]] name = "predicates" version = "1.0.8" @@ -602,15 +518,6 @@ dependencies = [ "libc", ] -[[package]] -name = "slab" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" -dependencies = [ - "autocfg", -] - [[package]] name = "strsim" version = "0.8.0" diff --git a/Cargo.toml b/Cargo.toml index 9d11d1e..b9daf1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ derive-getters = "0.3.0" anyhow = "1.0.78" tokio = { version = "1.35.1", features = ["rt-multi-thread", "macros", "time", "process", "io-util", "io-std"] } async-trait = "0.1.76" -futures = "0.3.30" structopt = "0.3.26" [dev-dependencies] diff --git a/src/app/components/cmd_executor.rs b/src/app/components/cmd_executor.rs index fc95c4a..bbd9117 100644 --- a/src/app/components/cmd_executor.rs +++ b/src/app/components/cmd_executor.rs @@ -2,7 +2,7 @@ use crate::io::*; #[derive(new)] pub struct CmdExecutor<'a> { - pub command: String, + pub command: &'a str, pub executor: &'a (dyn PipedCmdExecute + Send + Sync), } @@ -11,7 +11,7 @@ impl<'a> super::Component for CmdExecutor<'a> { type Output = anyhow::Result; async fn handle(&self) -> Self::Output { - let output = self.executor.piped_exec(self.command.as_str()).await?; + let output = self.executor.piped_exec(self.command).await?; Ok(output) } } diff --git a/src/app/components/cmd_not_found.rs b/src/app/components/cmd_not_found.rs index 493a17c..72c23b8 100644 --- a/src/app/components/cmd_not_found.rs +++ b/src/app/components/cmd_not_found.rs @@ -1,12 +1,12 @@ #[derive(new)] -pub struct PrintableCmdNotFound { - pub command: String, +pub struct PrintableCmdNotFound<'a, C> { + pub command: &'a str, pub inner: C, } #[async_trait::async_trait] -impl> + Send + Sync> super::Component - for PrintableCmdNotFound +impl<'a, T: 'static, C: super::Component> + Send + Sync> super::Component + for PrintableCmdNotFound<'a, C> { type Output = anyhow::Result; diff --git a/src/app/retry.rs b/src/app/retry.rs index 5a4ae27..0eccd07 100644 --- a/src/app/retry.rs +++ b/src/app/retry.rs @@ -53,7 +53,7 @@ where #[derive(new)] pub struct SharedParams<'a, C> { - command: String, + command: &'a str, interval: f64, executor: &'a (dyn PipedCmdExecute + Send + Sync), sleeper: &'a (dyn Sleep + Send + Sync), @@ -69,7 +69,7 @@ impl + Send + Sync> Component for SharedPar } } -impl<'a> From>>> +impl<'a> From>>> for SharedParams<'a, WaitSec<'a>> { fn from(state: SharedParams<'a, PrintableCmdNotFound>) -> Self { @@ -87,14 +87,14 @@ impl<'a> From>>> } impl<'a> From>> - for SharedParams<'a, PrintableCmdNotFound>> + for SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>> { fn from(state: SharedParams<'a, WaitSec>) -> Self { Self { inner: PrintableCmdNotFound { - command: state.command.to_owned(), + command: state.command, inner: CmdExecutor { - command: state.command.to_owned(), + command: state.command, executor: state.executor, }, }, @@ -107,10 +107,13 @@ impl<'a> From>> } impl<'a> - RetryApp>>, SharedParams<'a, WaitSec<'a>>> + RetryApp< + SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>, + SharedParams<'a, WaitSec<'a>>, + > { pub fn new( - command: String, + command: &'a str, count: Option, interval: f64, executor: &'a (dyn PipedCmdExecute + Send + Sync), @@ -118,11 +121,11 @@ impl<'a> ) -> Self { Self { state: State::ExecuteCommand(SharedParams::new( - command.to_owned(), + command, interval, executor, sleeper, - PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)), + PrintableCmdNotFound::new(command, CmdExecutor::new(command, executor)), )), count, } diff --git a/src/app/supervise.rs b/src/app/supervise.rs index 567d5c2..6fbb9ce 100644 --- a/src/app/supervise.rs +++ b/src/app/supervise.rs @@ -48,7 +48,7 @@ where #[derive(new)] pub struct SharedParams<'a, C> { - command: String, + command: &'a str, interval: f64, executor: &'a (dyn PipedCmdExecute + Send + Sync), sleeper: &'a (dyn Sleep + Send + Sync), @@ -64,10 +64,10 @@ impl + Send + Sync, 'a> Component for Share } } -impl<'a> From>>> +impl<'a> From>>> for SharedParams<'a, WaitSec<'a>> { - fn from(state: SharedParams<'a, PrintableCmdNotFound>>) -> Self { + fn from(state: SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>) -> Self { Self { inner: WaitSec { sec: state.interval, @@ -82,14 +82,14 @@ impl<'a> From>>> } impl<'a> From>> - for SharedParams<'a, PrintableCmdNotFound>> + for SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>> { fn from(state: SharedParams<'a, WaitSec<'a>>) -> Self { Self { inner: PrintableCmdNotFound { - command: state.command.to_owned(), + command: state.command, inner: CmdExecutor { - command: state.command.to_owned(), + command: state.command, executor: state.executor, }, }, @@ -103,12 +103,12 @@ impl<'a> From>> impl<'a> SuperviseApp< - SharedParams<'a, PrintableCmdNotFound>>, + SharedParams<'a, PrintableCmdNotFound<'a, CmdExecutor<'a>>>, SharedParams<'a, WaitSec<'a>>, > { pub fn new( - command: String, + command: &'a str, count: Option, interval: f64, executor: &'a (dyn PipedCmdExecute + Send + Sync), @@ -116,11 +116,11 @@ impl<'a> ) -> Self { Self { state: State::ExecuteCommand(SharedParams::new( - command.to_owned(), + command, interval, executor, sleeper, - PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)), + PrintableCmdNotFound::new(command, CmdExecutor::new(command, executor)), )), count, } diff --git a/src/main.rs b/src/main.rs index b69492a..9c8971f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,46 +30,37 @@ use io::*; #[tokio::main] async fn main() { - use futures::{future::Either, FutureExt}; use structopt::StructOpt; let config = Config::from_args(); let executor = PipedCmdExecutor; let sleeper = Sleeper; - let state_machine = match config { + let exit_code = match config { Config::retry { command, count, interval, - } => Either::Left( - run(RetryApp::new( - command.join(" "), - count, - interval, - &executor, - &sleeper, - )) - .map(|output| match output { + } => { + let command = command.join(" "); + let app = RetryApp::new(&command, count, interval, &executor, &sleeper); + let output = run(app).await; + match output { RetryResult::Success => 0, RetryResult::Failure => 1, - }), - ), + } + } Config::supervise { command, count, interval, - } => Either::Right( - run(SuperviseApp::new( - command.join(" "), - count, - interval, - &executor, - &sleeper, - )) - .map(|_| 0), - ), + } => { + let command = command.join(" "); + let app = SuperviseApp::new(&command, count, interval, &executor, &sleeper); + run(app).await; + 0 + } }; - std::process::exit(state_machine.await); + std::process::exit(exit_code); }