#async-stream #split #partition

stream-partition

Partition a single stream into multiple sub-streams based on a key

1 unstable release

Uses new Rust 2024

0.1.0 Jun 10, 2025

#1362 in Asynchronous

MIT/Apache

27KB
393 lines

Stream partitioning utilities for splitting a single stream into multiple streams based on keys.

This module provides functionality to partition a stream into multiple sub-streams, where each sub-stream contains only items that match a specific key determined by an async function.

Example

use futures::{stream, StreamExt};
use futures::future::ready;
use stream_partition::StreamPartitionExt;

let stream = stream::iter(vec![1, 2, 3, 4, 5, 6]);
let mut partitioner = stream.partition_by(|x| ready(x % 2));

// Get odd numbers
let mut odd_stream = partitioner.lock().unwrap().get_partition(1);
let first_odd = odd_stream.next().await.unwrap();
assert_eq!(first_odd, 1);

Test that demonstrates partitioning a stream of numbers into odd/even partitions.

This test creates a stream of integers 1-6 and partitions them by their remainder when divided by 2 (i.e., odd vs even). It verifies that the first partition encountered (for odd numbers) correctly yields the first odd number (1). Test that demonstrates getting a specific partition by key.

This test creates a stream of integers and uses get_partition to directly access the even numbers partition (key = 0) without waiting for it to appear naturally in the partitioner stream. Test that items for unknown keys are dropped when allow_new_queues is false.


stream-partition

CI status dependency status Last commit

A Rust library for partitioning a single stream into multiple sub-streams based on keys.

Overview

stream-partition provides a utility to split a stream into multiple sub-streams, where each sub-stream contains only items that match a specific key determined by an async function.

This can be used to process different types of items from a single stream with different logic.

Dependencies

~0.7–1MB
~19K SLoC