~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.
74744a7b
—
Tangent 128 6 years ago
Make RustEmbed trait static
- impl/src/lib.rs +4 -4
- readme.md +2 -2
- src/lib.rs +2 -2
- tests/lib.rs +5 -19
impl/src/lib.rs
CHANGED
|
@@ -45,10 +45,10 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
impl rust_embed::RustEmbed for #ident {
|
|
48
|
-
fn get(
|
|
48
|
+
fn get(file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>> {
|
|
49
49
|
#ident::get(file_path)
|
|
50
50
|
}
|
|
51
|
-
fn iter(
|
|
51
|
+
fn iter() -> rust_embed::Filenames {
|
|
52
52
|
// the return type of iter() is unnamable, so we have to box it
|
|
53
53
|
rust_embed::Filenames::Dynamic(Box::new(#ident::iter()))
|
|
54
54
|
}
|
|
@@ -93,10 +93,10 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
impl rust_embed::RustEmbed for #ident {
|
|
96
|
-
fn get(
|
|
96
|
+
fn get(file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>> {
|
|
97
97
|
#ident::get(file_path)
|
|
98
98
|
}
|
|
99
|
-
fn iter(
|
|
99
|
+
fn iter() -> rust_embed::Filenames {
|
|
100
100
|
rust_embed::Filenames::Embedded(#ident::names())
|
|
101
101
|
}
|
|
102
102
|
}
|
readme.md
CHANGED
|
@@ -45,10 +45,10 @@ impl Asset {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
impl RustEmbed for Asset {
|
|
48
|
-
fn get(
|
|
48
|
+
fn get(file_path: &str) -> Option<Cow<'static, [u8]>> {
|
|
49
49
|
...
|
|
50
50
|
}
|
|
51
|
-
fn iter(
|
|
51
|
+
fn iter() -> impl Iterator<Item = Cow<'static, str>> {
|
|
52
52
|
...
|
|
53
53
|
}
|
|
54
54
|
}
|
src/lib.rs
CHANGED
|
@@ -33,7 +33,7 @@ pub trait RustEmbed {
|
|
|
33
33
|
///
|
|
34
34
|
/// Otherwise, the bytes are read from the file system on each call and a
|
|
35
35
|
/// `Cow::Owned(Vec<u8>)` is returned.
|
|
36
|
-
fn get(
|
|
36
|
+
fn get(file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>>;
|
|
37
37
|
|
|
38
38
|
/// Iterates the files in this assets folder.
|
|
39
39
|
///
|
|
@@ -42,7 +42,7 @@ pub trait RustEmbed {
|
|
|
42
42
|
/// is used.
|
|
43
43
|
///
|
|
44
44
|
/// Otherwise, the files are listed from the file system on each call.
|
|
45
|
-
fn iter(
|
|
45
|
+
fn iter() -> Filenames;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/// An iterator type over filenames.
|
tests/lib.rs
CHANGED
|
@@ -34,28 +34,14 @@ fn iter_works() {
|
|
|
34
34
|
|
|
35
35
|
#[test]
|
|
36
36
|
fn trait_works_generic() {
|
|
37
|
-
trait_works_generic_helper
|
|
37
|
+
trait_works_generic_helper::<Asset>();
|
|
38
38
|
}
|
|
39
|
-
fn trait_works_generic_helper
|
|
39
|
+
fn trait_works_generic_helper<E: rust_embed::RustEmbed>() {
|
|
40
40
|
let mut num_files = 0;
|
|
41
|
-
for file in
|
|
41
|
+
for file in E::iter() {
|
|
42
|
-
assert!(
|
|
42
|
+
assert!(E::get(file.as_ref()).is_some());
|
|
43
43
|
num_files += 1;
|
|
44
44
|
}
|
|
45
45
|
assert_eq!(num_files, 6);
|
|
46
|
-
assert!(
|
|
46
|
+
assert!(E::get("gg.html").is_none(), "gg.html should not exist");
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
#[test]
|
|
50
|
-
fn trait_works_object() {
|
|
51
|
-
trait_works_object_helper(&Asset);
|
|
52
|
-
}
|
|
53
|
-
fn trait_works_object_helper(folder: &dyn rust_embed::RustEmbed) {
|
|
54
|
-
let mut num_files = 0;
|
|
55
|
-
for file in folder.iter() {
|
|
56
|
-
assert!(folder.get(file.as_ref()).is_some());
|
|
57
|
-
num_files += 1;
|
|
58
|
-
}
|
|
59
|
-
assert_eq!(num_files, 6);
|
|
60
|
-
assert!(folder.get("gg.html").is_none(), "gg.html should not exist");
|
|
61
47
|
}
|