From 5e2f9e322fbb886c51b0baf8c0653acd56329d48 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 14 Sep 2022 23:22:38 +0200 Subject: [PATCH 1/6] mbe: Return Bindings from build_inner --- crates/mbe/src/expander/matcher.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index c1aa14d6b7e74..0d970504da479 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -203,12 +203,11 @@ impl BindingsBuilder { } fn build(self, idx: &BindingsIdx) -> Bindings { - let mut bindings = Bindings::default(); - self.build_inner(&mut bindings, &self.nodes[idx.0]); - bindings + self.build_inner(&self.nodes[idx.0]) } - fn build_inner(&self, bindings: &mut Bindings, link_nodes: &[LinkNode>]) { + fn build_inner(&self, link_nodes: &[LinkNode>]) -> Bindings { + let mut bindings = Bindings::default(); let mut nodes = Vec::new(); self.collect_nodes(link_nodes, &mut nodes); @@ -246,6 +245,8 @@ impl BindingsBuilder { } } } + + bindings } fn collect_nested_ref<'a>( @@ -270,8 +271,7 @@ impl BindingsBuilder { nested_refs.push(last); nested_refs.into_iter().for_each(|iter| { - let mut child_bindings = Bindings::default(); - self.build_inner(&mut child_bindings, iter); + let child_bindings = self.build_inner(iter); nested.push(child_bindings) }) } From f7f6d2870fb930a67fe5622704953822521ddeb9 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 14 Sep 2022 23:26:24 +0200 Subject: [PATCH 2/6] mbe: Use extend instead of push in loop --- crates/mbe/src/expander/matcher.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index 0d970504da479..05b8636b8ccbe 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -269,11 +269,7 @@ impl BindingsBuilder { LinkNode::Parent { idx, len } => self.collect_nested_ref(idx, len, &mut nested_refs), }); nested_refs.push(last); - - nested_refs.into_iter().for_each(|iter| { - let child_bindings = self.build_inner(iter); - nested.push(child_bindings) - }) + nested.extend(nested_refs.into_iter().map(|iter| self.build_inner(iter))); } fn collect_nodes_ref<'a>( From b6aed7914d8172937664caf089ebe723504413cc Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 14 Sep 2022 23:30:44 +0200 Subject: [PATCH 3/6] mbe: Remove double reference in container --- crates/mbe/src/expander/matcher.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index 05b8636b8ccbe..7157d8b4328a1 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -212,7 +212,7 @@ impl BindingsBuilder { self.collect_nodes(link_nodes, &mut nodes); for cmd in nodes { - match &**cmd { + match cmd { BindingKind::Empty(name) => { bindings.push_empty(name); } @@ -272,12 +272,7 @@ impl BindingsBuilder { nested.extend(nested_refs.into_iter().map(|iter| self.build_inner(iter))); } - fn collect_nodes_ref<'a>( - &'a self, - id: usize, - len: usize, - nodes: &mut Vec<&'a Rc>, - ) { + fn collect_nodes_ref<'a>(&'a self, id: usize, len: usize, nodes: &mut Vec<&'a BindingKind>) { self.nodes[id].iter().take(len).for_each(|it| match it { LinkNode::Node(it) => nodes.push(it), LinkNode::Parent { idx, len } => self.collect_nodes_ref(*idx, *len, nodes), @@ -287,7 +282,7 @@ impl BindingsBuilder { fn collect_nodes<'a>( &'a self, link_nodes: &'a [LinkNode>], - nodes: &mut Vec<&'a Rc>, + nodes: &mut Vec<&'a BindingKind>, ) { link_nodes.iter().for_each(|it| match it { LinkNode::Node(it) => nodes.push(it), From 54305545a5c67484f13eb659ed7900d6a1cd6aec Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 14 Sep 2022 23:35:12 +0200 Subject: [PATCH 4/6] mbe: Remove Vec reference in container --- crates/mbe/src/expander/matcher.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index 7157d8b4328a1..e9b0aff6619ce 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -253,7 +253,7 @@ impl BindingsBuilder { &'a self, id: usize, len: usize, - nested_refs: &mut Vec<&'a Vec>>>, + nested_refs: &mut Vec<&'a [LinkNode>]>, ) { self.nested[id].iter().take(len).for_each(|it| match it { LinkNode::Node(id) => nested_refs.push(&self.nodes[*id]), @@ -263,7 +263,7 @@ impl BindingsBuilder { fn collect_nested(&self, idx: usize, nested_idx: usize, nested: &mut Vec) { let last = &self.nodes[idx]; - let mut nested_refs = Vec::new(); + let mut nested_refs: Vec<&[_]> = Vec::new(); self.nested[nested_idx].iter().for_each(|it| match *it { LinkNode::Node(idx) => nested_refs.push(&self.nodes[idx]), LinkNode::Parent { idx, len } => self.collect_nested_ref(idx, len, &mut nested_refs), From d6f0fd04ee2b6afcb93912f7531fbf2957f93909 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 14 Sep 2022 23:42:11 +0200 Subject: [PATCH 5/6] mbe: Remove unnecessary reference to usize --- crates/mbe/src/expander/matcher.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index e9b0aff6619ce..73082802808c6 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -393,7 +393,7 @@ fn match_loop_inner<'t>( // Check if we need a separator. // We check the separator one by one - let sep_idx = *item.sep_parsed.as_ref().unwrap_or(&0); + let sep_idx = item.sep_parsed.unwrap_or(0); let sep_len = item.sep.as_ref().map_or(0, Separator::tt_count); if item.sep.is_some() && sep_idx != sep_len { let sep = item.sep.as_ref().unwrap(); From c4a87ee0cefb57640a9d5219d221e8578a1e6733 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 14 Sep 2022 23:49:08 +0200 Subject: [PATCH 6/6] mbe: Remove unneeded unwrap --- crates/mbe/src/expander/matcher.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index 73082802808c6..139a8cb8cbe58 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -377,10 +377,10 @@ fn match_loop_inner<'t>( let op = match item.dot.peek() { None => { // We are at or past the end of the matcher of `item`. - if item.up.is_some() { + if let Some(up) = &item.up { if item.sep_parsed.is_none() { // Get the `up` matcher - let mut new_pos = *item.up.clone().unwrap(); + let mut new_pos = (**up).clone(); new_pos.bindings = bindings_builder.copy(&new_pos.bindings); // Add matches from this repetition to the `matches` of `up` bindings_builder.push_nested(&mut new_pos.bindings, &item.bindings);