Skip to main content

operese_dagx_test/
lib.rs

1//! DAG builder helpers for internal use in dagx tests and benchmarks.
2//!
3//! This crate is not meant for public use and offers no stability guarantees.
4//! The ergonomics are also such that you don't want to use it anyways. **Use the `#[task]` macro instead.**
5
6#![cfg(not(tarpaulin_include))]
7
8use std::marker::PhantomData;
9
10use crate::extract::ExtractInput;
11
12use operese_dagx::{Task, TaskInput};
13
14mod extract;
15
16pub struct TaskFn<I, O, F>
17where
18    for<'input> F: FnMut(I::Retv<'input>) -> O + Send,
19    I: ExtractInput + Send + Sync + 'static,
20    O: Send,
21{
22    f: F,
23    _phantom: PhantomData<fn(I) -> O>,
24}
25
26impl<I, O, F> Task<I::Input> for TaskFn<I, O, F>
27where
28    for<'input> F: FnMut(I::Retv<'input>) -> O + Send,
29    I: ExtractInput + Send + Sync + 'static,
30    I::Input: Send + Sync + 'static,
31    O: Send + Sync + 'static,
32{
33    type Output = O;
34
35    async fn run(mut self, input: TaskInput<'_, I::Input>) -> O {
36        (self.f)(I::extract_from_task_input(input).unwrap())
37    }
38}
39
40/// Convenience function to create a task from a closure.
41pub fn task_fn<I, O, F>(f: F) -> TaskFn<I, O, F>
42where
43    for<'input> F: FnMut(I::Retv<'input>) -> O + Send,
44    I: ExtractInput + Send + Sync + 'static,
45    I::Input: Send + Sync + 'static,
46    O: Send + Sync + 'static,
47{
48    TaskFn {
49        f,
50        _phantom: PhantomData,
51    }
52}