Why Stream forwarding requires TryStream?

I'm trying to understand the reasoning behind a requirement of stream forwarding which basically forces me to always "wrap" stream items with Ok.

Consider the following example:

   let (ch1_tx, ch1_rx) = futures_channel::mpsc::channel::<String>(16);
   let (ch2_tx, ch2_rx) = futures_channel::mpsc::channel::<String>(16);
   let f = ch1_rx.map(Ok).forward(ch2_tx);

So if I don't write map(Ok) above the code won't compile with an error:
error[E0271]: type mismatch resolving <futures_channel::mpsc::Receiver<std::string::String> as futures_core::stream::Stream>::Item == std::result::Result<_, _>
--> src/bin/my_test.rs:8:20
8 | let f = ch1_rx.forward(ch2_tx);
| ^^^^^^^ expected struct std::string::String, found enum std::result::Result
= note: expected type std::string::String
found enum std::result::Result<_, _>
= note: required because of the requirements on the impl of futures_core::stream::TryStream for futures_channel::mpsc::Receiver<std::string::String>

The question is why it was designed in such way that is it necessary to pass Result into the forwarding stream instead of just String? Also, when consuming items from ch2_rx stream, they will come up as Strings not as Results, so I'm not sure why would forwarding an Err be ever useful?

Writing to a Sink may fail, which means that forward returns a Result with that error. By taking a stream with Result item type, the Result returned by forward can return both kinds of errors in the same way and not forward errors in the stream.

If forward was implemented to just send the item, then you could not have the above behavior — errors in the stream would just always be forwarded as well. To make this early return on error in stream possible, you have to specialize on just taking streams with results. You can't have it do one or the other depending on whether the stream has result as item type or not.

I guess they could have provided both versions, but apparently they thought that requiring a .map(Ok) in your case was ok.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.