2020-11-28

Rust create a sink from a future

I want to transform some reader/writer into a record pipe of elements. I managed to make the reader->stream direction using futures::stream::unfold as per this answer. However, I'm having trouble with the sink->writer. I'm basically looking for some inverse function to unfold.

I know there is AsyncWriterExt::into_sink but this only works if I can generate all bytes to write in one batch. I also found this answer suggesting to use drain() after with(). This didn't work however due to lifetime issues (the FnMut cannot effectively store the reference to the writer, or at least I didn't manage to do this.

So what I'm looking for is some function that I could call like fold(initial_state, |element| {(writer.write(element).await, new_state)}). You get the idea (I hope).

I've also seen that there's async_codec but that seems like overkill for me. In the meantime I've resorted to storing all writes as a stream and then use writer.into_sink().with_flat_map(). But that's really ugly.



from Recent Questions - Stack Overflow https://ift.tt/3mgQ43Y
https://ift.tt/eA8V8J

No comments:

Post a Comment