1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::{any::Any, sync::Arc};
use crate::{builder::NodeId, runner::PassThroughHashMap, TaskHandle};
pub struct DagOutput {
outputs: PassThroughHashMap<NodeId, Arc<dyn Any + Send + Sync + 'static>>,
}
impl DagOutput {
/// Retrieve a task's output after [`crate::DagRunner::run`].
///
/// # Behavior
///
/// All task outputs are stored after execution and can be retrieved via get().
///
/// # Panics
///
/// This function panics if called with a [`TaskHandle`] from a [`crate::DagRunner`] instance other than the
/// one which produced this DagOutput.
///
/// # Examples
///
/// ```no_run
/// # use operese_dagx::{task, DagRunner, Task};
/// #
/// struct Configuration {
/// setting: i32,
/// }
///
/// impl Configuration {
/// fn new(setting: i32) -> Self {
/// Self { setting }
/// }
/// }
///
/// #[task]
/// impl Configuration {
/// async fn run(&mut self) -> i32 { self.setting }
/// }
///
/// # async {
/// let mut dag = DagRunner::new();
///
/// // Construct task with specific setting value
/// let task = dag.add_task(Configuration::new(42));
///
/// let mut output = dag.run(|fut| async move { tokio::spawn(fut).await.unwrap() }).await.unwrap();
///
/// assert_eq!(output.get(task), 42);
/// # };
/// ```
pub fn get<T: 'static + Send + Sync>(&mut self, handle: TaskHandle<T>) -> T {
// Because this consumes the task handle, it can only be retrieved once, so is certain to be there
let arc_output = self.outputs.remove(&handle.id).unwrap();
// Downcast Arc<dyn Any> to Arc<T>
let output = arc_output.downcast::<T>().unwrap();
// Because we never give the user access to anything but a reference to the Arc,
// we can guarantee that the strong reference count is exactly one here
Arc::into_inner(output).unwrap()
}
pub(super) fn new(
outputs: PassThroughHashMap<NodeId, Arc<dyn Any + Send + Sync + 'static>>,
) -> Self {
Self { outputs }
}
}