| 1 | # buf-list |
| 2 | |
| 3 | [](https://crates.io/crates/buf-list) [](https://docs.rs/buf-list/) [](https://sunshowers-code.github.io/buf-list/rustdoc/buf_list/) [](LICENSE) |
| 4 | |
| 5 | A segmented list of `bytes::Bytes` chunks. |
| 6 | |
| 7 | ## Overview |
| 8 | |
| 9 | This crate provides a `BufList` type that is a list of [`Bytes`](bytes::Bytes) chunks. The |
| 10 | type implements `bytes::Buf`, so it can be used in any APIs that use `Buf`. |
| 11 | |
| 12 | The main use case for `BufList` is to buffer data received as a stream of chunks without |
| 13 | having to copy them into a single contiguous chunk of memory. The `BufList` can then be passed |
| 14 | into any APIs that accept `Buf`. |
| 15 | |
| 16 | If you've ever wanted a `Vec<Bytes>` or a `VecDeque<Bytes>`, this type is for you. |
| 17 | |
| 18 | ## Cursors |
| 19 | |
| 20 | This crate also provides `Cursor`, which is a cursor type around a |
| 21 | `BufList`. Similar to similar to `std::io::Cursor`, a `Cursor` around |
| 22 | a `BufList` implements |
| 23 | |
| 24 | * [`Seek`](std::io::Seek), [`Read`](std::io::Read), and [`BufRead`](std::io::BufRead) |
| 25 | * `bytes::Buf` as well (in other words, both `BufList`s and `Cursor`s over them |
| 26 | can be passed into any APIs that accept `Buf`). |
| 27 | |
| 28 | ## Examples |
| 29 | |
| 30 | Gather chunks into a `BufList`, then write them all out to standard error in one go: |
| 31 | |
| 32 | ```rust |
| 33 | use buf_list::BufList; |
| 34 | use tokio::io::AsyncWriteExt; |
| 35 | |
| 36 | let mut buf_list = BufList::new(); |
| 37 | buf_list.push_chunk(&b"hello "[..]); |
| 38 | buf_list.push_chunk(&b"world"[..]); |
| 39 | buf_list.push_chunk(&b"!"[..]); |
| 40 | |
| 41 | let mut stderr = tokio::io::stderr(); |
| 42 | stderr.write_all_buf(&mut buf_list).await?; |
| 43 | ``` |
| 44 | |
| 45 | Collect a fallible stream of `Bytes` into a `BufList`: |
| 46 | |
| 47 | ```rust |
| 48 | use buf_list::BufList; |
| 49 | use bytes::Bytes; |
| 50 | use futures::TryStreamExt; |
| 51 | |
| 52 | // A common example is a stream of bytes read over HTTP. |
| 53 | let stream = futures::stream::iter( |
| 54 | vec![ |
| 55 | Ok(Bytes::from_static(&b"laputa, "[..])), |
| 56 | Ok(Bytes::from_static(&b"castle "[..])), |
| 57 | Ok(Bytes::from_static(&b"in the sky"[..])) |
| 58 | ], |
| 59 | ); |
| 60 | |
| 61 | let buf_list = stream.try_collect::<BufList>().await?; |
| 62 | assert_eq!(buf_list.num_chunks(), 3); |
| 63 | ``` |
| 64 | |
| 65 | ### Converting to `Stream`s |
| 66 | |
| 67 | A `BufList` can be converted into a `futures::Stream`, or a `TryStream`, of `Bytes` chunks. Use |
| 68 | this recipe to do so: |
| 69 | |
| 70 | (This will be exposed as an API on `BufList` once `Stream` and/or `TryStream` become part of |
| 71 | stable Rust.) |
| 72 | |
| 73 | ```rust |
| 74 | use buf_list::BufList; |
| 75 | use bytes::Bytes; |
| 76 | use futures::{Stream, TryStream}; |
| 77 | |
| 78 | fn into_stream(buf_list: BufList) -> impl Stream<Item = Bytes> { |
| 79 | futures::stream::iter(buf_list) |
| 80 | } |
| 81 | |
| 82 | fn into_try_stream<E>(buf_list: BufList) -> impl TryStream<Ok = Bytes, Error = E> { |
| 83 | futures::stream::iter(buf_list.into_iter().map(Ok)) |
| 84 | } |
| 85 | ``` |
| 86 | |
| 87 | ## Optional features |
| 88 | |
| 89 | * `tokio1`: With this feature enabled, `Cursor` implements the `tokio` crate's |
| 90 | [`AsyncSeek`](tokio::io::AsyncSeek), [`AsyncRead`](tokio::io::AsyncRead) and |
| 91 | [`AsyncBufRead`](tokio::io::AsyncBufRead). |
| 92 | |
| 93 | * `futures03`: With this feature enabled, `Cursor` implements the `futures` crate's |
| 94 | [`AsyncSeek`](futures_io_03::AsyncSeek), [`AsyncRead`](futures_io_03::AsyncRead) and |
| 95 | [`AsyncBufRead`](futures_io_03::AsyncBufRead). |
| 96 | |
| 97 | Note that supporting `futures03` means exporting 0.x types as a public interface. **This |
| 98 | violates the |
| 99 | [C-STABLE](https://rust-lang.github.io/api-guidelines/necessities.html#public-dependencies-of-a-stable-crate-are-stable-c-stable) |
| 100 | guideline.** However, the maintainer of `buf-list` considers that acceptable since `futures03` |
| 101 | is an optional feature and not critical to `buf-list`. As newer versions of the `futures` |
| 102 | crate are released, `buf-list` will support their versions of the async traits as well. |
| 103 | |
| 104 | ## Minimum supported Rust version |
| 105 | |
| 106 | The minimum supported Rust version (MSRV) is **1.70**. Optional features may |
| 107 | cause a bump in the MSRV. |
| 108 | |
| 109 | `buf-list` has a conservative MSRV policy. MSRV bumps will be sparing, and |
| 110 | if so, they will be accompanied by a minor version bump. |
| 111 | |
| 112 | ## Contributing |
| 113 | |
| 114 | Pull requests are welcome! Please follow the |
| 115 | [code of conduct](https://github.com/sunshowers-code/.github/blob/main/CODE_OF_CONDUCT.md). |
| 116 | |
| 117 | ## License |
| 118 | |
| 119 | buf-list is copyright 2022 The buf-list Contributors. All rights reserved. |
| 120 | |
| 121 | Copied and adapted from linkerd2-proxy; [original |
| 122 | code](https://github.com/linkerd/linkerd2-proxy/blob/d36e3a75ef428453945eedaa230a32982c17d30d/linkerd/http-retry/src/replay.rs#L421-L492) |
| 123 | written by Eliza Weisman. linkerd2-proxy is copyright 2018 the linkerd2-proxy authors. All rights |
| 124 | reserved. |
| 125 | |
| 126 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 127 | these files except in compliance with the License. You may obtain a copy of the |
| 128 | License at |
| 129 | |
| 130 | http://www.apache.org/licenses/LICENSE-2.0 |
| 131 | |
| 132 | Unless required by applicable law or agreed to in writing, software distributed |
| 133 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 134 | CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 135 | specific language governing permissions and limitations under the License. |