Fix using references for context elements.

This commit is contained in:
Tom Alexander 2023-09-02 22:44:21 -04:00
parent c309d14776
commit ba57eb16fd
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 22 additions and 38 deletions

View File

@ -31,14 +31,8 @@ impl<'parent, T> List<'parent, T> {
pub fn iter_list(&self) -> IterList<'_, T> {
IterList { next: Some(self) }
}
}
pub trait ListType<'parent, T> {
fn push(&'parent self, item: T) -> List<'parent, T>;
}
impl<'parent, T> ListType<'parent, T> for List<'parent, T> {
fn push(&'parent self, item: T) -> Self {
pub fn push(&'parent self, item: T) -> Self {
Self {
data: item,
parent: Some(self),
@ -46,15 +40,6 @@ impl<'parent, T> ListType<'parent, T> for List<'parent, T> {
}
}
impl<'parent, T> ListType<'parent, T> for Link<'parent, T> {
fn push(&'parent self, item: T) -> List<'parent, T> {
List {
data: item,
parent: *self,
}
}
}
pub struct Iter<'a, T> {
next: Link<'a, T>,
}

View File

@ -1,4 +1,3 @@
use self::parser_context::Context;
use crate::error::Res;
use crate::parser::OrgSource;
@ -8,14 +7,17 @@ mod list;
mod parser_context;
mod parser_with_context;
pub type RefContext<'r, 's> = &'r Context<'r, 's>;
trait ContextMatcher =
for<'r, 's> Fn(RefContext<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>;
pub type RefContext<'b, 'r, 's> = &'b Context<'r, 's>;
pub trait ContextMatcher =
for<'b, 'r, 's> Fn(RefContext<'b, 'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>;
type DynContextMatcher<'c> = dyn ContextMatcher + 'c;
trait Matcher = for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>;
pub trait Matcher = for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>;
type DynMatcher<'c> = dyn Matcher + 'c;
pub use exiting::ExitClass;
pub use global_settings::GlobalSettings;
pub use list::List;
pub use parser_context::Context;
pub use parser_context::ContextElement;
pub use parser_context::ExitMatcherNode;
pub(crate) use parser_with_context::parser_with_context;

View File

@ -3,7 +3,6 @@ use nom::combinator::eof;
use super::exiting::ExitClass;
use super::global_settings::GlobalSettings;
use super::list::List;
use super::list::ListType;
use super::DynContextMatcher;
use super::RefContext;
use crate::error::Res;
@ -134,13 +133,13 @@ impl<'r> std::fmt::Debug for ExitMatcherNode<'r> {
#[derive(Debug)]
pub struct Context<'r, 's> {
global_settings: &'s GlobalSettings<'s>,
tree: &'r List<'r, ContextElement<'r, 's>>,
tree: List<'r, &'r ContextElement<'r, 's>>,
}
impl<'r, 's> Context<'r, 's> {
pub fn new(
global_settings: &'s GlobalSettings<'s>,
tree: &'r List<'r, ContextElement<'r, 's>>,
tree: List<'r, &'r ContextElement<'r, 's>>,
) -> Self {
Self {
global_settings,
@ -148,36 +147,33 @@ impl<'r, 's> Context<'r, 's> {
}
}
pub fn with_additional_node(&self, data: ContextElement<'r, 's>) -> Self {
let new_tree = self.tree.push(data);
Self {
global_settings: self.global_settings,
tree: new_tree,
}
pub fn with_additional_node(&'r self, new_element: &'r ContextElement<'r, 's>) -> Self {
let new_tree = self.tree.push(new_element);
Self::new(self.global_settings, new_tree)
}
pub fn iter(&self) -> super::list::Iter<'r, ContextElement<'r, 's>> {
pub fn iter(&'r self) -> super::list::Iter<'r, &'r ContextElement<'r, 's>> {
self.tree.iter()
}
pub fn iter_context(&self) -> Iter<'r, 's> {
pub fn iter_context(&'r self) -> Iter<'r, 's> {
Iter {
next: self.tree.iter_list(),
global_settings: self.global_settings,
}
}
pub fn get_parent(&self) -> Option<Self> {
pub fn get_parent(&'r self) -> Option<Self> {
self.tree.get_parent().map(|parent_tree| Self {
global_settings: self.global_settings,
tree: parent_tree,
tree: parent_tree.clone(),
})
}
}
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn document_end<'r, 's>(
_context: RefContext<'r, 's>,
fn document_end<'b, 'r, 's>(
_context: RefContext<'b, 'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
eof(input)
@ -185,7 +181,7 @@ fn document_end<'r, 's>(
pub struct Iter<'r, 's> {
global_settings: &'s GlobalSettings<'s>,
next: super::list::IterList<'r, ContextElement<'r, 's>>,
next: super::list::IterList<'r, &'r ContextElement<'r, 's>>,
}
impl<'r, 's> Iterator for Iter<'r, 's> {
@ -193,7 +189,8 @@ impl<'r, 's> Iterator for Iter<'r, 's> {
fn next(&mut self) -> Option<Self::Item> {
let next_tree = self.next.next();
let ret = next_tree.map(|parent_tree| Context::new(self.global_settings, parent_tree));
let ret =
next_tree.map(|parent_tree| Context::new(self.global_settings, parent_tree.clone()));
ret
}
}