From e2ef293d19dfd00731b708418558102890d92fb0 Mon Sep 17 00:00:00 2001 From: Jose Quesada Date: Mon, 12 Dec 2022 13:39:41 -0600 Subject: [PATCH] added `IntoFragment` trait for all `IntoIterator` and also impl `FromIterator` for `Fragment` --- leptos_dom/src/components/fragment.rs | 30 +++++++++++++++++++++++++++ leptos_dom/src/lib.rs | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/leptos_dom/src/components/fragment.rs b/leptos_dom/src/components/fragment.rs index b606fea9c..8d821bf73 100644 --- a/leptos_dom/src/components/fragment.rs +++ b/leptos_dom/src/components/fragment.rs @@ -1,9 +1,39 @@ +use leptos_reactive::Scope; + use crate::{ComponentRepr, IntoView, View}; +/// Trait for converting any iterable into a [`Fragment`]. +pub trait IntoFragment { + /// Consumes this type, returning [`Fragment`]. + fn into_fragment(self, cx: Scope) -> Fragment; +} + +impl IntoFragment for I +where + I: IntoIterator, + V: IntoView, +{ + fn into_fragment(self, cx: Scope) -> Fragment { + self.into_iter().map(|v| v.into_view(cx)).collect() + } +} + /// Represents a group of [`views`](View). #[derive(Debug, Clone)] pub struct Fragment(Vec); +impl FromIterator for Fragment { + fn from_iter>(iter: T) -> Self { + Fragment::new(iter.into_iter().collect()) + } +} + +impl From for Fragment { + fn from(view: View) -> Self { + Fragment::new(vec![view]) + } +} + impl Fragment { /// Creates a new [`Fragment`] from a [`Vec`]. pub fn new(nodes: Vec) -> Self { diff --git a/leptos_dom/src/lib.rs b/leptos_dom/src/lib.rs index 5cd406198..f900ef788 100644 --- a/leptos_dom/src/lib.rs +++ b/leptos_dom/src/lib.rs @@ -24,8 +24,8 @@ pub use components::*; pub use events::typed as ev; pub use helpers::*; pub use html::*; -pub use js_sys; use hydration::HydrationCtx; +pub use js_sys; use leptos_reactive::Scope; pub use logging::*; pub use node_ref::*;