![](/img/trans.png)
[英]Why do I get the error “cannot infer type” when using futures::ok?
[英]Why do I get a mismatched type error (expected mpsc::Receiver, found ()) when using a local Futures 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 `()`
为什么? 从文档中 spawn_local
应该采用()
未来,我正在通过它。
如错误消息所述:
类型不匹配,将
<ForEach<Receiver<Ping>, Result<(), futures::Never>, [closure]>
解析为<futures::Future>::Item == ()
spawn_local
要求传递给它的spawn_local
的关联类型 Item
为单位类型/空元组/ ()
。 您的未来不会返回那种类型。
应该走一个
()
未来,我正在过去
我不知道你为什么相信这是真的。 for_each
通过将流作为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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.