alexcrichton / xz2-rs

Bindings to liblzma in Rust (xz streams in Rust)
Apache License 2.0
81 stars 52 forks source link

When is it possible to use BufRead and AsyncRead? #120

Closed bonsairobo closed 11 months ago

bonsairobo commented 11 months ago

https://github.com/alexcrichton/xz2-rs/blob/1a82c40d6d80171b7df328aea43b7054acd10c44/src/bufread.rs#L240C1-L241C59

I'm trying to figure out how to use XzDecoder with an async stream of bytes like you get from reqwest::Response::bytes_stream(). But I don't see how to transform the stream into something that implements BufRead. I don't really understand why BufRead is necessary in an async context either, where I would rather expect AsyncBufRead.

This is the closest I get:

    let stream = reqwest::get(url)
        .await?
        .error_for_status()?
        .bytes_stream()
        .map_err(|e| io::Error::new(io::ErrorKind::Other, e))
        .into_async_read();
    let decoder = XzDecoder::new(tokio::io::BufReader::new(stream));

But tokio::io::BufReader does not implement BufRead.

bonsairobo commented 11 months ago

It seems like this same problem applies to all different kinds of XzDecoder. Using any of them in an async context requires that they implement both the async and non-async versions of the io traits. E.g. AsyncRead + Read, AsyncWrite + Write, etc. I'm not aware of anything that implements both of these traits at the same time.

bonsairobo commented 11 months ago

Huh now I see this: https://github.com/alexcrichton/xz2-rs/pull/109

I guess I will give async-compression a try.