Remove redundant index field from tree::Parser

This commit is contained in:
David Hoppenbrouwers
2022-09-10 01:39:47 +02:00
parent 78ff3335fe
commit 5fd7738d7e

View File

@ -7,10 +7,9 @@ use {
#[derive(Debug)] #[derive(Debug)]
pub struct Parser<'a> { pub struct Parser<'a> {
// Splitting the fields and manually reconstructing the item parser allows // Manually reconstructing the item parser allows avoiding troubles with shared mutable
// avoiding troubles with shared mutable references, lifetimes etc. // references, lifetimes etc.
data: &'a [u8], data: Cell<&'a [u8]>,
index: Cell<usize>,
} }
impl<'a> Parser<'a> { impl<'a> Parser<'a> {
@ -74,14 +73,14 @@ impl<'a, 'p> Iterator for StackFrame<'a, 'p> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let mut it = super::Parser { let mut it = super::Parser {
data: self.inner.data.get(self.inner.index.get()..)?, data: self.inner.data.get(),
}; };
loop { loop {
let item = match it.next()? { let item = match it.next()? {
Ok(e) => e, Ok(e) => e,
Err(e) => return Some(Err(ParseError::from_item(e))), Err(e) => return Some(Err(ParseError::from_item(e))),
}; };
self.inner.index.set(self.inner.data.len() - it.data.len()); self.inner.data.set(it.data);
match item { match item {
Item::Collection(ty) => break Some(Ok(Value::Collection(ty))), Item::Collection(ty) => break Some(Ok(Value::Collection(ty))),
Item::EndCollection => break Some(Ok(Value::EndCollection)), Item::EndCollection => break Some(Ok(Value::EndCollection)),
@ -243,10 +242,7 @@ impl Field {
} }
pub fn parse(data: &[u8]) -> Parser<'_> { pub fn parse(data: &[u8]) -> Parser<'_> {
Parser { Parser { data: data.into() }
data,
index: Default::default(),
}
} }
#[cfg(test)] #[cfg(test)]