tokio_buf/util/
mod.rs

1//! Types and utilities for working with `BufStream`.
2
3mod chain;
4mod collect;
5mod from;
6mod iter;
7mod limit;
8mod stream;
9
10pub use self::chain::Chain;
11pub use self::collect::Collect;
12pub use self::from::FromBufStream;
13pub use self::iter::iter;
14pub use self::limit::Limit;
15pub use self::stream::{stream, IntoStream};
16
17pub mod error {
18    //! Error types
19
20    pub use super::collect::CollectError;
21    pub use super::from::{CollectBytesError, CollectVecError};
22    pub use super::limit::LimitError;
23}
24
25use BufStream;
26
27impl<T> BufStreamExt for T where T: BufStream {}
28
29/// An extension trait for `BufStream`'s that provides a variety of convenient
30/// adapters.
31pub trait BufStreamExt: BufStream {
32    /// Takes two buf streams and creates a new buf stream over both in
33    /// sequence.
34    ///
35    /// `chain()` returns a new `BufStream` value which will first yield all
36    /// data from `self` then all data from `other`.
37    ///
38    /// In other words, it links two buf streams together, in a chain.
39    fn chain<T>(self, other: T) -> Chain<Self, T>
40    where
41        Self: Sized,
42        T: BufStream<Error = Self::Error>,
43    {
44        Chain::new(self, other)
45    }
46
47    /// Consumes all data from `self`, storing it in byte storage of type `T`.
48    ///
49    /// `collect()` returns a future that buffers all data yielded from `self`
50    /// into storage of type of `T`. The future completes once `self` yield
51    /// `None`, returning the buffered data.
52    ///
53    /// The collect future will yield an error if `self` yields an error or if
54    /// the collect operation errors. The collect error cases are dependent on
55    /// the target storage type.
56    fn collect<T>(self) -> Collect<Self, T>
57    where
58        Self: Sized,
59        T: FromBufStream<Self::Item>,
60    {
61        Collect::new(self)
62    }
63
64    /// Limit the number of bytes that the stream can yield.
65    ///
66    /// `limit()` returns a new `BufStream` value which yields all the data from
67    /// `self` while ensuring that at most `amount` bytes are yielded.
68    ///
69    /// If `self` can yield greater than `amount` bytes, the returned stream
70    /// will yield an error.
71    fn limit(self, amount: u64) -> Limit<Self>
72    where
73        Self: Sized,
74    {
75        Limit::new(self, amount)
76    }
77
78    /// Creates a `Stream` from a `BufStream`.
79    ///
80    /// This produces a `Stream` of `BufStream::Items`.
81    fn into_stream(self) -> IntoStream<Self>
82    where
83        Self: Sized,
84    {
85        IntoStream::new(self)
86    }
87}