lazy_static_include/
macro_include_str.rs

1#[cfg(debug_assertions)]
2/// Includes a utf8-encoded file as a string slice (`&'static str`).
3///
4/// The file is located relative to the directory containing the manifest of your package.
5#[macro_export]
6macro_rules! lazy_static_include_str {
7    ( @impl $name:ident ) => {
8        impl ::std::cmp::PartialEq<str> for $name {
9            #[inline]
10            fn eq(&self, other: &str) -> bool {
11                (*$name).eq(other)
12            }
13        }
14
15        impl<'a> ::std::cmp::PartialEq<&'a str> for $name {
16            #[inline]
17            fn eq(&self, other: &&'a str) -> bool {
18                (&*$name).eq(other)
19            }
20        }
21
22        impl ::std::cmp::PartialEq for $name {
23            #[inline]
24            fn eq(&self, other: &$name) -> bool {
25                true
26            }
27        }
28
29        impl<'a> ::std::cmp::PartialEq<$name> for &'a str {
30            #[inline]
31            fn eq(&self, other: &$name) -> bool {
32                self.eq(&*$name)
33            }
34        }
35
36        impl ::std::fmt::Debug for $name {
37            #[inline]
38            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
39                ::std::fmt::Debug::fmt(*$name, f)
40            }
41        }
42
43        impl ::std::fmt::Display for $name {
44            #[inline]
45            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
46                ::std::fmt::Display::fmt(*$name, f)
47            }
48        }
49
50        impl<T> ::std::convert::AsRef<T> for $name
51        where
52            T: ?Sized,
53            str: ::std::convert::AsRef<T>,
54        {
55            #[inline]
56            fn as_ref(&self) -> &T {
57                (*$name).as_ref()
58            }
59        }
60    };
61    ( @inner $name:ident, $path:expr ) => {
62        {
63            use ::std::fs;
64            use ::std::mem::{forget, transmute};
65
66            let path = $crate::manifest_dir_macros::not_directory_path!($path);
67
68            let text = fs::read_to_string(path).unwrap();
69
70            unsafe {
71                let ret = transmute(text.as_str());
72                forget(text);
73                ret
74            }
75        }
76    };
77    ( @unit $(#[$attr: meta])* $name:ident => $path:expr ) => {
78        $crate::lazy_static::lazy_static! {
79            $(#[$attr])*
80            static ref $name: &'static str = $crate::lazy_static_include_str!(@inner $name, $path);
81        }
82
83        $crate::lazy_static_include_str!(@impl $name);
84    };
85    ( @unit $(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr ) => {
86        $crate::lazy_static::lazy_static! {
87            $(#[$attr])*
88            pub$(($($v)+))? static ref $name: &'static str = $crate::lazy_static_include_str!(@inner $name, $path);
89        }
90
91        $crate::lazy_static_include_str!(@impl $name);
92    };
93    ( $($(#[$attr: meta])* $name:ident => $path:expr),* $(,)* ) => {
94        $(
95            $crate::lazy_static_include_str! {
96                @unit
97                $(#[$attr])*
98                $name => $path
99            }
100        )*
101    };
102    ( $($(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr),* $(,)* ) => {
103        $(
104            $crate::lazy_static_include_str! {
105                @unit
106                $(#[$attr])*
107                pub$(($($v)+))? $name => $path
108            }
109        )*
110    };
111}
112
113#[cfg(not(debug_assertions))]
114/// Includes a utf8-encoded file as a string slice (`&'static str`).
115///
116/// The file is located relative to the directory containing the manifest of your package.
117#[macro_export]
118macro_rules! lazy_static_include_str {
119    ( @impl $name:ident ) => {
120        impl ::std::cmp::PartialEq<str> for $name {
121            #[inline]
122            fn eq(&self, other: &str) -> bool {
123                (*$name).eq(other)
124            }
125        }
126
127        impl<'a> ::std::cmp::PartialEq<&'a str> for $name {
128            #[inline]
129            fn eq(&self, other: &&'a str) -> bool {
130                (&*$name).eq(other)
131            }
132        }
133
134        impl ::std::cmp::PartialEq for $name {
135            #[inline]
136            fn eq(&self, other: &$name) -> bool {
137                true
138            }
139        }
140
141        impl<'a> ::std::cmp::PartialEq<$name> for &'a str {
142            #[inline]
143            fn eq(&self, other: &$name) -> bool {
144                self.eq(&*$name)
145            }
146        }
147
148        impl ::std::fmt::Debug for $name {
149            #[inline]
150            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
151                ::std::fmt::Debug::fmt(*$name, f)
152            }
153        }
154
155        impl ::std::fmt::Display for $name {
156            #[inline]
157            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
158                ::std::fmt::Display::fmt(*$name, f)
159            }
160        }
161
162        impl<T> ::std::convert::AsRef<T> for $name
163        where
164            T: ?Sized,
165            str: ::std::convert::AsRef<T>,
166        {
167            #[inline]
168            fn as_ref(&self) -> &T {
169                (*$name).as_ref()
170            }
171        }
172    };
173    ( @unit $(#[$attr: meta])* $name:ident => $path:expr ) => {
174        $crate::lazy_static::lazy_static! {
175            $(#[$attr])*
176            static ref $name: &'static str = include_str!($crate::manifest_dir_macros::path!($path));
177        }
178
179        $crate::lazy_static_include_str!(@impl $name);
180    };
181    ( @unit $(#[$attr: meta])* pub $name:ident => $path:expr ) => {
182        $crate::lazy_static::lazy_static! {
183            $(#[$attr])*
184            pub static ref $name: &'static str = include_str!($crate::manifest_dir_macros::path!($path));
185        }
186
187        $crate::lazy_static_include_str!(@impl $name);
188    };
189    ( @unit $(#[$attr: meta])* pub($($vis:tt)+) $name:ident => $path:expr ) => {
190        $crate::lazy_static::lazy_static! {
191            $(#[$attr])*
192            pub($($vis)+) static ref $name: &'static str = include_str!($crate::manifest_dir_macros::path!($path));
193        }
194
195        $crate::lazy_static_include_str!(@impl $name);
196    };
197    ( $($(#[$attr: meta])* $name:ident => $path:expr),* $(,)* ) => {
198        $(
199            $crate::lazy_static_include_str! {
200                @unit
201                $(#[$attr])*
202                $name => $path
203            }
204        )*
205    };
206    ( $($(#[$attr: meta])* pub$(($($v:tt)+))? $name:ident => $path:expr),* $(,)* ) => {
207        $(
208            $crate::lazy_static_include_str! {
209                @unit
210                $(#[$attr])*
211                pub$(($($v)+))? $name => $path
212            }
213        )*
214    };
215}