我正在使用Tokio框架在Rust中创建重复任务。下面的代码基于完成的更改请求,以将该功能添加到Tokio-Timer板条箱中。
尝试编译时,出现错误消息:
error[E0281]: type mismatch: the type `fn() {my_cron_func}` implements the trait `std::ops::FnMut<()>`, but the trait `std::ops::FnMut<((),)>` is required (expected tuple, found ())
--> src/main.rs:19:36
|
19 | let background_tasks = wakeups.for_each(my_cron_func);
| ^^^^^^^^
error[E0281]: type mismatch: the type `fn() {my_cron_func}` implements the trait `std::ops::FnOnce<()>`, but the trait `std::ops::FnOnce<((),)>` is required (expected tuple, found ())
--> src/main.rs:19:36
|
19 | let background_tasks = wakeups.for_each(my_cron_func);
| ^^^^^^^^
error[E0281]: type mismatch: the type `fn() {my_cron_func}` implements the trait `std::ops::FnMut<()>`, but the trait `std::ops::FnMut<((),)>` is required (expected tuple, found ())
--> src/main.rs:20:10
|
20 | core.run(background_tasks).unwrap();
| ^^^
|
= note: required because of the requirements on the impl of `futures::Future` for `futures::stream::ForEach<tokio_timer::Interval, fn() {my_cron_func}, _>`
error[E0281]: type mismatch: the type `fn() {my_cron_func}` implements the trait `std::ops::FnOnce<()>`, but the trait `std::ops::FnOnce<((),)>` is required (expected tuple, found ())
--> src/main.rs:20:10
|
20 | core.run(background_tasks).unwrap();
| ^^^
|
= note: required because of the requirements on the impl of `futures::Future` for `futures::stream::ForEach<tokio_timer::Interval, fn() {my_cron_func}, _>`
Run Code Online (Sandbox Code Playgroud)
该错误指出my_cron_func函数的返回签名不正确。我需要更改/添加什么才能使签名正确以进行编译?
extern crate futures;
extern crate tokio_core;
extern crate tokio_timer;
use std::time::*;
use futures::*;
use tokio_core::reactor::Core;
use tokio_timer::*;
pub fn main() {
println!("The start");
let mut core = Core::new().unwrap();
let timer = Timer::default();
let duration = Duration::new(2, 0); // 2 seconds
let wakeups = timer.interval(duration);
// issues here
let background_tasks = wakeups.for_each(my_cron_func);
core.run(background_tasks).unwrap();
println!("The end???");
}
fn my_cron_func() {
println!("Repeating");
Ok(());
}
Run Code Online (Sandbox Code Playgroud)
我不确定错误消息的哪一部分给您带来了麻烦,但是......
类型不匹配
您提供了错误的类型
类型
fn() {my_cron_func}实现了特征std::ops::FnMut<()>
当使用 时my_cron_func,这是一个不带参数的函数
但特质
std::ops::FnMut<((),)>是必需的
但是需要一个采用单个参数(空元组)的函数。
(预期元组,找到 ())
编译器会尝试缩小问题范围。
如果您查看正在使用的库的文档,特别是tokio_timer::Interval,您可以看到它是futures::Stream使用关联的类型实现的Item = ()。
这会更改错误消息:
error[E0277]: the trait bound `(): futures::Future` is not satisfied
--> src/main.rs:19:36
|
19 | let background_tasks = wakeups.for_each(my_cron_func);
| ^^^^^^^^ the trait `futures::Future` is not implemented for `()`
|
= note: required because of the requirements on the impl of `futures::IntoFuture` for `()`
error[E0277]: the trait bound `(): futures::Future` is not satisfied
--> src/main.rs:20:10
|
20 | core.run(background_tasks).unwrap();
| ^^^ the trait `futures::Future` is not implemented for `()`
|
= note: required because of the requirements on the impl of `futures::IntoFuture` for `()`
= note: required because of the requirements on the impl of `futures::Future` for `futures::stream::ForEach<tokio_timer::Interval, fn(()) {my_cron_func}, ()>`
Run Code Online (Sandbox Code Playgroud)
查看 的文档futures::Stream,我们可以看到传递给 的闭包for_each需要返回一个可以转换为将产生的 future 的值():
fn for_each<F, U>(self, f: F) -> ForEach<Self, F, U>
where F: FnMut(Self::Item) -> U,
U: IntoFuture<Item=(), Error=Self::Error>,
Self: Sized
Run Code Online (Sandbox Code Playgroud)
您的函数尝试返回某些内容,但没有返回类型并且您使用了 a;来结束该函数:
fn my_cron_func(a: ()) {
println!("Repeating");
Ok(());
}
Run Code Online (Sandbox Code Playgroud)
fn my_cron_func(_: ()) -> futures::future::FutureResult<(), tokio_timer::TimerError> {
println!("Repeating");
futures::future::ok(())
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1635 次 |
| 最近记录: |