macro_rules! flate {
($(#[$meta:meta])*
$(pub $(($($vis:tt)+))?)? static $name:ident: [u8] from $path:literal) => { ... };
($(#[$meta:meta])*
$(pub $(($($vis:tt)+))?)? static $name:ident: str from $path:literal) => { ... };
}Expand description
This macro is like include_bytes! or include_str!, but compresses at compile time
and lazily decompresses at runtime.
§Parameters
The macro can be used like this:
ⓘ
flate!($meta $vis static $name: $type from $file);$metais zero or more#[...]attributes that can be applied on the static parameters oflazy_static. For the actual semantics of the meta attributes, please refer tolazy_staticdocumentation.$visis a visibility modifier (e.g.pub,pub(crate)) or empty.$nameis the name of the static variable..$typecan be either[u8]orstr. However, the actual type created would dereference intoVec<u8>andString(although they areAsRef<[u8]>andAsRef<str>) respectively.$fileis a path relative to the currentCARGO_MANIFEST_DIR. Absolute paths are not supported. Note that this is distinct from the behaviour of the builtininclude_bytes!/include_str!macros —includle_bytes!/include_str!paths are relative to the current source file, whileflate!paths are relative toCARGO_MANIFEST_DIR.
§Returns
The macro expands to a lazy_static call, which lazily inflates the compressed bytes.
§Compile errors
- If the input format is incorrect
- If the referenced file does not exist or is not readable
- If
$typeisstrbut the file is not fully valid UTF-8
§Algorithm
Compression and decompression use the DEFLATE algorithm from libflate.
§Examples
Below are some basic examples. For actual compiled examples, see the tests directory.
ⓘ
// This declares a `static VAR_NAME: impl Deref<Vec<u8>>`
flate!(static VAR_NAME: [u8] from "binary-file.dat");
// This declares a `static VAR_NAME: impl Deref<String>`
flate!(static VAR_NAME: str from "text-file.txt");
// Visibility modifiers can be added in the front
flate!(pub static VAR_NAME: str from "public-file.txt");
// Meta attributes can also be added
flate!(#[allow(unused)]
#[doc = "Example const"]
pub static VAR_NAME: str from "file.txt");