Nullus157 / async-compression

Adaptors between compression crates and Rust's async IO types
https://docs.rs/async-compression
Apache License 2.0
404 stars 79 forks source link

GzipDecoder is not implementing AsyncRead #262

Closed incker closed 7 months ago

incker commented 7 months ago

Hello,

Here is an example.

use std::error;
use tokio::io::AsyncRead;

async fn main() -> Result<(), Box<dyn error::Error>> {
    use async_compression::futures::bufread::GzipDecoder;
    use futures::{
        io::{self, BufReader, ErrorKind},
        prelude::*,
    };
    let response = reqwest::get("http://localhost:8000/test.txt.gz").await?;
    let reader = response
        .bytes_stream()
        .map_err(|e| io::Error::new(ErrorKind::Other, e))
        .into_async_read();
    let decoder = GzipDecoder::new(BufReader::new(reader));

    some_func(decoder);

    Ok(())
}

pub fn some_func<St: AsyncRead>(_: St) {

}

And error is

46 |     some_func(decoder);
|     --------- ^^^^^^^ the trait `tokio::io::AsyncRead` is not implemented for `GzipDecoder<BufReader<IntoAsyncRead<MapErr<impl Stream<Item = Result<Bytes, Error>>, {closure@test_arr.rs:42:18}>>>>`
|     |
|     required by a bound introduced by this call

Maybe i do not understand something. hep please)

NobodyXu commented 7 months ago

You need to use async_compression::tokio::bufread::GzipDecoder

The one in the tokio module provides types that implements tokio io traits.

P.S. you need to enable feature tokio of async-compression

incker commented 7 months ago

Can you help? I have added tokio feature I have replaced to use async_compression::tokio::bufread::GzipDecoder; in my example, But I received more compilation errors At least this one:

--> dashd/miner-dash-daemon/src/my_test.rs:17:36
|
17 |     let decoder = GzipDecoder::new(BufReader::new(reader));
|                   ---------------- ^^^^^^^^^^^^^^^^^^^^^^ the trait `tokio::io::AsyncBufRead` is not implemented for `futures::io::BufReader<IntoAsyncRead<futures::stream::MapErr<impl Stream<Item = Result<bytes::Bytes, reqwest::Error>>, {closure@dashd/miner-dash-daemon/src/my_test.rs:15:18: 15:21}>>>`
|                   |
|                   required by a bound introduced by this call
NobodyXu commented 7 months ago

Is BufReader from tokio?

incker commented 7 months ago

Ufff. It was not easy, but it works now. Thank you !!!

use std::error;
use std::io;

use futures::TryStreamExt;
use tokio::io::AsyncRead;
use tokio_util::io::StreamReader;

async fn main() -> Result<(), Box<dyn error::Error>> {
    let response = reqwest::get("http://localhost:8000/test.txt.gz").await?;
    let reader = response
        .bytes_stream()
        .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err));
    let decoder = async_compression::tokio::bufread::GzipDecoder::new(StreamReader::new(reader));

    some_func(decoder);

    Ok(())
}

pub fn some_func<St: AsyncRead>(_: St) {}