forked from M-Labs/artiq
88bf7d2233
Before this commit, proto::io::{read,write}_* functions were taking a &mut {Read,Write}. This means a lot of virtual dispatch. After this commit, all these functions are specialized for the specific IO trait. This could be achieved with just changing the signature from fn read_x(reader: &mut Read) to fn read_x<R: Read>(reader: &mut R) but the functions were also grouped into ReadExt and WriteExt traits as a refactoring. Initially, it was expected that the generic traits from the byteorder crate could be used, but they require endianness to be specified on every call and thus aren't very ergonomic. They also lack the equivalent to our read_string and read_bytes. Thus, it seems fine to just define a slightly different extension trait. This also optimized the test_rpc_timing test: 1.7ms→1.2ms.
58 lines
1.8 KiB
Rust
58 lines
1.8 KiB
Rust
use std::io::{self, Read, Write};
|
|
use {ReadExt, WriteExt};
|
|
|
|
#[derive(Debug)]
|
|
pub enum HostMessage {
|
|
Monitor { enable: bool, channel: u32, probe: u8 },
|
|
Inject { channel: u32, overrd: u8, value: u8 },
|
|
GetInjectionStatus { channel: u32, overrd: u8 }
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum DeviceMessage {
|
|
MonitorStatus { channel: u32, probe: u8, value: u32 },
|
|
InjectionStatus { channel: u32, overrd: u8, value: u8 }
|
|
}
|
|
|
|
impl HostMessage {
|
|
pub fn read_from(reader: &mut Read) -> io::Result<HostMessage> {
|
|
Ok(match reader.read_u8()? {
|
|
0 => HostMessage::Monitor {
|
|
enable: if reader.read_u8()? == 0 { false } else { true },
|
|
channel: reader.read_u32()?,
|
|
probe: reader.read_u8()?
|
|
},
|
|
1 => HostMessage::Inject {
|
|
channel: reader.read_u32()?,
|
|
overrd: reader.read_u8()?,
|
|
value: reader.read_u8()?
|
|
},
|
|
2 => HostMessage::GetInjectionStatus {
|
|
channel: reader.read_u32()?,
|
|
overrd: reader.read_u8()?
|
|
},
|
|
_ => return Err(io::Error::new(io::ErrorKind::InvalidData, "unknown packet type"))
|
|
})
|
|
}
|
|
}
|
|
|
|
impl DeviceMessage {
|
|
pub fn write_to(&self, writer: &mut Write) -> io::Result<()> {
|
|
match *self {
|
|
DeviceMessage::MonitorStatus { channel, probe, value } => {
|
|
writer.write_u8(0)?;
|
|
writer.write_u32(channel)?;
|
|
writer.write_u8(probe)?;
|
|
writer.write_u32(value)?;
|
|
},
|
|
DeviceMessage::InjectionStatus { channel, overrd, value } => {
|
|
writer.write_u8(1)?;
|
|
writer.write_u32(channel)?;
|
|
writer.write_u8(overrd)?;
|
|
writer.write_u8(value)?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|