~repos /rust-embed
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.
01d1ddaf
—
pyros2097 6 years ago
release v5.1.0
readme.md
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
## Rust Embed [](https://travis-ci.org/pyros2097/rust-embed) [](https://ci.appveyor.com/project/pyros2097/rust-embed/branch/master) [](https://crates.io/crates/rust-embed)
|
|
2
|
+
|
|
2
3
|
Rust Custom Derive Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.
|
|
3
4
|
|
|
4
5
|
You can use this to embed your css, js and images into a single executable which can be deployed to your servers. Also it makes it easy to build a very small docker image for you to deploy.
|
|
5
6
|
|
|
6
7
|
### Dev
|
|
8
|
+
|
|
7
9
|
<img src="https://user-images.githubusercontent.com/1687946/40840773-b1ae1ce6-65c5-11e8-80ac-9e9196701ca2.png" width="700" height="100">
|
|
8
10
|
|
|
9
11
|
### Release
|
|
12
|
+
|
|
10
13
|
<img src="https://user-images.githubusercontent.com/1687946/40840774-b1dd709a-65c5-11e8-858d-73a88e25f07a.png" width="700" height="184">
|
|
11
14
|
|
|
12
15
|
## Installation
|
|
13
16
|
|
|
14
17
|
```toml
|
|
15
18
|
[dependencies]
|
|
16
|
-
rust-embed="5.
|
|
19
|
+
rust-embed="5.1.0"
|
|
17
20
|
```
|
|
18
21
|
|
|
19
22
|
## Documentation
|
|
@@ -31,15 +34,14 @@ The path resolution works as follows:
|
|
|
31
34
|
struct Asset;
|
|
32
35
|
```
|
|
33
36
|
|
|
34
|
-
|
|
35
37
|
The macro will generate the following code:
|
|
36
38
|
|
|
37
39
|
```rust
|
|
38
|
-
impl Asset {
|
|
40
|
+
impl Asset {
|
|
39
41
|
pub fn get(file_path: &str) -> Option<Cow<'static, [u8]>> {
|
|
40
|
-
...
|
|
42
|
+
...
|
|
41
43
|
}
|
|
42
|
-
|
|
44
|
+
|
|
43
45
|
pub fn iter() -> impl Iterator<Item = Cow<'static, str>> {
|
|
44
46
|
...
|
|
45
47
|
}
|
|
@@ -58,13 +60,12 @@ impl RustEmbed for Asset {
|
|
|
58
60
|
|
|
59
61
|
Given a relative path from the assets folder returns the bytes if found.
|
|
60
62
|
|
|
61
|
-
If the feature `debug-embed` is enabled or the binary
|
|
63
|
+
If the feature `debug-embed` is enabled or the binary compiled in release mode the bytes have been embeded in the binary and a `Cow::Borrowed(&'static [u8])` is returned.
|
|
62
64
|
|
|
63
65
|
Otherwise the bytes are read from the file system on each call and a `Cow::Owned(Vec<u8>)` is returned.
|
|
64
66
|
|
|
65
|
-
|
|
66
67
|
### `iter()`
|
|
67
|
-
|
|
68
|
+
|
|
68
69
|
Iterates the files in this assets folder.
|
|
69
70
|
|
|
70
71
|
If the feature `debug-embed` is enabled or the binary compiled in release mode a static array to the list of relative paths to the files is returned.
|
|
@@ -77,8 +78,8 @@ Otherwise the files are listed from the file system on each call.
|
|
|
77
78
|
|
|
78
79
|
Always embed the files in the binary, even in debug mode.
|
|
79
80
|
|
|
80
|
-
|
|
81
81
|
## Usage
|
|
82
|
+
|
|
82
83
|
```rust
|
|
83
84
|
#[macro_use]
|
|
84
85
|
extern crate rust_embed;
|
|
@@ -98,6 +99,7 @@ fn main() {
|
|
|
98
99
|
```
|
|
99
100
|
|
|
100
101
|
## Examples
|
|
102
|
+
|
|
101
103
|
To run the example in dev mode where it reads from the fs,
|
|
102
104
|
|
|
103
105
|
`cargo run --example basic`
|
|
@@ -115,6 +117,7 @@ Note: To run the `rocket` example, add the `nightly` feature flag and run on a n
|
|
|
115
117
|
`cargo +nightly run --example rocket --features nightly`
|
|
116
118
|
|
|
117
119
|
## Testing
|
|
120
|
+
|
|
118
121
|
debug: `cargo test --test lib`
|
|
119
122
|
|
|
120
123
|
release: `cargo test --test lib --release`
|