fennec_modbus/protocol/function/
read_multiple.rs1use core::marker::PhantomData;
4
5use bytes::{Buf, BufMut};
6
7use crate::{
8 Error,
9 protocol::{
10 Address,
11 codec::{BitSize, Decode, Encode, adapters::DropRemaining},
12 function,
13 function::size_argument,
14 },
15};
16
17#[must_use]
19pub struct Args<A, V, S>(
20 A,
22 PhantomData<V>,
24 PhantomData<S>,
26);
27
28impl<A, V: BitSize, S> From<A> for Args<A, V, S> {
29 fn from(address: A) -> Self {
31 Self::new(address)
32 }
33}
34
35impl<A, V: BitSize, S> Args<A, V, S> {
36 pub const fn new(starting_address: A) -> Self {
38 Self(starting_address, PhantomData, PhantomData)
39 }
40}
41
42impl<A: Address, V: BitSize> Encode for Args<A, V, size_argument::Bits> {
43 fn encode(&self, to: &mut impl BufMut) {
45 V::assert_valid::<246>();
46 self.0.encode(to);
47 to.put_u16(V::N_BITS);
48 }
49}
50
51impl<A: Address, V: BitSize> Encode for Args<A, V, size_argument::Words> {
52 fn encode(&self, to: &mut impl BufMut) {
54 V::assert_valid::<250>();
55 self.0.encode(to);
56 to.put_u16(V::N_WORDS);
57 }
58}
59
60pub struct Output<V>(V);
81
82impl<V: Decode> Decode for Output<V> {
83 fn decode(from: &mut impl Buf) -> Result<Self, Error> {
84 let n_bytes = from.try_get_u8()?;
85 let mut from = DropRemaining(from).take(usize::from(n_bytes));
86 V::decode(&mut from).map(Self)
87 }
88}
89
90impl<V> function::IntoValue for Output<V> {
91 type Value = V;
92
93 fn into_value(self) -> Self::Value {
94 self.0
95 }
96}