buf_list/
lib.rs

1// Copyright (c) 2018 the linkerd2-proxy authors
2// Copyright (c) The buf-list Contributors
3// SPDX-License-Identifier: Apache-2.0
4
5#![forbid(unsafe_code)]
6#![warn(missing_docs)]
7#![cfg_attr(doc_cfg, feature(doc_cfg))]
8
9//! A segmented list of [`bytes::Bytes`] chunks.
10//!
11//! # Overview
12//!
13//! This crate provides a [`BufList`] type that is a list of [`Bytes`](bytes::Bytes) chunks. The
14//! type implements [`bytes::Buf`], so it can be used in any APIs that use `Buf`.
15//!
16//! The main use case for [`BufList`] is to buffer data received as a stream of chunks without
17//! having to copy them into a single contiguous chunk of memory. The [`BufList`] can then be passed
18//! into any APIs that accept `Buf`.
19//!
20//! If you've ever wanted a `Vec<Bytes>` or a `VecDeque<Bytes>`, this type is for you.
21//!
22//! # Cursors
23//!
24//! This crate also provides [`Cursor`], which is a cursor type around a [`BufList`]. A [`Cursor`]
25//! around a [`BufList`] implements [`Seek`](std::io::Seek), [`Read`](std::io::Read) and
26//! [`BufRead`](std::io::BufRead), similar to [`std::io::Cursor`].
27//!
28//! # Examples
29//!
30//! Gather chunks into a `BufList`, then write them all out to standard error in one go:
31//!
32//! ```
33//! use buf_list::BufList;
34//! use tokio::io::AsyncWriteExt;
35//!
36//! # #[tokio::main(flavor = "current_thread")]
37//! # async fn main() -> Result<(), std::io::Error> {
38//! let mut buf_list = BufList::new();
39//! buf_list.push_chunk(&b"hello "[..]);
40//! buf_list.push_chunk(&b"world"[..]);
41//! buf_list.push_chunk(&b"!"[..]);
42//!
43//! let mut stderr = tokio::io::stderr();
44//! stderr.write_all_buf(&mut buf_list).await?;
45//! # Ok(()) }
46//! ```
47//!
48//! Collect a fallible stream of `Bytes` into a `BufList`:
49//!
50//! ```
51//! use buf_list::BufList;
52//! use bytes::Bytes;
53//! use futures::TryStreamExt;
54//!
55//! # #[tokio::main(flavor = "current_thread")]
56//! # async fn main() -> Result<(), ()> {
57//! // A common example is a stream of bytes read over HTTP.
58//! let stream = futures::stream::iter(
59//!     vec![
60//!         Ok(Bytes::from_static(&b"laputa, "[..])),
61//!         Ok(Bytes::from_static(&b"castle "[..])),
62//!         Ok(Bytes::from_static(&b"in the sky"[..]))
63//!     ],
64//! );
65//!
66//! let buf_list = stream.try_collect::<BufList>().await?;
67//! assert_eq!(buf_list.num_chunks(), 3);
68//! # Ok(()) }
69//! ```
70//!
71//! ## Converting to `Stream`s
72//!
73//! A `BufList` can be converted into a `futures::Stream`, or a `TryStream`, of `Bytes` chunks. Use
74//! this recipe to do so:
75//!
76//! (This will be exposed as an API on `BufList` once `Stream` and/or `TryStream` become part of
77//! stable Rust.)
78//!
79//! ```rust
80//! use buf_list::BufList;
81//! use bytes::Bytes;
82//! use futures::{Stream, TryStream};
83//!
84//! fn into_stream(buf_list: BufList) -> impl Stream<Item = Bytes> {
85//!     futures::stream::iter(buf_list)
86//! }
87//!
88//! fn into_try_stream<E>(buf_list: BufList) -> impl TryStream<Ok = Bytes, Error = E> {
89//!     futures::stream::iter(buf_list.into_iter().map(Ok))
90//! }
91//! ```
92//!
93//! # Optional features
94//!
95//! * `tokio1`: With this feature enabled, [`Cursor`] implements the `tokio` crate's
96//!   [`AsyncSeek`](tokio::io::AsyncSeek), [`AsyncRead`](tokio::io::AsyncRead) and
97//!   [`AsyncBufRead`](tokio::io::AsyncBufRead).
98//!
99//! * `futures03`: With this feature enabled, [`Cursor`] implements the `futures` crate's
100//!   [`AsyncSeek`](futures_io_03::AsyncSeek), [`AsyncRead`](futures_io_03::AsyncRead) and
101//!   [`AsyncBufRead`](futures_io_03::AsyncBufRead).
102//!
103//!   Note that supporting `futures03` means exporting 0.x types as a public interface. **This
104//!   violates the
105//!   [C-STABLE](https://rust-lang.github.io/api-guidelines/necessities.html#public-dependencies-of-a-stable-crate-are-stable-c-stable)
106//!   guideline.** However, the maintainer of `buf-list` considers that acceptable since `futures03`
107//!   is an optional feature and not critical to `buf-list`. As newer versions of the `futures`
108//!   crate are released, `buf-list` will support their versions of the async traits as well.
109//!
110//! # Minimum supported Rust version
111//!
112//! The minimum supported Rust version (MSRV) is **1.70**. Optional features may
113//! cause a bump in the MSRV.
114//!
115//! The MSRV is not expected to change in the future. If the MSRV changes, it will be accompanied by
116//! a major version bump to `buf-list`.
117
118mod cursor;
119pub(crate) mod errors;
120mod imp;
121
122pub use cursor::*;
123pub use imp::*;