Skip to main content

Crate stream_transfer_limit

Crate stream_transfer_limit 

Source
Expand description

Byte-count transfer limits for fallible futures streams.

This crate is intentionally small: it wraps a Stream<Item = Result<T, E>>, counts the bytes yielded by successful chunks, and returns a typed error as soon as the configured transfer limit is exceeded. This is useful when the limit is known only after request routing, authentication, or tenant lookup. The default counter type is usize; use TransferLimit::<u64>::from_limit or TransferLimit::<u128>::from_limit for streams that may exceed pointer-sized byte counts.

use futures::{StreamExt, stream};
use stream_transfer_limit::{TransferLimit, TransferLimitError};

let chunks = stream::iter([
    Ok::<_, std::io::Error>(vec![1, 2]),
    Ok(vec![3, 4, 5]),
]);
let mut chunks = TransferLimit::new(4).wrap(chunks);

assert_eq!(chunks.next().await.unwrap().unwrap(), vec![1, 2]);
assert!(matches!(
    chunks.next().await.unwrap(),
    Err(TransferLimitError::LimitExceeded { limit: 4, actual: 5 })
));
assert!(chunks.next().await.is_none());

Structs§

TransferLimit
Builder for applying byte-count transfer limits to fallible streams.

Enums§

TransferLimitError
Error returned by a transfer-limited stream.

Traits§

ChunkLength
Reports the byte length of a stream chunk.
TransferCounter
Integer type used to count cumulative transfer bytes.

Type Aliases§

NoopProgress
Default progress callback type used when no callback is configured.