Expand description


Crates.io Crates.io Crates.io Docs.io Docs master Rust codecov

nombytes is a library that provides a wrapper for the bytes::Bytes byte container for use with nom.

I originally made this so that I could have a function take a file name path and return parsed values that still had references to the loaded file without running into the lifetime issues associated with &[u8] and &str that would prevent me from doing so. I decided to release it as a crate so that others can make use of my efforts too.

This library has been tested to work with bytes down to v5.3.0 and nom down to v6.0.0 and has been marked as such in its Cargo.toml.


Put this in your Cargo.toml:

nombytes = "0.1.1"



With the miette feature enabled, the NomBytes implements its SourceCode trait so it can be used directly with miette’s #[source_code] error attribute. This feature also enables the std feature.

This library has been tested to work with miette down to v3.0.0 and has been marked as such in its Cargo.toml.


Adds serde::Serialize and serde::Deserialize implementations to the types in this library to allow for using them with serde.


Enabled by default; allows creating NomBytes directly from Strings through a From<String> impl. With this feature turned off, this crate is #![no_std] compatible.


Borrowed from the nom crate, using NomBytes instead of &str. Had to be modified slightly because NomBytes acts as &[u8] rather than as &str.

use nom::{
  bytes::complete::{tag, take_while_m_n},
use nombytes::NomBytes;

pub struct Color {
  pub red:     u8,
  pub green:   u8,
  pub blue:    u8,

fn from_hex(input: NomBytes) -> Result<u8, std::num::ParseIntError> {
  u8::from_str_radix(input.to_str(), 16)

fn is_hex_digit(c: u8) -> bool {
  (c as char).is_digit(16)

fn hex_primary(input: NomBytes) -> IResult<NomBytes, u8> {
    take_while_m_n(2, 2, is_hex_digit),

fn hex_color(input: NomBytes) -> IResult<NomBytes, Color> {
  let (input, output) = tag("#")(input)?;

  let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?;

  Ok((input, Color { red, green, blue }))

fn main() {
    Ok((r, Color {
      red: 47,
      green: 20,
      blue: 223,
    })) if r.to_str() == ""));


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.


A wrapper around bytes::Bytes to be able to use it with nom.


An enum that can hold all the Range* types without being generic/trait based. We need this type because SliceIndex<T> is implemented for the individual Range* types rather than for the RangeBounds<T> trait that we could’ve otherwise used.