~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 (3) hide show
  1. impl/src/lib.rs +2 -2
  2. src/lib.rs +1 -1
  3. tests/lib.rs +14 -0
impl/src/lib.rs CHANGED
@@ -93,7 +93,7 @@ fn embedded(
93
93
  }
94
94
 
95
95
  /// Iterates over the file paths in the folder.
96
- pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> {
96
+ pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> + 'static {
97
97
  Self::names().map(|x| ::std::borrow::Cow::from(*x))
98
98
  }
99
99
  }
@@ -188,7 +188,7 @@ fn dynamic(
188
188
  }
189
189
 
190
190
  /// Iterates over the file paths in the folder.
191
- pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> {
191
+ pub fn iter() -> impl ::std::iter::Iterator<Item = ::std::borrow::Cow<'static, str>> + 'static {
192
192
  use ::std::path::Path;
193
193
 
194
194
 
src/lib.rs CHANGED
@@ -45,7 +45,7 @@ pub trait RustEmbed {
45
45
  /// is used.
46
46
  ///
47
47
  /// Otherwise, the files are listed from the file system on each call.
48
- fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>>;
48
+ fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> + 'static;
49
49
  }
50
50
 
51
51
  pub use RustEmbed as Embed;
tests/lib.rs CHANGED
@@ -56,3 +56,17 @@ fn trait_works_generic_helper<E: rust_embed::Embed>() {
56
56
  assert_eq!(num_files, 7);
57
57
  assert!(E::get("gg.html").is_none(), "gg.html should not exist");
58
58
  }
59
+
60
+ /// Test that iter() can be boxed into a trait object.
61
+ /// This is required for libraries like i18n-embed that need to return
62
+ /// `Box<dyn Iterator<Item = String>>` from a trait implementation.
63
+ #[test]
64
+ fn iter_can_be_boxed() {
65
+ fn get_boxed_iter<E: rust_embed::Embed>() -> Box<dyn Iterator<Item = String>> {
66
+ Box::new(E::iter().map(|filename| filename.to_string()))
67
+ }
68
+
69
+ let filenames: Vec<String> = get_boxed_iter::<Asset>().collect();
70
+ assert_eq!(filenames.len(), 7);
71
+ assert!(filenames.contains(&"index.html".to_string()));
72
+ }