DefaultFn

Trait DefaultFn 

Source
pub trait DefaultFn<I, T>: Send + 'static {
    // Required method
    fn execute(&self, id: &I) -> Result<Option<T>>;
}
Expand description

Default function.

This trait defines a function allowing to create a value of type T from an optional identifier. It is useful in scenarios where you want to ensure that each item in a stream has associated data, such as providing default values when none are present.

This trait is also implemented for the [WithId] adapter. 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

use zrx_stream::function::DefaultFn;

// Define and execute function
let f = || Some(42);
f.execute(&"id")?;

Required Methods§

Source

fn execute(&self, id: &I) -> Result<Option<T>>

Executes the default function.

§Errors

This method returns an error if the function fails to execute.

Implementors§

Source§

impl<F, R, I, T> DefaultFn<I, T> for F
where F: Fn() -> R + Send + 'static, R: IntoReport<Option<T>>, I: Display,