我有一个AsyncRead
对象,想使用tokio 0.2和futures 0.3将其转换为Stream<Item = tokio::io::Result<Bytes>>
。
目前我能做到的最好方式如下:
use bytes::Bytes; // 0.4.12
use futures::stream::{Stream, TryStreamExt};; // 0.3.1
use tokio::{fs::File, io::Result}; // 0.2.4
use tokio_util::{BytesCodec, FramedRead}; // 0.2.0
#[tokio::main]
async fn main() -> Result<()> {
let file = File::open("some_file.txt").await?;
let stream = FramedRead::new(file, BytesCodec::new()).map_ok(|b| b.freeze());
fn_that_takes_stream(stream)
}
fn fn_that_takes_stream<S, O>(s: S) -> Result<()>
where
S: Stream<Item = Result<Bytes>>,
{
//...
Ok(())
}
看起来应该有一种更简单的方法;我很惊讶Tokio没有包含一个编解码器,可以获得Bytes
的流,而不是BytesMut
,或者没有提供一个将AsyncRead
转换为Stream
的扩展特性。我是否遗漏了什么?