I created an iterator that moves through a vector of Strings owned by a struct(SnippetSet) stored inside a DashMap, and returns Tuples of that contains that string and it's corresponding prefix from another DashMap.
pub fn triggers(
&self,
language: String,
snippet_set: String,
) -> Box<dyn Iterator<Item = (Vec<u8>, String)> + '_> {
Box::new(
self.snippet_sets //Arc<DashMap<(String,String),SnippetSets>
.get(&(language.clone(), snippet_set.to_string()))
.unwrap()
.contents//Vec<String>
.clone()//also trying to avoid cloning
.into_iter()
.map(move |s| {
(
self.snippets //Arc<DashMap<(String,String),Snippet>
.get(&(language.clone(), s.clone()))
.unwrap()
.prefix//Vec<u8>
.clone(),
s.clone(),
)
}),
)
}
I tried wrapping the iterator in a struct as described in the simple iterator delegation section of this post on returning rust iterators, but couldn't figure out how to handle the closure inside the map iterator.
Also, I'm wondering if wrapping the vector contents inside a Cow would work for avoiding cloning while iterating?
Any ideas how I can avoid the potential performance penalty of dynamically dispatching the iterator?