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. --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/device.rs | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index bb5177b..76dc8ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,6 +80,7 @@ dependencies = [ "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", "serde-big-array 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "ssmarshal 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "static_assertions 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "stm32f103xx-usb 0.1.0 (git+https://github.com/mvirkkunen/stm32f103xx-usb?rev=57d23751367461bec5f39322727bdd65e5c2aa30)", "stm32f1xx-hal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "usb-device 0.2.0 (git+https://github.com/mvirkkunen/usb-device?rev=e58e30f3b9c9bf4aab00ea039d129b964a3fd2d3)", @@ -189,6 +190,11 @@ dependencies = [ "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "static_assertions" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "stm32f1" version = "0.5.0" @@ -288,6 +294,7 @@ dependencies = [ "checksum serde-big-array 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "dbd629249ae3007dac9632aa92334c5ba9c8a4dbe0a6263e546121dd1c57394a" "checksum serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)" = "beed18e6f5175aef3ba670e57c60ef3b1b74d250d962a26604bff4c80e970dd4" "checksum ssmarshal 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f3e6ad23b128192ed337dfa4f1b8099ced0c2bf30d61e551b65fda5916dbb850" +"checksum static_assertions 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "389ce475f424f267dbed6479cbd8f126c5e1afb053b0acdaa019c74305fc65d1" "checksum stm32f1 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "48763f6f093674476399efbd8d2e52349dc32c3bbf57657234cbb15d309cd8a3" "checksum stm32f103xx-usb 0.1.0 (git+https://github.com/mvirkkunen/stm32f103xx-usb?rev=57d23751367461bec5f39322727bdd65e5c2aa30)" = "" "checksum stm32f1xx-hal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f410543fc282d51e181e3cf5c8adff303d9e5ae8b9eb731e6a07f58a4cec4265" diff --git a/Cargo.toml b/Cargo.toml index b5f2c65..0d773d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ cortex-m-rt = "0.6.7" embedded-hal = "0.2.2" panic-halt = "0.2.0" serde-big-array = "0.1.4" +static_assertions = "0.3.1" stm32f103xx-usb = { git = "https://github.com/mvirkkunen/stm32f103xx-usb", rev = "57d23751367461bec5f39322727bdd65e5c2aa30" } usb-device = { git = "https://github.com/mvirkkunen/usb-device", rev = "e58e30f3b9c9bf4aab00ea039d129b964a3fd2d3" } 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.1