使用 tokio_timer 重复 Rust 任务
Repeating a Rust task with tokio_timer
我正在使用 Tokio 框架在 Rust 中创建重复任务。以下代码基于 completed change request 将此功能添加到 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 函数的 return 签名不正确。我需要什么 change/add 才能使签名正确才能编译?
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(());
}
我不确定错误消息的哪一部分给您带来了麻烦,但是...
type mismatch
您提供的类型有误
the type fn() {my_cron_func}
implements the trait std::ops::FnMut<()>
当使用 my_cron_func
时,这是一个不带参数的函数
but the trait std::ops::FnMut<((),)>
is required
但是需要一个接受单个参数的函数,即空元组。
(expected tuple, found ())
并且编译器试图缩小问题范围。
如果查看您正在使用的库的文档,特别是 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}, ()>`
查看 documentation for futures::Stream
,我们可以看到传递给 for_each
的闭包需要 return 一个可以转化为未来的值,该值将产生 ()
:
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
您的函数尝试 return 某些东西,除了没有 return 类型并且您使用了 ;
来结束函数:
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(())
}
我正在使用 Tokio 框架在 Rust 中创建重复任务。以下代码基于 completed change request 将此功能添加到 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 函数的 return 签名不正确。我需要什么 change/add 才能使签名正确才能编译?
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(());
}
我不确定错误消息的哪一部分给您带来了麻烦,但是...
type mismatch
您提供的类型有误
the type
fn() {my_cron_func}
implements the traitstd::ops::FnMut<()>
当使用 my_cron_func
时,这是一个不带参数的函数
but the trait
std::ops::FnMut<((),)>
is required
但是需要一个接受单个参数的函数,即空元组。
(expected tuple, found ())
并且编译器试图缩小问题范围。
如果查看您正在使用的库的文档,特别是 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}, ()>`
查看 documentation for futures::Stream
,我们可以看到传递给 for_each
的闭包需要 return 一个可以转化为未来的值,该值将产生 ()
:
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
您的函数尝试 return 某些东西,除了没有 return 类型并且您使用了 ;
来结束函数:
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(())
}