pub struct LineParser { /* private fields */ }Expand description
Stateful parser for turning arbitrary byte chunks into lines.
Drive it by calling Self::next_line in a loop with a slice cursor that you advance
across calls; on EOF call Self::finish once to flush any unterminated trailing line.
On a stream gap (chunks dropped between deliveries) call Self::on_gap to discard the
in-progress partial line and resynchronize at the next newline.
Implementations§
Source§impl LineParser
impl LineParser
Sourcepub fn on_gap(&mut self)
pub fn on_gap(&mut self)
Notifies the parser that the upstream delivery dropped chunks. Discards any partial line in progress and resynchronizes at the next newline instead of joining bytes across the gap.
Sourcepub fn next_line<'a, 'b>(
&'a mut self,
chunk: &mut &'b [u8],
options: LineParsingOptions,
) -> Option<Cow<'a, str>>where
'b: 'a,
pub fn next_line<'a, 'b>(
&'a mut self,
chunk: &mut &'b [u8],
options: LineParsingOptions,
) -> Option<Cow<'a, str>>where
'b: 'a,
Advances through chunk and yields the next parsed line, if any.
chunk is mutated in place to advance past the consumed prefix. Call repeatedly,
reusing the same slice cursor, until this returns None; at that point the chunk is
exhausted and any partial line is buffered for the next chunk.
The returned Cow borrows from the chunk slice when the line fits entirely in this
call and no partial line was already buffered (zero-allocation fast path), and borrows
from the parser’s internal emitted-line slot otherwise. Either way, drop the Cow
before the next call — the borrow checker enforces this through the &'a mut self
signature.
Sourcepub fn finish(&mut self) -> Option<Cow<'_, str>>
pub fn finish(&mut self) -> Option<Cow<'_, str>>
Flushes any unterminated trailing line at EOF.
Returns None when there is nothing to flush — the buffer is empty, or the parser is
in DiscardUntilNewline mode (a gap or overflow truncation is still draining and the
buffered remainder is conservatively dropped). Otherwise returns the buffered line as a
Cow borrowing from the parser’s emitted-line slot.