#relative #compile-time #file #relative-path #macro #input #array

macro include-bytes-plus

Improved include_bytes! allowing to reinterpret input differently

4 stable releases

1.1.0 Sep 22, 2023
1.0.3 Feb 10, 2022
1.0.1 Feb 9, 2022
1.0.0 Feb 3, 2022

#2268 in Procedural macros

Download history 8/week @ 2024-07-08 9/week @ 2024-07-15 50/week @ 2024-07-29 8/week @ 2024-08-05 16/week @ 2024-08-19 9/week @ 2024-08-26 7/week @ 2024-09-02 10/week @ 2024-09-09 14/week @ 2024-09-16 15/week @ 2024-09-23 13/week @ 2024-09-30 14/week @ 2024-10-07 16/week @ 2024-10-14

59 downloads per month
Used in 2 crates

BSL-1.0 license

23KB
386 lines

include-bytes-plus

Crates.io Documentation Build

Improved version of Rust's include_bytes macro that allows to reinterpret input as differently typed array.

Due to inability to capture current file path in the stable Rust, this macro only accepts paths relative to crate's root.

Supported types:

  • Primitive fixed sized unsigned integers with optional endianness suffix;

  • 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);

assert_eq!(bytes.len(), bytes_u16.len() * 2);
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());

Debugging timings:

Set env variable RUST_INCLUDE_BYTES_LOG=1 to enable logging of each parsed file statistics

No runtime deps