Skip to main content

FifoQueue

Struct FifoQueue 

Source
pub struct FifoQueue { /* private fields */ }
Expand description

A collection of register values retrieved from a Modbus FIFO queue.

This structure maintains the pointer address used for the request and stores the resulting register values in a fixed-size array, making it suitable for no_std and memory-constrained environments.

§Internal Representation

The queue field is a fixed-size array ([u16; MAX_FIFO_QUEUE_COUNT_PER_PDU]) that stores the 16-bit register values. The length field tracks the actual number of valid registers currently present in the queue, allowing the struct to manage a variable number of registers within its fixed capacity.

§Examples

use mbus_core::models::fifo_queue::FifoQueue;
use mbus_core::models::fifo_queue::MAX_FIFO_QUEUE_COUNT_PER_PDU;

// 1. Create a new FifoQueue instance for pointer address 0x1000.
// Initially, it's empty.
let mut fifo = FifoQueue::new(0x1000);
assert_eq!(fifo.ptr_address(), 0x1000);
assert_eq!(fifo.length(), 0);
assert!(fifo.queue().is_empty());

// 2. Simulate receiving a Modbus response with FIFO data.
// Let's say we read 3 registers: 0x1111, 0x2222, 0x3333.
// The `values` array would typically come from parsing the PDU.
let mut received_values = [0; MAX_FIFO_QUEUE_COUNT_PER_PDU];
received_values[0] = 0x1111;
received_values[1] = 0x2222;
received_values[2] = 0x3333;
let received_length = 3;

// 3. Populate the FifoQueue with the received data using `with_values`.
fifo = fifo.with_values(received_values, received_length);

// 4. Verify the contents and properties of the FIFO queue.
assert_eq!(fifo.length(), 3);
assert_eq!(fifo.queue()[..3], [0x1111, 0x2222, 0x3333]);
assert_eq!(fifo.queue()[..1], [0x1111]);
assert_eq!(fifo.queue()[1..2], [0x2222]);
assert_eq!(fifo.queue()[2..3], [0x3333]);

Implementations§

Source§

impl FifoQueue

Source

pub fn new(ptr_address: u16) -> Self

Creates a new FifoQueue instance.

§What happens:

A new FifoQueue is created with the specified ptr_address. The internal queue array is initialized to all zeros, and the length is set to 0, indicating an empty queue.

§Arguments
  • ptr_address - The Modbus address of the FIFO pointer that was queried.
Source

pub fn ptr_address(&self) -> u16

Returns the Modbus address of the FIFO pointer.

This is the address that was provided in the original Read FIFO Queue request.

§Returns

The u16 Modbus address of the FIFO pointer.

Source

pub fn queue(&self) -> &[u16]

Returns a reference to the active values in the FIFO queue.

This method provides a slice &[u16] containing only the length valid registers, effectively hiding the unused capacity of the internal fixed-size array.

§Returns

A slice &[u16] of the register values.

Source

pub fn length(&self) -> usize

Returns the number of registers currently held in this FIFO response.

§Returns

The usize number of registers in the queue.

Source

pub fn with_values(self, values: [u16; 31], length: usize) -> Self

Loads the register values into the FIFO model and sets the active length.

This method is typically used by the service layer after parsing a Modbus response PDU.

§Arguments
  • values - A fixed-size array containing the 16-bit register values. This array should have a length of MAX_FIFO_QUEUE_COUNT_PER_PDU.
  • length - The number of registers to be considered active.
§What happens:
  1. The entire values array is copied into the internal self.queue.
  2. The length is set, but it is clamped to MAX_FIFO_QUEUE_COUNT_PER_PDU to prevent exceeding the buffer’s capacity.
§Returns

The updated FifoQueue instance.

Trait Implementations§

Source§

impl Clone for FifoQueue

Source§

fn clone(&self) -> FifoQueue

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FifoQueue

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.