Expand description
A segmented list of bytes::Bytes chunks.
Overview
This crate provides a BufList type that is a list of Bytes chunks.
The type implements bytes::Buf, so it can be used in any APIs that use Buf.
The main use case for BufList is to buffer data received as a stream of chunks without
having to copy them into a single contiguous chunk of memory. The BufList can then be passed
into any APIs that accept Buf.
If you’ve ever wanted a Vec<Bytes> or a VecDeque<Bytes>, this type is for you.
Examples
Gather chunks into a BufList, then write them all out to standard error in one go:
use buf_list::BufList;
use tokio::io::AsyncWriteExt;
let mut buf_list = BufList::new();
buf_list.push_chunk(&b"hello "[..]);
buf_list.push_chunk(&b"world"[..]);
buf_list.push_chunk(&b"!"[..]);
let mut stderr = tokio::io::stderr();
stderr.write_all_buf(&mut buf_list).await?;Collect a fallible stream of Bytes into a BufList:
use buf_list::BufList;
use bytes::Bytes;
use futures::stream::TryStreamExt;
// A common example is a stream of bytes read over HTTP.
let stream = futures::stream::iter(
vec![
Ok(Bytes::from_static(&b"laputa, "[..])),
Ok(Bytes::from_static(&b"castle "[..])),
Ok(Bytes::from_static(&b"in the sky"[..]))
],
);
let buf_list = stream.try_collect::<BufList>().await?;
assert_eq!(buf_list.num_chunks(), 3);Minimum supported Rust version
The minimum supported Rust version (MSRV) is 1.39, same as the bytes crate.
The MSRV is not expected to change in the future. If it does, it will be done as a breaking change.