pub trait LiftFn<I, T, U>: Send + 'staticwhere
T: ?Sized,{
// Required method
fn execute(&self, id: &I, data: &T) -> Result<Chunk<I, U>>;
}Expand description
Lift function.
This trait defines a function that transforms data of type T into a vector
of items of type U, which creates a vector of nested items, allowing us to
implement higher-order collections and relationships to manage data-centric
interdependencies. Transformations are expected to be immutable, which means
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.
Also note that it would be more efficient to return an impl Iterator from
the lift function, but this doesn’t work due to the RPITIT (return-position
impl trait in trait) limitations, as they’re not yet stabilized. Once those
hit the stable channel, we might should switching to this approach. We also
considered making the lift function generic over the iterator type, but it
would make the trait more complex and less ergonomic.
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
Lift data:
use zrx_stream::function::LiftFn;
use zrx_stream::value::Chunk;
// Define and execute function
let f = |data: &Vec<i32>| {
data.iter()
.map(|&n| (format!("id/{n}"), n))
.collect::<Chunk<_, _>>()
};
f.execute(&"id".to_string(), &vec![1, 2])?;Lift data with splat argument:
use zrx_stream::function::{LiftFn, Splat};
use zrx_stream::value::Chunk;
// Define and execute function
let f = |a: &i32, b: &i32| {
[a, b]
.into_iter()
.map(|&n| (format!("id/{n}"), n))
.collect::<Chunk<_, _>>()
};
f.execute(&"id".to_string(), &Splat::from((1, 2)))?;