2 use std
::rand
::{ random
, task_rng
, distributions
};
3 use std
::rand
::distributions
::IndependentSample
;
5 pub enum ReadingError
{
6 ReadIOError(io
::IoError
)
10 pub enum WritingError
{
11 WriteIOError(io
::IoError
)
15 pub type ReadingResult
= Result
<Packet
, ReadingError
>;
16 pub type WritingResult
= Result
<(), WritingError
>;
23 pub struct CommandPacket
{
27 commandPayload
: Vec
<u8>
30 pub struct ErrorPacket
{
35 Command(CommandPacket
),
40 pub fn newRandomCommand(timestamp
: u64) -> Packet
{
42 let mut rng
= task_rng();
43 Command(CommandPacket
{
46 commandID
: random
::<u8>(),
47 commandPayload
: Vec
::from_fn(distributions
::Range
::new(7u, 40u).ind_sample(&mut rng
), |idx
| 0u8)
51 pub fn write(&self, output
: &mut io
::Writer
) -> WritingResult
{
54 &Command(ref command
) => {
56 output
.write_be_u64(command
.timestamp
);
66 pub fn read(input
: &mut io
::Reader
) -> ReadingResult
{
67 Ok(Packet
::newRandomCommand(0))