Rust 为什么在使用本地期货执行器时会出现不匹配的类型错误(预期为mpsc::Receiver,found())?
我有以下代码:Rust 为什么在使用本地期货执行器时会出现不匹配的类型错误(预期为mpsc::Receiver,found())?,rust,future,channel,executor,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
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(())
}));
}
extern板条箱期货;
使用futures::channel::mpsc;
使用futures::executor::LocalPool;
使用期货::前奏::*;
结构Ping(usize);
fn main(){
let(最后一个发送,多个上一个接收)=mpsc::信道::(1);
让mut pool=LocalPool::new();
让mut executor=pool.executor();
let(tx_1,rx_1)=mpsc::channel::(1);
let(tx_2,rx_2)=mpsc::channel::(1);
执行者。本地产卵(rx_1.每个(移动| Ping(大小)|){
如果大小==10{
tx_2.close();
println!(“完成2”);
}否则{
设tx=tx_2.clone();
发送(Ping(大小+1));
}
好(())
}));
执行者。本地产卵(rx_2.for_每个(移动| Ping(大小)|{
如果大小==10{
tx_1.close();
println!(“完成1”);
}否则{
设tx=tx_1.clone();
发送(Ping(大小+1));
}
好(())
}));
}
它不编译:
错误[E0271]:类型不匹配解决`::项==()`
-->src/bin/futures\u ring\u poc.rs:16:14
|
16 |执行者。本地产卵(rx|u 1.每个产卵(移动| Ping(大小)|{
|^^^^^^^^^^^找到了预期的结构“futures::channel::mpsc::Receiver`()
|
=注意:预期类型为`futures::channel::mpsc::Receiver`
找到类型“%1”()`
错误[E0271]:类型不匹配解析“::项==()`
-->src/bin/futures\u ring\u poc.rs:27:14
|
27 |执行者。本地产卵(rx|u 2.每个产卵(移动| Ping(大小)|{
|^^^^^^^^^^^找到了预期的结构“futures::channel::mpsc::Receiver`()
|
=注意:预期类型为`futures::channel::mpsc::Receiver`
找到类型“%1”()`
为什么?从
spawn\u local
应该采用()
未来,我正在传递它。正如错误消息所述:
类型不匹配解析
,如错误消息所述:
类型不匹配解析
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);
}