Skip to main content

fennec_modbus/protocol/
address.rs

1use core::marker::PhantomData;
2
3use bytes::BufMut;
4
5use crate::protocol::{
6    Address,
7    codec::{BitSize, Encode},
8};
9
10/// Address specified via a constant generic argument.
11#[must_use]
12#[derive(Copy, Clone)]
13pub struct Const<const A: u16>;
14
15impl<const A: u16> Address for Const<A> {}
16
17impl<const A: u16> Encode for Const<A> {
18    fn encode(&self, to: &mut impl BufMut) {
19        to.put_u16(A);
20    }
21}
22
23/// Address computed as `BASE` + size-of-`V` × `index`.
24#[must_use]
25#[derive(Copy, Clone)]
26pub struct Stride<const BASE: u16, V>(
27    /// Value index within the stride.
28    pub u16,
29    /// Binding to the value type.
30    PhantomData<V>,
31);
32
33impl<const BASE: u16, V> From<u16> for Stride<BASE, V> {
34    fn from(index: u16) -> Self {
35        Self(index, PhantomData)
36    }
37}
38
39impl<const BASE: u16, V: BitSize> Address for Stride<BASE, V> {}
40
41impl<const BASE: u16, V: BitSize> Encode for Stride<BASE, V> {
42    fn encode(&self, to: &mut impl BufMut) {
43        to.put_u16(BASE + self.0 * V::N_WORDS);
44    }
45}