Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddAnyAttr experimentation on AnyView #3461

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 74 additions & 18 deletions tachys/src/view/any_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
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},
Expand All @@ -14,6 +19,43 @@
#[cfg(feature = "ssr")]
use std::{future::Future, pin::Pin};

// Minimizes deep generics via a single dynamic dispatch
trait DynValueAttr: DynValue {
fn apply_attr(self: Box<Self>, attr: AnyAttribute) -> AnyView;
}

trait DynValue: Send {
fn dyn_add_any_attr(self: Box<Self>, attr: AnyAttribute) -> AnyView;

fn dyn_any(self: Box<Self>) -> Box<dyn Any + Send>;
}

impl<T> DynValueAttr for T
where
T: Send + RenderHtml + 'static,
T::State: 'static,
{
fn apply_attr(self: Box<Self>, attr: AnyAttribute) -> AnyView {
self.add_any_attr(attr).into_any()
}
}

impl<T> DynValue for T
where
T: Send,
T: RenderHtml + 'static,
T::State: 'static,
{
#[inline(never)]
fn dyn_add_any_attr(self: Box<Self>, attr: AnyAttribute) -> AnyView {
DynValueAttr::apply_attr(self, attr)
}

fn dyn_any(self: Box<Self>) -> Box<dyn Any + Send> {
self
}
}

/// A type-erased view. This can be used if control flow requires that multiple different types of
/// view must be received, and it is either impossible or too cumbersome to use the `EitherOf___`
/// enums.
Expand All @@ -26,7 +68,7 @@
/// amount of type information possible.
pub struct AnyView {
type_id: TypeId,
value: Box<dyn Any + Send>,
value: Box<dyn DynValue>,
build: fn(Box<dyn Any>) -> AnyViewState,
rebuild: fn(TypeId, Box<dyn Any>, &mut AnyViewState),
// The fields below are cfg-gated so they will not be included in WASM bundles if not needed.
Expand All @@ -47,7 +89,7 @@
#[allow(clippy::type_complexity)]
resolve: fn(Box<dyn Any>) -> Pin<Box<dyn Future<Output = AnyView> + Send>>,
#[cfg(feature = "ssr")]
dry_resolve: fn(&mut Box<dyn Any + Send>),

Check warning on line 92 in tachys/src/view/any_view.rs

View workflow job for this annotation

GitHub Actions / autofix

field `dry_resolve` is never read
#[cfg(feature = "hydrate")]
#[cfg(feature = "hydrate")]
#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -130,7 +172,6 @@
{
// 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();
Expand Down Expand Up @@ -282,6 +323,10 @@
}
};

let value = value
.downcast::<T>()
.expect("AnyView::into_any couldn't downcast value");

AnyView {
type_id: TypeId::of::<T>(),
value,
Expand Down Expand Up @@ -311,37 +356,42 @@
type State = AnyViewState;

fn build(self) -> Self::State {
(self.build)(self.value)
(self.build)(self.value.dyn_any())
}

fn rebuild(self, state: &mut Self::State) {
(self.rebuild)(self.type_id, self.value, state)
(self.rebuild)(self.type_id, self.value.dyn_any(), state)
}
}

// Pre-erases output to reduce compile-time explosions
impl AddAnyAttr for AnyView {
type Output<SomeNewAttr: Attribute> = Self;
type Output<SomeNewAttr: Attribute> = AnyView;

#[inline(never)]
fn add_any_attr<NewAttr: Attribute>(
self,
_attr: NewAttr,
attr: NewAttr,
) -> Self::Output<NewAttr>
where
Self::Output<NewAttr>: RenderHtml,
{
self
let attr = attr.into_cloneable_owned();
let any_attr = attr.into_any_attr();
self.value.dyn_add_any_attr(any_attr)
}
}

impl RenderHtml for AnyView {
type AsyncOutput = Self;

fn dry_resolve(&mut self) {
#[cfg(feature = "ssr")]
{
(self.dry_resolve)(&mut self.value)
}
#[cfg(not(feature = "ssr"))]
// Just disabled to not have to fix:
// #[cfg(feature = "ssr")]
// {
// (self.dry_resolve)(&mut self.value.dyn_any())
// }
// #[cfg(not(feature = "ssr"))]
panic!(
"You are rendering AnyView to HTML without the `ssr` feature \
enabled."
Expand All @@ -351,7 +401,7 @@
async fn resolve(self) -> Self::AsyncOutput {
#[cfg(feature = "ssr")]
{
(self.resolve)(self.value).await
(self.resolve)(self.value.dyn_any()).await
}
#[cfg(not(feature = "ssr"))]
panic!(
Expand All @@ -370,7 +420,13 @@
mark_branches: bool,
) {
#[cfg(feature = "ssr")]
(self.to_html)(self.value, buf, position, escape, mark_branches);
(self.to_html)(
self.value.dyn_any(),
buf,
position,
escape,
mark_branches,
);
#[cfg(not(feature = "ssr"))]
{
_ = mark_branches;
Expand All @@ -396,15 +452,15 @@
#[cfg(feature = "ssr")]
if OUT_OF_ORDER {
(self.to_html_async_ooo)(
self.value,
self.value.dyn_any(),
buf,
position,
escape,
mark_branches,
);
} else {
(self.to_html_async)(
self.value,
self.value.dyn_any(),
buf,
position,
escape,
Expand All @@ -431,7 +487,7 @@
) -> Self::State {
#[cfg(feature = "hydrate")]
if FROM_SERVER {
(self.hydrate_from_server)(self.value, cursor, position)
(self.hydrate_from_server)(self.value.dyn_any(), cursor, position)
} else {
panic!(
"hydrating AnyView from inside a ViewTemplate is not \
Expand Down
Loading