From 7019a0a071a17df14073f4a5f0af673b9933dcf8 Mon Sep 17 00:00:00 2001 From: selfhoster1312 Date: Thu, 20 Apr 2023 12:44:48 +0200 Subject: [PATCH] ModuleSetup returns a concrete type via a generic type parameter --- src/modules/command/builder.rs | 2 +- src/modules/command/mod.rs | 6 +++--- src/modules/mod.rs | 8 ++++---- src/modules/package/mod.rs | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/modules/command/builder.rs b/src/modules/command/builder.rs index c5f6692..4354bc8 100644 --- a/src/modules/command/builder.rs +++ b/src/modules/command/builder.rs @@ -137,6 +137,6 @@ impl>> CommandBuilder { } pub fn run(self) -> Result { - self.build().run().into() + CommandModule::run(self.build()).into() } } diff --git a/src/modules/command/mod.rs b/src/modules/command/mod.rs index 56972e7..d36cc92 100644 --- a/src/modules/command/mod.rs +++ b/src/modules/command/mod.rs @@ -30,9 +30,9 @@ impl CommandModule { } } -impl ModuleSetup for CommandModule { - fn with_facts(self, _facts: &Facts) -> Box> { - Box::new(self) +impl ModuleSetup for CommandModule { + fn with_facts(self, _facts: &Facts) -> CommandModule { + self } } diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 825262c..0be72f4 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -69,12 +69,12 @@ impl PlaybookRun { println!("{}", serde_json::to_string_pretty(&self).unwrap()); } - pub fn run, A: Serialize, S: Serialize, E: Serialize>(&mut self, cmd: MS) -> Result { + pub fn run, MS: ModuleSetup, A: Serialize, S: Serialize, E: Serialize>(&mut self, cmd: MS) -> Result { let module = cmd.with_facts(&self.facts); self.run_inner(module) } - pub fn run_inner(&mut self, cmd: Box>) -> Result { + pub fn run_inner, A: Serialize, S: Serialize, E: Serialize>(&mut self, cmd: T) -> Result { // TODO: Check conditions for skip let module_name = cmd.module_name().to_string(); @@ -108,10 +108,10 @@ impl PlaybookRun { // } /// A trait for modules to be run in the context of a playbook, by gathering facts -pub trait ModuleSetup { + pub trait ModuleSetup, A: Serialize, S: Serialize, E: Serialize> { // type module: Module; - fn with_facts(self, facts: &Facts) -> Box>; + fn with_facts(self, facts: &Facts) -> T; } /// A Module takes some arguments which can be serialized back to the playbook run, and can be run to produce diff --git a/src/modules/package/mod.rs b/src/modules/package/mod.rs index a6299e4..202f442 100644 --- a/src/modules/package/mod.rs +++ b/src/modules/package/mod.rs @@ -53,8 +53,8 @@ impl Module for PackageModule for PackageModule { - fn with_facts(self, facts: &Facts) -> Box> { +impl ModuleSetup>, PackageArgs, (), PackageError> for PackageModule { + fn with_facts(self, facts: &Facts) -> PackageModule> { let Self { args, .. } = self; let manager: Box = match facts.os.family() { @@ -62,10 +62,10 @@ impl ModuleSetup for PackageModule { OsFamily::Archlinux => Box::new(ArchlinuxPackageManager), }; - Box::new(PackageModule { + PackageModule { args, manager, - }) + } } }