{data.into_iter().map_fragment(|item| html! {
<CustomElement/>
})}
errors with 'await' is only allowed inside 'async' functions and blocks
I am experimenting with the following trait and impl
#[async_trait]
pub trait MapFragmentAsyncExt<V> {
async fn map_fragment<F, B, C>(self, mut f: F) -> String
where
Self: Sized,
F: FnMut(V) -> C + std::marker::Send,
C: Future<Output = B> + std::marker::Send,
B: ToString,
V: std::marker::Send
;
}
#[async_trait]
impl<V> MapFragmentAsyncExt<V> for Vec<V>
{
async fn map_fragment<F, B, C>(self, mut f: F) -> String
where
Self: Sized,
F: FnMut(V) -> C + std::marker::Send,
C: Future<Output = B> + std::marker::Send,
B: ToString,
V: std::marker::Send
{
use futures::stream::StreamExt;
let mut out = Vec::with_capacity(self.len());
let mut stream = futures::stream::iter(self);
while let Some(v) = stream.next().await {
let c = f(v).await;
out.push(c.to_string());
}
out.into_iter().collect::<Vec<_>>().join("")
}
}
which allows the following :
{data.map_fragment(|item| async { html! {
<CustomElement/>
}}).await}
It moves data, and a bit awkward with the bracketing and .await but works none the less
errors with
'await' is only allowed inside 'async' functions and blocksI am experimenting with the following trait and impl
which allows the following :
It moves data, and a bit awkward with the bracketing and .await but works none the less