From 71a1e55230963b0ebccdfce56c8e2e88437e239a Mon Sep 17 00:00:00 2001 From: Markus Kohlhase Date: Tue, 19 Mar 2024 02:33:15 +0100 Subject: [PATCH] Mark some functions as const --- src/codec/mod.rs | 23 +++++++++++------------ src/codec/rtu/mod.rs | 2 +- src/codec/tcp/mod.rs | 2 +- src/frame/coils.rs | 11 ++++++++--- src/lib.rs | 1 + 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/codec/mod.rs b/src/codec/mod.rs index 4bc394d..f63a3e9 100644 --- a/src/codec/mod.rs +++ b/src/codec/mod.rs @@ -277,7 +277,6 @@ impl<'r> Encode for Request<'r> { impl<'r> Encode for Response<'r> { fn encode(&self, buf: &mut [u8]) -> Result { - use crate::frame::Response as R; if buf.len() < self.pdu_len() { return Err(Error::BufferSize); @@ -285,26 +284,26 @@ impl<'r> Encode for Response<'r> { buf[0] = FnCode::from(*self).into(); match self { - R::ReadCoils(coils) | R::ReadDiscreteInputs(coils) => { + Self::ReadCoils(coils) | Self::ReadDiscreteInputs(coils) => { buf[1] = coils.packed_len() as u8; coils.copy_to(&mut buf[2..]); } - R::ReadInputRegisters(registers) - | R::ReadHoldingRegisters(registers) - | R::ReadWriteMultipleRegisters(registers) => { + Self::ReadInputRegisters(registers) + | Self::ReadHoldingRegisters(registers) + | Self::ReadWriteMultipleRegisters(registers) => { buf[1] = (registers.len() * 2) as u8; registers.copy_to(&mut buf[2..]); } - R::WriteSingleCoil(address) => { + Self::WriteSingleCoil(address) => { BigEndian::write_u16(&mut buf[1..], *address); } - R::WriteMultipleCoils(address, payload) - | R::WriteMultipleRegisters(address, payload) - | R::WriteSingleRegister(address, payload) => { + Self::WriteMultipleCoils(address, payload) + | Self::WriteMultipleRegisters(address, payload) + | Self::WriteSingleRegister(address, payload) => { BigEndian::write_u16(&mut buf[1..], *address); BigEndian::write_u16(&mut buf[3..], *payload); } - R::Custom(_, custom_data) => { + Self::Custom(_, custom_data) => { for (idx, d) in custom_data.iter().enumerate() { buf[idx + 1] = *d; } @@ -343,7 +342,7 @@ impl Encode for ExceptionResponse { } } -fn min_request_pdu_len(fn_code: FnCode) -> usize { +const fn min_request_pdu_len(fn_code: FnCode) -> usize { use FnCode as F; match fn_code { F::ReadCoils @@ -358,7 +357,7 @@ fn min_request_pdu_len(fn_code: FnCode) -> usize { } } -fn min_response_pdu_len(fn_code: FnCode) -> usize { +const fn min_response_pdu_len(fn_code: FnCode) -> usize { use FnCode as F; match fn_code { F::ReadCoils diff --git a/src/codec/rtu/mod.rs b/src/codec/rtu/mod.rs index 9d41321..ae2e980 100644 --- a/src/codec/rtu/mod.rs +++ b/src/codec/rtu/mod.rs @@ -133,7 +133,7 @@ pub fn crc16(data: &[u8]) -> u16 { } /// Extract the PDU length out of the ADU request buffer. -pub fn request_pdu_len(adu_buf: &[u8]) -> Result> { +pub const fn request_pdu_len(adu_buf: &[u8]) -> Result> { if adu_buf.len() < 2 { return Ok(None); } diff --git a/src/codec/tcp/mod.rs b/src/codec/tcp/mod.rs index b02a51d..e2fd60c 100644 --- a/src/codec/tcp/mod.rs +++ b/src/codec/tcp/mod.rs @@ -118,7 +118,7 @@ pub fn extract_frame(buf: &[u8], pdu_len: usize) -> Result> } /// Extract the PDU length out of the ADU request buffer. -pub fn request_pdu_len(adu_buf: &[u8]) -> Result> { +pub const fn request_pdu_len(adu_buf: &[u8]) -> Result> { if adu_buf.len() < 8 { return Ok(None); } diff --git a/src/frame/coils.rs b/src/frame/coils.rs index b6a009d..6031de7 100644 --- a/src/frame/coils.rs +++ b/src/frame/coils.rs @@ -17,6 +17,7 @@ impl<'c> Coils<'c> { quantity: bools.len(), }) } + //TODO: add tests pub(crate) fn copy_to(&self, buf: &mut [u8]) { let packed_len = self.packed_len(); @@ -25,24 +26,28 @@ impl<'c> Coils<'c> { buf[idx] = self.data[idx]; }); } + /// Quantity of coils #[must_use] pub const fn len(&self) -> usize { self.quantity } + /// Number of bytes required to pack the coils. #[must_use] pub const fn packed_len(&self) -> usize { packed_coils_len(self.quantity) } + /// Returns `true` if the container has no items. #[must_use] pub const fn is_empty(&self) -> bool { self.quantity == 0 } + /// Get a specific coil. #[must_use] - pub fn get(&self, idx: usize) -> Option { + pub const fn get(&self, idx: usize) -> Option { if idx + 1 > self.quantity { return None; } @@ -82,7 +87,7 @@ impl<'c> IntoIterator for Coils<'c> { /// Turn a bool into a u16 coil value #[must_use] -pub fn bool_to_u16_coil(state: bool) -> u16 { +pub const fn bool_to_u16_coil(state: bool) -> u16 { if state { 0xFF00 } else { @@ -91,7 +96,7 @@ pub fn bool_to_u16_coil(state: bool) -> u16 { } /// Turn a u16 coil value into a boolean value. -pub fn u16_coil_to_bool(coil: u16) -> Result { +pub const fn u16_coil_to_bool(coil: u16) -> Result { match coil { 0xFF00 => Ok(true), 0x0000 => Ok(false), diff --git a/src/lib.rs b/src/lib.rs index 9dda669..ead8618 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ #![warn(unsafe_code)] #![warn(unused)] // Clippy lints +#![warn(clippy::missing_const_for_fn)] #![warn(clippy::pedantic)] #![allow(clippy::cast_possible_truncation)] // FIXME #![allow(clippy::missing_errors_doc)]