pass clippy (#575)

This commit is contained in:
Demonthos authored and GitHub committed 2022-10-02 14:12:24 -07:00
1 parent 047ed1e553
commit daeffd4149
20 files changed
+259 -345

No files matched your search

+93 -92
View File
@@ -157,7 +157,7 @@ impl<'b> DiffState<'b> {
}
(Component(old), Component(new)) => {
self.diff_component_nodes(*old, *new, old_node, new_node);
self.diff_component_nodes(old, new, old_node, new_node);
}
(Fragment(old), Fragment(new)) => {
@@ -181,8 +181,8 @@ impl<'b> DiffState<'b> {
VNode::Placeholder(anchor) => self.create_anchor_node(anchor, node),
VNode::Element(element) => self.create_element_node(element, node),
VNode::Fragment(frag) => self.create_fragment_node(frag),
VNode::Component(component) => self.create_component_node(*component),
VNode::TemplateRef(temp) => self.create_template_ref_node(*temp, node),
VNode::Component(component) => self.create_component_node(component),
VNode::TemplateRef(temp) => self.create_template_ref_node(temp, node),
}
}
@@ -572,6 +572,7 @@ impl<'b> DiffState<'b> {
self.diff_children(old.children, new.children);
}
#[allow(clippy::too_many_lines)]
fn diff_template_ref_nodes(
&mut self,
old: &'b VTemplateRef<'b>,
@@ -579,6 +580,94 @@ impl<'b> DiffState<'b> {
old_node: &'b VNode<'b>,
new_node: &'b VNode<'b>,
) {
fn diff_attributes<'b, Nodes, Attributes, V, Children, Listeners, TextSegments, Text>(
nodes: &Nodes,
ctx: (
&mut Mutations<'b>,
&'b Bump,
&VTemplateRef<'b>,
&Template,
usize,
),
) where
Nodes: AsRef<[TemplateNode<Attributes, V, Children, Listeners, TextSegments, Text>]>,
Attributes: AsRef<[TemplateAttribute<V>]>,
V: TemplateValue,
Children: AsRef<[TemplateNodeId]>,
Listeners: AsRef<[usize]>,
TextSegments: AsRef<[TextTemplateSegment<Text>]>,
Text: AsRef<str>,
{
let (mutations, scope_bump, new, template, idx) = ctx;
for (node_id, attr_idx) in template.get_dynamic_nodes_for_attribute_index(idx) {
if let TemplateNodeType::Element(el) = &nodes.as_ref()[node_id.0].node_type {
let TemplateElement { attributes, .. } = el;
let attr = &attributes.as_ref()[*attr_idx];
let attribute = Attribute {
attribute: attr.attribute,
value: new.dynamic_context.resolve_attribute(idx).clone(),
is_static: false,
};
mutations.set_attribute(scope_bump.alloc(attribute), *node_id);
} else {
panic!("expected element node");
}
}
}
fn set_attribute<'b, Attributes, V, Children, Listeners, TextSegments, Text>(
node: &TemplateNode<Attributes, V, Children, Listeners, TextSegments, Text>,
ctx: (&mut Mutations<'b>, &'b Bump, &VTemplateRef<'b>, usize),
) where
Attributes: AsRef<[TemplateAttribute<V>]>,
V: TemplateValue,
Children: AsRef<[TemplateNodeId]>,
Listeners: AsRef<[usize]>,
TextSegments: AsRef<[TextTemplateSegment<Text>]>,
Text: AsRef<str>,
{
let (mutations, scope_bump, new, template_attr_idx) = ctx;
if let TemplateNodeType::Element(el) = &node.node_type {
let TemplateElement { attributes, .. } = el;
let attr = &attributes.as_ref()[template_attr_idx];
let value = match &attr.value {
TemplateAttributeValue::Dynamic(idx) => {
new.dynamic_context.resolve_attribute(*idx).clone()
}
TemplateAttributeValue::Static(value) => value.allocate(scope_bump),
};
let attribute = Attribute {
attribute: attr.attribute,
value,
is_static: false,
};
mutations.set_attribute(scope_bump.alloc(attribute), node.id);
} else {
panic!("expected element node");
}
}
fn diff_dynamic_node<'b, Attributes, V, Children, Listeners, TextSegments, Text>(
node: &TemplateNode<Attributes, V, Children, Listeners, TextSegments, Text>,
ctx: (&mut DiffState<'b>, &'b VNode<'b>, &'b VNode<'b>, ElementId),
) where
Attributes: AsRef<[TemplateAttribute<V>]>,
V: TemplateValue,
Children: AsRef<[TemplateNodeId]>,
Listeners: AsRef<[usize]>,
TextSegments: AsRef<[TextTemplateSegment<Text>]>,
Text: AsRef<str>,
{
let (diff, old_node, new_node, root) = ctx;
if let TemplateNodeType::Element { .. } = node.node_type {
diff.element_stack.push(GlobalNodeId::VNodeId(root));
diff.diff_node(old_node, new_node);
diff.element_stack.pop();
} else {
diff.diff_node(old_node, new_node);
}
}
if std::ptr::eq(old, new) {
return;
}
@@ -617,41 +706,6 @@ impl<'b> DiffState<'b> {
};
let template = template.borrow();
fn diff_attributes<'b, Nodes, Attributes, V, Children, Listeners, TextSegments, Text>(
nodes: &Nodes,
ctx: (
&mut Mutations<'b>,
&'b Bump,
&VTemplateRef<'b>,
&Template,
usize,
),
) where
Nodes: AsRef<[TemplateNode<Attributes, V, Children, Listeners, TextSegments, Text>]>,
Attributes: AsRef<[TemplateAttribute<V>]>,
V: TemplateValue,
Children: AsRef<[TemplateNodeId]>,
Listeners: AsRef<[usize]>,
TextSegments: AsRef<[TextTemplateSegment<Text>]>,
Text: AsRef<str>,
{
let (mutations, scope_bump, new, template, idx) = ctx;
for (node_id, attr_idx) in template.get_dynamic_nodes_for_attribute_index(idx) {
if let TemplateNodeType::Element(el) = &nodes.as_ref()[node_id.0].node_type {
let TemplateElement { attributes, .. } = el;
let attr = &attributes.as_ref()[*attr_idx];
let attribute = Attribute {
attribute: attr.attribute,
value: new.dynamic_context.resolve_attribute(idx).clone(),
is_static: false,
};
mutations.set_attribute(scope_bump.alloc(attribute), *node_id);
} else {
panic!("expected element node");
}
}
}
// diff dynamic attributes
for (idx, (old_attr, new_attr)) in old
.dynamic_context
@@ -669,38 +723,6 @@ impl<'b> DiffState<'b> {
}
}
fn set_attribute<'b, Attributes, V, Children, Listeners, TextSegments, Text>(
node: &TemplateNode<Attributes, V, Children, Listeners, TextSegments, Text>,
ctx: (&mut Mutations<'b>, &'b Bump, &VTemplateRef<'b>, usize),
) where
Attributes: AsRef<[TemplateAttribute<V>]>,
V: TemplateValue,
Children: AsRef<[TemplateNodeId]>,
Listeners: AsRef<[usize]>,
TextSegments: AsRef<[TextTemplateSegment<Text>]>,
Text: AsRef<str>,
{
let (mutations, scope_bump, new, template_attr_idx) = ctx;
if let TemplateNodeType::Element(el) = &node.node_type {
let TemplateElement { attributes, .. } = el;
let attr = &attributes.as_ref()[template_attr_idx];
let value = match &attr.value {
TemplateAttributeValue::Dynamic(idx) => {
new.dynamic_context.resolve_attribute(*idx).clone()
}
TemplateAttributeValue::Static(value) => value.allocate(scope_bump),
};
let attribute = Attribute {
attribute: attr.attribute,
value,
is_static: false,
};
mutations.set_attribute(scope_bump.alloc(attribute), node.id);
} else {
panic!("expected element node");
}
}
// set all volatile attributes
for (id, idx) in template.volatile_attributes() {
template.with_node(
@@ -708,28 +730,7 @@ impl<'b> DiffState<'b> {
set_attribute,
set_attribute,
(&mut self.mutations, scope_bump, new, idx),
)
}
fn diff_dynamic_node<'b, Attributes, V, Children, Listeners, TextSegments, Text>(
node: &TemplateNode<Attributes, V, Children, Listeners, TextSegments, Text>,
ctx: (&mut DiffState<'b>, &'b VNode<'b>, &'b VNode<'b>, ElementId),
) where
Attributes: AsRef<[TemplateAttribute<V>]>,
V: TemplateValue,
Children: AsRef<[TemplateNodeId]>,
Listeners: AsRef<[usize]>,
TextSegments: AsRef<[TextTemplateSegment<Text>]>,
Text: AsRef<str>,
{
let (diff, old_node, new_node, root) = ctx;
if let TemplateNodeType::Element { .. } = node.node_type {
diff.element_stack.push(GlobalNodeId::VNodeId(root));
diff.diff_node(old_node, new_node);
diff.element_stack.pop();
} else {
diff.diff_node(old_node, new_node);
}
);
}
// diff dynmaic nodes
@@ -26,7 +26,7 @@ impl<T> PartialEq for LazyStaticVec<T> {
/// Stores what nodes depend on specific dynamic parts of the template to allow the diffing algorithm to jump to that part of the template instead of travering it
/// This makes adding constant template nodes add no additional cost to diffing.
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct DynamicNodeMapping<
Nodes,
@@ -97,7 +97,7 @@ where
}
}
pub(crate) fn all_dynamic<'a>(&'a self) -> impl Iterator<Item = TemplateNodeId> + 'a {
pub(crate) fn all_dynamic(&self) -> impl Iterator<Item = TemplateNodeId> + '_ {
self.nodes
.as_ref()
.iter()
@@ -106,16 +106,14 @@ where
self.text
.as_ref()
.iter()
.map(|ids| ids.as_ref().iter())
.flatten(),
.flat_map(|ids| ids.as_ref().iter()),
)
.copied()
.chain(
self.attributes
.as_ref()
.iter()
.map(|ids| ids.as_ref().iter())
.flatten()
.flat_map(|ids| ids.as_ref().iter())
.map(|dynamic| dynamic.0),
)
.chain(self.nodes_with_listeners.as_ref().iter().copied())
+6 -6
View File
@@ -37,9 +37,9 @@ pub enum GlobalNodeId {
VNodeId(ElementId),
}
impl Into<GlobalNodeId> for ElementId {
fn into(self) -> GlobalNodeId {
GlobalNodeId::VNodeId(self)
impl From<ElementId> for GlobalNodeId {
fn from(id: ElementId) -> GlobalNodeId {
GlobalNodeId::VNodeId(id)
}
}
@@ -279,9 +279,9 @@ impl ElementId {
}
}
impl Into<u64> for ElementId {
fn into(self) -> u64 {
self.0 as u64
impl From<ElementId> for u64 {
fn from(el: ElementId) -> u64 {
el.0 as u64
}
}
+1 -1
View File
@@ -357,7 +357,7 @@ impl ScopeArena {
(
&nodes,
&template_ref.dynamic_context,
&event,
event,
&state,
template_ref_id,
),
+24 -32
View File
@@ -264,9 +264,9 @@ pub struct TemplateId(pub CodeLocation);
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct RendererTemplateId(pub usize);
impl Into<u64> for RendererTemplateId {
fn into(self) -> u64 {
self.0 as u64
impl From<RendererTemplateId> for u64 {
fn from(id: RendererTemplateId) -> u64 {
id.0 as u64
}
}
@@ -278,9 +278,9 @@ impl Into<u64> for RendererTemplateId {
#[cfg_attr(feature = "serialize", serde(transparent))]
pub struct TemplateNodeId(pub usize);
impl Into<u64> for TemplateNodeId {
fn into(self) -> u64 {
JS_MAX_INT / 2 + self.0 as u64
impl From<TemplateNodeId> for u64 {
fn from(id: TemplateNodeId) -> u64 {
JS_MAX_INT / 2 + id.0 as u64
}
}
@@ -434,7 +434,7 @@ impl Template {
mutations.append_children(children_created);
}
TemplateNodeType::Text(text) => {
let mut text_iter = text.segments.as_ref().into_iter();
let mut text_iter = text.segments.as_ref().iter();
if let (Some(TextTemplateSegment::Static(txt)), None) =
(text_iter.next(), text_iter.next())
{
@@ -550,23 +550,20 @@ impl Template {
}
}
pub(crate) fn get_dynamic_nodes_for_text_index<'a>(
&'a self,
idx: usize,
) -> &'a [TemplateNodeId] {
pub(crate) fn get_dynamic_nodes_for_text_index(&self, idx: usize) -> &[TemplateNodeId] {
match self {
Template::Static(s) => s.dynamic_mapping.text[idx].as_ref(),
Template::Static(s) => s.dynamic_mapping.text[idx],
#[cfg(any(feature = "hot-reload", debug_assertions))]
Template::Owned(o) => o.dynamic_mapping.text[idx].as_ref(),
}
}
pub(crate) fn get_dynamic_nodes_for_attribute_index<'a>(
&'a self,
pub(crate) fn get_dynamic_nodes_for_attribute_index(
&self,
idx: usize,
) -> &'a [(TemplateNodeId, usize)] {
) -> &[(TemplateNodeId, usize)] {
match self {
Template::Static(s) => s.dynamic_mapping.attributes[idx].as_ref(),
Template::Static(s) => s.dynamic_mapping.attributes[idx],
#[cfg(any(feature = "hot-reload", debug_assertions))]
Template::Owned(o) => o.dynamic_mapping.attributes[idx].as_ref(),
}
@@ -610,7 +607,7 @@ pub type OwnedRootNodes = Vec<TemplateNodeId>;
/// Templates can only contain a limited subset of VNodes and keys are not needed, as diffing will be skipped.
/// Dynamic parts of the Template are inserted into the VNode using the `TemplateContext` by traversing the tree in order and filling in dynamic parts
/// This template node is generic over the storage of the nodes to allow for owned and &'static versions.
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
all(feature = "serialize", any(feature = "hot-reload", debug_assertions)),
derive(serde::Serialize, serde::Deserialize)
@@ -697,10 +694,9 @@ where
.set_text(scope_bump.alloc(new_text), self.id)
}
TemplateNodeType::DynamicNode(idx) => {
drop(self);
// this will only be triggered for root elements
let created =
diff_state.create_node(&template_ref.dynamic_context.resolve_node(*idx));
diff_state.create_node(template_ref.dynamic_context.resolve_node(*idx));
diff_state.mutations.replace_with(self.id, created as u32);
}
}
@@ -710,7 +706,7 @@ where
}
/// A template for an attribute
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
all(feature = "serialize", any(feature = "hot-reload", debug_assertions)),
derive(serde::Serialize, serde::Deserialize)
@@ -723,7 +719,7 @@ pub struct TemplateAttribute<V: TemplateValue> {
}
/// A template attribute value that is either dynamic or static
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
all(feature = "serialize", any(feature = "hot-reload", debug_assertions)),
derive(serde::Serialize, serde::Deserialize)
@@ -744,9 +740,9 @@ pub trait TemplateValue {
impl TemplateValue for StaticAttributeValue {
fn allocate<'b>(&self, bump: &'b Bump) -> AttributeValue<'b> {
match self.clone() {
StaticAttributeValue::Text(txt) => AttributeValue::Text(bump.alloc_str(&txt)),
StaticAttributeValue::Text(txt) => AttributeValue::Text(bump.alloc_str(txt)),
StaticAttributeValue::Bytes(bytes) => {
AttributeValue::Bytes(bump.alloc_slice_copy(&bytes))
AttributeValue::Bytes(bump.alloc_slice_copy(bytes))
}
StaticAttributeValue::Float32(f) => AttributeValue::Float32(f),
StaticAttributeValue::Float64(f) => AttributeValue::Float64(f),
@@ -799,7 +795,7 @@ impl TemplateValue for OwnedAttributeValue {
}
/// The kind of node the template is.
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
all(feature = "serialize", any(feature = "hot-reload", debug_assertions)),
derive(serde::Serialize, serde::Deserialize)
@@ -867,7 +863,7 @@ where
type StaticStr = &'static str;
/// A element template
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
all(feature = "serialize", any(feature = "hot-reload", debug_assertions)),
derive(serde::Serialize, serde::Deserialize)
@@ -931,7 +927,7 @@ where
}
/// A template for some text that may contain dynamic segments for example "Hello {name}" contains the static segment "Hello " and the dynamic segment "{name}".
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
all(feature = "serialize", any(feature = "hot-reload", debug_assertions)),
derive(serde::Serialize, serde::Deserialize)
@@ -961,7 +957,7 @@ where
}
/// A segment of a text template that may be dynamic or static.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
all(feature = "serialize", any(feature = "hot-reload", debug_assertions)),
derive(serde::Serialize, serde::Deserialize)
@@ -1019,11 +1015,7 @@ impl TemplateResolver {
}
pub fn is_dirty(&self, id: &TemplateId) -> bool {
if let Some((_, true)) = self.template_id_mapping.get(id) {
true
} else {
false
}
matches!(self.template_id_mapping.get(id), Some((_, true)))
}
// returns (id, if the id was created)
+1 -1
View File
@@ -727,7 +727,7 @@ impl VirtualDom {
.borrow()
.get(id)
.map(|inner| {
let borrow = &*inner;
let borrow = inner;
f(&borrow.borrow())
})
.unwrap()