From 566fe881e7f03351a2eae73844865ecee1f84898 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Mon, 27 Nov 2023 01:27:29 -0800 Subject: [PATCH] fix!: return Arc instead of own in get_result(s) BREAKING this returns an Arc instead of T in the get_result methods Signed-off-by: Amin Yahyaabadi --- README.md | 2 +- benches/compute_dag_bench.rs | 2 +- examples/compute_dag.rs | 2 +- examples/engine.rs | 10 ++++++++-- src/engine/dag.rs | 32 ++------------------------------ src/engine/mod.rs | 4 ++-- 6 files changed, 15 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index dca25c0..5d746d4 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ fn main() { // Start executing this dag assert!(dag.start().unwrap()); // Get execution result. - let res = dag.into_result::().unwrap(); + let res = dag.get_result::().unwrap(); println!("The result is {}.", res); } ``` diff --git a/benches/compute_dag_bench.rs b/benches/compute_dag_bench.rs index 15392cb..e190b06 100644 --- a/benches/compute_dag_bench.rs +++ b/benches/compute_dag_bench.rs @@ -26,7 +26,7 @@ fn compute_dag(tasks: Vec) { assert!(dag.start().unwrap()); // Get execution result. - let _res = dag.into_result::().unwrap(); + let _res = dag.get_result::().unwrap(); } fn compute_dag_bench(bencher: &mut Criterion) { diff --git a/examples/compute_dag.rs b/examples/compute_dag.rs index b689dfc..a46d604 100644 --- a/examples/compute_dag.rs +++ b/examples/compute_dag.rs @@ -85,6 +85,6 @@ fn main() { // Start executing this dag assert!(dag.start().unwrap()); // Get execution result. - let res = dag.into_result::().unwrap(); + let res = dag.get_result::().unwrap(); println!("The result is {}.", res); } diff --git a/examples/engine.rs b/examples/engine.rs index ee75c54..3f9f2a7 100644 --- a/examples/engine.rs +++ b/examples/engine.rs @@ -78,6 +78,12 @@ fn main() { // Execute dag in order, the order should be dag1, dag2, dag3. assert_eq!(engine.run_sequential(), vec![true, true, true]); // Get the execution results of dag1 and dag2. - assert_eq!(engine.get_dag_result::("graph1").unwrap(), 100); - assert_eq!(engine.get_dag_result::("graph2").unwrap(), 1024); + assert_eq!( + engine.get_dag_result::("graph1").unwrap().as_ref(), + &100 + ); + assert_eq!( + engine.get_dag_result::("graph2").unwrap().as_ref(), + &1024 + ); } diff --git a/src/engine/dag.rs b/src/engine/dag.rs index 282fad1..24e140e 100644 --- a/src/engine/dag.rs +++ b/src/engine/dag.rs @@ -340,7 +340,7 @@ impl Dag { } /// Get the final execution result. - pub fn into_result(&self) -> Option> { + pub fn get_result(&self) -> Option> { if self.exe_sequence.is_empty() { None } else { @@ -352,15 +352,8 @@ impl Dag { } } - /// Get the final execution result. - /// - /// Note: This method might clone the value if there are references to the value. To avoid cloning, use [`Dag::into_result`]. - pub fn get_result(&self) -> Option { - self.into_result::().map(unwrap_or_clone) - } - /// Get the output of all tasks. - pub fn into_results(&self) -> HashMap>> { + pub fn get_results(&self) -> HashMap>> { let mut hm = HashMap::new(); for (id, state) in &self.execute_states { let output = match state.get_output() { @@ -372,29 +365,8 @@ impl Dag { hm } - /// Get the output of all tasks. - /// - /// Note: This method might clone the value if there are references to the value. To avoid cloning, use [`Dag::into_results`]. - pub fn get_results(&self) -> HashMap> { - let mut hm = HashMap::new(); - for (id, state) in &self.execute_states { - let output = match state.get_output() { - Some(content) => content.into_inner().map(unwrap_or_clone), - None => None, - }; - hm.insert(*id, output); - } - hm - } - /// Before the dag starts executing, set the dag's global environment variable. pub fn set_env(&mut self, env: EnvVar) { self.env = Arc::new(env); } } - -/// Unwrap an Arc to T if possible, otherwise clone the value. -// This can be removed when Arc::unwrap_or_clone is stabilized. -fn unwrap_or_clone(arc: Arc) -> T { - Arc::try_unwrap(arc).unwrap_or_else(|arc| (*arc).clone()) -} diff --git a/src/engine/mod.rs b/src/engine/mod.rs index eba7551..e8302f3 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -17,7 +17,7 @@ mod dag; mod graph; use crate::ParseError; -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; use tokio::runtime::Runtime; /// The Engine. Manage multiple Dags. @@ -91,7 +91,7 @@ impl Engine { } /// Given the name of the Dag, get the execution result of the specified Dag. - pub fn get_dag_result(&self, name: &str) -> Option { + pub fn get_dag_result(&self, name: &str) -> Option> { if self.dags.contains_key(name) { self.dags.get(name).unwrap().get_result() } else {