rust


Repeating a Rust task with tokio_timer


I'm creating a repeating task in Rust using the Tokio framework. The following code is based on a completed change request to add this feature to the tokio-timer crate.
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. 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<()>
When using my_cron_func, which is a function that takes no arguments
but the trait std::ops::FnMut<((),)> is required
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 = ().
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 ():
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:
fn my_cron_func(_: ()) -> futures::future::FutureResult<(), tokio_timer::TimerError> {
println!("Repeating");
futures::future::ok(())
}

Related Links

Convert Arc<RwLock> to &mut
Cargo build results in “No match for id”
Cargo fails because the peer certificate cannot be authenticated with given CA certificates on SLES11
How to group 'Option' assignments in Rust?
Exclude dependencies when running Clippy
How to create a new empty Tree
How to access a vector multiple times within an 'Option'?
Iterator on Option<Vec<>>
Literal out of range warning when iterating over all values of u8
How to write a trait that has a method returning a reference and implement it correctly?
Mismatched types when displaying a matrix with a for loop
How to have a struct field with the same mutability as the parent struct?
`if` condition remains borrowed in body [duplicate]
“parameter `'a` is never used” error when 'a is used in type parameter bound
Linking Rust application with a dynamic library not in the runtime linker search path
Can't access environment variable in Rust

Categories

HOME
elasticsearch
webview
mc
xcode
freeradius
informatica
malware
tibco
angular-meteor
ui-automation
mithril.js
gprs
graphql-js
scheduler
tup
google-awareness
phonegap-build
nest-api
scheduled-tasks
opam
android-permissions
perfino
backendless
pyopencl
tortoisemerge
plyr
strophe.js
hdmi
xdebug
substance
source-insight
progid
rspec-rails
om-next
kendo-listview
spring-rabbitmq
cups
nanogallery
variable-variables
praat
jacoco-maven-plugin
uipickerview
grails3.2.0
secure-coding
1wire
julius-speech
permission-denied
asihttprequest
vesta
xcode8.2
bit-shift
tinyioc
nusoap
visual-format-language
aurelia-fetch-client
nslayoutconstraint
asp.net-web-api-routing
mongodb-aggregation
popen
stateless-session-bean
predicates
msxml
kendo-treeview
metabase
streamwriter
bilinear-interpolation
livecycle
p6spy
wininet
wolfram-language
fragmentstatepageradapter
unsigned
powerpoint-2013
noclassdeffounderror
i2b2
helm
jnlp
artisan
webproject
nssortdescriptor
ssmtp
directorysearcher
symja
adwords-apiv201402
oracle-adf-mobile
visual-c++-2012
xcode6.3.1
otl
prism.js
neo4jphp
commenting
typesafe-stack
hyphen
ggts
revmob
dynamic-rdlc-generation
mongo-jackson-mapper
spiral
oembed
xmlwriter
system-information
cakeyframeanimation
zotonic
kobold2d
firebird1.5
affinetransform
nosetests
rtd
turbine
.net-services
source-code-protection
tacit-programming
ning

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App