Skip to content

Commit

Permalink
RenderHtml::into_owned (#3580)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke authored Feb 10, 2025
1 parent c2289b2 commit b4e683d
Show file tree
Hide file tree
Showing 29 changed files with 317 additions and 36 deletions.
17 changes: 14 additions & 3 deletions leptos/src/attribute_interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn AttributeInterceptor<Chil, T>(
) -> impl IntoView
where
Chil: Fn(AnyAttribute) -> T + Send + Sync + 'static,
T: IntoView,
T: IntoView + 'static,
{
AttributeInterceptorInner::new(children)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<T: IntoView, A: Attribute> Render for AttributeInterceptorInner<T, A> {
}
}

impl<T: IntoView, A> AddAnyAttr for AttributeInterceptorInner<T, A>
impl<T: IntoView + 'static, A> AddAnyAttr for AttributeInterceptorInner<T, A>
where
A: Attribute,
{
Expand Down Expand Up @@ -114,8 +114,11 @@ where
}
}

impl<T: IntoView, A: Attribute> RenderHtml for AttributeInterceptorInner<T, A> {
impl<T: IntoView + 'static, A: Attribute> RenderHtml
for AttributeInterceptorInner<T, A>
{
type AsyncOutput = T::AsyncOutput;
type Owned = AttributeInterceptorInner<T, A::CloneableOwned>;

const MIN_LENGTH: usize = T::MIN_LENGTH;

Expand Down Expand Up @@ -153,4 +156,12 @@ impl<T: IntoView, A: Attribute> RenderHtml for AttributeInterceptorInner<T, A> {
) -> Self::State {
self.children.hydrate::<FROM_SERVER>(cursor, position)
}

fn into_owned(self) -> Self::Owned {
AttributeInterceptorInner {
children_builder: self.children_builder,
children: self.children,
attributes: self.attributes.into_cloneable_owned(),
}
}
}
5 changes: 5 additions & 0 deletions leptos/src/error_boundary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ where
Fal: RenderHtml + Send + 'static,
{
type AsyncOutput = ErrorBoundaryView<Chil::AsyncOutput, FalFn>;
type Owned = Self;

const MIN_LENGTH: usize = Chil::MIN_LENGTH;

Expand Down Expand Up @@ -437,6 +438,10 @@ where
},
)
}

fn into_owned(self) -> Self::Owned {
self
}
}

#[derive(Debug)]
Expand Down
9 changes: 9 additions & 0 deletions leptos/src/into_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl<T: Render> Render for View<T> {

impl<T: RenderHtml> RenderHtml for View<T> {
type AsyncOutput = T::AsyncOutput;
type Owned = View<T::Owned>;

const MIN_LENGTH: usize = <T as RenderHtml>::MIN_LENGTH;

Expand Down Expand Up @@ -165,6 +166,14 @@ impl<T: RenderHtml> RenderHtml for View<T> {
) -> Self::State {
self.inner.hydrate::<FROM_SERVER>(cursor, position)
}

fn into_owned(self) -> Self::Owned {
View {
inner: self.inner.into_owned(),
#[cfg(debug_assertions)]
view_marker: self.view_marker,
}
}
}

impl<T: ToTemplate> ToTemplate for View<T> {
Expand Down
10 changes: 10 additions & 0 deletions leptos/src/suspense_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ where
// i.e., if this is the child of another Suspense during SSR, don't wait for it: it will handle
// itself
type AsyncOutput = Self;
type Owned = Self;

const MIN_LENGTH: usize = Chil::MIN_LENGTH;

Expand Down Expand Up @@ -473,6 +474,10 @@ where
}
})
}

fn into_owned(self) -> Self::Owned {
self
}
}

/// A wrapper that prevents [`Suspense`] from waiting for any resource reads that happen inside
Expand Down Expand Up @@ -525,6 +530,7 @@ where
T: RenderHtml + 'static,
{
type AsyncOutput = Self;
type Owned = Self;

const MIN_LENGTH: usize = T::MIN_LENGTH;

Expand Down Expand Up @@ -577,4 +583,8 @@ where
) -> Self::State {
(self.0)().hydrate::<FROM_SERVER>(cursor, position)
}

