amebazii

Macro read_valid_data

Source
macro_rules! read_valid_data {
    ($target:expr, $length:expr, $reader:expr) => { ... };
}
Expand description

Reads valid data from the reader into the target, ensuring that the data does not contain any 0xFF bytes.

This macro attempts to read a specific amount of data from a reader, checks if the data is valid (i.e., it does not contain any 0xFF bytes), and if valid, assigns the data to the provided target.

§Parameters

  • $target: The target variable where the data will be stored (of type Option<[u8; $length]>).
  • $length: The length of the data to read (must match the expected size of the data).
  • $reader: The reader from which the data will be read. The reader must implement the Read trait.

§Example

let mut reader: &[u8] = &[0x01, 0x02, 0x03, 0x04, 0x05];
let mut target: Option<[u8; 5]> = None;
read_valid_data!(target, 5, reader);
assert!(target.is_some()); // The data read is valid, so target should be Some([0x01, 0x02, 0x03, 0x04, 0x05]).

§Error Handling

  • If the data contains any 0xFF byte, it will not be assigned to the target.
  • The macro expects the reader to support reading the exact number of bytes as specified by $length.
  • This macro will return an error if the reader cannot fulfill the request.