#byte #byte-slice #binary-parser #parser #wrapper #binary-data #primitive-types


A simple wrapper to parse primitive Rust types from a slice of bytes

6 releases

new 0.1.5 May 20, 2024
0.1.4 Mar 13, 2023
0.1.3 Jan 15, 2023

#706 in Encoding

Download history 3/week @ 2024-02-14 29/week @ 2024-02-21 15/week @ 2024-02-28 8/week @ 2024-03-06 32/week @ 2024-03-13 5/week @ 2024-03-20 11/week @ 2024-03-27 16/week @ 2024-04-03 9/week @ 2024-04-10 14/week @ 2024-04-17 17/week @ 2024-04-24 3/week @ 2024-05-01 2/week @ 2024-05-08 145/week @ 2024-05-15

170 downloads per month
Used in 2 crates (via konsumer_offsets)


425 lines

bytes_parser CI_s Doc_s Ver_s Down_s Lic_s

A simple wrapper to parse primitive Rust types from a slice of bytes [u8]

What is it for?

This is the crate for you, if all you need is to parse a bespoke binary protocol. You provide a reference slice of bytes, and assuming you know what those bytes represent, read the original values out of it.

The core of this crate is BytesParser, built with few principles in mind:

  • simplicity: just a thin wrapper around an array of bytes, with a cursor to track progress.
  • zero-copy: all you get back is either a bit-copied primitive, or a reference backed by the original bytes - never cloning.
  • only primitive types: if you need to serialize/deserialize complex data structures, you probably want serde.


  • Parse all primitive scalar types, signed and unsigned, as well as &str and sub-slice of &[u8].
  • Internal, auto-updating cursor, to implement a simple scanning logic.
  • Options to move the cursor arbitrarily, but safely, along the input slice.
  • Support for Endianness selection (see ParsingEndian).
  • Descriptive errors (see BytesParserError).
  • Minimal dependencies.


use bytes_parser::{BytesParser, ParsingEndian};

let input: &[u8] = /* a slice of bytes from somewhere */;

// Crate a parser from a given slice of bytes
let mut parser = BytesParser::from(input);

// Will use Big-Endian parsing
assert_eq!(parser.endian(), ParsingEndian::BE);
assert_eq!(ParsingEndian::BE, ParsingEndian::default());

// Parse a string length and the string itself
let str_len = parser.parse_usize()?;
let str = parser.parse_str_utf8(str_len)?;
assert_eq!(str, "Forza Napoli Sempre!");


  • nom: steeper learning curve, but more feature complete.
  • serde: mostly focused on SERialization/DEserialization of well known formats, but it can be setup to handle a slice of bytes.


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~19K SLoC