| Crates.io | netstring-parser |
| lib.rs | netstring-parser |
| version | 0.1.0 |
| created_at | 2025-08-27 19:08:33.060742+00 |
| updated_at | 2025-08-27 19:08:33.060742+00 |
| description | A simple, zero-copy netstring parser for Rust, designed for incremental parsing of streaming data with minimal allocations. |
| homepage | |
| repository | https://github.com/bikeshedder/netstring-parser |
| max_upload_size | |
| id | 1813135 |
| size | 30,516 |
A simple, zero-copy netstring parser for Rust, designed for incremental parsing of streaming data with minimal allocations.
unsafe code)The parser is optimized for streaming I/O.
You can avoid unnecessary copies by using the methods available_buffer and advance to read directly into the buffer of the parser:
use std::io::{self, Read};
use netstring_parser::NetstringParser;
fn read_from_stream<R: Read>(mut reader: R) -> io::Result<()> {
let mut parser = NetstringParser::new(128);
loop {
let read = reader.read(parser.available_buffer())?;
if read == 0 {
break;
}
parser.advance(read);
while let Some(ns) = parser.parse_next().unwrap() {
println!("Got: {}", ns);
}
}
Ok(())
}
available_buffer() gives you a mutable slice to write into.advance(n) informs the parser that n bytes were written.parse_next() retrieves the next complete netstring if available.If zero-copy parsing is not feasible, use the write method to copy data into the parser’s internal buffer:
use netstring_parser::NetstringParser;
let mut parser = NetstringParser::new(64);
// Imagine this data coming in via some stream in small chunks
let chunks: &[&[u8]] = &[
b"5:he",
b"llo,5:wor",
b"ld,3:bye,",
];
for chunk in chunks {
parser.write(chunk).unwrap();
while let Some(ns) = parser.parse_next().unwrap() {
println!("Got: {}", ns);
}
}
Licensed under either of
at your option.
netstring-parser makes parsing netstrings in Rust fast, safe, and efficient. 🚀