Skip to main content

LineBuffer

Struct LineBuffer 

Source
pub struct LineBuffer { /* private fields */ }
Expand description

Accumulates raw stream bytes and yields complete newline-terminated lines.

Streaming responses arrive as arbitrary byte chunks: a single multi-byte UTF-8 character (e.g. é, or an emoji) can be split across two chunks. The naive buffer.push_str(&String::from_utf8_lossy(&chunk)) decodes each raw chunk in isolation, so the leading bytes of a split character decode to U+FFFD (the replacement character) and the trailing bytes decode to another — permanently corrupting the text.

LineBuffer accumulates raw bytes and only decodes complete, newline-terminated lines. A \n (0x0A) byte can never appear inside a multi-byte UTF-8 sequence, so decoding a whole line is always safe even when the underlying chunk boundary fell mid-character.

Consumed bytes are tracked with a cursor rather than drained per line: draining left-shifts every remaining byte, which turns a chunk carrying K lines into O(K·chunk) work. The buffer compacts once the consumed prefix grows past a threshold, so memory stays bounded over a long stream.

Implementations§

Source§

impl LineBuffer

Source

pub fn new() -> Self

Creates an empty buffer.

Source

pub fn push(&mut self, chunk: &[u8])

Appends a raw byte chunk from the stream.

Source

pub fn next_line(&mut self) -> Option<String>

Removes and returns the next complete line (trailing \n stripped), or None when no complete line is buffered yet.

Source

pub fn take_remaining(&mut self) -> Option<String>

Drains and returns whatever bytes remain after the last complete line.

A stream can end without a trailing newline, leaving a final, otherwise-complete record buffered that next_line will never yield. Call this once at end-of-stream to recover it. Returns None when the buffer is empty or the remainder is only whitespace.

Trait Implementations§

Source§

impl Default for LineBuffer

Source§

fn default() -> LineBuffer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.