1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//! General ergonomic improvements.

use std::io;

use bytes::Bytes;

use futures::{Stream, TryStreamExt};

use tokio::io::AsyncRead;
use tokio_util::codec;

/// Converts a struct implementing [`AsyncRead`] into a stream of `Result<`[`Bytes`]`, `[`io::Error`]`>`.
///
/// [`AsyncRead`]: tokio::io::AsyncRead
/// [`Bytes`]: bytes::Bytes
/// [`io::Error`]: std::io::Error
///
pub fn reader_to_iostream<R: AsyncRead>(reader: R) -> impl Stream<Item = Result<Bytes, io::Error>> {
    codec::FramedRead::new(reader, codec::BytesCodec::new()).map_ok(|bytes| bytes.freeze())
}