From aa42a69849588c3abdcc6934cec1d1c11a895c0d Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 13 May 2018 13:39:14 +0000 Subject: [PATCH] libdrtioaux: always inline read/write helpers. This shrinks Packet::{read_from,write_to} by 1K. --- artiq/firmware/libdrtioaux/proto.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/artiq/firmware/libdrtioaux/proto.rs b/artiq/firmware/libdrtioaux/proto.rs index ca3101ba6..9a89796e0 100644 --- a/artiq/firmware/libdrtioaux/proto.rs +++ b/artiq/firmware/libdrtioaux/proto.rs @@ -6,17 +6,20 @@ use std::string::String; use byteorder::{ByteOrder, NetworkEndian}; // FIXME: replace these with byteorder core io traits once those are in +#[inline(always)] pub fn read_u8(reader: &mut Read) -> io::Result { let mut bytes = [0; 1]; reader.read_exact(&mut bytes)?; Ok(bytes[0]) } +#[inline(always)] pub fn write_u8(writer: &mut Write, value: u8) -> io::Result<()> { let bytes = [value; 1]; writer.write_all(&bytes) } +#[inline(always)] pub fn read_bool(reader: &mut Read) -> io::Result { if read_u8(reader)? == 0 { Ok(false) @@ -25,6 +28,7 @@ pub fn read_bool(reader: &mut Read) -> io::Result { } } +#[inline(always)] pub fn write_bool(writer: &mut Write, value: bool) -> io::Result<()> { if value { write_u8(writer, 1) @@ -33,42 +37,49 @@ pub fn write_bool(writer: &mut Write, value: bool) -> io::Result<()> { } } +#[inline(always)] pub fn read_u16(reader: &mut Read) -> io::Result { let mut bytes = [0; 2]; reader.read_exact(&mut bytes)?; Ok(NetworkEndian::read_u16(&bytes)) } +#[inline(always)] pub fn write_u16(writer: &mut Write, value: u16) -> io::Result<()> { let mut bytes = [0; 2]; NetworkEndian::write_u16(&mut bytes, value); writer.write_all(&bytes) } +#[inline(always)] pub fn read_u32(reader: &mut Read) -> io::Result { let mut bytes = [0; 4]; reader.read_exact(&mut bytes)?; Ok(NetworkEndian::read_u32(&bytes)) } +#[inline(always)] pub fn write_u32(writer: &mut Write, value: u32) -> io::Result<()> { let mut bytes = [0; 4]; NetworkEndian::write_u32(&mut bytes, value); writer.write_all(&bytes) } +#[inline(always)] pub fn read_u64(reader: &mut Read) -> io::Result { let mut bytes = [0; 8]; reader.read_exact(&mut bytes)?; Ok(NetworkEndian::read_u64(&bytes)) } +#[inline(always)] pub fn write_u64(writer: &mut Write, value: u64) -> io::Result<()> { let mut bytes = [0; 8]; NetworkEndian::write_u64(&mut bytes, value); writer.write_all(&bytes) } +#[inline(always)] pub fn read_bytes(reader: &mut Read) -> io::Result> { let length = read_u32(reader)?; let mut value = vec![0; length as usize]; @@ -76,17 +87,20 @@ pub fn read_bytes(reader: &mut Read) -> io::Result> { Ok(value) } +#[inline(always)] pub fn write_bytes(writer: &mut Write, value: &[u8]) -> io::Result<()> { write_u32(writer, value.len() as u32)?; writer.write_all(value) } +#[inline(always)] pub fn read_string(reader: &mut Read) -> io::Result { let bytes = read_bytes(reader)?; String::from_utf8(bytes) .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "invalid UTF-8")) } +#[inline(always)] pub fn write_string(writer: &mut Write, value: &str) -> io::Result<()> { write_bytes(writer, value.as_bytes()) }