使用tokio_timer重复执行Rust任务

8
我正在使用Tokio框架在Rust中创建一个重复任务。下面的代码基于对tokio-timer crate添加此功能的已完成的更改请求
尝试编译时,我收到以下错误消息:
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}, _>`

错误提示表明 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(());
}
1个回答

4

我不确定哪部分错误信息让您感到困扰,但是...

类型不匹配

您提供了错误的类型。

类型fn() {my_cron_func}实现了特性std::ops::FnMut<()>

当使用不带参数的函数my_cron_func时,

但需要特性std::ops::FnMut<((),)>

需要一个需要一个空元组作为单个参数的函数。

(期望元组,发现())

编译器试图缩小问题范围。

如果您查阅所使用的库的文档,特别是tokio_timer::Interval,您可以看到它实现了与相关类型Item = ()futures::Stream

这将改变错误信息:

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}, ()>`

在查看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

您的函数试图返回某些内容,但是没有返回类型,并且您使用了一个;来结束函数:

fn my_cron_func(a: ()) {
    println!("Repeating");
    Ok(());
}

futures::future::ok可以解决这个问题:

fn my_cron_func(_: ()) -> futures::future::FutureResult<(), tokio_timer::TimerError> {
    println!("Repeating");
    futures::future::ok(())
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接