From ee96a2796cf10a63bd1846692098eb1fccbefefe Mon Sep 17 00:00:00 2001 From: Hussein Hazem Date: Tue, 24 Sep 2024 14:51:28 +0200 Subject: [PATCH 1/3] #27 make TxMessage data members public --- src/message.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/message.rs b/src/message.rs index f9750bc..6c7103a 100644 --- a/src/message.rs +++ b/src/message.rs @@ -165,11 +165,11 @@ impl MessageType for CanFd { #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub struct TxMessage, const L: usize> { /// first 2 bytes of Transmit Message Object - pub(crate) header: TxHeader, + pub header: TxHeader, /// Payload bytes of Message Object - pub(crate) buff: Bytes, + pub buff: Bytes, /// CAN message type (CAN 2.0 or CAN FD) - pub(crate) message_type: T, + pub message_type: T, } impl, const L: usize> TxMessage { From 84e101cc897b99e79f0d391ec80ee1608871b31e Mon Sep 17 00:00:00 2001 From: Hussein Hazem Date: Wed, 25 Sep 2024 10:36:22 +0200 Subject: [PATCH 2/3] #27 use getters to get message payload and header --- src/message.rs | 17 ++++++++++++++--- src/tests/message.rs | 12 ++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/message.rs b/src/message.rs index 6c7103a..9e66472 100644 --- a/src/message.rs +++ b/src/message.rs @@ -165,14 +165,15 @@ impl MessageType for CanFd { #[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] pub struct TxMessage, const L: usize> { /// first 2 bytes of Transmit Message Object - pub header: TxHeader, + pub(crate) header: TxHeader, /// Payload bytes of Message Object - pub buff: Bytes, + pub(crate) buff: Bytes, /// CAN message type (CAN 2.0 or CAN FD) - pub message_type: T, + pub(crate) message_type: T, } impl, const L: usize> TxMessage { + /// Create new CAN message pub fn new(message_type: T, data: Bytes, identifier: Id) -> Result { let mut header = TxHeader::new(); @@ -202,6 +203,16 @@ impl, const L: usize> TxMessage { message_type, }) } + + /// Returns payload as a `&[u8]` + pub fn get_payload(&self) -> &[u8] { + &self.buff.as_ref() + } + + /// Returns Header register of Transmit Message Object + pub fn get_header(&self) -> &TxHeader { + &self.header + } } /// Receive message object header diff --git a/src/tests/message.rs b/src/tests/message.rs index 2c47fda..00a40bb 100644 --- a/src/tests/message.rs +++ b/src/tests/message.rs @@ -111,3 +111,15 @@ fn test_payload_greater_than_generic_type_args() { assert_eq!(message_2_0.unwrap_err(), MessageError::InvalidLength(5)); assert_eq!(message_fd.unwrap_err(), MessageError::InvalidLength(23)); } + +#[test] +fn test_get_payload() { + let payload_bytes = Bytes::copy_from_slice(&[1u8; 8]); + let standard_id = StandardId::new(STANDARD_ID).unwrap(); + + let msg_type = Can20::<8> {}; + + let message = TxMessage::new(msg_type, payload_bytes, Id::Standard(standard_id)).unwrap(); + + assert_eq!(message.get_payload(), &[1u8; 8]); +} From bb4050faa2f8c9cc2d074bd631e96159b6fc3227 Mon Sep 17 00:00:00 2001 From: Hussein Hazem Date: Wed, 25 Sep 2024 10:39:32 +0200 Subject: [PATCH 3/3] #27 apply clippy fix --- src/message.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/message.rs b/src/message.rs index 9e66472..be0e99a 100644 --- a/src/message.rs +++ b/src/message.rs @@ -206,7 +206,7 @@ impl, const L: usize> TxMessage { /// Returns payload as a `&[u8]` pub fn get_payload(&self) -> &[u8] { - &self.buff.as_ref() + self.buff.as_ref() } /// Returns Header register of Transmit Message Object