Skip to content

Commit

Permalink
codefmt
Browse files Browse the repository at this point in the history
  • Loading branch information
divi255 committed Dec 7, 2024
1 parent 38e3a47 commit 769e779
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
10 changes: 6 additions & 4 deletions src/server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const N: usize, T: RegisterRepresentable<N>>(
&self,
reg: u16,
) -> Result<T, ErrorKind> {
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(&regs))
}
Expand All @@ -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<const N: usize, T: RegisterRepresentable<N>>(
&self,
reg: u16,
) -> Result<T, ErrorKind> {
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(&regs))
}
Expand Down
25 changes: 15 additions & 10 deletions src/server/representable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const N: usize> {
/// Convert this type into a sequence of `u16`s which can be loaded
/// into modbus registers. (From lower to higher addresses)
Expand Down Expand Up @@ -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])))
}
}

Expand All @@ -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))
}
}

Expand All @@ -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,
Expand All @@ -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])),
)
}
}
Expand All @@ -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,
Expand All @@ -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,
)
}
}
Expand Down
34 changes: 18 additions & 16 deletions src/tests/test_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
);
}

Expand Down

0 comments on commit 769e779

Please sign in to comment.