Skip to content

Commit

Permalink
Mark some functions as const
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Mar 19, 2024
1 parent f97bb92 commit 71a1e55
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
23 changes: 11 additions & 12 deletions src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,34 +277,33 @@ impl<'r> Encode for Request<'r> {

impl<'r> Encode for Response<'r> {
fn encode(&self, buf: &mut [u8]) -> Result<usize> {
use crate::frame::Response as R;

if buf.len() < self.pdu_len() {
return Err(Error::BufferSize);
}

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;
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/codec/rtu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<usize>> {
pub const fn request_pdu_len(adu_buf: &[u8]) -> Result<Option<usize>> {
if adu_buf.len() < 2 {
return Ok(None);
}
Expand Down
2 changes: 1 addition & 1 deletion src/codec/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn extract_frame(buf: &[u8], pdu_len: usize) -> Result<Option<DecodedFrame>>
}

/// Extract the PDU length out of the ADU request buffer.
pub fn request_pdu_len(adu_buf: &[u8]) -> Result<Option<usize>> {
pub const fn request_pdu_len(adu_buf: &[u8]) -> Result<Option<usize>> {
if adu_buf.len() < 8 {
return Ok(None);
}
Expand Down
11 changes: 8 additions & 3 deletions src/frame/coils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<Coil> {
pub const fn get(&self, idx: usize) -> Option<Coil> {
if idx + 1 > self.quantity {
return None;
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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<bool, Error> {
pub const fn u16_coil_to_bool(coil: u16) -> Result<bool, Error> {
match coil {
0xFF00 => Ok(true),
0x0000 => Ok(false),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 71a1e55

Please sign in to comment.