portus/serialize/
ready.rs1use super::{u32_to_u8s, AsRawMsg, RawMsg, HDR_LENGTH};
4use crate::Result;
5use std::io::prelude::*;
6
7pub(crate) const READY: u8 = 5;
8
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct Msg {
11 pub id: u32,
12}
13
14impl AsRawMsg for Msg {
15 fn get_hdr(&self) -> (u8, u32, u32) {
16 (READY, HDR_LENGTH + 1 * 4, 0)
17 }
18
19 fn get_u32s<W: Write>(&self, w: &mut W) -> Result<()> {
20 let mut buf = [0u8; 4];
21 u32_to_u8s(&mut buf, self.id as u32);
22 w.write_all(&buf[..])?;
23 Ok(())
24 }
25
26 fn get_bytes<W: Write>(&self, _: &mut W) -> Result<()> {
27 Ok(())
28 }
29
30 fn from_raw_msg(msg: RawMsg) -> Result<Self> {
31 let u32s = unsafe { msg.get_u32s() }?;
32 Ok(Msg { id: u32s[0] })
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 macro_rules! check_ready_msg {
39 ($id: ident, $msg: expr) => {
40 check_msg!(
41 $id,
42 super::Msg,
43 $msg,
44 crate::serialize::Msg::Rdy(rdym),
45 rdym
46 );
47 };
48 }
49
50 check_ready_msg!(test_ready_1, super::Msg { id: 7 });
51}