How to use futures stream forward?


#1

I am using futures_preview and I am puzzled by futures::stream::StreamExt::forward signature. Why Self::Item is constrained to Result?
https://docs.rs/futures-preview/0.3.0-alpha.12/futures/stream/trait.StreamExt.html#method.forward

I looked at Forward implementation but did not get the point. Are there any examples of use?


#2

The reason that the stream must be of results is so that any errors from the stream or sink can be put together into the output future, i.e. you take a Stream<Item = Result<T, E>> and a Sink<SinkItem = T, SinkError = E> and get back a Future<Item = Result<_, E>> which will resolve with an error if either pulling an item from the stream or pushing an item into the sink fail.

It’s unfortunate that there’s no example for this adaptor, but I think if you have an infallible stream you should be able to just do something like stream.map(Ok).forward(sink) and type inference will correct the error type for you.


#3

Thank you @Nemo157. Now when you explain it, it seems so obvious! :slight_smile: