amebazii::util

Function write_fill

Source
pub fn write_fill<W>(writer: &mut W, fill: u8, length: u64) -> Result<(), Error>
where W: Write,
Expand description

Writes the specified byte fill repeatedly to the writer for the given length.

This function writes the byte fill to the writer in chunks, filling the writer with length bytes of the specified value. It is more memory-efficient as it avoids allocating a large buffer in memory by writing in smaller chunks.

§Parameters

  • writer: A mutable reference to a writer that implements the Write trait. This can be a file, buffer, or network stream to which the data will be written.
  • fill: The byte value to fill the output with. It will be written repeatedly to the writer.
  • length: The number of times the byte fill will be written to the writer.

§Returns

  • Err(io::Error): If an I/O error occurs while writing.

§Example

use std::io::{Cursor, Write};
use my_crate::write_fill;

let mut cursor = Cursor::new(vec![]);
write_fill(&mut cursor, b'A', 10).unwrap();
assert_eq!(cursor.into_inner(), vec![b'A'; 10]); // Writes 10 'A' bytes.

§Performance

This implementation writes the byte fill in smaller chunks, reducing memory usage and making it more efficient for large data sizes.