refactor: move task_fn into its own crate

This commit is contained in:
TechnoPorg committed 2026-02-12 23:31:24 +01:00
1 parent b8204c4ed9
commit fe8de2784b
35 files changed
+125 -135

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
[package]
name = "dagx-test"
version = "0.3.1"
edition = "2021"
rust-version = "1.81"
authors = ["TechnoPorg"]
license = "MIT"
description = "Utilities for dagx's internal tests"
repository = "https://github.com/swaits/dagx"
homepage = "https://github.com/swaits/dagx"
documentation = "https://docs.rs/dagx-test"
[lints]
workspace = true
[dependencies]
dagx = { version = "0.3.1", path = "../" }
+158
View File
@@ -0,0 +1,158 @@
//! Input extraction for tasks from a list of dependencies.
use std::{collections::HashMap, hash::Hash, sync::Arc};
use crate::TaskInput;
/// Helper trait for input extraction from list of dependencies.
pub trait ExtractInput: Sized + Clone {
type Input: Send;
type Retv<'input>;
fn extract_from_task_input<'input>(
input: TaskInput<'input, Self::Input>,
) -> Result<Self::Retv<'input>, String>;
}
// Unit type - no dependencies
impl ExtractInput for () {
type Input = ();
type Retv<'input> = ();
fn extract_from_task_input(_input: TaskInput<'_, Self>) -> Result<Self, String> {
Ok(())
}
}
// Implementations for common primitive and standard library types.
// These allow tasks to receive these types as single inputs without wrapping in tuples.
// Receives Arc<T> from runner, clones the inner value for task consumption.
// For custom types, implement ExtractInput following the same pattern.
macro_rules! impl_extract_single {
($($t:ty),+) => {
$(
impl ExtractInput for $t {
type Input = ($t,);
type Retv<'input> = &'input $t;
fn extract_from_task_input<'input>(input: TaskInput<'input, Self::Input>) -> Result<Self::Retv<'input>, String> {
Ok(input.next().0)
}
}
)+
};
}
impl_extract_single!(
i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, bool, char, String
);
// Implement ExtractInput for Result types to support error handling patterns
impl<T, E> ExtractInput for Result<T, E>
where
T: 'static + Clone + Send + Sync,
E: 'static + Clone + Send + Sync,
{
type Input = (Self,);
type Retv<'input> = &'input Result<T, E>;
fn extract_from_task_input<'input>(
input: TaskInput<'input, (Self,)>,
) -> Result<Self::Retv<'input>, String> {
Ok(input.next().0)
}
}
// Implement ExtractInput for Option types
impl<T> ExtractInput for Option<T>
where
T: 'static + Clone + Send + Sync,
{
type Input = (Self,);
type Retv<'input> = &'input Option<T>;
fn extract_from_task_input<'input>(
input: TaskInput<'input, (Self,)>,
) -> Result<Self::Retv<'input>, String> {
Ok(input.next().0)
}
}
// Implement ExtractInput for Vec types
impl<T> ExtractInput for Vec<T>
where
T: 'static + Clone + Send + Sync,
{
type Input = (Self,);
type Retv<'input> = &'input Vec<T>;
fn extract_from_task_input<'input>(
input: TaskInput<'input, (Self,)>,
) -> Result<Self::Retv<'input>, String> {
Ok(input.next().0)
}
}
// Implement ExtractInput for HashMap types
impl<K, V> ExtractInput for HashMap<K, V>
where
K: 'static + Clone + Send + Sync + Eq + Hash,
V: 'static + Clone + Send + Sync,
{
type Input = (Self,);
type Retv<'input> = &'input HashMap<K, V>;
fn extract_from_task_input<'input>(
input: TaskInput<'input, (Self,)>,
) -> Result<Self::Retv<'input>, String> {
Ok(input.next().0)
}
}
impl<T> ExtractInput for Arc<T>
where
T: 'static + Clone + Send + Sync,
{
type Input = (Self,);
type Retv<'input> = &'input Arc<T>;
fn extract_from_task_input<'input>(
input: TaskInput<'input, (Self,)>,
) -> Result<Self::Retv<'input>, String> {
Ok(input.next().0)
}
}
macro_rules! impl_extract_tuple {
($($T:ident),+) => {
impl<$($T: 'static + Clone + Send + Sync),+> ExtractInput for ($($T,)+) {
type Input = Self;
type Retv<'input> = ($(&'input $T,)+);
fn extract_from_task_input<'input>(input: TaskInput<'input, Self::Input>) -> Result<Self::Retv<'input>, String> {
$(
let ($T, input): (&'input $T, _) = input.next();
)+
let _input: TaskInput<'input, ()> = input;
Ok(($($T,)+))
}
}
};
}
// Generate ExtractInput implementations for tuples of size 2-8.
// Each invocation creates an impl for a specific tuple size.
// Supporting up to 8 elements covers the vast majority of use cases.
#[allow(non_snake_case)]
mod impls {
use super::*;
impl_extract_tuple!(A, B);
impl_extract_tuple!(A, B, C);
impl_extract_tuple!(A, B, C, D);
impl_extract_tuple!(A, B, C, D, E);
impl_extract_tuple!(A, B, C, D, E, F);
impl_extract_tuple!(A, B, C, D, E, F, G);
impl_extract_tuple!(A, B, C, D, E, F, G, H);
}
+50
View File
@@ -0,0 +1,50 @@
//! DAG builder helpers for internal use in dagx tests and benchmarks.
//!
//! This crate is not meant for public use and offers no stability guarantees.
//! The ergonomics are also such that you don't want to use it anyways. **Use the `#[task]` macro instead.**
#![cfg(not(tarpaulin_include))]
use crate::extract::ExtractInput;
use dagx::{Task, TaskInput};
mod extract;
pub struct TaskFn<I, O, F>
where
for<'input> F: FnMut(I::Retv<'input>) -> O + Send,
I: ExtractInput + Send + Sync + 'static,
O: Send,
{
f: F,
_phantom: std::marker::PhantomData<fn(I) -> O>,
}
impl<I, O, F> Task for TaskFn<I, O, F>
where
for<'input> F: FnMut(I::Retv<'input>) -> O + Send,
I: ExtractInput + Send + Sync + 'static,
O: Send + Sync,
{
type Input = I::Input;
type Output = O;
async fn run(mut self, input: TaskInput<'_, I::Input>) -> O {
(self.f)(I::extract_from_task_input(input).unwrap())
}
}
/// Convenience function to create a task from a closure.
pub fn task_fn<I, O, F>(f: F) -> TaskFn<I, O, F>
where
for<'input> F: FnMut(I::Retv<'input>) -> O + Send,
I: ExtractInput + Send + Sync + 'static,
I::Input: Send + Sync + 'static,
O: Send + Sync + 'static,
{
TaskFn {
f,
_phantom: std::marker::PhantomData,
}
}