Struct DataModel

Source
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>

Source

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]);
Source

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

Source

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);
Source

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>

Source

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);

Trait Implementations§

Source§

impl<const L: usize, T: Debug + Copy> Debug for DataModel<L, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const L: usize, T> Freeze for DataModel<L, T>
where T: Freeze,

§

impl<const L: usize, T> RefUnwindSafe for DataModel<L, T>
where T: RefUnwindSafe,

§

impl<const L: usize, T> Send for DataModel<L, T>
where T: Send,

§

impl<const L: usize, T> Sync for DataModel<L, T>
where T: Sync,

§

impl<const L: usize, T> Unpin for DataModel<L, T>
where T: Unpin,

§

impl<const L: usize, T> UnwindSafe for DataModel<L, T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.