~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 (2) hide show
  1. examples/actix.rs +8 -3
  2. impl/src/lib.rs +14 -11
examples/actix.rs CHANGED
@@ -6,6 +6,7 @@ extern crate mime_guess;
6
6
  use actix_web::http::Method;
7
7
  use actix_web::{server, App, Body, HttpRequest, HttpResponse};
8
8
  use mime_guess::guess_mime_type;
9
+ use std::borrow::Cow;
9
10
 
10
11
  #[derive(RustEmbed)]
11
12
  #[folder = "examples/public/"]
@@ -13,9 +14,13 @@ struct Asset;
13
14
 
14
15
  fn handle_embedded_file(path: &str) -> HttpResponse {
15
16
  match Asset::get(path) {
16
- Some(content) => HttpResponse::Ok()
17
+ Some(content) => {
18
+ let body: Body = match content {
19
+ Cow::Borrowed(bytes) => bytes.into(),
20
+ Cow::Owned(bytes) => bytes.into(),
21
+ };
17
- .content_type(guess_mime_type(path).as_ref())
22
+ HttpResponse::Ok().content_type(guess_mime_type(path).as_ref()).body(body)
18
- .body(Body::from_slice(content.as_ref())),
23
+ }
19
24
  None => HttpResponse::NotFound().body("404 Not Found"),
20
25
  }
21
26
  }
impl/src/lib.rs CHANGED
@@ -15,9 +15,9 @@ mod utils;
15
15
 
16
16
  #[cfg(all(debug_assertions, not(feature = "debug-embed")))]
17
17
  fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
18
- quote!{
18
+ quote! {
19
19
  impl #ident {
20
- pub fn get(file_path: &str) -> Option<impl AsRef<[u8]>> {
20
+ pub fn get(file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>> {
21
21
  use std::fs::File;
22
22
  use std::io::Read;
23
23
  use std::path::Path;
@@ -31,17 +31,17 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
31
31
  };
32
32
  let mut data: Vec<u8> = Vec::new();
33
33
  match file.read_to_end(&mut data) {
34
- Ok(_) => Some(data),
34
+ Ok(_) => Some(std::borrow::Cow::from(data)),
35
35
  Err(_e) => {
36
36
  return None
37
37
  }
38
38
  }
39
39
  }
40
40
 
41
- pub fn iter() -> impl Iterator<Item = impl AsRef<str>> {
41
+ pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
42
42
  use std::path::Path;
43
43
  use rust_embed::utils::get_files;
44
- get_files(String::from(#folder_path)).map(|e| e.rel_path)
44
+ get_files(String::from(#folder_path)).map(|e| std::borrow::Cow::from(e.rel_path))
45
45
  }
46
46
  }
47
47
  }
@@ -55,26 +55,29 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
55
55
  let mut list_values = Vec::<String>::new();
56
56
 
57
57
  for FileEntry { rel_path, full_canonical_path } in get_files(folder_path) {
58
- match_values.push(quote!{
58
+ match_values.push(quote! {
59
+ #rel_path => {
59
- #rel_path => Some(&include_bytes!(#full_canonical_path)[..]),
60
+ let bytes = &include_bytes!(#full_canonical_path)[..];
61
+ Some(std::borrow::Cow::from(bytes))
62
+ },
60
63
  });
61
64
  list_values.push(rel_path);
62
65
  }
63
66
 
64
67
  let array_len = list_values.len();
65
68
 
66
- quote!{
69
+ quote! {
67
70
  impl #ident {
68
- pub fn get(file_path: &str) -> Option<impl AsRef<[u8]>> {
71
+ pub fn get(file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>> {
69
72
  match file_path {
70
73
  #(#match_values)*
71
74
  _ => None,
72
75
  }
73
76
  }
74
77
 
75
- pub fn iter() -> impl Iterator<Item = impl AsRef<str>> {
78
+ pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
76
79
  static items: [&str; #array_len] = [#(#list_values),*];
77
- items.iter()
80
+ items.iter().map(|x| std::borrow::Cow::from(*x))
78
81
  }
79
82
  }
80
83
  }