为什么在使用本地Futures执行器时会出现不匹配的类型错误(预期mpsc :: Receiver,found())?

时间:2018-05-03 07:44:54

标签: rust future channel executor

我有以下代码:

extern crate futures;

use futures::channel::mpsc;
use futures::executor::LocalPool;
use futures::prelude::*;

struct Ping(usize);

fn main() {
    let (last_tx, mut prev_rx) = mpsc::channel::<Ping>(1);
    let mut pool = LocalPool::new();
    let mut executor = pool.executor();
    let (tx_1, rx_1) = mpsc::channel::<Ping>(1);
    let (tx_2, rx_2) = mpsc::channel::<Ping>(1);
    executor.spawn_local(rx_1.for_each(move |Ping(size)| {
        if size == 10 {
            tx_2.close();
            println!("Done 2");
        } else {
            let tx = tx_2.clone();
            tx.send(Ping(size + 1));
        }
        Ok(())
    }));

    executor.spawn_local(rx_2.for_each(move |Ping(size)| {
        if size == 10 {
            tx_1.close();
            println!("Done 1");
        } else {
            let tx = tx_1.clone();
            tx.send(Ping(size + 1));
        }
        Ok(())
    }));
}

它无法编译:

error[E0271]: type mismatch resolving `<futures::stream::ForEach<futures::channel::mpsc::Receiver<Ping>, std::result::Result<(), futures::Never>, [closure@src/bin/futures_ring_poc.rs:16:40: 25:6 tx_2:_]> as futures::Future>::Item == ()`
  --> src/bin/futures_ring_poc.rs:16:14
   |
16 |     executor.spawn_local(rx_1.for_each(move |Ping(size)| {
   |              ^^^^^^^^^^^ expected struct `futures::channel::mpsc::Receiver`, found ()
   |
   = note: expected type `futures::channel::mpsc::Receiver<Ping>`
              found type `()`

error[E0271]: type mismatch resolving `<futures::stream::ForEach<futures::channel::mpsc::Receiver<Ping>, std::result::Result<(), futures::Never>, [closure@src/bin/futures_ring_poc.rs:27:40: 36:6 tx_1:_]> as futures::Future>::Item == ()`
  --> src/bin/futures_ring_poc.rs:27:14
   |
27 |     executor.spawn_local(rx_2.for_each(move |Ping(size)| {
   |              ^^^^^^^^^^^ expected struct `futures::channel::mpsc::Receiver`, found ()
   |
   = note: expected type `futures::channel::mpsc::Receiver<Ping>`
              found type `()`

为什么呢?从the docs spawn_local开始,我需要()个未来,我将通过它。

1 个答案:

答案 0 :(得分:4)

如错误消息所示:

  

类型不匹配将<ForEach<Receiver<Ping>, Result<(), futures::Never>, [closure]>解析为<futures::Future>::Item == ()

spawn_local要求传递给它的未来关联类型 Item是单位类型/空元组/ ()。你的未来不会回归那种类型。

  

应该采用()未来,我将其传递给

我不知道为什么你认为这是真的。 for_each通过returning the stream as the Item实施Future

可以使用map来丢弃流:

executor.spawn_local(rx_1.for_each(move |Ping(size)| {
    // ...
}).map(|_| ()));

这不允许您的代码编译,但它修复了您的错误。

使代码工作是一个更大的变化。这是一种可能性,没有评论,因为它与您的问题无关:

extern crate futures;

use futures::{
    channel::mpsc::{self, Receiver, Sender},
    executor::LocalPool,
    prelude::*,
};

struct Ping(usize);

fn pinger(
    rx: Receiver<Ping>,
    tx: Sender<Ping>,
    id: &'static str,
) -> impl Future<Item = (), Error = Never> {
    rx.map_err(Never::never_into::<Box<std::error::Error>>)
        .fold(tx, move |tx, Ping(size)| {
            println!("{}: {}", id, size);

            if size >= 10 {
                println!("{}: Done", id);
                tx.close().err_into().right_future()
            } else {
                tx.send(Ping(size + 1)).err_into().left_future()
            }
        })
        .map(|_| ())
        .map_err(move |e| panic!("Task {} failed: {}", id, e))
}

fn main() {
    let mut pool = LocalPool::new();
    let mut executor = pool.executor();

    let (tx_1, rx_1) = mpsc::channel(1);
    let (tx_2, rx_2) = mpsc::channel(1);
    let tx_ignite = tx_1.clone();

    executor.spawn_local(pinger(rx_1, tx_2, "Rx 1")).unwrap();
    executor.spawn_local(pinger(rx_2, tx_1, "Rx 2")).unwrap();

    executor
        .spawn_local({
            tx_ignite
                .send(Ping(0))
                .map(drop)
                .map_err(|e| panic!("{:?}", e))
        })
        .unwrap();

    pool.run(&mut executor);
}