pub struct DataStructure<const L: usize>(/* private fields */);Expand description
Represents a statically defined set of Modbus RTU data addresses.
The addresses must be:
- Known at compile time
- Strictly increasing (ordered)
- Unique
This structure is intended to be defined as a global constant/static value, and should not be instantiated dynamically.
§Examples
use modbus_rtu::slave::DataStructure;
// Define data structure as const.
const DATA_STRUCTURE: DataStructure<4> = DataStructure::new([
0x0000,
0x0001,
0x1234,
0x5678,
]);Implementations§
Source§impl<const L: usize> DataStructure<L>
impl<const L: usize> DataStructure<L>
Sourcepub const fn new(addresses: [u16; L]) -> DataStructure<L>
pub const fn new(addresses: [u16; L]) -> DataStructure<L>
Creates a new DataStructure after validating the input addresses.
This function checks that the addresses are strictly ordered and unique at compile time. It will panic if the addresses are not ordered or contain duplicates.
§Arguments
addresses: The list of Modbus RTU register addresses to be stored in the structure.
§Returns
A new DataStructure instance if the validation passes.
§Examples
use modbus_rtu::slave::DataStructure;
const STRUCTURE: DataStructure<5> = DataStructure::new([
0x0000,
0x0001,
0x0002,
0x1234,
0x5678,
]);Sourcepub const fn get(&self, address: u16) -> usize
pub const fn get(&self, address: u16) -> usize
Get the index of the specified address using a manual binary search approach.
This function performs a binary search on the statically defined address list. It will panic if the address is not found in the list.
§Arguments
address: The address to look for in the data structure.
§Returns
The index of the address in the list if it exists, or panics if not found.
§Examples
use modbus_rtu::slave::DataStructure;
const STRUCTURE: DataStructure<5> = DataStructure::new([
0x0000,
0x0001,
0x0002,
0x1234,
0x5678,
]);
assert_eq!(STRUCTURE.get(0x0001), 1);The code below will panic at complie time.
// Will panic!!
let index = STRUCTURE.get(0x0003);§Panics
This function will panic if the specified address is not found in the address list. The panic message will indicate that the address is not registered and should be checked for validity.
Sourcepub fn find(&self, address: u16) -> Option<usize>
pub fn find(&self, address: u16) -> Option<usize>
Finds the index of the specified address using the binary search algorithm.
This function performs a binary search on the statically defined address list. It returns the index of the address if found.
§Arguments
address: The address to search for in the data structure.
§Returns
Some(index) if the address exists in the list, or None if not found.
§Examples
use modbus_rtu::slave::DataStructure;
const STRUCTURE: DataStructure<5> = DataStructure::new([
0x0000,
0x0001,
0x0002,
0x1234,
0x5678,
]);
assert_eq!(STRUCTURE.find(0x0001), Some(1));
assert_eq!(STRUCTURE.find(0x0003), None);Sourcepub fn get_address_by_index(&self, index: usize) -> u16
pub fn get_address_by_index(&self, index: usize) -> u16
Retrieves the address at the specified index.
This function accesses the address stored at the given index in the data structure’s array. It is a simple getter function that does not perform any validation.
§Arguments
index: The index of the address to retrieve. The number must be derived from the logical relationships defined within the data structure.
§Returns
The u16 address located at the given index.
§Examples
use modbus_rtu::slave::DataStructure;
const STRUCTURE: DataStructure<5> = DataStructure::new([
0x0000,
0x0001,
0x0002,
0x1234,
0x5678,
]);
assert_eq!(STRUCTURE.get_address_by_index(3), 0x1234);The code below will panic at compile time.
// Will panic!!
let address: u16 = STRUCTURE.get_address_by_index(10);§Panics
This function will panic if the index is out of bounds for the address list.
The number must be derived from the logical relationships defined within the data structure.
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of addresses stored in this data structure.
This is equivalent to the compile-time constant length L.
§Returns
Total count of valid(defined) addresses
§Examples
use modbus_rtu::slave::DataStructure;
const ADDRESSES: [u16; 4] = [
0x0000,
0x0001,
0x1234,
0x5678,
];
const DATA_STRUCTURE: DataStructure<4> = DataStructure::new(ADDRESSES);
assert_eq!(ADDRESSES.len(), DATA_STRUCTURE.len());Source§impl DataStructure<0>
impl DataStructure<0>
Sourcepub const EMPTY: DataStructure<0>
pub const EMPTY: DataStructure<0>
An empty data structure.
This is used to define a data model that does not contain any data.