From 9f595171ce34db3896c219a45431b26f4a5ccb50 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Tue, 16 Jan 2024 16:09:59 -0800 Subject: [PATCH] Remove boxedanyprops abstraction --- packages/core/src/any_props.rs | 36 ++++------------------------- packages/core/src/diff/component.rs | 4 ++-- packages/core/src/nodes.rs | 2 +- packages/core/src/virtual_dom.rs | 4 ++-- 4 files changed, 9 insertions(+), 37 deletions(-) diff --git a/packages/core/src/any_props.rs b/packages/core/src/any_props.rs index 1af7bb5b6..73c468fa3 100644 --- a/packages/core/src/any_props.rs +++ b/packages/core/src/any_props.rs @@ -1,41 +1,14 @@ use crate::{nodes::RenderReturn, Component}; -use std::{any::Any, ops::Deref, panic::AssertUnwindSafe}; +use std::{any::Any, panic::AssertUnwindSafe}; -/// A boxed version of AnyProps that can be cloned -pub(crate) struct BoxedAnyProps { - inner: Box, -} - -impl BoxedAnyProps { - pub fn new(inner: impl AnyProps + 'static) -> Self { - Self { - inner: Box::new(inner), - } - } -} - -impl Deref for BoxedAnyProps { - type Target = dyn AnyProps; - - fn deref(&self) -> &Self::Target { - &*self.inner - } -} - -impl Clone for BoxedAnyProps { - fn clone(&self) -> Self { - Self { - inner: self.inner.duplicate(), - } - } -} +pub type BoxedAnyProps = Box; /// A trait that essentially allows VComponentProps to be used generically pub(crate) trait AnyProps { fn render(&self) -> RenderReturn; fn memoize(&self, other: &dyn Any) -> bool; fn props(&self) -> &dyn Any; - fn duplicate(&self) -> Box; + fn duplicate(&self) -> BoxedAnyProps; } pub(crate) struct VProps { @@ -75,7 +48,6 @@ impl AnyProps for VProps

{ fn render(&self) -> RenderReturn { let res = std::panic::catch_unwind(AssertUnwindSafe(move || { - // Call the render function directly (self.render_fn)(self.props.clone()) })); @@ -90,7 +62,7 @@ impl AnyProps for VProps

{ } } - fn duplicate(&self) -> Box { + fn duplicate(&self) -> BoxedAnyProps { Box::new(Self { render_fn: self.render_fn.clone(), memo: self.memo, diff --git a/packages/core/src/diff/component.rs b/packages/core/src/diff/component.rs index 047951dfa..19ac6eb9a 100644 --- a/packages/core/src/diff/component.rs +++ b/packages/core/src/diff/component.rs @@ -88,7 +88,7 @@ impl VNode { } // First, move over the props from the old to the new, dropping old props in the process - dom.scopes[scope_id.0].props = new.props.clone(); + dom.scopes[scope_id.0].props = new.props.duplicate(); // Now run the component and diff it let new = dom.run_scope(scope_id); @@ -129,7 +129,7 @@ impl VNode { ) -> usize { // Load up a ScopeId for this vcomponent. If it's already mounted, then we can just use that let scope = dom - .new_scope(component.props.clone(), component.name) + .new_scope(component.props.duplicate(), component.name) .context() .id; diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index cbe0011b3..4de53955c 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -549,7 +549,7 @@ impl VComponent { VComponent { name: fn_name, - props: BoxedAnyProps::new(vcomp), + props: Box::new(vcomp), render_fn, } } diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index d5becf10d..3f1aeb94a 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -3,7 +3,7 @@ //! This module provides the primary mechanics to create a hook-based, concurrent VDOM for Rust. use crate::{ - any_props::{BoxedAnyProps, VProps}, + any_props::VProps, arena::ElementId, innerlude::{ DirtyScope, ElementRef, ErrorBoundary, NoOpMutations, SchedulerMsg, ScopeState, VNodeMount, @@ -287,7 +287,7 @@ impl VirtualDom { }; let root = dom.new_scope( - BoxedAnyProps::new(VProps::new( + Box::new(VProps::new( Rc::new(root).as_component(), |_, _| true, root_props,