pub struct DataModel<const L: usize, T: Copy> { /* private fields */ }Expand description
A data model for accessing values stored at unique 16-bit addresses.
§Examples
use modbus_rtu::slave::{DataModel, DataStructure};
// Define data structure first.
const DATA_STRUCTURE: DataStructure<4> = DataStructure::new([
0x0001,
0x0002,
0x1234,
0x5678,
]);
// And create a new data model instance with initial value array.
let data_model = DataModel::new(&DATA_STRUCTURE, [0; 4]);Implementations§
Source§impl<const L: usize, T: Copy> DataModel<L, T>
impl<const L: usize, T: Copy> DataModel<L, T>
Sourcepub fn new(
structure: &'static DataStructure<L>,
initial_values: [T; L],
) -> DataModel<L, T>
pub fn new( structure: &'static DataStructure<L>, initial_values: [T; L], ) -> DataModel<L, T>
Creates and initializes a new data model using the given data structure.
§Arguments
structure: A reference to the constant data structure.initial_values: The initial values to populate the data model.
§Returns
A new DataModel instance initialized with the provided structure and values.
§Examples
use modbus_rtu::slave::{DataModel, DataStructure};
const STRUCTURE: DataStructure<5> = DataStructure::new([
0x0000,
0x0001,
0x0002,
0x1234,
0x5678,
]);
let data_model = DataModel::new(&STRUCTURE, [0; 5]);Sourcepub const fn get_value(&self, address: u16) -> T
pub const fn get_value(&self, address: u16) -> T
Retrieves a value from the data model by using an address defined in the associated data structure.
This method uses the address defined in the data structure to access the corresponding value stored within the data model.
§Arguments
address: The address of the value to retrieve. Only addresses defined in the data structure are allowed.
§Returns
The value stored at the specified address.
§Examples
use modbus_rtu::slave::{DataModel, DataStructure};
const STRUCTURE: DataStructure<5> = DataStructure::new([
0x0000,
0x0001,
0x0002,
0x1234,
0x5678,
]);
let data_model = DataModel::new(&STRUCTURE, [0; 5]);
assert_eq!(data_model.get_value(0x0001), 0);The code below will panic at compile time.
// Will panic!!
let value = data_model.get_value(0x0003);§Panics
…
Sourcepub fn find_value(&self, address: u16) -> Option<T>
pub fn find_value(&self, address: u16) -> Option<T>
Retrieves a value from the data model using a given address, if it exists in the structure.
Unlike get_value, this method returns None instead of panicking if the address is not part of the structure.
This makes it suitable for use with dynamic or external input.
§Arguments
address: The address of the value to look up. The address may or may not be defined in the structure.
§Returns
An Option<T> containing the value if the address is found, or None if it is not.
§Examples
use modbus_rtu::slave::{DataModel, DataStructure};
const STRUCTURE: DataStructure<5> = DataStructure::new([
0x0000,
0x0001,
0x0002,
0x1234,
0x5678,
]);
let data_model = DataModel::new(&STRUCTURE, [10, 20, 30, 40, 50]);
assert_eq!(data_model.find_value(0x0002), Some(30));
assert_eq!(data_model.find_value(0x9999), None);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks whether the data model is empty.
This returns true if the data model contains no entries, which occurs when its length L is zero.
§Returns
true if the data model is empty, false otherwise.
§Examples
use modbus_rtu::slave::DataModel;
let empty_model = DataModel::<0, u16>::empty();
assert!(empty_model.is_empty());Source§impl<T: Copy> DataModel<0, T>
impl<T: Copy> DataModel<0, T>
Sourcepub fn empty() -> DataModel<0, T>
pub fn empty() -> DataModel<0, T>
Creates and returns an empty data model with no stored values.
This is useful when a data model is required but no data is needed or used.
§Returns
An empty DataModel instance with no associated data.
§Examples
use modbus_rtu::slave::{ModbusSlave, DataModel};
let holding_registers = DataModel::empty();
let input_registers = DataModel::empty();
// Create modbus slave instance with zero registers
let modbus_slave = ModbusSlave::new(0x01, holding_registers, input_registers);