[英]Repeating a Rust task with tokio_timer
我正在使用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}, _>`
該錯誤指出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(());
}
我不確定錯誤信息的哪一部分會給您帶來麻煩,但......
類型不匹配
你提供了錯誤的類型
類型
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}, ()>`
查看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
你的函數試圖返回一些東西,除了沒有返回類型你用過了;
結束功能:
fn my_cron_func(a: ()) {
println!("Repeating");
Ok(());
}
fn my_cron_func(_: ()) -> futures::future::FutureResult<(), tokio_timer::TimerError> {
println!("Repeating");
futures::future::ok(())
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.