Skip to content

Commit

Permalink
export packet size as constant
Browse files Browse the repository at this point in the history
Usefull for SPI, where we need provide read buffer simultaneously
with write buffer
  • Loading branch information
Dushistov committed Mar 13, 2020
1 parent 681e328 commit 0f45945
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 3 additions & 1 deletion ublox/src/ubx_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub struct UbxPacketRequest {
}

impl UbxPacketRequest {
pub const PACKET_LEN: usize = 8;

#[inline]
pub fn request_for<T: UbxPacketMeta>() -> Self {
Self {
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions ublox_derive/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,11 @@ pub fn generate_send_code_for_packet(pack_descr: &PackDesc) -> Vec<TokenStream>
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;
Expand Down
6 changes: 4 additions & 2 deletions ublox_derive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0f45945

Please sign in to comment.