Skip to content

Commit

Permalink
feat: Add no_std support
Browse files Browse the repository at this point in the history
  • Loading branch information
ball-hayden committed Mar 21, 2023
1 parent c8f384f commit c47c2f4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ jobs:
- stable
- 1.48.0
- nightly

features:
- ''
- '--no-default-features'
- '--no-default-features --features "alloc"'
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
Expand All @@ -24,6 +29,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: check
args: ${{ matrix.features }}

test:
name: Test Suite
Expand Down
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ include = [
members = ["nom-derive-impl"]

[dependencies]
nom = { version = "7.0", default-features = false, features = ["std"] }
nom = { version = "7.0", default-features = false }
nom-derive-impl = { version="=0.10.0", path="./nom-derive-impl" }
rustversion = "1.0"

[dev-dependencies]
pretty_assertions = "0.7"
trybuild = "1.0"

[features]
alloc = ["nom/alloc"]
default = ["std"]
std = ["alloc", "nom/std"]
2 changes: 1 addition & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::traits::*;
use core::marker::PhantomData;
use nom::error::ParseError;
use nom::{IResult, ToUsize};
use std::marker::PhantomData;

#[derive(Debug, PartialEq)]
pub struct LengthData<L, D> {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
//!
//! [nom]: https://github.com/geal/nom
#![cfg_attr(not(any(test, feature = "std")), no_std)]

pub mod docs;
mod helpers;
mod traits;
Expand Down
37 changes: 29 additions & 8 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
use nom::bytes::streaming::take;
use nom::combinator::{complete, map_res, opt};
use nom::error::{Error, FromExternalError, ParseError};
use nom::multi::{many0, many_m_n};
use core::ops::RangeFrom;
use nom::combinator::{complete, opt};
use nom::error::{Error, ParseError};
use nom::number::streaming::*;
use nom::sequence::pair;
use nom::*;
use std::convert::TryFrom;
use std::ops::RangeFrom;

#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, string::String, vec::Vec};

#[cfg(feature = "alloc")]
use core::convert::TryFrom;

#[cfg(feature = "alloc")]
use nom::bytes::streaming::take;

#[cfg(feature = "alloc")]
use nom::combinator::map_res;

#[cfg(feature = "alloc")]
use nom::error::FromExternalError;

#[cfg(feature = "alloc")]
use nom::multi::{many0, many_m_n};

pub use nom::{InputLength, Slice};

Expand Down Expand Up @@ -150,13 +168,14 @@ impl_primitive_type!(u128, be_u128, le_u128);
impl_primitive_type!(f32, be_f32, le_f32);
impl_primitive_type!(f64, be_f64, le_f64);

#[cfg(feature = "alloc")]
impl<'a, E> Parse<&'a [u8], E> for String
where
E: ParseError<&'a [u8]> + FromExternalError<&'a [u8], std::str::Utf8Error>,
E: ParseError<&'a [u8]> + FromExternalError<&'a [u8], core::str::Utf8Error>,
{
fn parse(i: &'a [u8]) -> IResult<&'a [u8], Self, E> {
let (rem, sz) = <u32>::parse(i)?;
let (rem, s) = map_res(take(sz as usize), std::str::from_utf8)(rem)?;
let (rem, s) = map_res(take(sz as usize), core::str::from_utf8)(rem)?;
Ok((rem, s.to_owned()))
}
}
Expand All @@ -178,6 +197,7 @@ where
}
}

#[cfg(feature = "alloc")]
impl<T, I, E> Parse<I, E> for Vec<T>
where
I: Clone + PartialEq + InputSlice,
Expand Down Expand Up @@ -215,6 +235,7 @@ where

/// *Note: this implementation uses const generics and requires rust >= 1.51*
#[rustversion::since(1.51)]
#[cfg(feature = "alloc")]
impl<T, I, E, const N: usize> Parse<I, E> for [T; N]
where
I: Clone + PartialEq + InputSlice,
Expand Down

0 comments on commit c47c2f4

Please sign in to comment.