p2panda_stream/processors/processor.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3/// Interface for implementing data processors.
4///
5/// Processors take in items (input) and yield "processed" or "enriched" versions of them at _some
6/// point_ (output). Please note that these items do _not_ necessarily need to come out in the same
7/// order as they came in. Processors can keep items around until their internal logic decides
8/// they're ready to go, and this can happen in any order required.
9///
10/// Users of processors will use the `process` method to insert "input" items and call `next` to
11/// find out if there's ready processed "output" items to take out.
12///
13/// The `process` method might apply back-pressure, for example when the processor is busy and
14/// can't take in more work.
15///
16/// Processors _never_ terminate. The future returned by `next` will stay in pending state whenever
17/// there's no work to do or the processor can't continue because of it's internal logic. Users can
18/// decide to drop a processor or escalate to a higher-level whenever an error occurs or the
19/// processor is no longer used.
20pub trait Processor<T> {
21 type Output;
22
23 type Error;
24
25 /// Consumes an item for further processing.
26 ///
27 /// Might apply back-pressure when being too busy.
28 fn process(&self, input: T) -> impl Future<Output = Result<(), Self::Error>>;
29
30 /// Returns future with processed output whenever ready.
31 ///
32 /// Please note that processors never terminate and rather return pending state (Poll::Pending)
33 /// in the future when `next` is called and no ready items are available.
34 fn next(&self) -> impl Future<Output = Result<Self::Output, Self::Error>>;
35}