include_bytes!() { /* proc-macro */ }
Expand description
Includes a file as a reference to a byte array.
This macro will yield an expression of type [u8; N] by default with content of file.
To reinterpret it as different type add as <type>
where type can be: u8
, u16
, u32
, u64
or u128
.
You can use an endianness suffix to change the expected endianness within a file.
When endianness is specified the macro shall treat integers within a file correspondingly to the suffix and
convert it to the target’s native endianness. Allowed suffixes: le and be, eg. u16le
or u32be
.
Without suffix, bytes are extracted as it is.
§NOTE:
Due to Span::source_file
being unstable, the file is searched relative to crate root.
§Supported types:
-
Primitive fixed sized unsigned integers;
-
Arrays with unsigned integers;
§Usage:
use include_bytes_plus::include_bytes;
let bytes = include_bytes!("tests/include.in");
let bytes_u16 = include_bytes!("tests/include.in" as u16);
let bytes_u16_2 = include_bytes!("tests/include with whitespaces.in" as u16);
let bytes_u16_3 = include_bytes!("tests/include with whitespaces.in" as [u8; 48]);
let bytes_u16_4 = include_bytes!("tests/include with whitespaces.in" as [u16; 12]);
let bytes_u16be = include_bytes!("tests/include.in" as u16be);
let bytes_u16be_4 = include_bytes!("tests/include with whitespaces.in" as [u16be; 12]);
assert_eq!(bytes.len(), bytes_u16.len() * 2);
assert_eq!(bytes.len(), bytes_u16_2.len() * 2);
assert_eq!(bytes_u16_3.len(), 1);
assert_eq!(bytes_u16_3[0].len(), 48);
assert_eq!(bytes_u16_4.len(), 2);
assert_eq!(bytes_u16_4[0].len(), 12);
assert_eq!(bytes_u16_4[1].len(), 12);
assert_eq!(bytes_u16be.len(), bytes_u16.len());
assert_eq!(bytes_u16be_4.len(), 2);
assert_eq!(bytes_u16be_4[0].len(), 12);
assert_eq!(bytes_u16be_4[1].len(), 12);
#[cfg(target_endian = "little")]
assert_ne!(bytes_u16_4, bytes_u16be_4);
§Debugging timings:
Set env variable RUST_INCLUDE_BYTES_LOG=1
to enable logging of each parsed file statistics