49 lines
2.1 KiB
Rust
49 lines
2.1 KiB
Rust
use crossbeam_channel::Sender;
|
|||
|
|
use std::sync::Arc;
|
||
|
|
|
||
|
|
use super::error::PipelineError;
|
||
|
|
|
||
|
|
// ── Function types ────────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
/// Source function: called repeatedly, returns the next item or `EndOfStream`.
|
||
|
|
/// `FnMut` because it keeps an internal state (position in the iterator).
|
||
|
|
pub type SourceFn<D> = Box<dyn FnMut() -> Result<D, PipelineError> + Send>;
|
||
|
|
|
||
|
|
/// Sink function: consumes a final item, may fail (I/O error, etc.).
|
||
|
|
pub type SinkFn<D> = Box<dyn Fn(D) -> Result<(), PipelineError> + Send>;
|
||
|
|
|
||
|
|
/// Transform function shared between workers through an `Arc`.
|
||
|
|
pub type SharedFn<D> = Arc<dyn Fn(D) -> Result<D, PipelineError> + Send + Sync>;
|
||
|
|
|
||
|
|
/// 1→N transform function (flat map) shared between workers through an `Arc`.
|
||
|
|
///
|
||
|
|
/// The function receives the input item, a `push` channel to send each produced
|
||
|
|
/// item, and a `delta` channel to tell the scheduler how many additional items
|
||
|
|
/// entered the pipeline (N-1 if N items are produced).
|
||
|
|
/// It must call `delta.send(N - 1)` **after** pushing all the items.
|
||
|
|
pub type SharedFlatFn<D> =
|
||
|
|
Arc<dyn Fn(D, &Sender<Result<D, PipelineError>>, &Sender<isize>) + Send + Sync>;
|
||
|
|
|
||
|
|
// ── Stage enum ────────────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
/// A pipeline stage: plain transform (1→1) or flat transform (1→N).
|
||
|
|
pub enum Stage<D> {
|
||
|
|
Transform(SharedFn<D>),
|
||
|
|
Flat(SharedFlatFn<D>),
|
||
|
|
}
|
||
|
|
|
||
|
|
impl<D> Clone for Stage<D> {
|
||
|
|
fn clone(&self) -> Self {
|
||
|
|
match self {
|
||
|
|
Stage::Transform(f) => Stage::Transform(Arc::clone(f)),
|
||
|
|
Stage::Flat(f) => Stage::Flat(Arc::clone(f)),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ── Worker task ───────────────────────────────────────────────────────────────
|
||
|
|
|
||
|
|
pub(super) enum WorkerTask<D> {
|
||
|
|
Transform(D, usize),
|
||
|
|
Flat(D, usize),
|
||
|
|
}
|