amebazii::util

Function skip_aligned

Source
pub fn skip_aligned<S>(reader: &mut S, align: u64) -> Result<(), Error>
where S: Seek,
Expand description

Skips bytes in the provided reader to ensure that the next read operation aligns with the specified alignment.

This function calculates the number of bytes to skip in order to make the current position in the stream a multiple of the given align value. If the current position is already aligned, no bytes are skipped.

§Parameters

  • reader: A mutable reference to a reader that implements the Seek trait. This is typically a stream or file from which you want to seek.
  • align: The alignment boundary (in bytes) to which the current stream position should be aligned. Must be a power of two.

§Returns

  • An Err(io::Error) if an I/O error occurs while seeking.

§Example

use std::io::{Cursor, Seek, SeekFrom};
use amebazii::util::skip_aligned;

let mut cursor = Cursor::new(vec![0u8; 100]);
let align = 16;
skip_aligned(&mut cursor, align).unwrap();
assert_eq!(cursor.position() % align, 0); // The position should now be aligned to 16.

§Errors

This function may return errors related to seeking if the underlying reader does not support seeking or encounters an I/O issue.