~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git
Discussions: https://groups.google.com/g/rust-embed-devs

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


Files changed (1) hide show
  1. impl/src/lib.rs +3 -11
impl/src/lib.rs CHANGED
@@ -12,20 +12,12 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> TokenStream2 {
12
12
  quote! {
13
13
  impl #ident {
14
14
  pub fn get(file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>> {
15
- use std::fs::File;
15
+ use std::fs;
16
- use std::io::Read;
17
16
  use std::path::Path;
18
17
 
19
18
  let file_path = Path::new(#folder_path).join(file_path);
20
- let mut file = match File::open(file_path) {
21
- Ok(mut file) => file,
22
- Err(_e) => {
23
- return None
24
- }
25
- };
26
- let mut data: Vec<u8> = Vec::new();
27
- match file.read_to_end(&mut data) {
19
+ match fs::read(file_path) {
28
- Ok(_) => Some(std::borrow::Cow::from(data)),
20
+ Ok(contents) => Some(std::borrow::Cow::from(contents)),
29
21
  Err(_e) => {
30
22
  return None
31
23
  }