1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::fmt::{self, Debug};
use std::sync::mpsc;
use state_machine::StateMachine;
pub struct ChannelStateMachine {
tx: mpsc::Sender<Vec<u8>>,
}
impl ChannelStateMachine {
pub fn new() -> (ChannelStateMachine, mpsc::Receiver<Vec<u8>>) {
let (tx, recv) = mpsc::channel();
(ChannelStateMachine { tx: tx }, recv)
}
}
impl StateMachine for ChannelStateMachine {
fn apply(&mut self, command: &[u8]) -> Vec<u8> {
self.tx
.send(command.to_vec())
.map(|_| Vec::new())
.unwrap_or_else(|_| b"An error occured."[..].into())
}
fn query(&self, _query: &[u8]) -> Vec<u8> {
unimplemented!()
}
fn snapshot(&self) -> Vec<u8> {
Vec::new()
}
fn restore_snapshot(&mut self, _snapshot: Vec<u8>) -> () {
()
}
}
impl Debug for ChannelStateMachine {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "ChannelStateMachine")
}
}