stream-transfer-limit 0.1.0

Byte-count transfer limits for fallible futures streams
Documentation
//! 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};
//!
//! # futures::executor::block_on(async {
//! 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());
//! # });
//! ```

mod chunk;
mod counter;
mod error;
mod transfer_limit;

pub use chunk::ChunkLength;
pub use counter::TransferCounter;
pub use error::TransferLimitError;
pub use transfer_limit::{NoopProgress, TransferLimit};

#[cfg(test)]
mod tests;