简体   繁体   English

使用tokio_timer重复Rust任务

[英]Repeating a Rust task with tokio_timer

I'm creating a repeating task in Rust using the Tokio framework. 我正在使用Tokio框架在Rust中创建一个重复任务。 The following code is based on a completed change request to add this feature to the tokio-timer crate. 以下代码基于已完成的更改请求,以将此功能添加到tokio-timer包。

When trying to compile, I get the error message: 在尝试编译时,我收到错误消息:

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}, _>`

The error states that the return signature for the my_cron_func function is incorrect. 该错误指出my_cron_func函数的返回签名不正确。 What do I need to change/add to get the signature correct so it compiles? 我需要更改/添加什么才能使签名正确以便编译?

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(());
}

I'm not sure what part of the error message is causing you trouble, but... 我不确定错误信息的哪一部分会给您带来麻烦,但......

type mismatch 类型不匹配

You've provided the wrong type 你提供了错误的类型

the type fn() {my_cron_func} implements the trait std::ops::FnMut<()> 类型fn() {my_cron_func}实现了特征std::ops::FnMut<()>

When using my_cron_func , which is a function that takes no arguments 使用my_cron_func ,这是一个不带参数的函数

but the trait std::ops::FnMut<((),)> is required 但特征std::ops::FnMut<((),)>是必需的

But a function that takes a single argument, the empty tuple, is required. 但是需要一个带有单个参数的函数,即空元组。

(expected tuple, found ()) (预期的元组,发现())

And the compiler tries to narrow down the problem. 编译器试图缩小问题的范围。

If you review the documentation for the library you are using, specifically tokio_timer::Interval , you can see that it implements futures::Stream with the associated type Item = () . 如果您查看您正在使用的库的文档,特别是tokio_timer::Interval ,您可以看到它实现了具有关联类型Item = () futures::Stream

This changes the error message: 这会更改错误消息:

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

Reviewing the documentation for futures::Stream , we can see that the closure passed to for_each needs to return a value that can be turned into a future that will yield () : 查看future futures::Stream文档 ,我们可以看到传递给for_each的闭包需要返回一个值,该值可以转换为yield ()的未来:

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

Your function attempts to return something, except that there's no return type and you've used a ; 你的函数试图返回一些东西,除了没有返回类型你用过了; to end the function: 结束功能:

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

futures::future::ok does the trick: futures::future::ok诀窍:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM