Skip to content

map_fragment and collect_fragment don't work with custom element children #13

@cameronbraid

Description

@cameronbraid
{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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions