2017-02-18 4 views
3

私はTokioフレームワークを使ってRustで繰り返しタスクを作成しています。次のコードは、tokio-timer crateにこの機能を追加するためのcompleted change requestに基づいています。tokio_timerでRustタスクを繰り返す

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

答えて

3

私は...あなたがトラブルの原因をエラーメッセージの一部わからないんだけど、

あなたが間違った型

を提供してきました

型の不一致

タイプfn() {my_cron_func}は形質を実装しますstd::ops::FnMut<()>

引数

しかし

必要ですstd::ops::FnMut<((),)>トレイトしかし、一つの引数を取る関数、空のタプルをとらない機能がある、my_cron_funcを使用して、必要とされます。

(見つけ予想タプルは、())

コンパイラは、問題を絞り込むしようとします。

使用しているライブラリのドキュメント、特に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ニーズに渡されたクロージャは()をもたらすであろう未来に回すことができる値を返すようにすることを見ることができます

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(()) 
} 
関連する問題