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
impl NetstringParser
Sourcepub fn available_buffer(&mut self) -> &mut [u8] ⓘ
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);Sourcepub fn advance(&mut self, count: usize)
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.
Sourcepub fn write(&mut self, data: &[u8]) -> Result<(), WriteError>
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.
Sourcepub fn is_buffer_full(&self) -> bool
pub fn is_buffer_full(&self) -> bool
Returns true if the internal buffer is full.
Sourcepub fn is_buffer_empty(&self) -> bool
pub fn is_buffer_empty(&self) -> bool
Returns true if the internal buffer is empty.
Sourcepub fn parse_next<'a>(
&'a mut self,
) -> Result<Option<Netstring<'a>>, NetstringError>
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.