pub trait MapFn<I, T, U>: Send + 'static {
// Required method
fn execute(&self, id: &I, data: T) -> Result<U>;
}Expand description
Map function.
This trait defines a function that transforms data of type T into data of
type U. It’s the most commonly used function in stream processing, as it
is accepted by multiple operators. Note that transformations are expected
to be immutable, meaning they can’t capture mutable variables. This allows
operators to move function execution to a Task, which is also why
it expects owned data.
There’s a range of different implementations of this trait, allowing you to
use a variety of function shapes, including support for Splat, as well
as support for the [WithId] and [WithSplat] adapters. Furthermore, the
trait can be implemented for custom types to add new behaviors. Note that
all implementations also allow to return a Report, which makes it
possible to return diagnostics from the function execution.
The 'static lifetimes is mandatory as closures must be moved into actions,
so requiring it here allows us to reduce the verbosity of trait bounds.
§Examples
Transform data:
use zrx_stream::function::MapFn;
// Define and execute function
let f = |n: i32| n * n;
f.execute(&"id", 42)?;Transform data with splat argument:
use zrx_stream::function::{MapFn, Splat};
// Define and execute function
let f = |a: i32, b: i32| a + b;
f.execute(&"id", Splat::from((1, 2)))?;