Mercurial > templog
annotate rust/src/paramwaiter.rs @ 593:bf138339d20a rust
fiddling with timeouts and closures
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 27 Dec 2016 00:51:28 +0800 |
parents | rust/src/configwaiter.rs@03b48ec0bb03 |
children | aff50ee77252 |
rev | line source |
---|---|
592 | 1 extern crate tokio_core; |
2 extern crate futures; | |
3 extern crate rand; | |
4 | |
5 use std::time::Duration; | |
6 use std::io; | |
7 | |
8 use tokio_core::reactor::Interval; | |
9 use tokio_core::reactor::Handle; | |
10 use futures::Stream; | |
11 use types::*; | |
12 | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
13 pub struct ParamWaiter { |
592 | 14 } |
15 | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
16 impl ParamWaiter { |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
17 fn step(&mut self) -> Params { |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
18 let mut p = Params::defaults(); |
592 | 19 let mut rng = rand::thread_rng(); |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
20 p.fridge_setpoint = 17.0 + 4.0*rand::random::<f32>(); |
592 | 21 p |
22 } | |
23 | |
24 pub fn new() -> Self { | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
25 ParamWaiter {} |
592 | 26 } |
27 | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
28 pub fn run(handle: &Handle, rate: u64) -> Box<Stream<Item=Params, Error=io::Error>> { |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
29 let mut s = ParamWaiter::new(); |
592 | 30 |
31 let dur = Duration::from_millis(rate); | |
32 Interval::new(dur, handle).unwrap().map(move |()| { | |
33 s.step() | |
34 }).boxed() | |
35 } | |
36 } | |
37 |