From e0d33dae2f2ab70b936467590284030d759f9db7 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 20 Feb 2019 11:47:25 +0000 Subject: Add static assertions for Request and Response size The Request and Response types have to have a size of 64 bytes. Previously, we only verified this property by visual inspection. Accidental changes to the size could lead to runtime errors. This patch adds static assertions using the static_assertions crate to enforce this property at compile time. --- src/device.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src') diff --git a/src/device.rs b/src/device.rs index b25cfb7..8f6f6ef 100644 --- a/src/device.rs +++ b/src/device.rs @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize}; use serde_big_array::big_array; +use static_assertions::assert_eq_size; use usb_device::bus::{UsbBus, UsbBusAllocator}; use usb_device::device::{UsbDevice, UsbDeviceBuilder, UsbVidPid}; @@ -65,6 +66,8 @@ struct Request { pub crc: u32, } +assert_eq_size!(request; [u8; REPORT_LEN], Request); + #[derive(Serialize)] struct Response { pub device_status: DeviceStatus, @@ -76,6 +79,8 @@ struct Response { pub crc: u32, } +assert_eq_size!(response; [u8; REPORT_LEN], Response); + impl Response { fn new(device_status: DeviceStatus, command_id: u8, last_crc: u32) -> Response { Response { -- cgit v1.2.3