From 9d0d863cfa13e86d7c21dd1e456f477d037f4b81 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Wed, 8 Jan 2025 13:49:30 -0500 Subject: [PATCH] another try at adding AddAnyAttr to AnyView --- tachys/src/view/any_view.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tachys/src/view/any_view.rs b/tachys/src/view/any_view.rs index 8a82555f30..d226b49358 100644 --- a/tachys/src/view/any_view.rs +++ b/tachys/src/view/any_view.rs @@ -5,7 +5,12 @@ use super::{ RenderHtml, }; use crate::{ - html::attribute::Attribute, hydration::Cursor, ssr::StreamBuilder, + html::attribute::{ + any_attribute::{AnyAttribute, IntoAnyAttribute}, + Attribute, + }, + hydration::Cursor, + ssr::StreamBuilder, }; use std::{ any::{Any, TypeId}, @@ -29,6 +34,7 @@ pub struct AnyView { value: Box, build: fn(Box) -> AnyViewState, rebuild: fn(TypeId, Box, &mut AnyViewState), + add_any_attr: fn(Box, AnyAttribute) -> AnyView, // The fields below are cfg-gated so they will not be included in WASM bundles if not needed. // Ordinarily, the compiler can simply omit this dead code because the methods are not called. // With this type-erased wrapper, however, the compiler is not *always* able to correctly @@ -130,7 +136,6 @@ where { // inlining allows the compiler to remove the unused functions // i.e., doesn't ship HTML-generating code that isn't used - #[inline(always)] fn into_any(self) -> AnyView { #[cfg(feature = "ssr")] let html_len = self.html_len(); @@ -282,11 +287,19 @@ where } }; + let add_any_attr = |value: Box, attr: AnyAttribute| { + let value = value + .downcast::() + .expect("AnyView::add_any_attr could not be downcast"); + value.add_any_attr(attr).into_any() + }; + AnyView { type_id: TypeId::of::(), value, build, rebuild, + add_any_attr, #[cfg(feature = "ssr")] resolve, #[cfg(feature = "ssr")] @@ -324,12 +337,13 @@ impl AddAnyAttr for AnyView { fn add_any_attr( self, - _attr: NewAttr, + attr: NewAttr, ) -> Self::Output where Self::Output: RenderHtml, { - self + let attr = attr.into_cloneable_owned(); + (self.add_any_attr)(self.value, attr.into_any_attr()) } }