Qubit Batch
Batch-oriented task execution abstractions and sequential utilities for the Qubit Rust libraries.
When to use this crate
Use qubit-batch when you already have a finite set of fallible tasks and want
to run the whole set as one batch with consistent accounting:
- data import or validation jobs where every record should be attempted;
- maintenance scripts that need a final success/failure summary;
- pipelines that want stable task indexes for diagnostics and retries;
- shared library code that should not commit to Tokio, Rayon, or another concrete runtime.
This crate is not a queue, scheduler, worker pool, or retry framework. It executes the supplied batch once and returns a structured result.
Overview
Qubit Batch focuses on one-shot execution of whole task batches instead of single-task submission. The crate provides:
BatchExecutor: trait for executing a batch of fallible runnable tasks.BatchExecutor::call: convenience API for executing fallible callables and collecting successful return values by index.BatchProcessor: trait for processing data items directly, without first wrapping each item as a task.ChunkedBatchProcessor: processor adapter that submits fixed-size chunks to a delegate processor.SequentialBatchExecutor: deterministic, in-order execution on the caller thread.ProgressReporter: pluggable progress callbacks for start, in-flight progress, and finish notifications.ConsoleProgressReporterandLoggerProgressReporter: concrete reporters for stdout and thelogcrate.BatchExecutionResult: structured batch outcome with failure aggregation and monotonic elapsed-duration reporting.
Rayon-backed parallel batch execution lives in the companion
qubit-rayon-batch crate.
Features
- Accept eager or lazy task sources through
IntoIterator. - Keep batch-level statistics for completed, succeeded, failed, and panicked tasks.
- Record per-task failures with stable batch indexes and readable panic messages.
- Treat task failures as batch data instead of short-circuiting the whole execution.
- Detect declared task-count mismatches and return the partial result collected before the mismatch was observed.
- Keep the core API free of runtime-specific dependencies.
Installation
[]
= "0.3.1"
Quick Start
Process items with for_each
For item-oriented jobs, for_each is usually the smallest API surface. The
closure is converted into runnable tasks internally, and every item is attempted.
use ;
let executor = new;
let records = ;
let result = executor
.for_each
.expect;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
let failure = &result.failures;
assert_eq!;
match failure.error
Execute explicit tasks
Use execute when your tasks already implement Runnable or when you want to
build task values before running the batch.
use ;
use Runnable;
let tasks = vec!;
let executor = new;
let result = executor
.execute
.expect;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
for failure in result.failures
If you implement Runnable in a downstream crate, add qubit-function as a
dependency too:
[]
= "0.3.1"
= "0.11"
Process data in fixed-size chunks
Implement BatchProcessor for the real batch target, such as a SQL insert
operation. Wrap it with ChunkedBatchProcessor when the logical batch must be
submitted in smaller chunks.
use ;
use ;
;
let mut processor = new;
let result = processor
.process
.expect;
assert_eq!;
assert_eq!;
assert_eq!;
Progress Reporting
SequentialBatchExecutor uses NoOpProgressReporter by default. You can attach
your own reporter and tune the minimum interval between in-flight callbacks.
Sequential execution emits progress only between tasks, so a single long-running
task will not produce intermediate process callbacks.
use Duration;
use ;
;
let executor = new
.with_reporter
.with_report_interval;
let result = executor
.for_each
.expect;
assert!;
Panics from task bodies are captured as BatchTaskError::Panicked. Panics from
the reporter itself are not captured as task failures; they propagate to the
caller because progress reporting is outside the task failure model.
Count Contract
Both execute and for_each require the caller to pass the declared item count.
This lets the executor report consistent totals before consuming the iterator and
also detect producer bugs:
use ;
let executor = new;
let error = executor
.for_each
.expect_err;
match error
Important result semantics:
Ok(BatchExecutionResult)does not mean every task succeeded. It means the supplied iterator matched the declared count.result.is_success()is the convenience check for “all declared tasks completed without task errors or panics.”Err(BatchExecutionError)means the iterator produced fewer or more items than declared. The error still carries a partialBatchExecutionResult.
API Notes
SequentialBatchExecutor::new()is deterministic and runs tasks on the caller thread in iterator order.BatchExecutionResult::failures()returns failure records sorted by task index.BatchTaskFailure::index()is zero-based and refers to the task's position in the batch.- The core crate intentionally avoids runtime dependencies. Use the companion
qubit-rayon-batchcrate when you need Rayon-backed parallel execution.
Public API Cheat Sheet
BatchExecutor: trait for executing a declared batch of fallible runnable tasks.BatchCallResult<R, E>: callable batch result containing the execution summary and indexed success values.SequentialBatchExecutor: default executor that runs tasks sequentially on the caller thread.BatchProcessor: trait for processing a declared batch of data items.BatchProcessResult: aggregate result containing item, processed, chunk, and monotonic elapsed-duration counters.ChunkedBatchProcessor: processor wrapper that splits a logical batch into fixed-size chunks and delegates each chunk.ChunkedBatchProcessError<E>: chunked processor error for source count mismatches or delegate failures, carrying the partial process result.ProgressReporter: lifecycle callback trait for batch start, periodic progress, and finish notifications.NoOpProgressReporter: default reporter that accepts callbacks without doing any work.WriterProgressReporter,ConsoleProgressReporter, andLoggerProgressReporter: concrete reporters for writers, stdout, andlog.BatchExecutionResult<E>: aggregate result containing task counts, monotonic elapsed duration, and detailed task failures.BatchExecutionError<E>: batch-level contract error for declared count shortfall or overflow, carrying the partial result collected so far.BatchTaskFailure<E>: one failed or panicked task with its stable zero-based batch index.BatchTaskError<E>: task-level failure classified as either a returned business error or a captured panic.
Project Layout
src/executor: executor traits and the sequential executor implementation.src/error: batch execution results, count mismatch errors, task failures, and task panic conversion.src/processor: data-item batch processor traits, results, and the chunked processor.src/progress: progress reporter traits and no-op, stdout, writer, and logger reporters.tests/executor: behavior tests for sequential execution, progress callbacks, failures, panics, and count mismatches.tests/processor: behavior tests for chunking, delegate errors, and progress callbacks.tests/progress: behavior tests for concrete progress reporters.tests/error: tests for result invariants and error helper methods.tests/docs: README consistency checks.
Documentation
- API documentation: docs.rs/qubit-batch
- Crate package: crates.io/crates/qubit-batch
- Source repository: github.com/qubit-ltd/rs-batch
Testing and CI
Run the fast local checks from the crate root:
To match the repository CI environment, run:
./align-ci.sh aligns the local toolchain and CI-related configuration before
./ci-check.sh runs the same checks used by the pipeline. Use ./coverage.sh
when changing behavior that should be reflected in coverage reports.
Contributing
Issues and pull requests are welcome. Please keep changes focused, add or update tests when behavior changes, and update this README or rustdoc when public API or user-visible behavior changes.
By contributing, you agree that your contribution is licensed under the same Apache License, Version 2.0 as this project.
License and Copyright
Copyright © 2026 Haixing Hu, Qubit Co. Ltd.
This software is licensed under the Apache License, Version 2.0.
Author and Maintenance
Haixing Hu — Qubit Co. Ltd.
| Repository | github.com/qubit-ltd/rs-batch |
| API documentation | docs.rs/qubit-batch |
| Crate | crates.io/crates/qubit-batch |