pub fn write_byte_arrays(
index_output_path: &Path,
byte_arrays: Vec<(&str, Bytes)>,
) -> Result<()>Expand description
Writes byte arrays to separate .bin files in a specified directory and generates
a .rs file adjacent to the directory. The .rs file uses include_bytes! to
reference the .bin files.
§Arguments
index_output_path- The path to the directory where the.binfiles will be saved. This directory must exist or will be created by the function.byte_arrays- A vector of tuples(name, content), where:nameis the name of the static variable to be generated in the.rsfile.contentis aBytesobject containing the binary data.
§Behavior
- Ensures the
index_output_pathexists or creates it if it does not. - For each entry in
byte_arrays, writes the binary data to a.binfile inside the specified directory. - Generates a
.rsfile adjacent to the directory. The.rsfile includes static variable declarations that useinclude_bytes!to reference the corresponding.binfiles.
§Errors
Returns an io::Error if:
- The specified
index_output_pathis not a directory. - A file operation (e.g., creating or writing files) fails.
§Example
use bytes::Bytes;
use std::path::Path;
use your_crate::write_byte_arrays;
// Define the output directory
let output_dir = Path::new("embed");
// Define the byte arrays to write
let byte_arrays = vec![
("ARRAY_ONE", Bytes::from(vec![1, 2, 3, 4])),
("ARRAY_TWO", Bytes::from(vec![5, 6, 7, 8])),
];
// Write the byte arrays and generate the Rust file
write_byte_arrays(output_dir, byte_arrays).unwrap();§Output
If index_output_path is embed, the following structure will be created:
embed/
├── ARRAY_ONE.bin
├── ARRAY_TWO.bin
embed.rsThe content of embed.rs will look like:
// Automatically generated file. Do not edit.
// Generated by build-resource-byte-arrays crate.
pub static ARRAY_ONE: &[u8] = include_bytes!("embed/ARRAY_ONE.bin");
pub static ARRAY_TWO: &[u8] = include_bytes!("embed/ARRAY_TWO.bin");