UnboundedReceiver try_for_each method not working

I'm trying to establish a channel between two threads to communicate so I'm passing one thread an UnboundedReceiver and inside of that thread I want to check if a message has been received and then act on it. It looks like it has a blanket implementation for TryStream however when I try to call the try_for_each method on the UnboundedReceiver I received this error message:

287 |     let incoming = recv.try_for_each(|msg| {
    |                         ^^^^^^^^^^^^ method not found in `futures::channel::mpsc::UnboundedReceiver<MaintMsg>`
   ::: /home/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-channel-0.3.6/src/mpsc/mod.rs:148:1
148 | pub struct UnboundedReceiver<T> {
    | -------------------------------
    | |
    | doesn't satisfy `_: TryStreamExt`
    | doesn't satisfy `_: TryStream`
    = note: the method `try_for_each` exists but the following trait bounds were not satisfied:
            `futures::channel::mpsc::UnboundedReceiver<MaintMsg>: TryStream`
            which is required by `futures::channel::mpsc::UnboundedReceiver<MaintMsg>: TryStreamExt`
            `&futures::channel::mpsc::UnboundedReceiver<MaintMsg>: TryStream`
            which is required by `&futures::channel::mpsc::UnboundedReceiver<MaintMsg>: TryStreamExt`
            `&mut futures::channel::mpsc::UnboundedReceiver<MaintMsg>: TryStream`
            which is required by `&mut futures::channel::mpsc::UnboundedReceiver<MaintMsg>: TryStreamExt`

I thought this could be a scope issue but this is what I have included:

use futures::{
    channel::mpsc::{unbounded, UnboundedSender, UnboundedReceiver},
    future, pin_mut,
    stream::{TryStream, TryStreamExt},

And lastly this is the actual function where I'm trying to call the try_for_each method:

async fn check_recv(recv: UnboundedReceiver<MaintMsg>) {

    let incoming = recv.try_for_each(|msg| {


Could this be happening because I'm using tokio::spawn to kick off the thread and it's causing something to not be in scope? I'm really unsure of where to go from here. Potentially might be a version mismatch?

The TryStream has blanket impl for all S: Stream<Item = Result<T, E>> types. UnboundedReceiver<T> imps Stream<Item = T> so your receiver implements Stream<Item = MaintMsg> which can't be Stream<Item = Result<T, E>>

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.