Skip to main content

SliceHandler

Trait SliceHandler 

Source
pub trait SliceHandler<T>: Send + Sync {
    // Required method
    fn handle_slice(
        &self,
        batch: &[T],
        ctx: &mut Context<'_>,
    ) -> impl Future<Output = BatchResult> + Send;
}
Expand description

A handler invoked with one whole decoded batch.

The batch parameter is a slice: per-message broker handles stay with the dispatcher, which settles every message of the batch according to the returned BatchResult - one uniform outcome, or one outcome per element.

§Examples

Closures returning any IntoBatchResult implement SliceHandler automatically:

use ruststream::runtime::{Context, HandlerResult, SliceHandler};

fn assert_slice_handler<T, H: SliceHandler<T>>(_: H) {}

fn use_closures() {
    // One outcome for the whole batch.
    assert_slice_handler::<u32, _>(|batch: &[u32], _ctx: &mut Context| {
        let _ = batch.len();
        async { HandlerResult::Ack }
    });
    // One outcome per element: entries that are not ready yet retry individually.
    assert_slice_handler::<u32, _>(|batch: &[u32], _ctx: &mut Context| {
        let outcomes: Vec<HandlerResult> = batch
            .iter()
            .map(|n| {
                if *n == 0 {
                    HandlerResult::retry()
                } else {
                    HandlerResult::Ack
                }
            })
            .collect();
        async move { outcomes }
    });
}

Required Methods§

Source

fn handle_slice( &self, batch: &[T], ctx: &mut Context<'_>, ) -> impl Future<Output = BatchResult> + Send

Handles one decoded batch, with the per-batch Context.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T, F, Fut> SliceHandler<T> for F
where F: Fn(&[T], &mut Context<'_>) -> Fut + Send + Sync, Fut: Future + Send, Fut::Output: IntoBatchResult,