amebazii

Macro write_aligned

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

Writes padding to a binary stream to ensure that the next write operation aligns to a specified size.

This macro writes padding bytes to the stream in order to align the current write position to the specified size. The padding is done with a specified fill byte and can optionally be skipped if the alignment is already met.

The macro can be used in different forms depending on whether you need to specify a fill byte and whether the padding is optional. The following variants are available:

  1. Default Padding with 0x00 Fill (non-optional): Aligns the current stream position to the next boundary of the specified size and fills with 0x00.
write_aligned!(writer, 16);

This will ensure the stream is aligned to a 16-byte boundary, and 0x00 is used for padding.

  1. Default Padding with Custom Fill (non-optional): Aligns the current stream position to the next boundary of the specified size and fills with a custom byte value.
write_aligned!(writer, 16, 0xFF);

This will align to a 16-byte boundary and use 0xFF as the padding byte.

  1. Optional Padding with 0x00 Fill: Optionally applies padding if necessary to align the stream position to the specified size. If the stream is already aligned, no padding is written.
write_aligned!(writer, 16, optional);

This will only write padding if needed to align to a 16-byte boundary and will use 0x00 as the fill byte.

  1. Optional Padding with Custom Fill: Optionally applies padding with a custom fill byte if the stream is not already aligned to the specified size.
write_aligned!(writer, 16, 0xFF, optional);

This will apply padding with 0xFF only if needed to align to a 16-byte boundary.