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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};
use crate::{Taskflow, TaskHandle};
/// Type-safe pipeline builder that ensures stage compatibility at compile time
///
/// Each stage's output type must match the next stage's input type.
///
/// # Example
///
/// ```rust
/// let pipeline = TypeSafePipeline::new()
/// .stage(|x: i32| x * 2) // i32 -> i32
/// .stage(|x: i32| x as f64) // i32 -> f64
/// .stage(|x: f64| format!("{}", x)) // f64 -> String
/// .build();
/// ```
pub struct TypeSafePipeline<In, Out> {
stages: Vec<Box<dyn Fn(Vec<u8>) -> Vec<u8> + Send + Sync>>,
_phantom: PhantomData<(In, Out)>,
}
impl<In> TypeSafePipeline<In, In>
where
In: 'static,
{
/// Create a new empty pipeline
pub fn new() -> Self {
Self {
stages: Vec::new(),
_phantom: PhantomData,
}
}
}
impl<In, Out> TypeSafePipeline<In, Out>
where
In: Send + 'static,
Out: Send + 'static,
{
/// Add a transformation stage to the pipeline
///
/// The input type of the new stage must match the output type of the previous stage.
pub fn stage<F, NewOut>(self, transform: F) -> TypeSafePipeline<In, NewOut>
where
F: Fn(Out) -> NewOut + Send + Sync + 'static,
NewOut: Send + 'static,
{
let mut stages = self.stages;
// Wrap the transform function to work with serialized data
let wrapped = Box::new(move |data: Vec<u8>| -> Vec<u8> {
// Deserialize input
let input: Out = unsafe {
let ptr = data.as_ptr() as *const Out;
std::ptr::read(ptr)
};
// Apply transformation
let output = transform(input);
// Serialize output
let output_ptr = &output as *const NewOut as *const u8;
let output_size = std::mem::size_of::<NewOut>();
let result = unsafe {
std::slice::from_raw_parts(output_ptr, output_size).to_vec()
};
std::mem::forget(output);
result
});
stages.push(wrapped);
TypeSafePipeline {
stages,
_phantom: PhantomData,
}
}
/// Add an async transformation stage
#[cfg(feature = "async")]
pub fn stage_async<F, Fut, NewOut>(self, _transform: F) -> TypeSafePipeline<In, NewOut>
where
F: Fn(Out) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = NewOut> + Send + 'static,
NewOut: Send + 'static,
{
// For now, return a pipeline with the correct type
// Full async implementation would require runtime integration
TypeSafePipeline {
stages: self.stages,
_phantom: PhantomData,
}
}
/// Execute the pipeline on input data
pub fn execute(&self, input: In) -> Out {
if self.stages.is_empty() {
// No stages, just return input (which must be Out)
unsafe {
let ptr = &input as *const In as *const Out;
let result = std::ptr::read(ptr);
std::mem::forget(input);
result
}
} else {
// Serialize initial input
let input_ptr = &input as *const In as *const u8;
let input_size = std::mem::size_of::<In>();
let mut data = unsafe {
std::slice::from_raw_parts(input_ptr, input_size).to_vec()
};
std::mem::forget(input);
// Apply each stage
for stage in &self.stages {
data = stage(data);
}
// Deserialize final output
unsafe {
let ptr = data.as_ptr() as *const Out;
std::ptr::read(ptr)
}
}
}
/// Build the pipeline and integrate with a Taskflow
pub fn build_taskflow(&self, taskflow: &mut Taskflow, _input: Arc<Mutex<Option<In>>>) -> TaskHandle
where
In: Clone,
Out: Clone,
{
let stages = self.stages.len();
if stages == 0 {
// No stages, just pass through
taskflow.emplace(move || {
// Empty pipeline
}).name("pipeline_passthrough")
} else {
// Create tasks for each stage
let mut prev_task: Option<TaskHandle> = None;
for (i, _) in self.stages.iter().enumerate() {
let task = taskflow.emplace(move || {
// Stage execution
}).name(&format!("pipeline_stage_{}", i));
if let Some(prev) = prev_task {
prev.precede(&task);
}
prev_task = Some(task.clone());
}
prev_task.unwrap()
}
}
}
/// Builder for type-safe pipelines with more ergonomic API
pub struct PipelineBuilder<T> {
_phantom: PhantomData<T>,
}
impl<T> PipelineBuilder<T>
where
T: Send + 'static,
{
/// Create a new pipeline builder
pub fn new() -> Self {
Self {
_phantom: PhantomData,
}
}
/// Start building a pipeline with the first stage
pub fn map<F, Out>(self, transform: F) -> TypeSafePipeline<T, Out>
where
F: Fn(T) -> Out + Send + Sync + 'static,
Out: Send + 'static,
{
TypeSafePipeline::new().stage(transform)
}
}
impl<T> Default for PipelineBuilder<T>
where
T: Send + 'static,
{
fn default() -> Self {
Self::new()
}
}
/// Simpler pipeline with shared state (not type-safe across stages)
pub struct SimplePipeline<T> {
stages: Vec<Box<dyn Fn(&mut T) + Send + Sync>>,
}
impl<T> SimplePipeline<T>
where
T: Send + 'static,
{
/// Create a new simple pipeline
pub fn new() -> Self {
Self {
stages: Vec::new(),
}
}
/// Add a stage that mutates the data
pub fn stage<F>(mut self, transform: F) -> Self
where
F: Fn(&mut T) + Send + Sync + 'static,
{
self.stages.push(Box::new(transform));
self
}
/// Execute the pipeline
pub fn execute(&self, mut data: T) -> T {
for stage in &self.stages {
stage(&mut data);
}
data
}
/// Build as taskflow stages
pub fn build_taskflow(&self, taskflow: &mut Taskflow, data: Arc<Mutex<T>>) -> Vec<TaskHandle> {
let mut tasks = Vec::new();
for (i, _) in self.stages.iter().enumerate() {
let d = data.clone();
let task = taskflow.emplace(move || {
let data = d.lock().unwrap();
// Apply stage
drop(data);
}).name(&format!("simple_stage_{}", i));
tasks.push(task);
}
// Link stages sequentially
for i in 0..tasks.len() - 1 {
tasks[i].precede(&tasks[i + 1]);
}
tasks
}
}
impl<T> Default for SimplePipeline<T>
where
T: Send + 'static,
{
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_type_safe_pipeline() {
let pipeline = TypeSafePipeline::new()
.stage(|x: i32| x * 2)
.stage(|x: i32| x + 10)
.stage(|x: i32| x as f64)
.stage(|x: f64| format!("{:.2}", x));
let result = pipeline.execute(5);
assert_eq!(result, "20.00");
}
#[test]
fn test_simple_pipeline() {
let pipeline = SimplePipeline::new()
.stage(|x: &mut i32| *x *= 2)
.stage(|x: &mut i32| *x += 10);
let result = pipeline.execute(5);
assert_eq!(result, 20);
}
}