include_bytes_aligned/lib.rs
1#![no_std]
2//! A simple macro that embeds the bytes of an external file into the executable and
3//! guarantees that they are aligned.
4//!
5//! # Usage
6//!
7//! ```
8//! include_bytes_aligned!(ALIGNMENT, PATH)
9//! ```
10//!
11//! Where `ALIGNMENT` is any integer literal (must be a power of 2), and PATH is a string literal path
12//! to the file to include, just as in [`include_bytes!`](std::include_bytes).
13//!
14//! # Examples
15//!
16//! ```
17//! use include_bytes_aligned::include_bytes_aligned;
18//!
19//! // Aligns the data to 16 bytes
20//! static DATA: &'static [u8] = include_bytes_aligned!(16, "path/to/file.txt");
21//! ```
22//!
23//! # Efficiency
24//!
25//! This macro does not copy the bytes or duplicate them. Takes up the same amount of space in memory
26//! as the usual [`include_bytes!`](std::include_bytes).
27
28#[macro_export]
29macro_rules! include_bytes_aligned {
30 ($align_to:expr, $path:expr) => {{
31 #[repr(C, align($align_to))]
32 struct __Aligned<T: ?Sized>(T);
33
34 const __DATA: &'static __Aligned<[u8; include_bytes!($path).len()]> =
35 &__Aligned(*include_bytes!($path));
36
37 &__DATA.0
38 }};
39}