Wrap a hyper::Body in a BufReader in Rust

388 views Asked by At

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.

0

There are 0 answers