netstring-parser

Crates.ionetstring-parser
lib.rsnetstring-parser
version0.1.0
created_at2025-08-27 19:08:33.060742+00
updated_at2025-08-27 19:08:33.060742+00
descriptionA simple, zero-copy netstring parser for Rust, designed for incremental parsing of streaming data with minimal allocations.
homepage
repositoryhttps://github.com/bikeshedder/netstring-parser
max_upload_size
id1813135
size30,516
Michael P. Jung (bikeshedder)

documentation

README

netstring-parser

Latest Version Build Status Unsafe forbidden Rust 1.65+

A simple, zero-copy netstring parser for Rust, designed for incremental parsing of streaming data with minimal allocations.

Features

  • ✅ Zero-copy parsing for streaming I/O
  • ✅ Incremental parsing of partial data
  • ✅ Safe Rust (no unsafe code)
  • ✅ Optional non-zero-copy mode for convenience

Zero-Copy Parsing with Streaming I/O

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.

Non-Zero-Copy Parsing

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);
    }
}

License

Licensed under either of

at your option.


netstring-parser makes parsing netstrings in Rust fast, safe, and efficient. 🚀

Commit count: 2

cargo fmt