Now it even runs

This commit is contained in:
Sam W 2019-02-10 17:53:24 +00:00
parent 873c069d09
commit 267a99d6b9
2 changed files with 7 additions and 8 deletions

View File

@ -1,4 +1,4 @@
use crate::TallyState; use crate::{TallyState,Channel};
use std::sync::mpsc; use std::sync::mpsc;
use std::sync::mpsc::{Sender, Receiver}; use std::sync::mpsc::{Sender, Receiver};
use std::thread; use std::thread;
@ -27,8 +27,8 @@ impl FakeAdaptor {
println!("Hello, I have {} channels", numchannels); println!("Hello, I have {} channels", numchannels);
loop { loop {
let state = TallyState{ let state = TallyState{
on_air: Some(rand::thread_rng().gen_range(0, numchannels)), on_air: Channel(Some(rand::thread_rng().gen_range(0, numchannels))),
preview: Some(rand::thread_rng().gen_range(0, numchannels)), preview: Channel(Some(rand::thread_rng().gen_range(0, numchannels))),
}; };
sender.send(state).unwrap(); sender.send(state).unwrap();
thread::sleep(Duration::new(rand::thread_rng().gen_range(0,5), 0)); thread::sleep(Duration::new(rand::thread_rng().gen_range(0,5), 0));

View File

@ -1,10 +1,9 @@
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
mod adaptors; mod adaptors;
use crate::adaptors::{Adaptor,FakeAdaptor}; use crate::adaptors::{Adaptor,FakeAdaptor};
use std::fmt; use std::fmt;
type Channel = Option<u8>; #[derive(Clone)]
pub struct Channel(Option<u8>);
#[derive(Clone)] #[derive(Clone)]
pub struct TallyState { pub struct TallyState {
@ -14,7 +13,7 @@ pub struct TallyState {
impl fmt::Display for Channel { impl fmt::Display for Channel {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self.0 {
Some(n) => write!(f, "{}", n), Some(n) => write!(f, "{}", n),
None => write!(f, "-"), None => write!(f, "-"),
} }
@ -33,8 +32,8 @@ struct Server<T:Adaptor> {
impl<T:Adaptor> Server<T> { impl<T:Adaptor> Server<T> {
fn run(&mut self) { fn run(&mut self) {
loop{
let rx = self.adaptor.run(); let rx = self.adaptor.run();
loop{
let newstate = rx.recv().unwrap(); let newstate = rx.recv().unwrap();
println!("{}", newstate); println!("{}", newstate);
} }