From 7e07e00e4c5bf0b97f0a5d26e51b480dd13da827 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 Jul 2022 17:52:04 -0400 Subject: [PATCH] Trying a raw borrow with explicit lifetime. --- src/parser/nom_context.rs | 23 ++++++++++++----------- src/parser/parser_with_context.rs | 4 ++-- src/parser/text_element_parser.rs | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/parser/nom_context.rs b/src/parser/nom_context.rs index 26275470..33943e34 100644 --- a/src/parser/nom_context.rs +++ b/src/parser/nom_context.rs @@ -4,16 +4,17 @@ use nom::IResult; use std::cell::RefCell; use std::rc::Rc; -pub struct NomContext { - pub fail_matcher: Box IResult>, +#[derive(Clone)] +pub struct NomContext<'a, I, O, E> { + pub fail_matcher: &'a dyn FnMut(I) -> IResult, /// You can't have nested bolds in org-mode pub can_match_bold: bool, pub can_match_link: bool, } -impl NomContext { - pub fn new(fail_matcher: Box IResult>) -> Self { +impl<'a, I, O, E> NomContext<'a, I, O, E> { + pub fn new(fail_matcher: &'a dyn FnMut(I) -> IResult) -> Self { NomContext { fail_matcher: fail_matcher, can_match_bold: true, @@ -21,13 +22,13 @@ impl NomContext { } } - // pub fn with_no_bold(&self) -> NomContext { - // NomContext { - // fail_matcher: self.fail_matcher.clone(), - // can_match_bold: false, - // can_match_link: self.can_match_link, - // } - // } + pub fn with_no_bold(&self) -> NomContext { + NomContext { + fail_matcher: self.fail_matcher.clone(), + can_match_bold: false, + can_match_link: self.can_match_link, + } + } // pub fn with_additional_fail_matcher(&self, additional_matcher: G) -> NomContext // where diff --git a/src/parser/parser_with_context.rs b/src/parser/parser_with_context.rs index 6706ca8a..d9fdb322 100644 --- a/src/parser/parser_with_context.rs +++ b/src/parser/parser_with_context.rs @@ -1,7 +1,7 @@ macro_rules! parser_with_context { ($name:ident,$typ:ty,$inp:ident,$context:ident,$fnbody:block) => { - pub fn $name( - $context: &NomContext, + pub fn $name>( + $context: &mut NomContext, ) -> impl for<'a> FnMut(&'a str) -> IResult<&'a str, $typ, VerboseError<&'a str>> { |$inp: &str| $fnbody } diff --git a/src/parser/text_element_parser.rs b/src/parser/text_element_parser.rs index 86a478cd..ccc234dc 100644 --- a/src/parser/text_element_parser.rs +++ b/src/parser/text_element_parser.rs @@ -13,7 +13,7 @@ use nom::error::VerboseError; use nom::IResult; parser_with_context!(text_element, TextElement, i, context, { - // not(|i| context.fail_matcher.borrow_mut().parse(i))(i)?; + not(|i| (context.fail_matcher)(i))(i)?; alt(( // map( // BoldParser::new(slf.context.fail_matcher.clone()),