~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git

rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.


663001dd Tangent 128

6 years ago
Define a trait describing the interface for embedded asset directories
Files changed (2) hide show
  1. readme.md +8 -0
  2. src/lib.rs +35 -0
readme.md CHANGED
@@ -44,6 +44,14 @@ impl Asset {
44
44
  ...
45
45
  }
46
46
  }
47
+ impl RustEmbed for Asset {
48
+ fn get(&self, file_path: &str) -> Option<Cow<'static, [u8]>> {
49
+ ...
50
+ }
51
+ fn iter(&self) -> impl Iterator<Item = Cow<'static, str>> {
52
+ ...
53
+ }
54
+ }
47
55
  ```
48
56
 
49
57
  ### `get(file_path: &str)`
src/lib.rs CHANGED
@@ -9,3 +9,38 @@ pub use rust_embed_impl::*;
9
9
  #[doc(hidden)]
10
10
  #[cfg(all(debug_assertions, not(feature = "debug-embed")))]
11
11
  pub mod utils;
12
+
13
+ /// A directory of binary assets.
14
+ ///
15
+ /// They should be embedded into the executable for release builds,
16
+ /// but can be read from the filesystem for debug builds.
17
+ ///
18
+ /// This trait is meant to be derived like so:
19
+ /// ```
20
+ /// #[macro_use]
21
+ /// extern crate rust_embed;
22
+ /// #[derive(RustEmbed)]
23
+ /// #[folder = "examples/public/"]
24
+ /// struct Asset;
25
+ /// ```
26
+
27
+ pub trait RustEmbed {
28
+ /// Given a relative path from the assets folder, returns the bytes if found.
29
+ ///
30
+ /// If the feature `debug-embed` is enabled or the binary is compiled in
31
+ /// release mode, the bytes have been embeded in the binary and a
32
+ /// `Cow::Borrowed(&'static [u8])` is returned.
33
+ ///
34
+ /// Otherwise, the bytes are read from the file system on each call and a
35
+ /// `Cow::Owned(Vec<u8>)` is returned.
36
+ fn get(&self, file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>>;
37
+
38
+ /// Iterates the files in this assets folder.
39
+ ///
40
+ /// If the feature `debug-embed` is enabled or the binary is compiled in
41
+ /// release mode, a static array to the list of relative paths to the files
42
+ /// is used.
43
+ ///
44
+ /// Otherwise, the files are listed from the file system on each call.
45
+ fn iter(&self) -> Filenames;
46
+ }