Struct DataStructure

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

Source

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

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.

Source

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

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.

Source

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>

Source

pub const EMPTY: DataStructure<0>

An empty data structure.

This is used to define a data model that does not contain any data.

Trait Implementations§

Source§

impl<const L: usize> Debug for DataStructure<L>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const L: usize> Freeze for DataStructure<L>

§

impl<const L: usize> RefUnwindSafe for DataStructure<L>

§

impl<const L: usize> Send for DataStructure<L>

§

impl<const L: usize> Sync for DataStructure<L>

§

impl<const L: usize> Unpin for DataStructure<L>

§

impl<const L: usize> UnwindSafe for DataStructure<L>

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.