netstring-parser 0.1.0

A simple, zero-copy netstring parser for Rust, designed for incremental parsing of streaming data with minimal allocations.
Documentation
  • Coverage
  • 100%
    21 out of 21 items documented1 out of 15 items with examples
  • Size
  • Source code size: 30.51 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.38 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 16s Average build duration of successful builds.
  • all releases: 16s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • bikeshedder/netstring-parser
    1 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • bikeshedder

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. 🚀