From a2bef05a4b8f9a5e148c94817457f6d101102f3b Mon Sep 17 00:00:00 2001 From: Ari Seyhun Date: Sat, 28 Oct 2023 04:12:27 +1030 Subject: [PATCH] perf: `IntoView` and `IntoAttribute` for `std::fmt::Arguments` improvements (#1947) * fix: use static str when possible in `std::fmt::Arguments` in views * feat: impl `IntoAttribute` for `std::fmt::Arguments` --- leptos_dom/src/lib.rs | 14 +++++++++++++- leptos_dom/src/macro_helpers/into_attribute.rs | 11 +++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/leptos_dom/src/lib.rs b/leptos_dom/src/lib.rs index 5ee56ba3b..105312609 100644 --- a/leptos_dom/src/lib.rs +++ b/leptos_dom/src/lib.rs @@ -1173,6 +1173,19 @@ where } } +impl IntoView for std::fmt::Arguments<'_> { + #[cfg_attr( + any(debug_assertions, feature = "ssr"), + instrument(level = "info", name = "#text", skip_all) + )] + fn into_view(self) -> View { + match self.as_str() { + Some(s) => s.into_view(), + None => self.to_string().into_view(), + } + } +} + macro_rules! viewable_primitive { ($($child_type:ty),* $(,)?) => { $( @@ -1226,7 +1239,6 @@ viewable_primitive![ std::num::NonZeroIsize, std::num::NonZeroUsize, std::panic::Location<'_>, - std::fmt::Arguments<'_>, ]; cfg_if! { diff --git a/leptos_dom/src/macro_helpers/into_attribute.rs b/leptos_dom/src/macro_helpers/into_attribute.rs index 28307b270..34ca99da5 100644 --- a/leptos_dom/src/macro_helpers/into_attribute.rs +++ b/leptos_dom/src/macro_helpers/into_attribute.rs @@ -261,6 +261,17 @@ impl IntoAttribute for Option> { impl_into_attr_boxed! {} } +impl IntoAttribute for std::fmt::Arguments<'_> { + fn into_attribute(self) -> Attribute { + match self.as_str() { + Some(s) => s.into_attribute(), + None => self.to_string().into_attribute(), + } + } + + impl_into_attr_boxed! {} +} + /* impl IntoAttribute for Box { #[inline(always)] fn into_attribute(self) -> Attribute {