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    ( @impl $name:ident ) => {
8        impl<'a> ::std::cmp::PartialEq<&'a [u8]> for $name {
9            fn eq(&self, other: &&'a [u8]) -> bool {
10                (&*$name).eq(other)
11            }
12        }
13
14        impl ::std::cmp::PartialEq for $name {
15            fn eq(&self, other: &$name) -> bool {
16                true
17            }
18        }
19
20        impl<'a> ::std::cmp::PartialEq<$name> for &'a [u8] {
21            fn eq(&self, other: &$name) -> bool {
22                self.eq(&*$name)
23            }
24        }
25
26        impl ::std::fmt::Debug for $name {
27            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
28                ::std::fmt::Debug::fmt(*$name, f)
29            }
30        }
31
32        impl<T> ::std::convert::AsRef<T> for $name
33        where
34            T: ?Sized,
35            [u8]: ::std::convert::AsRef<T>,
36        {
37            fn as_ref(&self) -> &T {
38                (*$name).as_ref()
39            }
40        }
41    };
42    ( @inner $name:ident, $path:expr ) => {
43        {
44            use ::std::fs;
45            use ::std::mem::{forget, transmute};
46
47            let path = $crate::manifest_dir_macros::not_directory_path!($path);
48
49            let data = fs::read(path).unwrap();
50
51            unsafe {
52                let ret = transmute(data.as_slice());
53                forget(data);
54                ret
55            }
56        }
57    };
58    ( @unit $(#[$attr: meta])* $name:ident => $path:expr ) => {
59        $crate::lazy_static::lazy_static! {
60            $(#[$attr])*
61            static ref $name: &'static [u8] = $crate::lazy_static_include_bytes!(@inner $name, $path);
62        }
63
64        $crate::lazy_static_include_bytes!(@impl $name);
65    };
66    ( @unit $(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr ) => {
67        $crate::lazy_static::lazy_static! {
68            $(#[$attr])*
69            pub$(($($v)+))? static ref $name: &'static [u8] = $crate::lazy_static_include_bytes!(@inner $name, $path);
70        }
71
72        $crate::lazy_static_include_bytes!(@impl $name);
73    };
74    ( $($(#[$attr: meta])* $name:ident => $path:expr),* $(,)* ) => {
75        $(
76            $crate::lazy_static_include_bytes! {
77                @unit
78                $(#[$attr])*
79                $name => $path
80            }
81        )*
82    };
83    ( $($(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr),* $(,)* ) => {
84        $(
85            $crate::lazy_static_include_bytes! {
86                @unit
87                $(#[$attr])*
88                pub$(($($v)+))? $name => $path
89            }
90        )*
91    };
92}
93
94#[cfg(not(debug_assertions))]
95/// Includes a file as a reference to a byte array (`&'static [u8]`).
96///
97/// The file is located relative to the directory containing the manifest of your package.
98#[macro_export]
99macro_rules! lazy_static_include_bytes {
100    ( @impl $name:ident ) => {
101        impl<'a> ::std::cmp::PartialEq<&'a [u8]> for $name {
102            fn eq(&self, other: &&'a [u8]) -> bool {
103                (&*$name).eq(other)
104            }
105        }
106
107        impl ::std::cmp::PartialEq for $name {
108            fn eq(&self, other: &$name) -> bool {
109                true
110            }
111        }
112
113        impl<'a> ::std::cmp::PartialEq<$name> for &'a [u8] {
114            fn eq(&self, other: &$name) -> bool {
115                self.eq(&*$name)
116            }
117        }
118
119        impl ::std::fmt::Debug for $name {
120            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
121                ::std::fmt::Debug::fmt(*$name, f)
122            }
123        }
124
125        impl<T> ::std::convert::AsRef<T> for $name
126        where
127            T: ?Sized,
128            [u8]: ::std::convert::AsRef<T>,
129        {
130            fn as_ref(&self) -> &T {
131                (*$name).as_ref()
132            }
133        }
134    };
135    ( @unit $(#[$attr: meta])* $name:ident => $path:expr ) => {
136        $crate::lazy_static::lazy_static! {
137            $(#[$attr])*
138            static ref $name: &'static [u8] = include_bytes!($crate::manifest_dir_macros::path!($path));
139        }
140
141        $crate::lazy_static_include_bytes!(@impl $name);
142    };
143    ( @unit $(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr ) => {
144        $crate::lazy_static::lazy_static! {
145            $(#[$attr])*
146            pub$(($($v)+))? static ref $name: &'static [u8] = include_bytes!($crate::manifest_dir_macros::path!($path));
147        }
148
149        $crate::lazy_static_include_bytes!(@impl $name);
150    };
151    ( $($(#[$attr: meta])* $name:ident => $path:expr),* $(,)* ) => {
152        $(
153            $crate::lazy_static_include_bytes! {
154                @unit
155                $(#[$attr])*
156                $name => $path
157            }
158        )*
159    };
160    ( $($(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr),* $(,)* ) => {
161        $(
162            $crate::lazy_static_include_bytes! {
163                @unit
164                $(#[$attr])*
165                pub$(($($v)+))? $name => $path
166            }
167        )*
168    };
169}