Skip to content

TryStream bound for SinkExt::send_all #2024

@vskh

Description

@vskh

I noticed that recently send_all signature was updated to require TryStream bound on passed stream (#1946).

I wonder if there is a reason for this restriction?

Consider the following code that I have:

type TestData = HashMap<String, usize>;

let sink: Sink<TestData> = ...;
let msg1 = HashMap::new();
let msg2 = HashMap::new();

let mut msgs = futures::stream::iter(vec![msg1, msg2]);

sender.send_all(&mut msgs).await?;

This fails with following error:

error[E0271]: type mismatch resolving `<futures_util::stream::iter::Iter<std::vec::IntoIter<std::collections::HashMap<std::string::String, usize>>> as futures_core::stream::Stream>::Item == std::result::Result<_, _>`
  --> examples/basic.rs:55:12
   |
55 |     sender.send_all(&mut msgs).await?;
   |            ^^^^^^^^ expected struct `std::collections::HashMap`, found enum `std::result::Result`
   |
   = note: expected type `std::collections::HashMap<std::string::String, usize>`
              found enum `std::result::Result<_, _>`
   = note: required because of the requirements on the impl of `futures_core::stream::TryStream` for `futures_util::stream::iter::Iter<std::vec::IntoIter<std::collections::HashMap<std::string::String, usize>>>`

Took me some time to decrypt that this happens because stream::iter::Iter does not implement TryStream and gets automatic implementation only if stream's item is std::result::Result. So, in simple code above I have to wrap each message into Ok like futures::stream::iter(vec![Ok(msg1), Ok(msg2)]) which looks weird.

Shouldn't it be possible to send streams that produce items for sure and can't fail? Unless I am missing something obvious that makes it undesirable/not possible, I think sinking collection of values is rather often use case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-sinkArea: futures::sinkS-needs-decisionStatus: A decision on whether or not to do this is needed.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions