From 769e7797c940a9c8926199e0454317ad0af794e8 Mon Sep 17 00:00:00 2001 From: Serhij S Date: Sat, 7 Dec 2024 22:02:29 +0100 Subject: [PATCH] codefmt --- src/server/context.rs | 10 ++++++---- src/server/representable.rs | 25 +++++++++++++++---------- src/tests/test_std.rs | 34 ++++++++++++++++++---------------- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/server/context.rs b/src/server/context.rs index 9e43cfa..5e5474f 100644 --- a/src/server/context.rs +++ b/src/server/context.rs @@ -225,13 +225,14 @@ pub trait ModbusContext { /// /// Returns the [`RegisterRepresentable`] once converted using /// [`RegisterRepresentable::from_registers_sequential`] + #[allow(clippy::cast_possible_truncation)] fn get_inputs_as_representable>( &self, reg: u16, ) -> Result { let mut regs: [u16; N] = [0u16; N]; - for i in 0..N { - regs[i] = self.get_input(reg + i as u16)?; + for (i, r) in regs.iter_mut().enumerate().take(N) { + *r = self.get_input(reg + i as u16)?; } Ok(T::from_registers_sequential(®s)) } @@ -240,13 +241,14 @@ pub trait ModbusContext { /// /// Returns the [`RegisterRepresentable`] once converted using /// [`RegisterRepresentable::from_registers_sequential`] + #[allow(clippy::cast_possible_truncation)] fn get_holdings_as_representable>( &self, reg: u16, ) -> Result { let mut regs: [u16; N] = [0u16; N]; - for i in 0..N { - regs[i] = self.get_holding(reg + i as u16)?; + for (i, r) in regs.iter_mut().enumerate().take(N) { + *r = self.get_holding(reg + i as u16)?; } Ok(T::from_registers_sequential(®s)) } diff --git a/src/server/representable.rs b/src/server/representable.rs index ac7c59f..5f12ac7 100644 --- a/src/server/representable.rs +++ b/src/server/representable.rs @@ -3,6 +3,7 @@ /// [`RegisterRepresentable::to_registers_sequential`] and /// [`RegisterRepresentable::from_registers_sequential`] are exact /// inverses of each other. +#[allow(clippy::module_name_repetitions)] pub trait RegisterRepresentable { /// Convert this type into a sequence of `u16`s which can be loaded /// into modbus registers. (From lower to higher addresses) @@ -85,11 +86,12 @@ pub mod representations { #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct U32BigEndian(pub u32); impl RegisterRepresentable<2> for U32BigEndian { + #[allow(clippy::cast_possible_truncation)] fn to_registers_sequential(&self) -> [u16; 2] { [(self.0 >> 16) as u16, self.0 as u16] } fn from_registers_sequential(value: &[u16; 2]) -> Self { - Self(((value[0] as u32) << 16) | (value[1] as u32)) + Self(((u32::from(value[0])) << 16) | (u32::from(value[1]))) } } @@ -100,11 +102,12 @@ pub mod representations { #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct U32LittleEndian(pub u32); impl RegisterRepresentable<2> for U32LittleEndian { + #[allow(clippy::cast_possible_truncation)] fn to_registers_sequential(&self) -> [u16; 2] { [self.0 as u16, (self.0 >> 16) as u16] } fn from_registers_sequential(value: &[u16; 2]) -> Self { - Self((value[0] as u32) | ((value[1] as u32) << 16)) + Self((u32::from(value[0])) | ((u32::from(value[1])) << 16)) } } @@ -115,6 +118,7 @@ pub mod representations { #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct U64BigEndian(pub u64); impl RegisterRepresentable<4> for U64BigEndian { + #[allow(clippy::cast_possible_truncation)] fn to_registers_sequential(&self) -> [u16; 4] { [ ((self.0 & 0xFFFF_0000_0000_0000) >> 48) as u16, @@ -125,10 +129,10 @@ pub mod representations { } fn from_registers_sequential(value: &[u16; 4]) -> Self { Self( - (value[0] as u64) << 48 - | (value[1] as u64) << 32 - | (value[2] as u64) << 16 - | (value[3] as u64), + (u64::from(value[0])) << 48 + | (u64::from(value[1])) << 32 + | (u64::from(value[2])) << 16 + | (u64::from(value[3])), ) } } @@ -139,6 +143,7 @@ pub mod representations { #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct U64LittleEndian(pub u64); impl RegisterRepresentable<4> for U64LittleEndian { + #[allow(clippy::cast_possible_truncation)] fn to_registers_sequential(&self) -> [u16; 4] { [ self.0 as u16, @@ -149,10 +154,10 @@ pub mod representations { } fn from_registers_sequential(value: &[u16; 4]) -> Self { Self( - (value[0] as u64) - | (value[1] as u64) << 16 - | (value[2] as u64) << 32 - | (value[3] as u64) << 48, + (u64::from(value[0])) + | (u64::from(value[1])) << 16 + | (u64::from(value[2])) << 32 + | (u64::from(value[3])) << 48, ) } } diff --git a/src/tests/test_std.rs b/src/tests/test_std.rs index e5651dd..ffde165 100644 --- a/src/tests/test_std.rs +++ b/src/tests/test_std.rs @@ -161,6 +161,7 @@ fn test_std_discrete_get_set_bulk() { } #[test] +#[allow(clippy::too_many_lines)] fn test_std_read_inputs_as_bytes_oob() { let mut ctx = CTX.write().unwrap(); let mut result = Vec::new(); @@ -310,40 +311,41 @@ fn test_std_get_set_inputs() { ctx.set_inputs_from_f32(200, 1234.567).unwrap(); assert_eq!(ctx.get_inputs_as_f32(200).unwrap(), 1234.567f32); - ctx.set_inputs_from_representable(300, &representations::U32LittleEndian(1234567)) + ctx.set_inputs_from_representable(300, &representations::U32LittleEndian(1_234_567)) .unwrap(); assert_eq!( ctx.get_inputs_as_representable::<2, representations::U32LittleEndian>(300) .unwrap(), - representations::U32LittleEndian(1234567) + representations::U32LittleEndian(1_234_567) ); - ctx.set_inputs_from_representable(300, &representations::U32BigEndian(1234567)) + ctx.set_inputs_from_representable(300, &representations::U32BigEndian(1_234_567)) .unwrap(); assert_eq!( ctx.get_inputs_as_representable::<2, representations::U32BigEndian>(300) .unwrap(), - representations::U32BigEndian(1234567) + representations::U32BigEndian(1_234_567) ); - ctx.set_inputs_from_representable(300, &representations::U64BigEndian(1234567)) + ctx.set_inputs_from_representable(300, &representations::U64BigEndian(1_234_567)) .unwrap(); assert_eq!( ctx.get_inputs_as_representable::<4, representations::U64BigEndian>(300) .unwrap(), - representations::U64BigEndian(1234567) + representations::U64BigEndian(1_234_567) ); - ctx.set_inputs_from_representable(300, &representations::U64LittleEndian(1234567)) + ctx.set_inputs_from_representable(300, &representations::U64LittleEndian(1_234_567)) .unwrap(); assert_eq!( ctx.get_inputs_as_representable::<4, representations::U64LittleEndian>(300) .unwrap(), - representations::U64LittleEndian(1234567) + representations::U64LittleEndian(1_234_567) ); } #[test] +#[allow(clippy::too_many_lines)] fn test_std_read_holdings_as_bytes_oob() { let mut ctx = CTX.write().unwrap(); let mut result = Vec::new(); @@ -494,36 +496,36 @@ fn test_std_get_set_holdings() { ctx.set_holdings_from_f32(200, 1234.567).unwrap(); assert_eq!(ctx.get_holdings_as_f32(200).unwrap(), 1234.567f32); - ctx.set_holdings_from_representable(300, &representations::U32LittleEndian(1234567)) + ctx.set_holdings_from_representable(300, &representations::U32LittleEndian(1_234_567)) .unwrap(); assert_eq!( ctx.get_holdings_as_representable::<2, representations::U32LittleEndian>(300) .unwrap(), - representations::U32LittleEndian(1234567) + representations::U32LittleEndian(1_234_567) ); - ctx.set_holdings_from_representable(300, &representations::U32BigEndian(1234567)) + ctx.set_holdings_from_representable(300, &representations::U32BigEndian(1_234_567)) .unwrap(); assert_eq!( ctx.get_holdings_as_representable::<2, representations::U32BigEndian>(300) .unwrap(), - representations::U32BigEndian(1234567) + representations::U32BigEndian(1_234_567) ); - ctx.set_holdings_from_representable(300, &representations::U64BigEndian(1234567)) + ctx.set_holdings_from_representable(300, &representations::U64BigEndian(1_234_567)) .unwrap(); assert_eq!( ctx.get_holdings_as_representable::<4, representations::U64BigEndian>(300) .unwrap(), - representations::U64BigEndian(1234567) + representations::U64BigEndian(1_234_567) ); - ctx.set_holdings_from_representable(300, &representations::U64LittleEndian(1234567)) + ctx.set_holdings_from_representable(300, &representations::U64LittleEndian(1_234_567)) .unwrap(); assert_eq!( ctx.get_holdings_as_representable::<4, representations::U64LittleEndian>(300) .unwrap(), - representations::U64LittleEndian(1234567) + representations::U64LittleEndian(1_234_567) ); }