lz4_flex icon indicating copy to clipboard operation
lz4_flex copied to clipboard

AsyncWrite support

Open Alexei-Kornienko opened this issue 4 years ago • 6 comments

Would be good to add impl of FrameEncoder/FrameDecoder where W: AsyncWrite (futures::io::AsyncWrite), R: AsyncRead

This would enable user the possibility to use compression in async data streams

Alexei-Kornienko avatar Jun 01 '21 11:06 Alexei-Kornienko

Thanks for the issue, yeah I also thought about that. It would be nice to have, but I'm not sure how good it can be managed to have both in terms of code duplication.

PSeitz avatar Jun 06 '21 15:06 PSeitz

One solution that I could propose is:

  1. Refactor implementation to always work with AsyncWrite trait
  2. Add a thin Wrapper that would take any type that implement Write and impl AsyncWrite for it without returning Poll::Pending

In result Async version would be used as is. Sync version could internally use block_on (https://docs.rs/futures/0.3.15/futures/executor/fn.block_on.html) to convert sync call to async call

Pros:

  • only 1 impl of the algorithm without copy/paste
  • for sync version compiler should be able to optimize out all await keywords and make it zero cost (need to benchmark)

Cons:

  • hard dependency on futures crate
  • slightly more complicated code using async/await syntax

Alexei-Kornienko avatar Jun 06 '21 20:06 Alexei-Kornienko

One significant problem right now is that the ecosystem is split regarding AsyncRead/Write. Every runtime has an its own (tokio, ..) set of traits and the futures crates have another one.

arthurprs avatar Jun 09 '21 15:06 arthurprs

A good idea may be to add lz4_flex support to https://github.com/Nemo157/async-compression

PSeitz avatar Jan 24 '23 13:01 PSeitz