fn into_owned(self) -> Self::Owned {
self
}
}
5 changes: 5 additions & 0 deletions leptos_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ mod view_implementations {
Ser: Send + 'static,
{
type AsyncOutput = Option<T>;
type Owned = Self;

const MIN_LENGTH: usize = 0;

Expand Down Expand Up @@ -191,5 +192,9 @@ mod view_implementations {
(move || Suspend::new(async move { self.await }))
.hydrate::<FROM_SERVER>(cursor, position)
}

fn into_owned(self) -> Self::Owned {
self
}
}
}
7 changes: 7 additions & 0 deletions meta/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ where
At: Attribute,
{
type AsyncOutput = BodyView<At::AsyncOutput>;
type Owned = BodyView<At::CloneableOwned>;

const MIN_LENGTH: usize = At::MIN_LENGTH;

Expand Down Expand Up @@ -146,6 +147,12 @@ where

BodyViewState { attributes }
}

fn into_owned(self) -> Self::Owned {
BodyView {
attributes: self.attributes.into_cloneable_owned(),
}
}
}

impl<At> Mountable for BodyViewState<At>
Expand Down
7 changes: 7 additions & 0 deletions meta/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ where
At: Attribute,
{
type AsyncOutput = HtmlView<At::AsyncOutput>;
type Owned = HtmlView<At::CloneableOwned>;

const MIN_LENGTH: usize = At::MIN_LENGTH;

Expand Down Expand Up @@ -149,6 +150,12 @@ where

HtmlViewState { attributes }
}

fn into_owned(self) -> Self::Owned {
HtmlView {
attributes: self.attributes.into_cloneable_owned(),
}
}
}

impl<At> Mountable for HtmlViewState<At>
Expand Down
12 changes: 12 additions & 0 deletions meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ where
Ch: RenderHtml + Send,
{
type AsyncOutput = Self;
type Owned = RegisteredMetaTag<E, At::CloneableOwned, Ch::Owned>;

const MIN_LENGTH: usize = 0;

Expand Down Expand Up @@ -470,6 +471,12 @@ where
);
RegisteredMetaTagState { state }
}

fn into_owned(self) -> Self::Owned {
RegisteredMetaTag {
el: self.el.map(|inner| inner.into_owned()),
}
}
}

impl<E, At, Ch> Mountable for RegisteredMetaTagState<E, At, Ch>
Expand Down Expand Up @@ -547,6 +554,7 @@ impl AddAnyAttr for MetaTagsView {

impl RenderHtml for MetaTagsView {
type AsyncOutput = Self;
type Owned = Self;

const MIN_LENGTH: usize = 0;

Expand All @@ -573,6 +581,10 @@ impl RenderHtml for MetaTagsView {
_position: &PositionState,
) -> Self::State {
}

fn into_owned(self) -> Self::Owned {
self
}
}

pub(crate) trait OrDefaultNonce {
Expand Down
5 changes: 5 additions & 0 deletions meta/src/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl AddAnyAttr for TitleView {

impl RenderHtml for TitleView {
type AsyncOutput = Self;
type Owned = Self;

const MIN_LENGTH: usize = 0;

Expand Down Expand Up @@ -283,6 +284,10 @@ impl RenderHtml for TitleView {
});
TitleViewState { effect }
}

fn into_owned(self) -> Self::Owned {
self
}
}

impl Mountable for TitleViewState {
Expand Down
9 changes: 7 additions & 2 deletions router/src/flat_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl<Loc, Defs, FalFn, Fal> AddAnyAttr for FlatRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider + Send,
Defs: MatchNestedRoutes + Send + 'static,
FalFn: FnOnce() -> Fal + Send,
FalFn: FnOnce() -> Fal + Send + 'static,
Fal: RenderHtml + 'static,
{
type Output<SomeNewAttr: leptos::attr::Attribute> =
Expand Down Expand Up @@ -421,10 +421,11 @@ impl<Loc, Defs, FalFn, Fal> RenderHtml for FlatRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider + Send,
Defs: MatchNestedRoutes + Send + 'static,
FalFn: FnOnce() -> Fal + Send,
FalFn: FnOnce() -> Fal + Send + 'static,
Fal: RenderHtml + 'static,
{
type AsyncOutput = Self;
type Owned = Self;

const MIN_LENGTH: usize = <Either<Fal, AnyView> as RenderHtml>::MIN_LENGTH;

Expand Down Expand Up @@ -618,4 +619,8 @@ where
}
}
}

fn into_owned(self) -> Self::Owned {
self
}
}
13 changes: 9 additions & 4 deletions router/src/nested_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ where
impl<Loc, Defs, Fal, FalFn> AddAnyAttr for NestedRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider + Send,
Defs: MatchNestedRoutes + Send,
FalFn: FnOnce() -> Fal + Send,
Defs: MatchNestedRoutes + Send + 'static,
FalFn: FnOnce() -> Fal + Send + 'static,
Fal: RenderHtml + 'static,
{
type Output<SomeNewAttr: leptos::attr::Attribute> =
Expand All @@ -249,11 +249,12 @@ where
impl<Loc, Defs, FalFn, Fal> RenderHtml for NestedRoutesView<Loc, Defs, FalFn>
where
Loc: LocationProvider + Send,
Defs: MatchNestedRoutes + Send,
FalFn: FnOnce() -> Fal + Send,
Defs: MatchNestedRoutes + Send + 'static,
FalFn: FnOnce() -> Fal + Send + 'static,
Fal: RenderHtml + 'static,
{
type AsyncOutput = Self;
type Owned = Self;

const MIN_LENGTH: usize = 0; // TODO

Expand Down Expand Up @@ -465,6 +466,10 @@ where
view,
}
}

