Skip to content

Commit

Permalink
refactor: removed unnecessary use of std::sync::Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtmtoo committed Jan 7, 2024
1 parent ad30c46 commit ee356c3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/app/components/cmd_executor.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::io::*;

#[derive(new)]
pub struct CmdExecutor {
pub struct CmdExecutor<'a> {
pub command: String,
pub executor: std::sync::Arc<dyn PipedCmdExecute + Send + Sync>,
pub executor: &'a (dyn PipedCmdExecute + Send + Sync),
}

#[async_trait::async_trait]
impl super::Component for CmdExecutor {
impl<'a> super::Component for CmdExecutor<'a> {
type Output = anyhow::Result<Exit>;

async fn handle(&self) -> Self::Output {
Expand Down
6 changes: 3 additions & 3 deletions src/app/components/wait.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::io::*;

pub struct WaitSec {
pub struct WaitSec<'a> {
pub sec: f64,
pub sleeper: std::sync::Arc<dyn Sleep + Send + Sync>,
pub sleeper: &'a (dyn Sleep + Send + Sync),
}

#[async_trait::async_trait]
impl super::Component for WaitSec {
impl<'a> super::Component for WaitSec<'a> {
type Output = ();

async fn handle(&self) -> Self::Output {
Expand Down
39 changes: 24 additions & 15 deletions src/app/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,27 @@ where
}

#[derive(new)]
pub struct SharedParams<C> {
pub struct SharedParams<'a, C> {
command: String,
interval: f64,
executor: std::sync::Arc<dyn PipedCmdExecute + Send + Sync>,
sleeper: std::sync::Arc<dyn Sleep + Send + Sync>,
executor: &'a (dyn PipedCmdExecute + Send + Sync),
sleeper: &'a (dyn Sleep + Send + Sync),
inner: C,
}

#[async_trait::async_trait]
impl<T: 'static, C: Component<Output = T> + Send + Sync> Component for SharedParams<C> {
impl<T: 'static, C: Component<Output = T> + Send + Sync> Component for SharedParams<'_, C> {
type Output = T;

async fn handle(&self) -> Self::Output {
self.inner.handle().await
}
}

impl From<SharedParams<PrintableCmdNotFound<CmdExecutor>>> for SharedParams<WaitSec> {
fn from(state: SharedParams<PrintableCmdNotFound<CmdExecutor>>) -> Self {
impl<'a> From<SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>>
for SharedParams<'a, WaitSec<'a>>
{
fn from(state: SharedParams<'a, PrintableCmdNotFound<CmdExecutor>>) -> Self {
Self {
inner: WaitSec {
sec: state.interval,
Expand All @@ -84,14 +86,16 @@ impl From<SharedParams<PrintableCmdNotFound<CmdExecutor>>> for SharedParams<Wait
}
}

impl From<SharedParams<WaitSec>> for SharedParams<PrintableCmdNotFound<CmdExecutor>> {
fn from(state: SharedParams<WaitSec>) -> Self {
impl<'a> From<SharedParams<'a, WaitSec<'a>>>
for SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>
{
fn from(state: SharedParams<'a, WaitSec>) -> Self {
Self {
inner: PrintableCmdNotFound {
command: state.command.to_owned(),
inner: CmdExecutor {
command: state.command.to_owned(),
executor: state.executor.clone(),
executor: state.executor,
},
},
command: state.command,
Expand All @@ -102,16 +106,21 @@ impl From<SharedParams<WaitSec>> for SharedParams<PrintableCmdNotFound<CmdExecut
}
}

impl RetryApp<SharedParams<PrintableCmdNotFound<CmdExecutor>>, SharedParams<WaitSec>> {
pub fn new(command: String, count: Option<usize>, interval: f64) -> Self {
let executor = std::sync::Arc::new(PipedCmdExecutor);
let sleeper = std::sync::Arc::new(Sleeper);

impl<'a>
RetryApp<SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>, SharedParams<'a, WaitSec<'a>>>
{
pub fn new(
command: String,
count: Option<usize>,
interval: f64,
executor: &'a (dyn PipedCmdExecute + Send + Sync),
sleeper: &'a (dyn Sleep + Send + Sync),
) -> Self {
Self {
state: State::ExecuteCommand(SharedParams::new(
command.to_owned(),
interval,
executor.clone(),
executor,
sleeper,
PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)),
)),
Expand Down
42 changes: 27 additions & 15 deletions src/app/supervise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,27 @@ where
}

#[derive(new)]
pub struct SharedParams<C> {
pub struct SharedParams<'a, C> {
command: String,
interval: f64,
executor: std::sync::Arc<dyn PipedCmdExecute + Send + Sync>,
sleeper: std::sync::Arc<dyn Sleep + Send + Sync>,
executor: &'a (dyn PipedCmdExecute + Send + Sync),
sleeper: &'a (dyn Sleep + Send + Sync),
inner: C,
}

#[async_trait::async_trait]
impl<T: 'static, C: Component<Output = T> + Send + Sync> Component for SharedParams<C> {
impl<T: 'static, C: Component<Output = T> + Send + Sync, 'a> Component for SharedParams<'a, C> {
type Output = T;

async fn handle(&self) -> Self::Output {
self.inner.handle().await
}
}

impl From<SharedParams<PrintableCmdNotFound<CmdExecutor>>> for SharedParams<WaitSec> {
fn from(state: SharedParams<PrintableCmdNotFound<CmdExecutor>>) -> Self {
impl<'a> From<SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>>
for SharedParams<'a, WaitSec<'a>>
{
fn from(state: SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>) -> Self {
Self {
inner: WaitSec {
sec: state.interval,
Expand All @@ -79,14 +81,16 @@ impl From<SharedParams<PrintableCmdNotFound<CmdExecutor>>> for SharedParams<Wait
}
}

impl From<SharedParams<WaitSec>> for SharedParams<PrintableCmdNotFound<CmdExecutor>> {
fn from(state: SharedParams<WaitSec>) -> Self {
impl<'a> From<SharedParams<'a, WaitSec<'a>>>
for SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>
{
fn from(state: SharedParams<'a, WaitSec<'a>>) -> Self {
Self {
inner: PrintableCmdNotFound {
command: state.command.to_owned(),
inner: CmdExecutor {
command: state.command.to_owned(),
executor: state.executor.clone(),
executor: &*state.executor,
},
},
command: state.command,
Expand All @@ -97,16 +101,24 @@ impl From<SharedParams<WaitSec>> for SharedParams<PrintableCmdNotFound<CmdExecut
}
}

impl SuperviseApp<SharedParams<PrintableCmdNotFound<CmdExecutor>>, SharedParams<WaitSec>> {
pub fn new(command: String, count: Option<usize>, interval: f64) -> Self {
let executor = std::sync::Arc::new(PipedCmdExecutor);
let sleeper = std::sync::Arc::new(Sleeper);

impl<'a>
SuperviseApp<
SharedParams<'a, PrintableCmdNotFound<CmdExecutor<'a>>>,
SharedParams<'a, WaitSec<'a>>,
>
{
pub fn new(
command: String,
count: Option<usize>,
interval: f64,
executor: &'a (dyn PipedCmdExecute + Send + Sync),
sleeper: &'a (dyn Sleep + Send + Sync),
) -> Self {
Self {
state: State::ExecuteCommand(SharedParams::new(
command.to_owned(),
interval,
executor.clone(),
executor,
sleeper,
PrintableCmdNotFound::new(command.to_owned(), CmdExecutor::new(command, executor)),
)),
Expand Down
23 changes: 21 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,31 @@ mod io;

use app::*;
use config::*;
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 {
Config::retry {
command,
count,
interval,
} => Either::Left(
run(RetryApp::new(command.join(" "), count, interval)).map(|output| match output {
run(RetryApp::new(
command.join(" "),
count,
interval,
&executor,
&sleeper,
))
.map(|output| match output {
RetryResult::Success => 0,
RetryResult::Failure => 1,
}),
Expand All @@ -49,7 +59,16 @@ async fn main() {
command,
count,
interval,
} => Either::Right(run(SuperviseApp::new(command.join(" "), count, interval)).map(|_| 0)),
} => Either::Right(
run(SuperviseApp::new(
command.join(" "),
count,
interval,
&executor,
&sleeper,
))
.map(|_| 0),
),
};

std::process::exit(state_machine.await);
Expand Down

0 comments on commit ee356c3

Please sign in to comment.