annotate rust/src/main.rs @ 589:f2508125adf1 rust

Try using traits for periodic stream
author Matt Johnston <matt@ucc.asn.au>
date Wed, 21 Dec 2016 08:16:13 +0800
parents 038734052b20
children dccd8504aa38
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
1 #![feature(conservative_impl_trait)]
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
2 extern crate tokio_core;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
3 extern crate futures;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
4
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
5 use std::time::Duration;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
6 use std::cell::RefCell;
589
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
7 use std::io::{self, Read, Write};
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
9 use tokio_core::reactor::Interval;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
10 use tokio_core::reactor::Core;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
11 use tokio_core::reactor::Handle;
589
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
12 use futures::{Future, failed, Poll, Async, Stream};
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
13
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
14 pub struct Reading {
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
15 name: String,
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
16 value: Option<f32>,
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
17 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
18
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
19 pub type Readings = Vec<Reading>;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
20
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
21 pub struct Sensor {
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
22 current: f32,
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
23 }
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
589
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
25 struct Periodic<T> {
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
26 interval: Interval,
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
27 }
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
28
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
29 trait Ticker {
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
30 type TickItem;
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
31 fn tick(&self) -> Self::TickItem;
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
32 }
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
33
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
34 impl<T: Ticker> Stream for Periodic<T> {
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
35 type Item = T::TickItem;
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
36 type Error = io::Error;
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
37
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
38 fn poll(&mut self) -> Poll<Option<Self::Item>, io::Error> {
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
39 self.interval.poll().map(|a| {
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
40 if let Async::Ready(t) = a {
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
41 Async::Ready(self.tick())
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
42 } else {
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
43 a
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
44 }
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
45 })
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
46 }
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
47 }
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
48
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
49 impl Sensor {
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
50
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
51 fn step(&mut self) -> Readings {
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
52 let mut r = Vec::new();
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
53 self.current = self.current + 0.1;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
54 r.push(Reading { name: "aaa".to_string(), value: Some(self.current) });
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
55 r
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
56 }
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
58 pub fn new() -> Sensor {
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
59 Sensor { current: 22.0 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
60 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
61
589
f2508125adf1 Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
62 pub fn run(handle: &Handle) -> Box<Stream<Item=Readings, Error=io::Error>> {
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
63 let mut s = Sensor::new();
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
64
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
65 let dur = Duration::from_millis(400);
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
66 Interval::new(dur, handle).unwrap().map(move |()| {
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
67 println!("each one");
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
68 // TODO read the sensor here
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
69 s.step()
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
70 } )
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
71 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
72 }
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 fn main() {
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 println!("Wort Templog");
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
76
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
77 let mut core = Core::new().unwrap();
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
78 let handle = core.handle();
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
80 let s = Sensor::run(&handle);
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
81
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
82 let mut re = Readings::new();
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
84 let h = s.for_each(|r| {
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
85 re = r;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
86 for rx in &re {
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
87 match rx.value {
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
88 Some(x) => println!("re is {} {}", rx.name, x),
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
89 None => println!("re is {} broken", rx.name),
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
90 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
91 };
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
92 Ok(())
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
93 });
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
95 core.run(h);
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97