From 0f4594540afd60c63a116ca3ef549d7f8e43b41a Mon Sep 17 00:00:00 2001 From: "Evgeniy A. Dushistov" Date: Fri, 13 Mar 2020 18:11:44 +0300 Subject: [PATCH] export packet size as constant Usefull for SPI, where we need provide read buffer simultaneously with write buffer --- ublox/src/ubx_packets.rs | 4 +++- ublox_derive/src/output.rs | 6 ++++-- ublox_derive/src/tests.rs | 6 ++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ublox/src/ubx_packets.rs b/ublox/src/ubx_packets.rs index 8611ed1..7b0ce63 100644 --- a/ublox/src/ubx_packets.rs +++ b/ublox/src/ubx_packets.rs @@ -94,6 +94,8 @@ pub struct UbxPacketRequest { } impl UbxPacketRequest { + pub const PACKET_LEN: usize = 8; + #[inline] pub fn request_for() -> Self { Self { @@ -107,7 +109,7 @@ impl UbxPacketRequest { } #[inline] - pub fn to_packet_bytes(self) -> [u8; 8] { + pub fn to_packet_bytes(self) -> [u8; Self::PACKET_LEN] { let mut ret = [ SYNC_CHAR_1, SYNC_CHAR_2, diff --git a/ublox_derive/src/output.rs b/ublox_derive/src/output.rs index 91df834..f82418d 100644 --- a/ublox_derive/src/output.rs +++ b/ublox_derive/src/output.rs @@ -245,9 +245,11 @@ pub fn generate_send_code_for_packet(pack_descr: &PackDesc) -> Vec let packet_payload_size_u16 = u16::try_from(packet_payload_size).unwrap(); ret.push(quote! { impl #payload_struct { + pub const PACKET_LEN: usize = #packet_size; + #[inline] - pub fn to_packet_bytes(self) -> [u8; #packet_size] { - let mut ret = [0u8; #packet_size]; + pub fn to_packet_bytes(self) -> [u8; Self::PACKET_LEN] { + let mut ret = [0u8; Self::PACKET_LEN]; ret[0] = SYNC_CHAR_1; ret[1] = SYNC_CHAR_2; ret[2] = #main_name::CLASS; diff --git a/ublox_derive/src/tests.rs b/ublox_derive/src/tests.rs index ba767be..7fd741c 100644 --- a/ublox_derive/src/tests.rs +++ b/ublox_derive/src/tests.rs @@ -222,9 +222,11 @@ fn test_ubx_packet_send() { pub a: u8, } impl TestBuilder { + pub const PACKET_LEN: usize = 17usize; + #[inline] - pub fn to_packet_bytes(self) -> [u8; 17usize] { - let mut ret = [0u8; 17usize]; + pub fn to_packet_bytes(self) -> [u8; Self::PACKET_LEN] { + let mut ret = [0u8; Self::PACKET_LEN]; ret[0] = SYNC_CHAR_1; ret[1] = SYNC_CHAR_2; ret[2] = Test::CLASS;