Skip to main content

lazy_static_include/
macro_include_bytes.rs

1#[cfg(debug_assertions)]
2/// Includes a file as a reference to a byte array (`&'static [u8]`).
3///
4/// The file is located relative to the directory containing the manifest of your package.
5#[macro_export]
6macro_rules! lazy_static_include_bytes {
7    ( @inner $path:expr ) => {
8        {
9            let path = $crate::manifest_dir_macros::not_directory_path!($path);
10
11            // Leak the file content to get a `&'static [u8]` reference, because the data needs to live as long as the program anyway.
12            let data: &'static [u8] = ::std::fs::read(path).unwrap().leak();
13
14            data
15        }
16    };
17    ( @unit $(#[$attr: meta])* $name:ident => $path:expr ) => {
18        $(#[$attr])*
19        static $name: ::std::sync::LazyLock<&'static [u8]> = ::std::sync::LazyLock::new(|| $crate::lazy_static_include_bytes!(@inner $path));
20    };
21    ( @unit $(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr ) => {
22        $(#[$attr])*
23        pub$(($($v)+))? static $name: ::std::sync::LazyLock<&'static [u8]> = ::std::sync::LazyLock::new(|| $crate::lazy_static_include_bytes!(@inner $path));
24    };
25    ( $($(#[$attr: meta])* $name:ident => $path:expr),* $(,)* ) => {
26        $(
27            $crate::lazy_static_include_bytes! {
28                @unit
29                $(#[$attr])*
30                $name => $path
31            }
32        )*
33    };
34    ( $($(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr),* $(,)* ) => {
35        $(
36            $crate::lazy_static_include_bytes! {
37                @unit
38                $(#[$attr])*
39                pub$(($($v)+))? $name => $path
40            }
41        )*
42    };
43}
44
45#[cfg(not(debug_assertions))]
46/// Includes a file as a reference to a byte array (`&'static [u8]`).
47///
48/// The file is located relative to the directory containing the manifest of your package.
49#[macro_export]
50macro_rules! lazy_static_include_bytes {
51    ( @unit $(#[$attr: meta])* $name:ident => $path:expr ) => {
52        $(#[$attr])*
53        static $name: ::std::sync::LazyLock<&'static [u8]> = ::std::sync::LazyLock::new(|| include_bytes!($crate::manifest_dir_macros::path!($path)));
54    };
55    ( @unit $(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr ) => {
56        $(#[$attr])*
57        pub$(($($v)+))? static $name: ::std::sync::LazyLock<&'static [u8]> = ::std::sync::LazyLock::new(|| include_bytes!($crate::manifest_dir_macros::path!($path)));
58    };
59    ( $($(#[$attr: meta])* $name:ident => $path:expr),* $(,)* ) => {
60        $(
61            $crate::lazy_static_include_bytes! {
62                @unit
63                $(#[$attr])*
64                $name => $path
65            }
66        )*
67    };
68    ( $($(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr),* $(,)* ) => {
69        $(
70            $crate::lazy_static_include_bytes! {
71                @unit
72                $(#[$attr])*
73                pub$(($($v)+))? $name => $path
74            }
75        )*
76    };
77}