Skip to main content

NetstringParser

Struct NetstringParser 

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

A parser for netstrings (length-prefixed strings of the form len:data,).

This parser maintains an internal buffer of received bytes. You can append data to the buffer, parse complete netstrings, and discard processed data.

Implementations§

Source§

impl NetstringParser

Source

pub fn new(buf_size: usize) -> Self

Creates a new parser with a buffer of the given size.

Source

pub fn available_buffer(&mut self) -> &mut [u8]

Returns a mutable slice of the unused portion of the internal buffer.

You can write data directly into this slice. After writing, you must call advance with the number of bytes actually written to update the parser’s internal length.

§Example
let mut parser = NetstringParser::new(1024);
let buf = parser.available_buffer();
let bytes_written = some_io_read(buf); // hypothetical function
parser.advance(bytes_written);
Source

pub fn advance(&mut self, count: usize)

Advances the internal buffer position by count bytes.

This method must be called after writing to the slice returned by available_buffer to update the parser state.

Source

pub fn write(&mut self, data: &[u8]) -> Result<(), WriteError>

Writes data into the parser’s internal buffer.

§Note

In most cases, you should prefer using available_buffer to get a mutable slice and advance to indicate how many bytes were written. This avoids unnecessary copying with the typical I/O methods.

Source

pub fn is_buffer_full(&self) -> bool

Returns true if the internal buffer is full.

Source

pub fn is_buffer_empty(&self) -> bool

Returns true if the internal buffer is empty.

Source

pub fn parse_next<'a>( &'a mut self, ) -> Result<Option<Netstring<'a>>, NetstringError>

Attempts to parse the next complete netstring from the buffer.

Returns Ok(Some(Netstring)) if a full netstring is available, Ok(None) if more data is needed, or an error if the data is malformed.

Source

pub fn clear(&mut self)

Clears the parser, discarding all buffered data.

Trait Implementations§

Source§

impl Debug for NetstringParser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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.