From 0bcc3d9dc6571d375cdf08e2f63af3579558d435 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 11 Dec 2022 00:39:35 -0500 Subject: [PATCH] Borrow the context instead of clone it for fewer reference count increments. --- src/parser/parser_with_context.rs | 2 +- src/parser/text_element_parser.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/parser/parser_with_context.rs b/src/parser/parser_with_context.rs index 4506d648..e5d60862 100644 --- a/src/parser/parser_with_context.rs +++ b/src/parser/parser_with_context.rs @@ -1,6 +1,6 @@ macro_rules! parser_with_context { ($target:ident) => { - move |context| move |i| $target(&context, i) + move |context| move |i| $target(context, i) }; } pub(crate) use parser_with_context; diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index b4404a5e..5ba26c54 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -107,7 +107,7 @@ where pub fn document(input: &str) -> Res<&str, Vec<(Vec, &str)>> { let initial_context: ContextTree<'_, '_> = ContextTree::new(); let paragraph_parser = parser_with_context!(paragraph); - let ret = many1(paragraph_parser(initial_context))(input); + let ret = many1(paragraph_parser(&initial_context))(input); ret } @@ -157,8 +157,8 @@ fn flat_text_element<'s, 'r>( ) -> Res<&'s str, TextElement<'s>> { not(|i| context.check_fail_matcher(i))(i)?; - let bold_matcher = parser_with_context!(flat_bold)(context.clone()); - let link_matcher = parser_with_context!(flat_link)(context.clone()); + let bold_matcher = parser_with_context!(flat_bold)(&context); + let link_matcher = parser_with_context!(flat_link)(&context); alt(( map(bold_matcher, TextElement::Bold), @@ -177,7 +177,7 @@ fn recognize_bold_end(input: &str) -> Res<&str, &str> { } fn flat_bold<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Bold<'s>> { - let bold_start = parser_with_context!(context_bold_start)(context.clone()); + let bold_start = parser_with_context!(context_bold_start)(&context); let nom_context = context.with_additional_node(ContextElement::FailMatcherNode(FailMatcherNode { fail_matcher: ChainBehavior::AndParent(Some(&recognize_bold_end)), @@ -199,7 +199,7 @@ fn flat_link<'s, 'r>(context: Context<'r, 's>, i: &'s str) -> Res<&'s str, Link< fail_matcher: ChainBehavior::AndParent(Some(&recognize_link_end)), })); // let nom_context = context.with_additional_fail_matcher(&recognize_link_end); - let text_element_parser = parser_with_context!(flat_text_element)(nom_context); + let text_element_parser = parser_with_context!(flat_text_element)(&nom_context); let (remaining, captured) = recognize(tuple(( link_start, many_till(text_element_parser, link_end),