1

I have a hyper::Body and I want to wrap it in a BufReader (for example tokio::io::BufReader, but futures_util::io::BufReader or any other is fine too).

Using it directly didn't work:

fn foo(body: hyper::Body) {
    let bufreader = tokio::io::BufReader::new(body);
}
error[E0277]: the trait bound `Body: AsyncRead` is not satisfied
  --> src/lib.rs:2:47
   |
2  |     let bufreader = tokio::io::BufReader::new(body);
   |                     ------------------------- ^^^^ the trait `AsyncRead` is not implemented for `Body`
   |                     |
   |                     required by a bound introduced by this call
   |
   = help: the following other types implement trait `AsyncRead`:
             &[u8]
             &mut T
             AddrStream
             Box<T>
             BufStream<RW>
             DuplexStream
             Pin<P>
             Upgraded
           and 20 others
note: required by a bound in `tokio::io::BufReader::<R>::new`
  --> /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.23.0/src/io/util/buf_reader.rs:37:9
   |
37 | impl<R: AsyncRead> BufReader<R> {
   |         ^^^^^^^^^ required by this bound in `tokio::io::BufReader::<R>::new`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error

I'm aware there are multiple AsyncRead in rust, tokio_util::compat::* and futures_util::compat::* can be used to convert between them, but it seems hyper::Body doesn't implement either of them.

It implements futures_core::stream::Stream but I don't know how to feed this into a BufReader.

kpcyrd
  • 11
  • 2
  • 1
    Maybe you want [`TryStreamExt::into_async_read`](https://docs.rs/futures/latest/futures/stream/trait.TryStreamExt.html#method.into_async_read)? – PitaJ Dec 22 '22 at 23:23
  • They are not compatible unfortunately: `futures_util::io::BufReader::new(body.into_async_read())` -> `type mismatch resolving ::Item == Result<_, std::io::Error>`: [Rust playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5b71c617599c9800387e5cebc5de6d54) – kpcyrd Dec 22 '22 at 23:37
  • The return value of that already implements `AsyncBufRead`, so wrapping it in another `BufReader` would be redundant. The one trait that only `BufReader` has is `AsyncSeek`. – Lucas S. Dec 23 '22 at 08:54

0 Answers0