amebazii

Macro write_padding

Source
macro_rules! write_padding {
    ($writer:expr, $size:expr) => { ... };
    ($writer:expr, $size:literal, $fill:literal) => { ... };
}
Expand description

write_padding! - A macro to write padding bytes to a writer.

This macro writes a series of padding bytes (either filled with 0xFF or a custom byte) to a writer, ensuring that the stream is correctly aligned or that the desired padding size is achieved. It provides two variants:

  1. Default fill (0xFF): The first variant writes padding filled with the byte 0xFF.
  2. Custom fill: The second variant allows for specifying a custom byte for padding.

The macro handles the error propagation automatically, returning the result of the write_all method.

§Parameters:

  • $writer: The writer to which padding bytes should be written. This must implement the std::io::Write trait.
  • $size: The size (in bytes) of the padding to be written.
  • $fill (optional): The byte value to fill the padding. Defaults to 0xFF if not provided.

§Example 1: Default padding (filled with 0xFF):

use std::io::Cursor;
let mut buffer = Cursor::new(Vec::new());
write_padding!(buffer, 16); // Writes 16 bytes of `0xFF` to the buffer

§Example 2: Custom padding byte:

use std::io::Cursor;
let mut buffer = Cursor::new(Vec::new());
write_padding!(buffer, 8, 0x00); // Writes 8 bytes of `0x00` to the buffer