返回 Future 类型时无法推断类型 F
Can't infer type F when returning a Future type
我正在尝试编写一个 return 和 Future
的函数,所以在 a tutorial from Tokio 之后,我想到了这个:
extern crate tokio;
use std::time::{Duration, Instant};
use tokio::prelude::*;
use tokio::timer::Interval;
fn run<F>() -> impl Future<Item = (), Error = F::Error>
where
F: Future<Item = ()>,
{
Interval::new(Instant::now(), Duration::from_millis(1000))
.for_each(move |instant| {
println!("fire; instant={:?}", instant);
Ok(())
})
.map_err(|e| panic!("interval errored; err={:?}", e))
}
fn main() {
tokio::run(run());
}
我收到这个错误:
error[E0282]: type annotations needed
--> src/main.rs:20:16
|
20 | tokio::run(run());
| ^^^ cannot infer type for `F`
我假设一旦我指定完整的 return 类型,错误就会消失,我什至无法弄清楚(我的 IDE 给我 <futures::MapErr<futures::stream::ForEach<tokio::timer::Interval, [closure@src/ir.rs:24:23: 38:14 self:_], std::result::Result<(), tokio::timer::Error>>, [closure@src/ir.rs:39:22: 39:65]>
)
如何判断类型?任何 IDE 提示或技巧? (我正在使用带有 ide-rust 的 Atom)
我可以通过定义 impl Future<Item = (), Error = F::Error> where F: Future<Item = ()>
以某种方式逃脱吗?
我可以在 run
函数的 inside 某处定义完整类型,但我想在函数的 outside 处公开 <Future<Item = (), Error = F::Error>>
或 <Future<Item = (), Error = io::Error>>
看看tokio::run
的签名:
pub fn run<F>(future: F)
where
F: Future<Item = (), Error = ()> + Send + 'static,
消费的未来必须具有等于 ()
的关联 Error
类型。这意味着您不能对错误通用。
这个有效:
fn run() -> impl Future<Item = (), Error = ()> {
Interval::new(Instant::now(), Duration::from_millis(1000))
.for_each(move |instant| {
println!("fire; instant={:?}", instant);
Ok(())
})
.map_err(|e| panic!("interval errored; err={:?}", e))
}
fn main() {
tokio::run(run());
}
我正在尝试编写一个 return 和 Future
的函数,所以在 a tutorial from Tokio 之后,我想到了这个:
extern crate tokio;
use std::time::{Duration, Instant};
use tokio::prelude::*;
use tokio::timer::Interval;
fn run<F>() -> impl Future<Item = (), Error = F::Error>
where
F: Future<Item = ()>,
{
Interval::new(Instant::now(), Duration::from_millis(1000))
.for_each(move |instant| {
println!("fire; instant={:?}", instant);
Ok(())
})
.map_err(|e| panic!("interval errored; err={:?}", e))
}
fn main() {
tokio::run(run());
}
我收到这个错误:
error[E0282]: type annotations needed
--> src/main.rs:20:16
|
20 | tokio::run(run());
| ^^^ cannot infer type for `F`
我假设一旦我指定完整的 return 类型,错误就会消失,我什至无法弄清楚(我的 IDE 给我 <futures::MapErr<futures::stream::ForEach<tokio::timer::Interval, [closure@src/ir.rs:24:23: 38:14 self:_], std::result::Result<(), tokio::timer::Error>>, [closure@src/ir.rs:39:22: 39:65]>
)
如何判断类型?任何 IDE 提示或技巧? (我正在使用带有 ide-rust 的 Atom)
我可以通过定义
impl Future<Item = (), Error = F::Error> where F: Future<Item = ()>
以某种方式逃脱吗?我可以在
run
函数的 inside 某处定义完整类型,但我想在函数的 outside 处公开<Future<Item = (), Error = F::Error>>
或<Future<Item = (), Error = io::Error>>
看看tokio::run
的签名:
pub fn run<F>(future: F)
where
F: Future<Item = (), Error = ()> + Send + 'static,
消费的未来必须具有等于 ()
的关联 Error
类型。这意味着您不能对错误通用。
这个有效:
fn run() -> impl Future<Item = (), Error = ()> {
Interval::new(Instant::now(), Duration::from_millis(1000))
.for_each(move |instant| {
println!("fire; instant={:?}", instant);
Ok(())
})
.map_err(|e| panic!("interval errored; err={:?}", e))
}
fn main() {
tokio::run(run());
}