fn into_owned(self) -> Self::Owned {
self
}
}

type OutletViewFn = Box<dyn Fn() -> Suspend<AnyView> + Send>;
Expand Down
11 changes: 11 additions & 0 deletions tachys/src/html/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ where
Ch: RenderHtml + Send,
{
type AsyncOutput = HtmlElement<E, At::AsyncOutput, Ch::AsyncOutput>;
type Owned = HtmlElement<E, At::CloneableOwned, Ch::Owned>;

const MIN_LENGTH: usize = if E::SELF_CLOSING {
3 // < ... />
Expand Down Expand Up @@ -406,6 +407,16 @@ where
children,
}
}

fn into_owned(self) -> Self::Owned {
HtmlElement {
#[cfg(any(debug_assertions, leptos_debuginfo))]
defined_at: self.defined_at,
tag: self.tag,
attributes: self.attributes.into_cloneable_owned(),
children: self.children.into_owned(),
}
}
}

/// Renders an [`Attribute`] (which can be one or more HTML attributes) into an HTML buffer.
Expand Down
17 changes: 17 additions & 0 deletions tachys/src/html/islands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ where
View: RenderHtml,
{
type AsyncOutput = Island<View::AsyncOutput>;
type Owned = Island<View::Owned>;

const MIN_LENGTH: usize = ISLAND_TAG.len() * 2
+ "<>".len()
Expand Down Expand Up @@ -187,6 +188,14 @@ where

self.view.hydrate::<FROM_SERVER>(cursor, position)
}

fn into_owned(self) -> Self::Owned {
Island {
component: self.component,
props_json: self.props_json,
view: self.view.into_owned(),
}
}
}

/// The children that will be projected into an [`Island`].
Expand Down Expand Up @@ -267,6 +276,7 @@ where
View: RenderHtml,
{
type AsyncOutput = IslandChildren<View::AsyncOutput>;
type Owned = IslandChildren<View::Owned>;

const MIN_LENGTH: usize = ISLAND_CHILDREN_TAG.len() * 2
+ "<>".len()
Expand Down Expand Up @@ -372,4 +382,11 @@ where
);
}
}

fn into_owned(self) -> Self::Owned {
IslandChildren {
view: self.view.into_owned(),
on_hydrate: self.on_hydrate,
}
}
}
Loading

0 comments on commit b4e683d

Please sign in to comment.