Remove boxedanyprops abstraction

This commit is contained in:
Jonathan Kelley committed 2024-01-16 16:09:59 -08:00
1 parent fb4f97406c
commit 9f595171ce
4 files changed
+9 -37

No files matched your search

+4 -32
View File
@@ -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<dyn AnyProps>,
}
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<dyn AnyProps>;
/// 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<dyn AnyProps>;
fn duplicate(&self) -> BoxedAnyProps;
}
pub(crate) struct VProps<P: 'static> {
@@ -75,7 +48,6 @@ impl<P: Clone + 'static> AnyProps for VProps<P> {
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<P: Clone + 'static> AnyProps for VProps<P> {
}
}
fn duplicate(&self) -> Box<dyn AnyProps> {
fn duplicate(&self) -> BoxedAnyProps {
Box::new(Self {
render_fn: self.render_fn.clone(),
memo: self.memo,
+2 -2
View File
@@ -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;
+1 -1
View File
@@ -549,7 +549,7 @@ impl VComponent {
VComponent {
name: fn_name,
props: BoxedAnyProps::new(vcomp),
props: Box::new(vcomp),
render_fn,
}
}
+2 -2
View File
@@ -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,